KSquare Utilities
RunLog.cpp
Go to the documentation of this file.
1 /* RunLog.cpp -- Logging Class.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include "FirstIncludes.h"
6 
7 #include <stdio.h>
8 #include <string>
9 #include <time.h>
10 #include <fstream>
11 #include <iostream>
12 #include <string.h>
13 #include <string>
14 #include <sstream>
15 #include <vector>
16 #include "MemoryDebug.h"
17 using namespace std;
18 
19 #include "KKBaseTypes.h"
20 #include "DateTime.h"
21 #include "GoalKeeper.h"
22 #include "GlobalGoalKeeper.h"
23 #include "MsgQueue.h"
24 #include "RunLog.h"
25 #include "OSservices.h"
26 using namespace KKB;
27 
28 
29 
30 
31 
33  msgQueue (NULL),
34  ourLogFile (NULL)
35 {
36  logFile = &cout;
37  ourLogFile = NULL;
38  GetLoggingLevel ();
39  lineCount = 0;
40 }
41 
42 
43 
44 RunLog::RunLog (const char* _fileName):
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 }
55 
56 
57 RunLog::RunLog (const KKStr& _fileName):
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 }
67 
68 
69 
70 RunLog::RunLog (std::ostream& logStream):
71  msgQueue (NULL),
72  ourLogFile (NULL)
73 {
74  fileName = "";
75  lineCount = 0;
76  ourLogFile = NULL;
77  logFile = &logStream;
78  GetLoggingLevel ();
79 }
80 
81 
82 
83 RunLog::RunLog (std::ostream* logStream):
84  msgQueue (NULL),
85  ourLogFile (NULL)
86 {
87  fileName = "";
88  lineCount = 0;
89  ourLogFile = NULL;
90  logFile = logStream;
91  GetLoggingLevel ();
92 }
93 
94 
95 
96 RunLog::RunLog (MsgQueuePtr _msgQueue):
97  msgQueue (_msgQueue),
98  ourLogFile (NULL)
99 {
100  logFile = NULL;
101  ourLogFile = NULL;
102  GetLoggingLevel ();
103  lineCount = 0;
104 }
105 
106 
107 
109 {
110  if (ourLogFile)
111  delete ourLogFile;
112 }
113 
114 
115 
117 {
118  return sizeof (RunLog) +
119  curLine.MemoryConsumedEstimated () +
120  fileName.MemoryConsumedEstimated () +
121  lastLine.MemoryConsumedEstimated () +
122  sizeof (*ourLogFile);
123 } /* MemoryConsumedEstimated */
124 
125 
126 
127 void RunLog::AttachFile (const KKStr& _fileName)
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 */
140 
141 
142 
143 void RunLog::AttachFileAppend (const KKStr& _fileName)
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 */
156 
157 
158 
159 void RunLog::AttachMsgQueue (MsgQueuePtr _msgQueue)
160 {
161  msgQueue = _msgQueue;
162 }
163 
164 
165 
166 void RunLog::AttachFile (std::ostream& _logFile)
167 {
168  fileName = "";
169 
170  if (ourLogFile)
171  {
172  delete ourLogFile;
173  ourLogFile = NULL;
174  }
175 
176  logFile = &_logFile;
177 } /* AttachFile */
178 
179 
180 
182 {
183  fileName = "";
184  logFile = &cout;
185 
186  if (ourLogFile)
187  delete ourLogFile;
188  ourLogFile = NULL;
189 }
190 
191 
192 
194 {
195  return fileName;
196 }
197 
198 
199 
200 void RunLog::GetLoggingLevel ()
201 {
202  curLevel = 0;
203  loggingLevel = 10;
204  lineEmpty = true;
205 
206  procId = osGetProcessId ();
207 }
208 
209 
210 
211 void RunLog::SetLoggingLevel (kkint32 _loggingLevel)
212 {
213  curLevel = 0;
214  loggingLevel = _loggingLevel;
215  lineEmpty = true;
216 }
217 
218 
219 
221 {
222  if (!lineEmpty)
223  Append ("\n");
224 
225  curLevel = _level;
226  lineEmpty = true;
227  return *this;
228 }
229 
230 
231 
232 void RunLog::DisplayTimeStamp ()
233 {
234  if (!logFile)
235  return;
236 
237  if (!lineEmpty)
238  return;
239 
240  if (curLevel > loggingLevel)
241  return;
242 
243  DateTime curTime = osGetLocalDateTime ();
244  if (procId > 0)
245  (*logFile) << procId << " - " ;
246  (*logFile) << curTime.Time () << "->";
247 
248  lineEmpty = false;
249 } /* DisplayTimeStamp */
250 
251 
252 
253 void RunLog::Append (const char* str)
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 */
293 
294 
295 
296 void RunLog::Flush ()
297 {
298  if (logFile)
299  {
300  (*logFile).flush ();
301  }
302 }
303 
304 
305 RunLog& RunLog::operator<< (bool right)
306 {
307  if (right)
308  Append ("True");
309  else
310  Append ("False");
311  return *this;
312 }
313 
314 
315 
316 
317 RunLog& RunLog::operator<< (kkint16 right)
318 {
319  KKStr s (30);
320  s = StrFormatInt (right, "0");
321  Append (s.Str ());
322  return *this;
323 }
324 
325 
326 
327 RunLog& RunLog::operator<< (kkuint16 right)
328 {
329  KKStr s (30);
330  s = StrFormatInt (right, "0");
331  Append (s.Str ());
332  return *this;
333 }
334 
335 
336 
337 RunLog& RunLog::operator<< (kkint32 right)
338 {
339  KKStr s (30);
340  s.AppendInt32 (right);
341  Append (s.Str ());
342  return *this;
343 }
344 
345 
346 
347 RunLog& RunLog::operator<< (kkuint32 right)
348 {
349  KKStr s (30);
350  s.AppendUInt32 (right);
351  Append (s.Str ());
352  return *this;
353 }
354 
355 
356 
357 RunLog& RunLog::operator<< (kkint64 right)
358 {
359  KKStr s (30);
360  s = StrFormatInt64 (right, "0");
361  Append (s.Str ());
362  return *this;
363 }
364 
365 
366 
367 RunLog& RunLog::operator<< (kkuint64 right)
368 {
369  KKStr s (30);
370  s = StrFormatInt64 (right, "0");
371  Append (s.Str ());
372  return *this;
373 }
374 
375 
376 
377 RunLog& RunLog::operator<< (double right)
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 }
390 
391 
392 
393 RunLog& RunLog::operator<< (char right)
394 {
395  char buff[20];
396  buff[0] = right;
397  buff[1] = 0;
398  Append (buff);
399  return *this;
400 }
401 
402 
403 
404 RunLog& RunLog::operator<< (const char* right)
405 {
406  Append (right);
407  return *this;
408 }
409 
410 
411 
412 RunLog& RunLog::operator<< (const KKStr& right)
413 {
414  Append (right.Str ());
415  return *this;
416 }
417 
418 
419 
420 RunLog& RunLog::operator<< (KKStrConstPtr right)
421 {
422  if (right)
423  Append (right->Str ());
424  return *this;
425 }
426 
427 
428 
429 RunLog& RunLog::operator<< (const VectorKKStr& right)
430 {
431  VectorKKStr::const_iterator idx;
432  for (idx = right.begin (); idx != right.end (); ++idx)
433  (*this) << *idx << endl;
434  return *this;
435 }
436 
437 
438 
439 
440 RunLog& RunLog::operator<< (ostream& (* mf)(ostream &))
441 {
442  if (curLevel <= loggingLevel)
443  {
444  ostringstream o;
445  mf (o);
446  Append (o.str ().c_str ());
447  }
448  return *this;
449 }
450 
451 
452 
453 void RunLog::WriteLine (const KKStr& s)
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 }
476 
477 
478 
479 void RunLog::WriteLine (const char* s)
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 }
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
void AppendInt32(kkint32 i)
Definition: KKStr.cpp:1901
void DetachFile()
Closes out current log file and starts writing to stdout aka &#39;cout&#39;.
Definition: RunLog.cpp:181
KKStr StrFormatInt64(kkint64 val, const char *mask)
Definition: KKStr.cpp:5013
RunLog(const char *_fileName)
Creates instance with a log messages written to file &#39;fileName&#39;.
Definition: RunLog.cpp:44
__int32 kkint32
Definition: KKBaseTypes.h:88
KKB::DateTime osGetLocalDateTime()
Returned the current local date and time.
void AddMsg(const KKStr &msg)
Definition: MsgQueue.cpp:76
RunLog(std::ostream &errorLogStream)
Creates instance where logging messages are written to file stream specified by &#39;errorLogStream&#39;.
Definition: RunLog.cpp:70
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
KKStr & operator=(const char *src)
Definition: KKStr.cpp:1442
kkint32 osGetThreadId()
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
__int64 kkint64
Definition: KKBaseTypes.h:90
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
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 ...
Definition: RunLog.cpp:453
KKTHread * KKTHreadPtr
RunLog(const KKStr &_fileName)
Creates instance with a log messages written to file &#39;fileName&#39;.
Definition: RunLog.cpp:57
void AppendUInt32(kkuint32 i)
Definition: KKStr.cpp:1940
RunLog()
Creates an instance were logging will go to the console.
Definition: RunLog.cpp:32
kkint32 osGetProcessId()
kkint32 MemoryConsumedEstimated() const
Definition: RunLog.cpp:116
bool Empty() const
Definition: KKStr.h:241
void AttachMsgQueue(MsgQueuePtr _msgQueue)
All activity that is written to the log file will be added as messages to &#39;_msgQueue&#39;.
Definition: RunLog.cpp:159
RunLog(std::ostream *logStream)
Creates instance where logging messages are written to file stream pointed to by &#39;logStream&#39;.
Definition: RunLog.cpp:83
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
unsigned __int64 kkuint64
Definition: KKBaseTypes.h:91
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
void AttachFileAppend(const KKStr &_fileName)
Call this method to start appending logging messages to a different file.
Definition: RunLog.cpp:143
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void SetLoggingLevel(kkint32 _loggingLevel)
Definition: RunLog.cpp:211
void AttachFile(const KKStr &_fileName)
Call this method to start logging messages to a different file.
Definition: RunLog.cpp:127
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
Used for logging messages.
Definition: RunLog.h:49
void Flush()
Definition: RunLog.cpp:296
void AttachFile(std::ostream &_logFile)
Call this method to start logging messages to a specific file stream.
Definition: RunLog.cpp:166
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 WriteLine(const char *s)
Writes a line of text to log file; does not check logging level; and does not write any preview text ...
Definition: RunLog.cpp:479
KKStr FileName()
Definition: RunLog.cpp:193
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
const TimeType & Time() const
Definition: DateTime.h:232
RunLog(MsgQueuePtr _msgQueue)
Definition: RunLog.cpp:96