1 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.9 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
|
4 mike 1.2 // The Open Group, Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
|
12 kumpf 1.9 //
|
13 mike 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Sushma Fernandes, Hewlett-Packard Company (sushma_fernandes@hp.com)
25 //
|
26 kumpf 1.6 // Modified By: Jenny Yu, Hewlett-Packard Company (jenny_yu@hp.com)
|
27 mike 1.2 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30 #ifndef Pegasus_Tracer_h
31 #define Pegasus_Tracer_h
32
33 #include <stdarg.h>
34 #include <fstream>
35 #include <Pegasus/Common/String.h>
36 #include <Pegasus/Common/System.h>
37 #include <Pegasus/Common/Logger.h>
|
38 kumpf 1.6 #include <Pegasus/Common/Exception.h>
|
39 mike 1.2 #include <Pegasus/Common/TraceComponents.h>
40 #include <Pegasus/Common/TraceFileHandler.h>
|
41 kumpf 1.11 #include <Pegasus/Common/Linkage.h>
|
42 mike 1.2
43 PEGASUS_NAMESPACE_BEGIN
44
45 /** Tracer implements tracing of messages to a defined file
46 */
47
48 class PEGASUS_COMMON_LINKAGE Tracer
49 {
50 public:
51
52 /** Levels of trace
53 Trace messages are written to the trace file only if they are at or
54 above a given trace level
55 LEVEL1 - Function Entry/Exit
56 LEVEL2 - Basic flow trace messages, low data detail
57 LEVEL3 - Inter-function logic flow, medium data detail
58 LEVEL4 - High data detail
59 */
60 static const Uint32 LEVEL1;
61 static const Uint32 LEVEL2;
62 static const Uint32 LEVEL3;
63 mike 1.2 static const Uint32 LEVEL4;
64
65 // PEGASUS_REMOVE_TRACE defines the compile time inclusion of the Trace
66 // interfaces. If defined the interfaces map to empty functions
67
68 #ifdef PEGASUS_REMOVE_TRACE
69
70 inline static void traceBuffer(
71 const char* fileName,
72 const Uint32 lineNum,
73 const Uint32 traceComponent,
74 const Uint32 traceLevel,
75 const char* data,
76 const Uint32 size)
77 {
78 // empty function
79 }
80 inline static void traceBuffer(
81 const Uint32 traceComponent,
82 const Uint32 traceLevel,
83 const char* data,
84 mike 1.2 const Uint32 size)
85 {
86 // empty function
87 }
88
89 inline static void trace(
90 const Uint32 traceComponent,
91 const Uint32 traceLevel,
92 const char *fmt,
93 ...)
94 {
95 // empty function
96 }
97
98 inline static void trace(
99 const char* fileName,
100 const Uint32 lineNum,
101 const Uint32 traceComponent,
102 const Uint32 traceLevel,
103 const char* fmt,
104 ...)
105 mike 1.2 {
106 // empty function
107 }
108
|
109 kumpf 1.6 inline static void trace(
110 const char* fileName,
111 const Uint32 lineNum,
112 const Uint32 traceComponent,
113 const Uint32 traceLevel,
114 const String& traceString)
115 {
116 // empty function
117 }
118
119 inline static void traceCIMException(
120 const Uint32 traceComponent,
121 const Uint32 traceLevel,
122 CIMException cimException)
123 {
124 // empty function
125 }
126
|
127 mike 1.2 static Uint32 setTraceFile(const char* traceFile)
128 {
129 // empty function
130 return 0;
131 }
132
133 static Uint32 setTraceLevel(const Uint32 traceLevel)
134 {
135 // empty function
136 return 0;
137 }
138
139 static void setTraceComponents(
140 const String traceComponents)
141 {
142 // empty function
143 }
144
145
146 #else
147 /** Traces the specified number of bytes in a given buffer
148 mike 1.2 @param traceComponent component being traced
149 @param traceLevel trace level of the trace message
150 @param data buffer to be traced
151 @param size number of bytes to be traced
152 */
153 inline static void traceBuffer(
154 const Uint32 traceComponent,
155 const Uint32 traceLevel,
156 const char* data,
157 const Uint32 size)
158 {
159 _traceBuffer( traceComponent, traceLevel, data, size );
160 }
161
162 /** Traces the specified number of bytes in a given buffer
163 Overloaded to include the filename and the line number
164 of trace origin.
165 @param fileName filename of the trace originator
166 @param lineNum line number of the trace originator
167 @param traceComponent component being traced
168 @param traceLevel trace level of the trace message
169 mike 1.2 @param data buffer to be traced
170 @param size size of the buffer
171 */
172 inline static void traceBuffer(
173 const char* fileName,
174 const Uint32 lineNum,
175 const Uint32 traceComponent,
176 const Uint32 traceLevel,
177 const char* data,
178 const Uint32 size)
179 {
180 _traceBuffer( fileName, lineNum,
181 traceComponent, traceLevel, data, size );
182 }
183
184
185 /** Traces the given message
186 @param traceComponent component being traced
187 @param traceLevel trace level of the trace message
188 @param *fmt printf style format string
189 @param ... variable argument list
190 mike 1.2 */
191 inline static void trace(
192 const Uint32 traceComponent,
193 const Uint32 traceLevel,
194 const char *fmt,
195 ...)
196 {
197 va_list argList;
198
199 va_start(argList,fmt);
200 _trace(traceComponent,traceLevel,fmt,argList);
201 va_end(argList);
202 }
203
204 /** Traces the given message. Overloaded to include the filename and
205 the line number of trace origin.
206 @param fileName filename of the trace originator
207 @param lineNum line number of the trace originator
208 @param traceComponent component being traced
209 @param traceLevel trace level of the trace message
210 @param *fmt printf style format string
211 mike 1.2 @param ... variable argument list
212 */
213 inline static void trace(
214 const char* fileName,
215 const Uint32 lineNum,
216 const Uint32 traceComponent,
217 const Uint32 traceLevel,
218 const char* fmt,
219 ...)
220 {
221 va_list argList;
222
223 va_start(argList,fmt);
224 _trace(fileName,lineNum,traceComponent,traceLevel,fmt,argList);
225 va_end(argList);
226 }
227
|
228 kumpf 1.6 /** Traces the given string. Overloaded to include the filename
229 and line number of trace origin.
230 @param fileName filename of the trace originator
231 @param lineNum line number of the trace originator
232 @param traceComponent component being traced
233 @param traceLevel trace level of the trace message
234 @param traceString the string to be traced
235 */
236 inline static void trace(
237 const char* fileName,
238 const Uint32 lineNum,
239 const Uint32 traceComponent,
240 const Uint32 traceLevel,
241 const String& traceString)
242 {
243 _traceString( fileName, lineNum, traceComponent, traceLevel,
244 traceString );
245 }
246
247 /** Traces the message in the given CIMException object. The message
248 written to the trace file will include the source filename and
249 kumpf 1.6 line number of the CIMException originator.
250 @param traceComponent component being traced
251 @param traceLevel trace level of the trace message
252 @param CIMException the CIMException to be traced.
253 */
254 inline static void traceCIMException(
255 const Uint32 traceComponent,
256 const Uint32 traceLevel,
257 CIMException cimException)
258 {
259 _traceCIMException( traceComponent, traceLevel, cimException );
260 }
261
|
262 mike 1.2 /** Set the trace file to the given file
263 @param traceFile full path of the trace file
264 @return 0 if the filepath is valid
265 1 if an error occurs while opening the file
266 in append mode
267 */
268 static Uint32 setTraceFile(const char* traceFile);
269
270 /** Set the trace level to the given level
271 @param traceLevel trace level to be set
272 @return 0 if trace level is valid
273 1 if trace level is invalid
274 */
275 static Uint32 setTraceLevel(const Uint32 traceLevel);
276
277 /** Set components to be traced
278 @param traceComponents list of components to be traced,
279 components should be separated by ','
280 */
281 static void setTraceComponents(
282 const String traceComponents);
283 mike 1.2
284 #endif
285
286 // End of PEGASUS_REMOVE_TRACE
287
288 /** Traces method entry.
289 @param fileName filename of the trace originator
290 @param lineNum line number of the trace originator
291 @param traceComponent component being traced
292 @param methodName method being traced
293 */
294 inline static void traceEnter(
295 const char* fileName,
296 const Uint32 lineNum,
297 const Uint32 traceComponent,
298 const char* methodName)
299 {
300 _traceEnter( fileName, lineNum, traceComponent, "%s %s",
|
301 kumpf 1.10 _METHOD_ENTER_MSG, methodName);
|
302 mike 1.2 }
303
304 /** Traces method exit.
305 @param fileName filename of the trace originator
306 @param lineNum line number of the trace originator
307 @param traceComponent component being traced
308 @param methodName method being traced
309 */
310 inline static void traceExit(
311 const char* fileName,
312 const Uint32 lineNum,
313 const Uint32 traceComponent,
314 const char* methodName)
315 {
316 _traceExit( fileName, lineNum, traceComponent, "%s %s",
|
317 kumpf 1.10 _METHOD_EXIT_MSG, methodName);
|
318 mike 1.2 }
319
320 /** Validates the File Path for the trace File
321 @param filePath full path of the file
322 @return 1 if the file path is valid
323 0 if the file path is invalid
324 */
|
325 kumpf 1.7 static Boolean isValidFileName(const char* filePath);
326
327 /** Validates the trace components
328 @param traceComponents comma separated list of trace components
329 @return 1 if the components are valid
330 0 if one or more components are invalid
331 */
332 static Boolean isValidComponents(const String traceComponents);
|
333 mike 1.2
334 /** Validates the trace components
335 @param traceComponents comma separated list of trace components
336 @param invalidComponents comma separated list of invalid components
337 @return 1 if the components are valid
338 0 if one or more components are invalid
339 */
|
340 kumpf 1.7 static Boolean isValidComponents(
341 const String traceComponents,
342 String& invalidComponents);
|
343 mike 1.2
344 private:
345
346 static const char _COMPONENT_SEPARATOR;
347 static const Uint32 _NUM_COMPONENTS;
348 static const Uint32 _STRLEN_MAX_UNSIGNED_INT;
|
349 kumpf 1.4 static const Uint32 _STRLEN_MAX_PID_TID;
|
350 mike 1.2 static const Boolean _SUCCESS;
351 static const Boolean _FAILURE;
352 Boolean* _traceComponentMask;
353 Uint32 _traceLevelMask;
354 TraceFileHandler* _traceHandler;
355 static Tracer* _tracerInstance;
356
|
357 kumpf 1.10 // Message Strings for function Entry and Exit
358 static const char _METHOD_ENTER_MSG[];
359 static const char _METHOD_EXIT_MSG[];
|
360 mike 1.2
361 // Message Strings for Logger
|
362 kumpf 1.6 static const char _LOG_MSG[];
|
363 mike 1.2
364 // Checks if trace is enabled for the given component and trace level
365 // @param traceComponent component being traced
366 // @param traceLevel level of the trace message
367 // @return 0 if the component and level are not enabled
368 // 1 if the component and level are enabled
369 static Boolean _isTraceEnabled(
370 const Uint32 traceComponent,
371 const Uint32 traceLevel);
372
373 // Traces the given message
374 // @param traceComponent component being traced
375 // @param traceLevel level of the trace message
376 // @param *fmt printf style format string
377 // @param argList variable argument list
378 static void _trace(
379 const Uint32 traceComponent,
380 const Uint32 traceLevel,
381 const char* fmt,
382 va_list argList);
383
384 mike 1.2 // Traces the given message. Overloaded to include the file name and the
385 // line number as one of the parameters.
386 // @param traceComponent component being traced
387 // @param traceLevel level of the trace message
388 // @param message message header (file name:line number)
389 // @param *fmt printf style format string
390 // @param argList variable argument list
391 static void _trace(
392 const char* fileName,
393 const Uint32 lineNum,
394 const Uint32 traceComponent,
395 const Uint32 traceLevel,
396 const char* fmt,
397 va_list argList);
398
399 // Traces the specified number of bytes in a given buffer
400 // @param traceComponent component being traced
401 // @param traceLevel trace level of the trace message
402 // @param data buffer to be traced
403 // @param size number of bytes to be traced
404 static void _traceBuffer(
405 mike 1.2 const Uint32 traceComponent,
406 const Uint32 traceLevel,
407 const char* data,
408 const Uint32 size);
409
410 // Traces the specified number of bytes in a given buffer
411 // Overloaded to include the filename and the line number
412 // of trace origin.
413 // @param fileName filename of the trace originator
414 // @param lineNum line number of the trace originator
415 // @param traceComponent component being traced
416 // @param traceLevel trace level of the trace message
417 // @param data buffer to be traced
418 // @param size size of the buffer
419 static void _traceBuffer(
420 const char* fileName,
421 const Uint32 lineNum,
422 const Uint32 traceComponent,
423 const Uint32 traceLevel,
424 const char* data,
425 const Uint32 size);
426 mike 1.2
|
427 kumpf 1.6 // Traces the given string.
428 // @param traceComponent component being traced
429 // @param traceLevel trace level of the trace message
430 // @param traceString the string to be traced
431 //
432 static void _traceString(
433 const Uint32 traceComponent,
434 const Uint32 traceLevel,
435 const String& traceString);
436
437 // Traces a given string. Overloaded to include the filename
438 // and line number of trace origin.
439 // @param fileName filename of the trace originator
440 // @param lineNum line number of the trace originator
441 // @param traceComponent component being traced
442 // @param traceLevel trace level of the trace message
443 // @param traceString the string to be traced
444 //
445 static void _traceString(
446 const char* fileName,
447 const Uint32 lineNum,
448 kumpf 1.6 const Uint32 traceComponent,
449 const Uint32 traceLevel,
450 const String& traceString);
451
452 // Traces the message in the given CIMException object. The message
453 // to be written to the trace file will include the source filename and
454 // line number of the CIMException originator.
455 // @param traceComponent component being traced
456 // @param traceLevel trace level of the trace message
457 // @param CIMException the CIMException to be traced.
458 //
459 static void _traceCIMException(
460 const Uint32 traceComponent,
461 const Uint32 traceLevel,
462 CIMException cimException);
463
|
464 mike 1.2 // Called by all the trace interfaces to log message to the
465 // trace file
466 // @param fileName filename of the trace originator
467 // @param lineNum line number of the trace originator
468 // @param traceComponent component being traced
469 // @param *fmt printf style format string
470 // @param argList variable argument list
471 static void _trace(
472 const Uint32 traceComponent,
473 const char* message,
474 const char* fmt,
475 va_list argList);
476
477 // Traces method enter
478 // @param fileName filename of the trace originator
479 // @param lineNum line number of the trace originator
480 // @param traceComponent component being traced
481 // @param *fmt printf style format string
482 // @param ... variable argument list
483 static void _traceEnter(
484 const char* fileName,
485 mike 1.2 const Uint32 lineNum,
486 const Uint32 traceComponent,
487 const char* fmt,
488 ...);
489
490 // Traces method exit
491 // @param fileName filename of the trace originator
492 // @param traceComponent component being traced
493 // @param *fmt printf style format string
494 // @param ... variable argument list
495 static void _traceExit(
496 const char* fileName,
497 const Uint32 lineNum,
498 const Uint32 traceComponent,
499 const char* fmt,
500 ...);
501
502 // Tracer constructor
503 // Constructor is private to prevent construction of Tracer objects
504 // Single Instance of Tracer is maintained for each process.
505 Tracer();
506 mike 1.2
507 // Tracer destructor
508 ~Tracer();
509
510 // Returns the Singleton instance of the Tracer
511 // @return Tracer* Instance of Tracer
512 static Tracer* _getInstance();
513 };
514
|
515 kumpf 1.6 // Define the macros for method entry/exit, and tracing a given string
|
516 mike 1.2 #ifdef PEGASUS_REMOVE_TRACE
|
517 kumpf 1.5 #define PEG_METHOD_ENTER(traceComponent,methodName)
518 #define PEG_METHOD_EXIT()
|
519 kumpf 1.6 #define PEG_TRACE_STRING(traceComponent,traceLevel,traceString)
|
520 mike 1.2 #else
|
521 kumpf 1.5 /** Macro for tracing method entry
522 @param traceComponent component being traced
523 @param methodName name of the method
524 */
525 #define PEG_METHOD_ENTER(traceComponent,methodName) \
526 const char *PEG_METHOD_NAME = methodName; \
527 const Uint32 PEG_TRACE_COMPONENT = traceComponent; \
528 Tracer::traceEnter(__FILE__,__LINE__,PEG_TRACE_COMPONENT,PEG_METHOD_NAME)
529
530 /** Macro for tracing method exit
531 */
532 #define PEG_METHOD_EXIT() \
533 Tracer::traceExit(__FILE__,__LINE__,PEG_TRACE_COMPONENT,PEG_METHOD_NAME)
|
534 kumpf 1.6
535 /** Macro for tracing a string
536 @param traceComponent component being traced
537 @param traceLevel trace level of the trace message
538 @param traceString the string to be traced
539 */
540 #define PEG_TRACE_STRING(traceComponent,traceLevel,traceString) \
541 Tracer::trace(__FILE__, __LINE__,traceComponent,traceLevel,traceString)
|
542 mday 1.8
|
543 mike 1.2 #endif
544
545 PEGASUS_NAMESPACE_END
546
547 #endif /* Pegasus_Tracer_h */
|