00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include <iostream.h>
00028 #include <time.h>
00029 #include "muo_logging.h"
00030 using namespace krmuo;
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
00133
00134
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
00445
00446
00461
00462
00463
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
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
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
00598
00599
00600
00601
00602
00603
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646