Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

muo_logging.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001 Karsten Reincke <reincke@gnukose.org>
00003  *  
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software 
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *  
00018  * file <muo_logging.cc> version <#1.3.1#> of project <MrProjext>
00019  */   
00020 
00027 #include <iostream.h>
00028 #include <time.h>
00029 #include "muo_logging.h"
00030 using namespace krmuo;
00031 /* &&& (1) local preprocessor-defines &&&&&&&&&&&&&&&&&&&&&&& */
00032 
00033 /* &&& (2) definitions of static global variables &&&&&&&&&&& */
00034           // note: they mustnīt again be declared as static
00035  
00036 /* &&& (3) &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */
00037 /* &&&     methods for class LoggingSuitcase &&& */
00038 
00039 /* --- (3.A) public ------------------------------------------ */
00040 
00041 /* :-: (3.A.1) public constructors, inits & operators -:-:-:-: */
00042 
00050 LoggingUnit::LoggingUnit
00051 ( const unsigned int pMaxVisibleMessageLevel,
00052   FILE *pScreen)
00053 : mMaxVisibleMessageLevel(pMaxVisibleMessageLevel),
00054   mScreen(pScreen),
00055   mLogFileName(""),
00056   mFile(NULL),
00057   mUsesSharedLoggingFile(false)
00058 {
00059 }
00060 
00069 LoggingUnit::LoggingUnit
00070 ( const unsigned int pMaxVisibleMessageLevel,
00071   const string& pLogFileName,
00072   bool pUsesSharedLoggingFile)
00073 : mMaxVisibleMessageLevel(pMaxVisibleMessageLevel),
00074   mScreen(NULL),
00075   mLogFileName(pLogFileName),
00076   mFile(NULL),
00077   mUsesSharedLoggingFile(pUsesSharedLoggingFile)
00078 {
00079   if (!(mUsesSharedLoggingFile))
00080   {
00081     mFile=fopen(mLogFileName.c_str(),"a");
00082     if (!(mFile))
00083     {
00084       mScreen=stderr;
00085       cerr << "Can't open logfile <" << pLogFileName<<">\n";
00086     }
00087   }
00088 }
00089 
00099 LoggingUnit::LoggingUnit
00100 ( const unsigned int pMaxVisibleMessageLevel,
00101   FILE *pScreen,
00102   const string& pLogFileName,
00103   bool pUsesSharedLoggingFile)
00104 : mMaxVisibleMessageLevel(pMaxVisibleMessageLevel),
00105   mScreen(pScreen),
00106   mLogFileName(pLogFileName),
00107   mFile(NULL),
00108   mUsesSharedLoggingFile(pUsesSharedLoggingFile)
00109 {
00110   if (!(mUsesSharedLoggingFile))
00111   {
00112     mFile=fopen(mLogFileName.c_str(),"a");
00113     if (!(mFile))
00114     {
00115       mScreen=stderr;
00116       cerr << "Can't open logfile <" << pLogFileName<<">\n";
00117     }
00118   }
00119 }
00120 
00126 LoggingUnit::~LoggingUnit()
00127 {
00128   if (mFile)
00129     fclose(mFile);
00130 }
00131 
00132 /* :-: (3.A.2) public getter -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00133 /* :-: (3.A.3) public setter -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00134 /* :-: (3.A.4) public others -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00143 void LoggingUnit::makeEntry
00144 ( const string& pLogString,
00145   const unsigned int pLogEntryLevel
00146 ) const
00147 {
00148   if (pLogEntryLevel<=mMaxVisibleMessageLevel)
00149   {
00150     string timeLabel="";
00151     fillTimeLabel(timeLabel);  
00152     if (mScreen)
00153     {
00154       fprintf(mScreen,"%s (LL%d): %s\n",timeLabel.c_str(),pLogEntryLevel,pLogString.c_str());
00155     }
00156     FILE *lFile;
00157     if (mUsesSharedLoggingFile)
00158       lFile=fopen(mLogFileName.c_str(),"a");
00159     else
00160       lFile=mFile;
00161     if (lFile)
00162     {
00163       fprintf(lFile,"%s (LL%d): %s\n",timeLabel.c_str(),pLogEntryLevel,pLogString.c_str());
00164       fflush(lFile);
00165       if (mUsesSharedLoggingFile)
00166       {
00167         fclose(lFile);
00168       }
00169     }    
00170   }
00171 }
00181 void LoggingUnit::makeEntry
00182 ( const string& pLogString1,
00183   const string& pLogString2, 
00184   const unsigned int pLogEntryLevel
00185 ) const
00186 {
00187   if (pLogEntryLevel<=mMaxVisibleMessageLevel)
00188   {    
00189     string timeLabel="";
00190     fillTimeLabel(timeLabel);  
00191     if (mScreen)
00192     {
00193       fprintf(mScreen,"%s (LL%d): %s: <%s>\n",timeLabel.c_str(),pLogEntryLevel,pLogString1.c_str(),pLogString2.c_str());
00194     }
00195     FILE *lFile;  
00196     if (mUsesSharedLoggingFile)
00197       lFile=fopen(mLogFileName.c_str(),"a");
00198     else
00199       lFile=mFile;
00200     if (lFile)
00201     {
00202       fprintf(lFile,"%s (LL%d): %s: <%s>\n",timeLabel.c_str(),pLogEntryLevel,pLogString1.c_str(),pLogString2.c_str());
00203       fflush(lFile);
00204       if (mUsesSharedLoggingFile)
00205       {
00206         fclose(lFile);
00207       }
00208     }    
00209   }  
00210 }
00211 
00222 void LoggingUnit::makeEntry
00223 ( const string& pLogString1,
00224   const string& pLogString2, 
00225   const string& pLogString3,  
00226   const unsigned int pLogEntryLevel
00227 )const
00228 {
00229   if (pLogEntryLevel<=mMaxVisibleMessageLevel)
00230   {    
00231     string timeLabel="";
00232     fillTimeLabel(timeLabel);  
00233     if (mScreen)
00234     {
00235       fprintf(mScreen,"%s (LL%d): %s: <%s [%s]>\n",
00236         timeLabel.c_str(),pLogEntryLevel,
00237           pLogString1.c_str(),pLogString2.c_str(),pLogString3.c_str());
00238     }
00239     FILE *lFile;  
00240     if (mUsesSharedLoggingFile)
00241       lFile=fopen(mLogFileName.c_str(),"a");
00242     else
00243       lFile=mFile;
00244     if (lFile)
00245     {
00246       fprintf(lFile,"%s (LL%d): %s: <%s [%s]>\n",
00247         timeLabel.c_str(),pLogEntryLevel,
00248           pLogString1.c_str(),pLogString2.c_str(),pLogString3.c_str());
00249       fflush(lFile);
00250       if (mUsesSharedLoggingFile)
00251       {
00252         fclose(lFile);
00253       }
00254     }    
00255   }  
00256 }
00257 
00268 void LoggingUnit::makeEntry
00269 ( const string& pLogString1,
00270   const string& pLogString2, 
00271   const unsigned int pLogEntryLevel,
00272   bool pTwoLine
00273 ) const
00274 {
00275   if (pLogEntryLevel<=mMaxVisibleMessageLevel)
00276   {    
00277     string timeLabel="";
00278     fillTimeLabel(timeLabel);  
00279     if (mScreen)
00280     {
00281       if (pTwoLine)
00282         fprintf(mScreen,"%s (LL%d): %s:\n%s\n",timeLabel.c_str(),pLogEntryLevel,pLogString1.c_str(),pLogString2.c_str());
00283       else
00284         fprintf(mScreen,"%s (LL%d): %s: <%s>\n",timeLabel.c_str(),pLogEntryLevel,pLogString1.c_str(),pLogString2.c_str());
00285     }
00286     FILE *lFile;  
00287     if (mUsesSharedLoggingFile)
00288       lFile=fopen(mLogFileName.c_str(),"a");
00289     else
00290       lFile=mFile;
00291     if (lFile)
00292     {
00293       if (pTwoLine)
00294         fprintf(lFile,"%s (LL%d): %s:\n%s\n",timeLabel.c_str(),pLogEntryLevel,pLogString1.c_str(),pLogString2.c_str());
00295       else
00296         fprintf(lFile,"%s (LL%d): %s: <%s>\n",timeLabel.c_str(),pLogEntryLevel,pLogString1.c_str(),pLogString2.c_str());
00297       fflush(lFile);
00298       if (mUsesSharedLoggingFile)
00299       {
00300         fclose(lFile);
00301       }
00302     }       
00303   }
00304 }
00305 
00317 void LoggingUnit::makeEntry
00318 ( const string& pLogString1,
00319   const string& pLogString2, 
00320   const string& pLogString3, 
00321   const unsigned int pLogEntryLevel,
00322   bool pTwoLine
00323 ) const
00324 {
00325   if (pLogEntryLevel<=mMaxVisibleMessageLevel)
00326   {    
00327     string timeLabel="";
00328     fillTimeLabel(timeLabel);  
00329     if (mScreen)
00330     {
00331       if (pTwoLine)
00332         fprintf(mScreen,"%s (LL%d): %s:\n%s [%s]\n",
00333           timeLabel.c_str(),pLogEntryLevel,
00334             pLogString1.c_str(),pLogString2.c_str(),pLogString3.c_str());
00335       else
00336         fprintf(mScreen,"%s (LL%d): %s: <%s [%s]>\n",
00337           timeLabel.c_str(),pLogEntryLevel,
00338             pLogString1.c_str(),pLogString2.c_str(),pLogString3.c_str());
00339     }
00340     FILE* lFile;
00341     if (mUsesSharedLoggingFile)
00342       lFile=fopen(mLogFileName.c_str(),"a");
00343     else
00344       lFile=mFile;
00345     if (lFile)
00346     {
00347       if (pTwoLine)
00348         fprintf(lFile,"%s (LL%d): %s:\n%s [%s]\n",
00349           timeLabel.c_str(),pLogEntryLevel,
00350             pLogString1.c_str(),pLogString2.c_str(),pLogString3.c_str());
00351       else
00352         fprintf(lFile,"%s (LL%d): %s: <%s [%s]>\n",
00353           timeLabel.c_str(),pLogEntryLevel,
00354             pLogString1.c_str(),pLogString2.c_str(),pLogString3.c_str());
00355       fflush(lFile);
00356       if (mUsesSharedLoggingFile)
00357       {
00358         fclose(lFile);
00359       }
00360     }       
00361   }
00362 }
00363 
00373 void LoggingUnit::makeEntry
00374 ( const string& pLogString,
00375   const unsigned int pLogUint, 
00376   const unsigned int pLogEntryLevel
00377 )const
00378 {
00379   if (pLogEntryLevel<=mMaxVisibleMessageLevel)
00380   {    
00381     string timeLabel="";
00382     fillTimeLabel(timeLabel);  
00383     if (mScreen)
00384     {
00385       fprintf(mScreen,"%s (LL%d): %s <%d>\n",timeLabel.c_str(),pLogEntryLevel,pLogString.c_str(),pLogUint);
00386     }
00387     FILE* lFile;
00388     if (mUsesSharedLoggingFile)
00389       lFile=fopen(mLogFileName.c_str(),"a");
00390     else
00391       lFile=mFile;
00392     if (lFile)
00393     {
00394       fprintf(lFile,"%s (LL%d): %s <%d>\n",timeLabel.c_str(),pLogEntryLevel,pLogString.c_str(),pLogUint);
00395       fflush(lFile);
00396       if (mUsesSharedLoggingFile)
00397       {
00398         fclose(lFile);
00399       }
00400     }    
00401   }  
00402 }
00403 
00413 void LoggingUnit::makeEntry
00414 ( const string& pLogString,
00415   const int pLogInt, 
00416   const unsigned int pLogEntryLevel
00417 )const
00418 {
00419   if (pLogEntryLevel<=mMaxVisibleMessageLevel)
00420   {
00421     string timeLabel="";
00422     fillTimeLabel(timeLabel);  
00423     if (mScreen)
00424     {
00425       fprintf(mScreen,"%s (LL%d): %s <%d>\n",timeLabel.c_str(),pLogEntryLevel,pLogString.c_str(),pLogInt);
00426     }
00427     FILE* lFile;    
00428     if (mUsesSharedLoggingFile)
00429       lFile=fopen(mLogFileName.c_str(),"a");
00430     else
00431       lFile=mFile;
00432     if (lFile)
00433     {
00434       fprintf(lFile,"%s (LL%d): %s <%d>\n",timeLabel.c_str(),pLogEntryLevel,pLogString.c_str(),pLogInt);
00435       fflush(lFile);
00436       if (mUsesSharedLoggingFile)
00437       {
00438         fclose(lFile);
00439       }
00440     }    
00441   }  
00442 }
00443 
00444 /* --- (3.B) - private ---------------------------------------- */
00445 /* :-: (3.B.1) private constructors, inits & operators -:-:-:-: */
00446 
00461 /* :-: (3.B.2) private getter -:-:-:-:-:-:-:-:-:-::-:-:-:-:-:-: */
00462 /* :-: (3.B.3) private setter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00463 /* :-: (3.B.4) private others :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00470 void LoggingUnit::fillTimeLabel(string& pTimeLabel)const
00471 {
00472   char lBuffer[100];
00473   time_t now=time(NULL);
00474   tm *np=localtime(&now);
00475   sprintf
00476   ( lBuffer,"%04d%02d%02dT%02d%02d%02d",
00477             (np->tm_year+1900),(np->tm_mon+1),np->tm_mday,
00478             np->tm_hour,np->tm_min,np->tm_sec);
00479   pTimeLabel=lBuffer;
00480 }  
00481 
00482 /* --- (3.C) - protected -------------------------------------- */
00483 /* :-: (3.C.1) protected getter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00484 /* :-: (3.C.2) protected setter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00485 /* :-: (3.C.3) protected others :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00486 
00487 /* &&& (4) &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */
00488 /* &&&     methods for class LoggingSuitcase &&& */
00489 
00490 /* --- (4.A) public ------------------------------------------ */
00491 
00492 /* :-: (4.A.1) public constructors, inits & operators -:-:-:-: */
00493 
00502 LoggingSuitcase::LoggingSuitcase
00503 ( const unsigned int pShowActionMessages,
00504   const unsigned int pShowErrorMessages
00505 )
00506 : mActionLoggingUnit(pShowActionMessages,stdout),
00507   mWarnLoggingUnit(pShowErrorMessages,stderr),
00508   mErrorLoggingUnit(pShowErrorMessages,stderr)
00509 {
00510 }
00511 
00522 LoggingSuitcase::LoggingSuitcase
00523 ( const unsigned int pShowActionMessages,
00524   const unsigned int pShowErrorMessages,
00525   const string& pLogDirPath,
00526   const string& pLogFileNameKernel,
00527   bool pUsesSharedLoggingFile
00528 )
00529 : mActionLoggingUnit
00530   ( pShowActionMessages, 
00531     (pLogDirPath + "/" + pLogFileNameKernel + "_action.log"),
00532     pUsesSharedLoggingFile
00533   ),
00534   mWarnLoggingUnit
00535   ( pShowErrorMessages, 
00536     (pLogDirPath + "/" + pLogFileNameKernel + "_warn.log"),
00537     pUsesSharedLoggingFile
00538   ),
00539   mErrorLoggingUnit
00540   ( pShowErrorMessages, 
00541     (pLogDirPath + "/" + pLogFileNameKernel + "_error.log"),
00542     pUsesSharedLoggingFile
00543   )
00544 {
00545 }
00546 
00558 LoggingSuitcase::LoggingSuitcase
00559 ( const unsigned int pShowActionMessages,
00560   const unsigned int pShowErrorMessages,
00561   bool pWithScreenMessages,
00562   const string& pLogDirPath,
00563   const string& pLogFileNameKernel,
00564   bool pUsesSharedLoggingFile
00565 )
00566 : mActionLoggingUnit
00567   ( pShowActionMessages, 
00568     stdout,
00569     (pLogDirPath + "/" + pLogFileNameKernel + "_action.log"),
00570     pUsesSharedLoggingFile
00571   ),
00572   mWarnLoggingUnit
00573   ( pShowErrorMessages, 
00574     stderr,
00575     (pLogDirPath + "/" + pLogFileNameKernel + "_warn.log"),
00576     pUsesSharedLoggingFile
00577   ),
00578   mErrorLoggingUnit
00579   ( pShowErrorMessages, 
00580     stderr,
00581     (pLogDirPath + "/" + pLogFileNameKernel + "_error.log"),
00582     pUsesSharedLoggingFile
00583   )
00584 {
00585   if (!(pWithScreenMessages))
00586   {
00587     turnOffScreenWriting();
00588   }   
00589 }
00593 LoggingSuitcase::~LoggingSuitcase()
00594 {
00595 
00596 }
00597 /* :-: (4.A.2) public getter -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00598 /* :-: (4.A.3) public setter -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00599 /* :-: (4.A.4) public others -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00600 
00601 /* --- (4.B) - private ---------------------------------------- */
00602 /* :-: (4.B.1) private constructors, inits & operators -:-:-:-: */
00603 
00618 /* :-: (4.B.2) private getter -:-:-:-:-:-:-:-:-:-::-:-:-:-:-:-: */
00619 /* :-: (4.B.3) private setter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00620 /* :-: (4.B.4) private others :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00621 
00622 /* --- (4.C) - protected -------------------------------------- */
00623 /* :-: (4.C.1) protected getter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00624 /* :-: (4.C.2) protected setter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00625 /* :-: (4.C.3) protected others :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00626 
00627 /* &&& (X) &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */
00628 /* &&&     methods for class &&& */
00629 
00630 /* --- (X.A) - public ----------------------------------------- */
00631 /* :-: (X.A.1) public constructors, inits & operators :-:-:-:-: */
00632 /* :-: (X.A.2) public getter -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00633 /* :-: (X.A.3) public setter -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00634 /* :-: (X.A.4) public others -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00635 
00636 /* --- (X.B) - private ---------------------------------------- */
00637 /* :-: (X.B.1) private constructors, inits & operators -:-:-:-: */
00638 /* :-: (X.B.2) private getter -:-:-:-:-:-:-:-:-:-::-:-:-:-:-:-: */
00639 /* :-: (X.B.3) private setter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00640 /* :-: (X.B.4) private others :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00641 
00642 /* --- (X.C) - protected -------------------------------------- */
00643 /* :-: (X.C.1) protected getter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00644 /* :-: (X.C.2) protected setter :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00645 /* :-: (X.C.3) protected others :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: */
00646 

Generated on Sat Mar 4 11:54:46 2006 for MRPROJEXT by  doxygen 1.4.1