KSquare Utilities
KKB::RunLog Class Reference

Used for logging messages. More...

#include <RunLog.h>

Public Member Functions

 RunLog ()
 Creates an instance were logging will go to the console. More...
 
 RunLog (const char *_fileName)
 Creates instance with a log messages written to file 'fileName'. More...
 
 RunLog (const KKStr &_fileName)
 Creates instance with a log messages written to file 'fileName'. More...
 
 RunLog (std::ostream &errorLogStream)
 Creates instance where logging messages are written to file stream specified by 'errorLogStream'. More...
 
 RunLog (std::ostream *logStream)
 Creates instance where logging messages are written to file stream pointed to by 'logStream'. More...
 
 RunLog (MsgQueuePtr _msgQueue)
 
 ~RunLog ()
 
void Append (const char *str)
 Appends string to log; generally used by other Log methods but can be called by public. More...
 
void AttachFile (const KKStr &_fileName)
 Call this method to start logging messages to a different file. More...
 
void AttachFile (std::ostream &_logFile)
 Call this method to start logging messages to a specific file stream. More...
 
void AttachFileAppend (const KKStr &_fileName)
 Call this method to start appending logging messages to a different file. More...
 
void AttachMsgQueue (MsgQueuePtr _msgQueue)
 All activity that is written to the log file will be added as messages to '_msgQueue'. More...
 
void DetachFile ()
 Closes out current log file and starts writing to stdout aka 'cout'. More...
 
KKStr FileName ()
 
void Flush ()
 
const KKStrLastLine ()
 Returns the last line of text written to the log file. More...
 
RunLogLevel (kkint32 _level)
 
kkint32 Level () const
 
kkint32 LineCount () const
 
kkint32 MemoryConsumedEstimated () const
 
RunLogoperator<< (bool right)
 
RunLogoperator<< (kkint16 right)
 
RunLogoperator<< (kkuint16 right)
 
RunLogoperator<< (kkint32 right)
 
RunLogoperator<< (kkuint32 right)
 
RunLogoperator<< (kkint64 right)
 
RunLogoperator<< (kkuint64 right)
 
RunLogoperator<< (double right)
 
RunLogoperator<< (char right)
 
RunLogoperator<< (const char *right)
 
RunLogoperator<< (const KKStr &right)
 
RunLogoperator<< (KKStrConstPtr right)
 
RunLogoperator<< (const VectorKKStr &right)
 
RunLogoperator<< (std::ostream &(*mf)(std::ostream &))
 
void SetLevel (kkint32 _level)
 
void SetLoggingLevel (kkint32 _loggingLevel)
 
void WriteLine (const KKStr &s)
 Writes a line of text to log file; does not check logging level; and does not write any preview text such as DateTime or processId. More...
 
void WriteLine (const char *s)
 Writes a line of text to log file; does not check logging level; and does not write any preview text such as DateTime or processId. More...
 

Detailed Description

Used for logging messages.

Has facilities to deal with different levels of logging. You use the normal insertion operators to write to this object. You can specify at any point a Logging level. If this level is less than or equal to a specified threshold the message will be recorded in the log. <br > The idea is that during normal production runs log level would be set to 10. But if there appears to be some issue then you would set the logging level to a higher level to get more details. The higher the level the more detail you will get.

Definition at line 49 of file RunLog.h.

Constructor & Destructor Documentation

RunLog::RunLog ( )

Creates an instance were logging will go to the console.

Definition at line 32 of file RunLog.cpp.

Referenced by KKB::Application::Application(), and KKMLL::FactoryFVProducer::RegisterFactory().

32  :
33  msgQueue (NULL),
34  ourLogFile (NULL)
35 {
36  logFile = &cout;
37  ourLogFile = NULL;
38  GetLoggingLevel ();
39  lineCount = 0;
40 }
RunLog::RunLog ( const char *  _fileName)

Creates instance with a log messages written to file 'fileName'.

Definition at line 44 of file RunLog.cpp.

References KKB::KKStr::operator=().

44  :
45  msgQueue (NULL),
46  ourLogFile (NULL)
47 {
48  fileName = _fileName;
49  lineCount = 0;
50 
51  ourLogFile = new ofstream (fileName.Str ());
52  logFile = ourLogFile;
53  GetLoggingLevel ();
54 }
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
RunLog::RunLog ( const KKStr _fileName)

Creates instance with a log messages written to file 'fileName'.

Definition at line 57 of file RunLog.cpp.

References KKB::KKStr::operator=().

57  :
58  msgQueue (NULL),
59  ourLogFile (NULL)
60 {
61  fileName = _fileName;
62  lineCount = 0;
63  ourLogFile = new ofstream (fileName.Str ());
64  logFile = ourLogFile;
65  GetLoggingLevel ();
66 }
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
RunLog::RunLog ( std::ostream &  errorLogStream)

Creates instance where logging messages are written to file stream specified by 'errorLogStream'.

Definition at line 70 of file RunLog.cpp.

References KKB::KKStr::operator=().

70  :
71  msgQueue (NULL),
72  ourLogFile (NULL)
73 {
74  fileName = "";
75  lineCount = 0;
76  ourLogFile = NULL;
77  logFile = &logStream;
78  GetLoggingLevel ();
79 }
RunLog::RunLog ( std::ostream *  logStream)

Creates instance where logging messages are written to file stream pointed to by 'logStream'.

Definition at line 83 of file RunLog.cpp.

References KKB::KKStr::operator=().

83  :
84  msgQueue (NULL),
85  ourLogFile (NULL)
86 {
87  fileName = "";
88  lineCount = 0;
89  ourLogFile = NULL;
90  logFile = logStream;
91  GetLoggingLevel ();
92 }
RunLog::RunLog ( MsgQueuePtr  _msgQueue)

Definition at line 96 of file RunLog.cpp.

96  :
97  msgQueue (_msgQueue),
98  ourLogFile (NULL)
99 {
100  logFile = NULL;
101  ourLogFile = NULL;
102  GetLoggingLevel ();
103  lineCount = 0;
104 }
RunLog::~RunLog ( )

summary> Sets current message level you would normally call this method just before you wish to start logging a message. If you are not changing the level since previous call then no need to call. /summary>

Definition at line 108 of file RunLog.cpp.

109 {
110  if (ourLogFile)
111  delete ourLogFile;
112 }

Member Function Documentation

void RunLog::Append ( const char *  str)

Appends string to log; generally used by other Log methods but can be called by public.

Definition at line 253 of file RunLog.cpp.

References KKB::GlobalGoalKeeper::EndBlock(), Flush(), KKB::operator<<(), KKB::osGetThreadId(), and KKB::GlobalGoalKeeper::StartBlock().

Referenced by Level(), operator<<(), and WriteLine().

254 {
255  if ((curLevel > loggingLevel) || (str == NULL))
256  return;
257 
259 
260  procId = osGetThreadId ();
261 
262  if (logFile)
263  {
264  if (lineEmpty)
265  DisplayTimeStamp ();
266  *logFile << str;
267  }
268 
269  if (strcmp (str, "\n") == 0)
270  {
271  if (msgQueue)
272  {
273  KKStrPtr msgStr = new KKStr (curLine.Len () + 10);
274  *msgStr << procId << " - " << osGetLocalDateTime ().Time () << "->" << curLine;
275  msgQueue->AddMsg (msgStr);
276  }
277  lastLine = curLine;
278  curLine = "";
279  lineCount++;
280  lineEmpty = true;
281  }
282  else
283  {
284  curLine << str;
285  lineEmpty = false;
286  }
287 
288  if (curLevel <= 10)
289  Flush ();
290 
292 } /* Append */
void AddMsg(KKStrPtr msg)
Take ownership of &#39;msg&#39; and add to end of the queue.
Definition: MsgQueue.cpp:58
KKB::DateTime osGetLocalDateTime()
Returned the current local date and time.
kkint32 osGetThreadId()
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
void Flush()
Definition: RunLog.cpp:296
const TimeType & Time() const
Definition: DateTime.h:232
void RunLog::AttachFile ( const KKStr _fileName)

Call this method to start logging messages to a different file.

Will close out current log file and open new one specified by 'fileName'.

Definition at line 127 of file RunLog.cpp.

References KKB::KKStr::operator=().

Referenced by KKB::Application::InitalizeApplication().

128 {
129  fileName = _fileName;
130 
131  ofstream* newLogFile = new ofstream (fileName.Str ());
132 
133  logFile = newLogFile;
134 
135  if (ourLogFile)
136  delete ourLogFile;
137 
138  ourLogFile = newLogFile;
139 } /* AttachFile */
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void RunLog::AttachFile ( std::ostream &  _logFile)

Call this method to start logging messages to a specific file stream.

Will close out current log file and write to the specified '_logFile' in the future.

Definition at line 166 of file RunLog.cpp.

References KKB::KKStr::operator=().

167 {
168  fileName = "";
169 
170  if (ourLogFile)
171  {
172  delete ourLogFile;
173  ourLogFile = NULL;
174  }
175 
176  logFile = &_logFile;
177 } /* AttachFile */
void RunLog::AttachFileAppend ( const KKStr _fileName)

Call this method to start appending logging messages to a different file.

Will close out current log file and open new one specified by 'fileName' with the append flag set.

Definition at line 143 of file RunLog.cpp.

References KKB::KKStr::operator=().

144 {
145  fileName = _fileName;
146 
147  ofstream* newLogFile = new ofstream (fileName.Str (), ios::app);
148 
149  logFile = newLogFile;
150 
151  if (ourLogFile)
152  delete ourLogFile;
153 
154  ourLogFile = newLogFile;
155 } /* AttachFileAppend */
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void RunLog::AttachMsgQueue ( MsgQueuePtr  _msgQueue)

All activity that is written to the log file will be added as messages to '_msgQueue'.

Definition at line 159 of file RunLog.cpp.

160 {
161  msgQueue = _msgQueue;
162 }
void RunLog::DetachFile ( )

Closes out current log file and starts writing to stdout aka 'cout'.

Definition at line 181 of file RunLog.cpp.

References KKB::KKStr::operator=().

182 {
183  fileName = "";
184  logFile = &cout;
185 
186  if (ourLogFile)
187  delete ourLogFile;
188  ourLogFile = NULL;
189 }
KKStr RunLog::FileName ( )

Definition at line 193 of file RunLog.cpp.

194 {
195  return fileName;
196 }
void RunLog::Flush ( )

Definition at line 296 of file RunLog.cpp.

Referenced by Append().

297 {
298  if (logFile)
299  {
300  (*logFile).flush ();
301  }
302 }
const KKStr& KKB::RunLog::LastLine ( )
inline

Returns the last line of text written to the log file.

Definition at line 117 of file RunLog.h.

117 {return lastLine;}
RunLog & RunLog::Level ( kkint32  _level)

Definition at line 220 of file RunLog.cpp.

References Append().

Referenced by KKMLL::TrainingProcess2::BuildTrainingProcess().

221 {
222  if (!lineEmpty)
223  Append ("\n");
224 
225  curLevel = _level;
226  lineEmpty = true;
227  return *this;
228 }
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
kkint32 KKB::RunLog::Level ( ) const
inline

Definition at line 78 of file RunLog.h.

78 {return curLevel;}
kkint32 KKB::RunLog::LineCount ( ) const
inline

Definition at line 119 of file RunLog.h.

119 {return lineCount;}
kkint32 RunLog::MemoryConsumedEstimated ( ) const

Definition at line 116 of file RunLog.cpp.

117 {
118  return sizeof (RunLog) +
119  curLine.MemoryConsumedEstimated () +
120  fileName.MemoryConsumedEstimated () +
121  lastLine.MemoryConsumedEstimated () +
122  sizeof (*ourLogFile);
123 } /* MemoryConsumedEstimated */
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
RunLog()
Creates an instance were logging will go to the console.
Definition: RunLog.cpp:32
RunLog & RunLog::operator<< ( bool  right)

Definition at line 305 of file RunLog.cpp.

References Append().

306 {
307  if (right)
308  Append ("True");
309  else
310  Append ("False");
311  return *this;
312 }
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( kkint16  right)

Definition at line 317 of file RunLog.cpp.

References Append(), KKB::KKStr::Concat(), KKB::KKStr::KKStr(), KKB::KKStr::operator=(), KKB::KKStr::Str(), and KKB::StrFormatInt().

318 {
319  KKStr s (30);
320  s = StrFormatInt (right, "0");
321  Append (s.Str ());
322  return *this;
323 }
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( kkuint16  right)

Definition at line 327 of file RunLog.cpp.

References Append(), KKB::KKStr::Concat(), KKB::KKStr::KKStr(), KKB::KKStr::operator=(), KKB::KKStr::Str(), and KKB::StrFormatInt().

328 {
329  KKStr s (30);
330  s = StrFormatInt (right, "0");
331  Append (s.Str ());
332  return *this;
333 }
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( kkint32  right)

Definition at line 337 of file RunLog.cpp.

References Append(), KKB::KKStr::AppendInt32(), KKB::KKStr::Concat(), KKB::KKStr::KKStr(), and KKB::KKStr::Str().

338 {
339  KKStr s (30);
340  s.AppendInt32 (right);
341  Append (s.Str ());
342  return *this;
343 }
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( kkuint32  right)

Definition at line 347 of file RunLog.cpp.

References Append(), KKB::KKStr::AppendUInt32(), KKB::KKStr::Concat(), KKB::KKStr::KKStr(), and KKB::KKStr::Str().

348 {
349  KKStr s (30);
350  s.AppendUInt32 (right);
351  Append (s.Str ());
352  return *this;
353 }
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( kkint64  right)

Definition at line 357 of file RunLog.cpp.

References Append(), KKB::KKStr::Concat(), KKB::KKStr::KKStr(), KKB::KKStr::operator=(), KKB::KKStr::Str(), and KKB::StrFormatInt64().

358 {
359  KKStr s (30);
360  s = StrFormatInt64 (right, "0");
361  Append (s.Str ());
362  return *this;
363 }
KKStr StrFormatInt64(kkint64 val, const char *mask)
Definition: KKStr.cpp:5013
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( kkuint64  right)

Definition at line 367 of file RunLog.cpp.

References Append(), KKB::KKStr::Concat(), KKB::KKStr::KKStr(), KKB::KKStr::operator=(), KKB::KKStr::Str(), and KKB::StrFormatInt64().

368 {
369  KKStr s (30);
370  s = StrFormatInt64 (right, "0");
371  Append (s.Str ());
372  return *this;
373 }
KKStr StrFormatInt64(kkint64 val, const char *mask)
Definition: KKStr.cpp:5013
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( double  right)

Definition at line 377 of file RunLog.cpp.

References Append().

378 {
379  char buff[50];
380 
381 # ifdef USE_SECURE_FUNCS
382  sprintf_s (buff, sizeof (buff), "%f", right);
383 # else
384  sprintf (buff, "%f", right);
385 # endif
386 
387  Append (buff);
388  return *this;
389 }
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( char  right)

Definition at line 393 of file RunLog.cpp.

References Append().

394 {
395  char buff[20];
396  buff[0] = right;
397  buff[1] = 0;
398  Append (buff);
399  return *this;
400 }
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( const char *  right)

Definition at line 404 of file RunLog.cpp.

References Append().

405 {
406  Append (right);
407  return *this;
408 }
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( const KKStr right)

Definition at line 412 of file RunLog.cpp.

References Append(), and KKB::KKStr::Str().

413 {
414  Append (right.Str ());
415  return *this;
416 }
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( KKStrConstPtr  right)

Definition at line 420 of file RunLog.cpp.

References Append(), and KKB::KKStr::Str().

421 {
422  if (right)
423  Append (right->Str ());
424  return *this;
425 }
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
RunLog & RunLog::operator<< ( const VectorKKStr right)

Definition at line 429 of file RunLog.cpp.

430 {
431  VectorKKStr::const_iterator idx;
432  for (idx = right.begin (); idx != right.end (); ++idx)
433  (*this) << *idx << endl;
434  return *this;
435 }
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
RunLog& KKB::RunLog::operator<< ( std::ostream &(*)(std::ostream &)  mf)
void KKB::RunLog::SetLevel ( kkint32  _level)
inline

Definition at line 157 of file RunLog.h.

157 {loggingLevel = _level;}
void RunLog::SetLoggingLevel ( kkint32  _loggingLevel)

Definition at line 211 of file RunLog.cpp.

212 {
213  curLevel = 0;
214  loggingLevel = _loggingLevel;
215  lineEmpty = true;
216 }
void RunLog::WriteLine ( const KKStr s)

Writes a line of text to log file; does not check logging level; and does not write any preview text such as DateTime or processId.

If there is any data in Current-Line it will be write out first before writing this line of text.

Definition at line 453 of file RunLog.cpp.

References KKB::MsgQueue::AddMsg(), Append(), KKB::KKStr::Empty(), KKB::GlobalGoalKeeper::EndBlock(), KKB::KKStr::operator=(), and KKB::GlobalGoalKeeper::StartBlock().

454 {
456 
457  if (!curLine.Empty ())
458  Append ("\n");
459 
460  if (logFile)
461  {
462  logFile->write (s.Str (), s.Len ());
463  (*logFile) << endl;
464  }
465 
466  if (msgQueue)
467  msgQueue->AddMsg (s);
468 
469  lastLine = s;
470  curLine = "";
471  lineCount++;
472  lineEmpty = true;
473 
475 }
void AddMsg(KKStrPtr msg)
Take ownership of &#39;msg&#39; and add to end of the queue.
Definition: MsgQueue.cpp:58
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
bool Empty() const
Definition: KKStr.h:241
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253
void RunLog::WriteLine ( const char *  s)

Writes a line of text to log file; does not check logging level; and does not write any preview text such as DateTime or processId.

If there is any data in Current-Line it will be write out first before writing this line of text.

Definition at line 479 of file RunLog.cpp.

References KKB::MsgQueue::AddMsg(), Append(), KKB::KKStr::Empty(), KKB::GlobalGoalKeeper::EndBlock(), KKB::KKStr::operator=(), and KKB::GlobalGoalKeeper::StartBlock().

480 {
482 
483  if (curLine.Empty ())
484  Append ("\n");
485 
486  if (logFile)
487  (*logFile) << s << endl;
488 
489  if (msgQueue)
490  msgQueue->AddMsg (s);
491 
492  lastLine = s;
493  curLine = "";
494  lineCount++;
495  lineEmpty = true;
496 
498 }
void AddMsg(KKStrPtr msg)
Take ownership of &#39;msg&#39; and add to end of the queue.
Definition: MsgQueue.cpp:58
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
bool Empty() const
Definition: KKStr.h:241
void Append(const char *str)
Appends string to log; generally used by other Log methods but can be called by public.
Definition: RunLog.cpp:253

The documentation for this class was generated from the following files: