Main Page   Namespace List   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

log.cpp

Go to the documentation of this file.
00001 
00013 #include "sysconfig.h"
00014 #include "log.h"
00015 #include "guard.h"
00016 
00027 Log::Log(string& r_fname) {
00028   InitializeCriticalSection(&mMutex);
00029   mFileName = r_fname;
00030   if (mFileName != "") {  // We want to use a file
00031     mpFile = new ofstream(mFileName.c_str(),ios_base::out|ios_base::app);
00032   } else {  // No file - logging will be done to cout (console)
00033     mpFile = NULL;
00034   }
00035   mActive = true;
00036 }
00037 
00048 Log::Log(const char * fname) {
00049   InitializeCriticalSection(&mMutex);
00050   mFileName = fname;
00051   if (mFileName != "") { // We want to use a file
00052     mpFile = new ofstream(mFileName.c_str(),ios_base::out|ios_base::app);
00053   } else { // No file - logging will be done to cout (console)
00054     mpFile = NULL;
00055   }
00056   mActive = true;
00057 }
00058 
00062 Log::~Log() {
00063   if (mpFile) { // Nothing to be done for cout logs
00064     mpFile->close();
00065     delete mpFile;
00066   }
00067   DeleteCriticalSection(&mMutex);
00068 }
00069 
00077 void Log::SetTime() {
00078   time_t tm_secs = time(&tm_secs);
00079   struct tm *tm_time = localtime(&tm_secs);
00080 
00081   strftime(mTimeStamp, 40, "[%Y-%m-%d %H:%M:%S] ", tm_time);
00082 }
00083 
00089 void Log::Write(const string& r_msg) {
00090   Guard g(mMutex);
00091   if(mActive) {
00092     SetTime();
00093     if (mpFile)
00094       *mpFile << mTimeStamp << r_msg << endl;
00095     else
00096       cout << mTimeStamp << r_msg << endl;
00097   }
00098 }
00099 
00115 void Log::Write(const char *fmt, ...)
00116 {
00117   Guard g(mMutex);
00118   if(mActive) {
00119     char buf[1024];
00120     va_list args;
00121     va_start (args, fmt);
00122     vsprintf (buf, fmt, args);
00123     va_end (args);
00124     SetTime();
00125     if (mpFile)
00126       *mpFile << mTimeStamp << buf << endl;
00127     else
00128       cout << mTimeStamp << buf << endl;
00129   }
00130 }
00131 
00137 void Log::SetActive(bool b) {
00138   Guard g(mMutex);
00139   mActive = b;
00140 }
00141 
00147 bool Log::IsActive() {
00148   Guard g(mMutex);
00149   return mActive;
00150 }
00151 

Generated on Mon Mar 29 23:12:53 2004 for Paradigm by doxygen1.2.17