(file) Return to Tracer.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2