NLMISC::CFileDisplayer Class Reference

#include <displayer.h>

Inheritance diagram for NLMISC::CFileDisplayer:

NLMISC::IDisplayer

Detailed Description

File displayer. Put string into a file. log_howto
Author:
Vianney Lecroart

Nevrax France

Date:
2000

Definition at line 122 of file displayer.h.

Public Member Functions

 CFileDisplayer ()
 CFileDisplayer (const std::string &filename, bool eraseLastLog=false, const char *displayerName="", bool raw=false)
 Constructor.

void display (const CLog::TDisplayInfo &args, const char *message)
 Display the string where it does.

void setParam (const std::string &filename, bool eraseLastLog=false)
 Set Parameter of the displayer if not set at the ctor time.

 ~CFileDisplayer ()

Static Public Member Functions

const char * dateToComputerString (time_t date)
 Convert date to "784551148" string (time in second from 1975).

const char * dateToHumanString (time_t date)
 Convert date to "2000/01/14 10:05:17" string.

const char * dateToHumanString ()
 Convert the current date to human string.

const char * logTypeToString (CLog::TLogType logType, bool longFormat=false)
 Convert log type to string.


Data Fields

std::string DisplayerName
 This is the idenfiant for a displayer, it is used to fond or remove a displayer.


Protected Member Functions

virtual void doDisplay (const CLog::TDisplayInfo &args, const char *message)
 Put the string into the file.


Static Protected Member Functions

const char * HeaderString ()

Private Attributes

std::string _FileName
FILE * _FilePointer
uint _LastLogSizeChecked
bool _NeedHeader
bool _Raw


Constructor & Destructor Documentation

NLMISC::CFileDisplayer::CFileDisplayer const std::string &  filename,
bool  eraseLastLog = false,
const char *  displayerName = "",
bool  raw = false
 

Constructor.

Definition at line 328 of file displayer.cpp.

References _FilePointer, and setParam().

00328                                                                                                                  :
00329         IDisplayer (displayerName), _NeedHeader(true), _LastLogSizeChecked(0), _Raw(raw)
00330 {
00331         _FilePointer = (FILE*)1;
00332         setParam (filename, eraseLastLog);
00333 }

NLMISC::CFileDisplayer::CFileDisplayer  ) 
 

Definition at line 335 of file displayer.cpp.

References _FilePointer.

00335                                 :
00336         IDisplayer (""), _NeedHeader(true), _LastLogSizeChecked(0), _Raw(false)
00337 {
00338         _FilePointer = (FILE*)1;
00339 }

NLMISC::CFileDisplayer::~CFileDisplayer  ) 
 

Definition at line 341 of file displayer.cpp.

References _FilePointer.

00342 {
00343         if (_FilePointer > (FILE*)1)
00344         {
00345                 fclose(_FilePointer);
00346                 _FilePointer = NULL;
00347         }
00348 }


Member Function Documentation

const char * NLMISC::IDisplayer::dateToComputerString time_t  date  )  [static, inherited]
 

Convert date to "784551148" string (time in second from 1975).

Definition at line 103 of file displayer.cpp.

References NLMISC::smprintf().

00104 {
00105         static char cstime[25];
00106         smprintf (cstime, 25, "%ld", &date);
00107         return cstime;
00108 }

const char * NLMISC::IDisplayer::dateToHumanString time_t  date  )  [static, inherited]
 

Convert date to "2000/01/14 10:05:17" string.

Definition at line 92 of file displayer.cpp.

References uint32.

00093 {
00094         static char cstime[25];
00095         struct tm *tms = localtime(&date);
00096         if (tms)
00097                 strftime (cstime, 25, "%Y/%m/%d %H:%M:%S", tms);
00098         else
00099                 sprintf(cstime, "bad date %d", (uint32)date);
00100         return cstime;
00101 }

const char * NLMISC::IDisplayer::dateToHumanString  )  [static, inherited]
 

Convert the current date to human string.

Definition at line 85 of file displayer.cpp.

Referenced by NLNET::CNetDisplayer::doDisplay(), NLMISC::CMemDisplayer::doDisplay(), NLMISC::CMsgBoxDisplayer::doDisplay(), doDisplay(), and NLMISC::IDisplayer::HeaderString().

00086 {
00087         time_t date;
00088         time (&date);
00089         return dateToHumanString (date);
00090 }

void NLMISC::IDisplayer::display const CLog::TDisplayInfo args,
const char *  message
[inherited]
 

Display the string where it does.

Definition at line 132 of file displayer.cpp.

References NLMISC::IDisplayer::doDisplay().

00133 {
00134         _Mutex->enter();
00135         try
00136         {
00137                 doDisplay( args, message );
00138         }
00139         catch (Exception &)
00140         {
00141                 // silence
00142         }
00143         _Mutex->leave();
00144 }

void NLMISC::CFileDisplayer::doDisplay const CLog::TDisplayInfo args,
const char *  message
[protected, virtual]
 

Put the string into the file.

Implements NLMISC::IDisplayer.

Definition at line 379 of file displayer.cpp.

References _FilePointer, _LastLogSizeChecked, _NeedHeader, _Raw, NLMISC::CLog::TDisplayInfo::CallstackAndLog, NLMISC::CLog::TDisplayInfo::Date, NLMISC::IDisplayer::dateToHumanString(), NLMISC::CLog::TDisplayInfo::Filename, NLMISC::IDisplayer::HeaderString(), NLMISC::CLog::TDisplayInfo::Line, NLMISC::CLog::TDisplayInfo::LogType, NLMISC::IDisplayer::logTypeToString(), NLMISC::CLog::TDisplayInfo::ProcessName, res, NLMISC::CLog::TDisplayInfo::ThreadId, and NLMISC::toString().

00380 {
00381         bool needSpace = false;
00382         //stringstream ss;
00383         string str;
00384 
00385         // if the filename is not set, don't log
00386         if (_FileName.empty()) return;
00387 
00388         if (args.Date != 0 && !_Raw)
00389         {
00390                 str += dateToHumanString(args.Date);
00391                 needSpace = true;
00392         }
00393 
00394         if (args.LogType != CLog::LOG_NO && !_Raw)
00395         {
00396                 if (needSpace) { str += " "; needSpace = false; }
00397                 str += logTypeToString(args.LogType);
00398                 needSpace = true;
00399         }
00400 
00401         // Write thread identifier
00402         if ( args.ThreadId != 0 && !_Raw)
00403         {
00404                 if (needSpace) { str += " "; needSpace = false; }
00405                 str += NLMISC::toString(args.ThreadId);
00406                 needSpace = true;
00407         }
00408 
00409         if (!args.ProcessName.empty() && !_Raw)
00410         {
00411                 if (needSpace) { str += " "; needSpace = false; }
00412                 str += args.ProcessName;
00413                 needSpace = true;
00414         }
00415 
00416         if (args.Filename != NULL && !_Raw)
00417         {
00418                 if (needSpace) { str += " "; needSpace = false; }
00419                 str += CFile::getFilename(args.Filename);
00420                 needSpace = true;
00421         }
00422 
00423         if (args.Line != -1 && !_Raw)
00424         {
00425                 if (needSpace) { str += " "; needSpace = false; }
00426                 str += NLMISC::toString(args.Line);
00427                 needSpace = true;
00428         }
00429         
00430         if (needSpace) { str += " : "; needSpace = false; }
00431 
00432         str += message;
00433 
00434         if (_FilePointer > (FILE*)1)
00435         {
00436                 // if the file is too big (>5mb), rename it and create another one (check only after 20 lines to speed up)
00437                 if (_LastLogSizeChecked++ > 20)
00438                 {
00439                   int res = ftell (_FilePointer);
00440                   if (res > 5*1024*1024)
00441                     {
00442                         fclose (_FilePointer);
00443                         rename (_FileName.c_str(), CFile::findNewFile (_FileName).c_str());
00444                         _FilePointer = (FILE*) 1;
00445                         _LastLogSizeChecked = 0;
00446                     }
00447                 }
00448         }
00449 
00450         if (_FilePointer == (FILE*)1)
00451         {
00452                 _FilePointer = fopen (_FileName.c_str(), "at");
00453                 if (_FilePointer == NULL)
00454                         printf ("Can't open log file '%s': %s\n", _FileName.c_str(), strerror (errno));
00455         }
00456         
00457         if (_FilePointer != 0)
00458         {
00459                 if (_NeedHeader)
00460                 {
00461                         const char *hs = HeaderString();
00462                         fwrite (hs, strlen (hs), 1, _FilePointer);
00463                         _NeedHeader = false;
00464                 }
00465                 
00466                 if(!str.empty())
00467                         fwrite (str.c_str(), str.size(), 1, _FilePointer);
00468 
00469                 if(!args.CallstackAndLog.empty())
00470                         fwrite (args.CallstackAndLog.c_str(), args.CallstackAndLog.size (), 1, _FilePointer);
00471 
00472                 fflush (_FilePointer);
00473         }
00474 }

const char * NLMISC::IDisplayer::HeaderString  )  [static, protected, inherited]
 

Definition at line 110 of file displayer.cpp.

References NLMISC::IDisplayer::dateToHumanString(), and NLMISC::smprintf().

Referenced by NLMISC::CMemDisplayer::doDisplay(), and doDisplay().

00111 {
00112         static char header[1024];
00113         smprintf(header, 1024, "\nLog Starting [%s]\n", dateToHumanString());
00114         return header;
00115 }

const char * NLMISC::IDisplayer::logTypeToString CLog::TLogType  logType,
bool  longFormat = false
[static, inherited]
 

Convert log type to string.

Definition at line 77 of file displayer.cpp.

References NLMISC::LogTypeToString.

Referenced by NLMISC::CWindowDisplayer::doDisplay(), NLNET::CNetDisplayer::doDisplay(), NLMISC::CMemDisplayer::doDisplay(), NLMISC::CMsgBoxDisplayer::doDisplay(), doDisplay(), and NLMISC::CStdDisplayer::doDisplay().

00078 {
00079         if (logType < CLog::LOG_NO || logType > CLog::LOG_UNKNOWN)
00080                 return "<NotDefined>";
00081 
00082         return LogTypeToString[longFormat?1:0][logType];
00083 }

void NLMISC::CFileDisplayer::setParam const std::string &  filename,
bool  eraseLastLog = false
 

Set Parameter of the displayer if not set at the ctor time.

Definition at line 350 of file displayer.cpp.

References _FilePointer.

Referenced by CFileDisplayer(), NLMISC::changeLogDirectory(), and NLNET::CUnifiedNetwork::init().

00351 {
00352         _FileName = filename;
00353 
00354         if (filename.empty())
00355         {
00356                 // can't do nlwarning or infinite recurs
00357                 printf ("CFileDisplayer::setParam(): Can't create file with empty filename\n");
00358                 return;
00359         }
00360 
00361         if (eraseLastLog)
00362         {
00363                 ofstream ofs (filename.c_str(), ios::out | ios::trunc);
00364                 if (!ofs.is_open())
00365                 {
00366                         // can't do nlwarning or infinite recurs
00367                         printf ("CFileDisplayer::setParam(): Can't open and clear the log file '%s'\n", filename.c_str());
00368                 }
00369         }
00370 
00371         if (_FilePointer > (FILE*)1)
00372         {
00373                 fclose (_FilePointer);
00374                 _FilePointer = (FILE*)1;
00375         }
00376 }


Field Documentation

std::string NLMISC::CFileDisplayer::_FileName [private]
 

Definition at line 141 of file displayer.h.

FILE* NLMISC::CFileDisplayer::_FilePointer [private]
 

Definition at line 143 of file displayer.h.

Referenced by CFileDisplayer(), doDisplay(), setParam(), and ~CFileDisplayer().

uint NLMISC::CFileDisplayer::_LastLogSizeChecked [private]
 

Definition at line 147 of file displayer.h.

Referenced by doDisplay().

bool NLMISC::CFileDisplayer::_NeedHeader [private]
 

Definition at line 145 of file displayer.h.

Referenced by doDisplay().

bool NLMISC::CFileDisplayer::_Raw [private]
 

Definition at line 149 of file displayer.h.

Referenced by doDisplay().

std::string NLMISC::IDisplayer::DisplayerName [inherited]
 

This is the idenfiant for a displayer, it is used to fond or remove a displayer.

Definition at line 63 of file displayer.h.

Referenced by NLMISC::IDisplayer::IDisplayer().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:13:01 2004 for NeL by doxygen 1.3.6