KSquare Utilities
RunLog.h
Go to the documentation of this file.
1 /* RunLog.h -- Logging Class.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #if !defined(_RUNLOG_)
6 #define _RUNLOG_
7 
8 /**
9  *@class KKB::RunLog
10  *@brief Used for logging messages.
11  *@details
12  * Has facilities to deal with different levels of logging. You use the normal insertion operators to write
13  * to this object. You can specify at any point a Logging level. If this level is less than or equal to a
14  * specified threshold the message will be recorded in the log.
15  * <br >
16  * The idea is that during normal production runs log level would be set to 10. But if there appears to be
17  * some issue then you would set the logging level to a higher level to get more details. The higher the
18  * level the more detail you will get.
19  */
20 
21 /*--------------------------------------------------------------------
22  * History
23  *--------------------------------------------------------------------
24  *
25  * date Programmer Description
26  * --------- ----------- ----------------------------------------
27  * July-2002 Kurt Kramer Original Development.
28  *
29  */
30 
31 
32 #include "KKStr.h"
33 
34 
35 #ifndef WIN32
36 #define __cdecl
37 #endif
38 
39 
40 
41 namespace KKB
42 {
43 
44  #if !defined(_KKU_MSGQUEUE_)
45  class MsgQueue;
46  typedef MsgQueue* MsgQueuePtr;
47  #endif
48 
49  class RunLog
50  {
51  public:
52  /** @brief Creates an instance were logging will go to the console. */
53  RunLog ();
54 
55  /** @brief Creates instance with a log messages written to file 'fileName'. */
56  RunLog (const char* _fileName);
57 
58  /** @brief Creates instance with a log messages written to file 'fileName'. */
59  RunLog (const KKStr& _fileName);
60 
61  /** @brief Creates instance where logging messages are written to file stream specified by 'errorLogStream'. */
62  RunLog (std::ostream& errorLogStream);
63 
64  /** @brief Creates instance where logging messages are written to file stream pointed to by 'logStream'. */
65  RunLog (std::ostream* logStream);
66 
67  RunLog (MsgQueuePtr _msgQueue);
68 
69  ~RunLog ();
70 
71 
72  ///<summary>
73  /// Sets current message level you would normally call this method just before you wish to start logging a
74  /// message. If you are not changing the level since previous call then no need to call.
75  ///</summary>
76  RunLog& Level (kkint32 _level);
77 
78  kkint32 Level () const {return curLevel;}
79 
80 
81  /** @brief Appends string to log; generally used by other Log methods but can be called by public. */
82  void Append (const char* str);
83 
84 
85  /**
86  *@brief Call this method to start logging messages to a different file.
87  *@details Will close out current log file and open new one specified by 'fileName'.
88  */
89  void AttachFile (const KKStr& _fileName);
90 
91 
92  /**
93  *@brief Call this method to start logging messages to a specific file stream.
94  *@details Will close out current log file and write to the specified '_logFile' in the future.
95  */
96  void AttachFile (std::ostream& _logFile);
97 
98 
99  /**
100  *@brief Call this method to start appending logging messages to a different file.
101  *@details Will close out current log file and open new one specified by 'fileName' with the append flag set.
102  */
103  void AttachFileAppend (const KKStr& _fileName);
104 
105 
106  /**
107  *@brief All activity that is written to the log file will be added as messages to '_msgQueue'.
108  */
109  void AttachMsgQueue (MsgQueuePtr _msgQueue);
110 
111  /** @brief Closes out current log file and starts writing to stdout aka 'cout'. */
112  void DetachFile ();
113 
114  KKStr FileName ();
115 
116  /** @brief Returns the last line of text written to the log file. */
117  const KKStr& LastLine () {return lastLine;}
118 
119  kkint32 LineCount () const {return lineCount;}
120 
121  void SetLoggingLevel (kkint32 _loggingLevel);
122 
123 
124  /**
125  *@brief 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.
126  *@details If there is any data in Current-Line it will be write out first before writing this line of text.
127  */
128  void WriteLine (const KKStr& s);
129 
130  /**
131  *@brief 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.
132  *@details If there is any data in Current-Line it will be write out first before writing this line of text.
133  */
134  void WriteLine (const char* s);
135 
136 
137  RunLog& operator<< (bool right);
138  RunLog& operator<< (kkint16 right);
139  RunLog& operator<< (kkuint16 right);
140  RunLog& operator<< (kkint32 right);
141  RunLog& operator<< (kkuint32 right);
142  RunLog& operator<< (kkint64 right);
143  RunLog& operator<< (kkuint64 right);
144  RunLog& operator<< (double right);
145  RunLog& operator<< (char right);
146  RunLog& operator<< (const char* right);
147  RunLog& operator<< (const KKStr& right);
148  RunLog& operator<< (KKStrConstPtr right);
149  RunLog& operator<< (const VectorKKStr& right);
150 
151  RunLog& operator<< (std::ostream& (* mf)(std::ostream &));
152 
153  //friend RunLog& endl (RunLog& _outs);
154 
155  void Flush ();
156 
157  void SetLevel (kkint32 _level) {loggingLevel = _level;}
158 
160 
161 
162  private:
163  void DisplayTimeStamp ();
164 
165  void GetLoggingLevel ();
166 
167  bool callDisplayTimeStamp;
168 
169  kkint32 curLevel; // Current level of Message being written.
170 
171  KKStr curLine;
172 
173  KKStr fileName;
174 
175  KKStr lastLine;
176 
177  kkint32 lineCount;
178 
179  bool lineEmpty;
180 
181  std::ostream* logFile; /**< File stream that we will be writing logging messages to; if fileName was
182  * specified then 'logFile = ourLogFile'.
183  */
184 
185  kkint32 loggingLevel; /**< Logging level that we are to be writing messages for any messages written
186  * while 'curLevel' <= 'loggingLevel' will be written to '*logFile'.
187  */
188 
189  MsgQueuePtr msgQueue;
190 
191 
192  std::ofstream* ourLogFile; // Will be NULL if we are writing to the console.
193 
194  kkint32 procId; // Processor Id assigned by OS
195  };
196 
197 
198  typedef RunLog* RunLogPtr;
199 
200 #define _RunLog_Defined_
201 
202  /*
203  #ifdef WIN32
204  RunLog& __cdecl operator<< (RunLog & log,
205  RunLog & (__cdecl*)(RunLog &));
206 
207  RunLog& __cdecl endl(RunLog & log);
208  #else
209 
210  RunLog& operator<< (RunLog & log,
211  RunLog & (*)(RunLog &));
212 
213 
214  RunLog& endl(RunLog & log);
215 
216  #endif
217  */
218 }
219 
220 #endif
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
void DetachFile()
Closes out current log file and starts writing to stdout aka &#39;cout&#39;.
Definition: RunLog.cpp:181
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
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
MsgQueue * MsgQueuePtr
Definition: RunLog.h:45
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
__int64 kkint64
Definition: KKBaseTypes.h:90
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
RunLog()
Creates an instance were logging will go to the console.
Definition: RunLog.cpp:32
kkint32 MemoryConsumedEstimated() const
Definition: RunLog.cpp:116
kkint32 Level() const
Definition: RunLog.h:78
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
void SetLevel(kkint32 _level)
Definition: RunLog.h:157
RunLog(std::ostream *logStream)
Creates instance where logging messages are written to file stream pointed to by &#39;logStream&#39;.
Definition: RunLog.cpp:83
Will manage a buffer that will allow multiple threads to add and remove messages to a queue...
Definition: MsgQueue.h:19
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
RunLog * RunLogPtr
Definition: RunLog.h:198
unsigned __int64 kkuint64
Definition: KKBaseTypes.h:91
void AttachFileAppend(const KKStr &_fileName)
Call this method to start appending logging messages to a different file.
Definition: RunLog.cpp:143
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
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
kkint32 LineCount() const
Definition: RunLog.h:119
KKStr FileName()
Definition: RunLog.cpp:193
const KKStr & LastLine()
Returns the last line of text written to the log file.
Definition: RunLog.h:117
RunLog(MsgQueuePtr _msgQueue)
Definition: RunLog.cpp:96