1 /*----------------------------------------------------------------------------
2 Name: Log.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Handling the Client data
6 -----------------------------------------------------------------------------*/
7
8 #ifndef _UTIL_LOG_H
9 #define _UTIL_LOG_H
10
11 #include <util/xmlBlasterDef.h>
12 #include <util/PropertyDef.h>
13 #include <util/Property.h>
14 #include <util/I_Log.h>
15
16 /**
17 * Logging output.
18 * <p />
19 * Note that the layout of this class is for optimum performance and ease of
20 * use.<br />
21 * to CALL/TIME/TRACE/DUMP variables to achieve dead code elimination (see
22 * code below).
23 */
24 namespace org { namespace xmlBlaster { namespace util {
25
26 class Dll_Export Log : public I_Log {
27
28 private:
29
30 /**
31 * private copy constructor
32 */
33 Log(const Log& entry);
34
35 /**
36 * private assignment constructor
37 */
38 Log& operator =(const Log& entry);
39
40 bool withXtermColor_;
41
42 /**
43 * Produce logging output on important method calls.
44 * <p />
45 * Switch CALL mode to <code>false</code> for performance reasons<br />
46 * or switch it to <code>true</code> for debugging reasons.
47 * <p />
48 * Setting to false:<br />
49 * <code>if (Log.trace()) Log.trace(....); </code> -> dead code
50 * elimination<br />
51 * Note that you need to uncomment the final declaration, set all booleans
52 * to false and comment out the code in method setPreLogLevelCheck().
53 * Then recompile to achieve real dead code elimination.
54 * <p />
55 * The same applies for CALL, TIME, TRACE and DUMP
56 */
57
58 /**
59 * Logging levels
60 */
61 enum {L_NOLOG= 0x0, // No logs at all
62 L_PANIC= 0x1, // Do exit on error
63 L_ERROR= 0x2, // Internal error occured
64 L_WARN = 0x4, // Warning about user actions
65 L_INFO = 0x8, // Important informational logs only
66 L_CALL = 0x10, // Trace entry of methods
67 L_TIME = 0x20, // Show elapsed milliseconds
68 L_TRACE= 0x40, // Trace application flow
69 L_DUMP = 0x80, // Dump internal state
70 L_EXIT = 0x100, // Do a normal exit
71 L_PLAIN= 0x200 // Marker for plain output
72 };
73 // static int lookAndFeelTime = LC_TIME;
74 /**
75 * colors foreground/background
76 */
77 static const char* const ESC ;
78 static const char* const BOLD ;
79 static const char* const RED_BLACK ;
80 static const char* const GREEN_BLACK ;
81 static const char* const YELLOW_BLACK ;
82 static const char* const BLUE_BLACK ;
83 static const char* const PINK_BLACK ;
84 static const char* const LTGREEN_BLACK;
85 static const char* const WHITE_BLACK ;
86 static const char* const WHITE_RED ;
87 static const char* const BLACK_RED ;
88 static const char* const BLACK_GREEN ;
89 static const char* const BLACK_PINK ;
90 static const char* const BLACK_LTGREEN;
91
92 Property& properties_;
93 int logLevel_;
94
95 /**
96 * Counter for occurred warnings/errors
97 */
98 int numWarnInvocations;
99 int numErrorInvocations;
100 std::string currentLogFormat;
101 bool logFormatPropertyRead;
102
103 std::string ME;
104 std::string name_;
105
106 std::string timeE, callE, traceE, plainE, infoE;
107 std::string warnE, errorE, panicE, exitE;
108
109 /**
110 * Output text for different logging levels
111 * The DOS box does not know any colors
112 */
113 std::string timeX, callX, traceX, plainX, infoX;
114 std::string warnX, errorX, panicX, exitX;
115
116 private:
117 /**
118 * Converts the String logLevel (e.g."ERROR") to the bit setting L_ERROR.
119 * @param for example "TRACE"
120 * @return bit setting L_TRACE
121 */
122 int logLevelToBit(const std::string &logLevel) {
123 if (logLevel == "PANIC") return L_PANIC;
124 else if (logLevel == "ERROR") return L_ERROR;
125 else if (logLevel == "WARN" ) return L_WARN;
126 else if (logLevel == "INFO" ) return L_INFO;
127 else if (logLevel == "CALL") return L_CALL;
128 else if (logLevel == "TIME" ) return L_TIME;
129 else if (logLevel == "TRACE") return L_TRACE;
130 else if (logLevel == "DUMP" ) return L_DUMP;
131 return L_NOLOG;
132 }
133
134 void initSpecificTrace(const std::string& trace, const std::string& traceId);
135
136
137 /**
138 * The only way to stop the server
139 * @param val exit code for operating system
140 */
141 void exitLow(int val);
142
143 public:
144 /** Return the log channel */
145 inline std::string getName() { return name_; }
146
147 Log(Property& properties, int args=0, const char * const argc[]=0, const std::string& name="default");
148
149
150 ~Log();
151
152 /**
153 * Switch the colored logging output on or off.
154 * Defaults on UNIX to 'on' and on Windows to 'off'
155 */
156 void setWithXtermColor(bool val=true);
157
158 /**
159 * Sets the default loglevel L_PANIC | L_ERROR | L_WARN | L_INFO
160 */
161 void setDefaultLogLevel();
162
163
164 /**
165 * Sets the loglevel
166 */
167 void setLogLevel(int level);
168
169
170 /**
171 * Set logging level from start parameter and initialize properties.
172 * <br />
173 * Example:<br />
174 * <pre>jaco org.xmlBlaster.Main -trace true -dump true -call true -dump true</pre>
175 *
176 */
177 void setLogLevel(int argc, const char * const args[]);
178
179
180 /**
181 * Removes the loglevel
182 * @param for example "TRACE"
183 */
184 void removeLogLevel(std::string logLevel);
185
186
187 /**
188 * Adds the loglevel
189 * @param for example "DUMP"
190 */
191 void addLogLevel(std::string logLevel);
192
193
194 /**
195 * Set the boolean values of CALL, TIME, TRACE, DUMP accordingly.
196 * <p />
197 * This allows to use 'if (Log.trace())' in your code, so that the
198 * following Log.trace(...) is not executed if not needed (performance
199 * gain).
200 */
201 void setPreLogLevelCheck();
202
203
204 /**
205 * Converts for example the bit setting L_ERROR to String "ERROR|WARN".
206 * @param bit setting for example L_TRACE
207 * @return for example "ERROR|WARN|TRACE"
208 */
209 std::string bitToLogLevel(int level) const;
210
211 /**
212 * My current log level setting in human readable notation.
213 * @return for example "ERROR|WARN|TRACE"
214 */
215 std::string getLogLevelStr() const { return bitToLogLevel(logLevel_); }
216
217 /**
218 * Gets the loglevel 0,10,20,30,40,50,60
219 */
220 int getLogLevel() const {
221 return logLevel_;
222 }
223
224
225 void setLogFormat(const std::string &format) {
226 currentLogFormat = format;
227 }
228
229
230 /**
231 * Use this exit for errors
232 */
233 void panic(const std::string &instance, const std::string &text);
234
235
236 /**
237 * Exit without errors
238 */
239 void exit(const std::string &instance, const std::string &text);
240
241
242 /**
243 * Use this for normal logging output
244 */
245 void info(const std::string &instance, const std::string &text);
246
247
248 /**
249 * Use this for logging output where the xmlBlaster administrator shall
250 * be informed<br /> for example a login denied event
251 */
252 void warn(const std::string &instance, const std::string &text);
253
254
255 /**
256 * Use this for internal xmlBlaster errors reporting
257 */
258 void error(const std::string &instance, const std::string &text);
259
260
261 /*
262 * Log without time/date/instance
263 * @param instance (not currently used)
264 * @param text the std::string to log
265 */
266 void plain(const std::string &instance, const std::string &text);
267
268
269 /*
270 * Log without time/date
271 */
272 void dump(const std::string &instance, const std::string &text);
273
274
275 /**
276 * Tracing execution
277 */
278 void trace(const std::string &instance, const std::string &text);
279
280
281 /**
282 * Tracing when entering methods
283 */
284 void call(const std::string &instance, const std::string &text);
285
286
287 /**
288 * Output of performant measurements (elapsed milliseconds)
289 */
290 void time(const std::string &instance, const std::string &text);
291
292
293 /**
294 * Command line usage.
295 * <p />
296 * These variables may be set in xmlBlaster.properties as well.
297 * Don't use the "-" or "+" prefix there.
298 */
299 virtual std::string usage() const;
300
301 /**
302 * Display some statistic on exit
303 */
304 void displayStatistics();
305
306
307 /**
308 * Display the current stack<br />
309 * Very slow and not filtered for usefulness
310 */
311 void printStack();
312
313
314 /**
315 * Force setting logging properties from xmlBlaster.properties
316 * <p />
317 * Called from Property after reading xmlBlaster.properties
318 */
319 void initialize();
320
321
322 std::string getTime();
323
324
325 /**
326 * Log example:
327 * <p />
328 * Feb 11, 2000 3:44:48 PM INFO : [Main] xmlBlaster is ready for
329 * requests.
330 *
331 * @param levelStr e.g. "WARNING" or "INFO" or null
332 * @param level for performance reasons, an int for the levelStr
333 * @param instance e.g. "RequestBroker" or "Authentication"
334 * @param text e.g. "Login denied"
335 */
336 void log(const std::string &levelStr, int level, const std::string &instance,
337 const std::string &text);
338
339
340 Property& getProperties() {
341 return properties_;
342 }
343
344 }; // end of class Log
345
346
347
348 }}} // end of namespace util
349
350 #endif // _UTIL_LOG_H
syntax highlighted by Code2HTML, v. 0.9.1