Skip to content
Snippets Groups Projects
Log.h 2.25 KiB
Newer Older
Stephen Smith's avatar
Stephen Smith committed
/*  Log.h

    Mark Woolrich, FMRIB Image Analysis Group

    Copyright (C) 1999-2000 University of Oxford  */

/*  CCOPYRIGHT  */

#include "newmat/newmatap.h"
#include "newmat/newmatio.h"
Stephen Smith's avatar
Stephen Smith committed

using namespace NEWMAT;
namespace UTILS{

#if !defined(__Log_h)
#define __Log_h
  
  class Log
    {
    public:
      static Log& getInstance();
      ~Log() { delete logger; }

      void establishDir(const string& name); 
      void setDir(const string& name); 
      void setLogFile(const string& name) {logfilename = name;}
      const string& getDir() const { return dir; }
      
Mark Woolrich's avatar
Mark Woolrich committed
      void out(const string& p_fname, const Matrix& p_mat, bool p_addMatrixString = true);
Stephen Smith's avatar
Stephen Smith committed
      void out(const string& p_fname, const RowVector& p_mat);
      void out(const string& p_fname, const ColumnVector& p_mat);
      
      ofstream& str() { return logfileout; }
      
    private:
      Log() {}
      
      const Log& operator=(Log&);
      Log(Log&);
      
      static Log* logger;
      string dir;
      ofstream logfileout;
      string logfilename;
    };
  
Mark Woolrich's avatar
Mark Woolrich committed
  inline void Log::out(const string& p_fname, const Matrix& p_mat, bool p_addMatrixString)
Stephen Smith's avatar
Stephen Smith committed
    {
      ofstream out;
      out.open((dir + "/" + p_fname).c_str(), ios::out);
Mark Woolrich's avatar
Mark Woolrich committed
      out.setf(ios::scientific, ios::floatfield);

      if(p_addMatrixString)
	out << "/Matrix" << endl;
      for(int i=1; i<=p_mat.Nrows(); i++)	   
	{
	  for(int j=1; j<=p_mat.Ncols(); j++)
	    {
	      out << p_mat(i,j) << " ";	
	    }
	  out << endl;
	}

Stephen Smith's avatar
Stephen Smith committed
      out.close();
    }
  
  inline void Log::out(const string& p_fname, const ColumnVector& p_mat)
    {
      ofstream out;
Mark Woolrich's avatar
Mark Woolrich committed
      out.open((dir + "/" + p_fname).c_str(), ios::out);     
      out.setf(ios::scientific, ios::floatfield);
      for(int j=1; j<=p_mat.Nrows(); j++)
	{
	  out << p_mat(j);	  
	  out << endl;
	}
      
Stephen Smith's avatar
Stephen Smith committed
      out.close();
    }

  inline void Log::out(const string& p_fname, const RowVector& p_mat)
    {
      ofstream out;
      out.open((dir + "/" + p_fname).c_str(), ios::out);
Mark Woolrich's avatar
Mark Woolrich committed
      out.setf(ios::scientific, ios::floatfield);

      for(int j=1; j<=p_mat.Ncols(); j++)
	{
	  out << p_mat(j) << " ";	  
	}
      out << endl;
Stephen Smith's avatar
Stephen Smith committed
      out.close();
    }

  inline Log& Log::getInstance(){
    if(logger == NULL)
      logger = new Log();
  
    return *logger;
  }

#endif

}