Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

lobtracestream.h

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00036 /*
00037    $Id: lobtracestream.h,v 1.5 2002/12/10 23:56:39 lbayle Exp $
00038 
00039    This file is part of the LobTraceStream project
00040    Copyright (C) 2002 Louis BAYLE <lbayle@users.sourceforge.net>
00041 
00042    This library is free software; you can redistribute it and/or
00043    modify it under the terms of the GNU Library General Public
00044    License as published by the Free Software Foundation; either
00045    version 2 of the License, or (at your option) any later version.
00046 */
00047 //-----------------------------------------------------------------------------
00048 
00049 #ifndef __LOBTRACESTREAM_H__
00050 #define __LOBTRACESTREAM_H__
00051 
00052 #if __GNUC__>2
00053 #include <iostream>
00054 #include <fstream>
00055 #include <cstring>
00056 #else
00057 #include <iostream.h>
00058 #include <fstream.h>
00059 #include <string.h>   // for strncpy
00060 #endif
00061 #define FILE_PATH_LENGTH   255
00062 #define BUFFERSTRING_SIZE 1024  // LobTraceStream buffer size
00063 #define U_MAXTOKENSIZE      30  /* string length in lexer  */
00064 #define PREFIX_PATH_LENGTH 255  // log file prefix max length (myAPP_)
00065 #define EXT_PATH_LENGTH     30  // log file extension max length (.log)
00066 
00067 class LobTraceStream;
00068 class LobTraceException;
00069 class LobTraceProvider;
00070 class LobTraceStreamEnd;
00071 
00072 extern LobTraceProvider    g_lobTraceProvider; // global, used by trace macros
00073 extern LobTraceStreamEnd   lt_endl;            // global, flush trigger
00074 extern LobTraceStream&     g_lobTraceStream;   // global, used by trace macros
00075 
00076 #ifdef LOBTRACE_QT
00077 class LobTraceStreamQt;
00078 #define G_LTS_INSTANCE ((LobTraceStreamQt&)g_lobTraceStream)
00079 #else
00080 #define G_LTS_INSTANCE g_lobTraceStream
00081 #endif
00082 
00083 //========================================================================
00084 
00085 /* CUSTO Defines module identification constants used for tracing */
00086 #define U_MOD_GENERAL     0  // mandatory, used by LobTrace
00087 #define U_MOD_UTILS       1  // mandatory, used by LobTrace
00088 
00089 #define U_MOD_COUNT 2        /* CUSTO number of modules in current project */
00090 
00091 
00092 /* Configuration parameters for the trace facility */
00093 
00094 /* Severity of the traces constants */
00095 #define U_TRA_EXCEPTION 0    /* for Exceptions*/
00096 #define U_TRA_CRITICAL  1    /* for fatal errors */
00097 #define U_TRA_ERROR     2    /* for other errors */
00098 #define U_TRA_WARNING   3    /* for warning messages */
00099 #define U_TRA_TRACE     4    /* for normal operating messages */
00100 #define U_TRA_DEBUG     5    /* for debugging messages */
00101 #define U_TRA_INOUT     6    /* use LOB_TRA_ENTRY and LOB_TRA_RETURN !
00102                                 -> logs enter/exit from functions */
00103 
00104 #define U_TRA_COUNT     7             /* number of trace levels */
00105 
00106 // \todo find a better way, see LOB_EXCEPTION2 macro
00107 #define LOBEX_BADVALUE LobTraceException::bad_value
00108 
00109 //========================================================================
00132 #define LOB_TRACE_INIT(confFilename, logPrefix)\
00133             g_lobTraceProvider.initialize(confFilename, logPrefix);
00134 
00147 #define LOB_TRACE(module, level)\
00148             g_lobTraceStream.setContextInfo(U_MOD_##module, U_TRA_##level, __FILE__, __LINE__);\
00149             G_LTS_INSTANCE  // here, concat the msg << "toto" << 123 << lt_endl;
00150 
00162 #define LOB_TRACE_ENTRY(module, funcname)\
00163             g_lobTraceStream.setContextInfo(U_MOD_##module, U_TRA_INOUT, __FILE__, __LINE__);\
00164             g_lobTraceStream  << "FUNCTION Enter : " << funcname << lt_endl;
00165 
00177 #define LOB_TRACE_RETURN(module, funcname, retcode)\
00178             g_lobTraceStream.setContextInfo(U_MOD_##module, U_TRA_INOUT, __FILE__, __LINE__);\
00179             g_lobTraceStream  << "FUNCTION Exit  : " << funcname << " [RetCode = " << retcode << "]" << lt_endl;
00180 
00190 #define LOB_EXCEPTION(type, msg) throw LobTraceException(g_lobTraceStream, __FILE__, __LINE__, type, msg);
00191 
00205 #define LOB_EXCEPTION2(type, msg) throw LobTraceException(g_lobTraceStream, __FILE__, __LINE__, LobTraceException:##:##type, msg);
00206 
00207 // \todo add assertion macro(s)
00208 /*#define LOB_ASSERT(expression)\
00209             if (false == (expression)) {  }
00210 */
00211 
00212 //========================================================================
00213 // Macros to use non-default traceStream & provider
00215 #define LOB_TRACE_INIT_MT(provider, confFilename, logPrefix)\
00216             provider.initialize(confFilename, logPrefix);
00217 
00218 #define LOB_TRACE_MT(lobstream, module, level)\
00219             lobstream.setContextInfo(U_MOD_##module, U_TRA_##level, __FILE__, __LINE__);\
00220             lobstream  // here, concat the msg << "toto" << 123 << lt_endl;
00221 
00222 #define LOB_TRACE_ENTRY_MT(lobstream, module, funcname)\
00223             lobstream.setContextInfo(U_MOD_##module, U_TRA_INOUT, __FILE__, __LINE__);\
00224             lobstream << "FUNCTION Enter : " << funcname << lt_endl;
00225 
00226 
00227 #define LOB_TRACE_RETURN_MT(lobstream, module, funcname, retcode)\
00228             lobstream.setContextInfo(U_MOD_##module, U_TRA_INOUT, __FILE__, __LINE__);\
00229             lobstream << "FUNCTION Exit  : " << funcname << " [RetCode = " << retcode << "]" << lt_endl;
00230 
00231 #define LOB_EXCEPTION_MT(lobstream, type, msg) throw LobTraceException(lobstream, __FILE__, __LINE__, type, msg);
00232 #define LOB_EXCEPTION_MT2(lobstream, type, msg) throw LobTraceException(lobstream, __FILE__, __LINE__, LobTraceException:##:##type, msg);
00233 
00234 //========================================================================
00246 class LobTraceProvider
00247 {
00248   public:
00258     enum traceLevels { InOut = 0, Debug, Trace, Warning, Error, Critical, Exception};
00259 
00260   private:
00261     bool      m_isInitialized;  
00262     ostream   *m_pOutputStream; 
00263     ofstream  m_traceFile;                         
00264     ifstream  m_confFile;                          
00265     char      m_logDir[FILE_PATH_LENGTH];     
00266     char      m_confFilename[FILE_PATH_LENGTH];    
00267     int       m_logFileMaxLines;                   
00268     int       m_logFileNbLines;              
00269     char      m_logFilePrefix[PREFIX_PATH_LENGTH]; 
00270     char      m_logFileExt[EXT_PATH_LENGTH]; 
00271     char      m_logFilename[FILE_PATH_LENGTH];   
00272     bool      m_isXmlTrace;     
00273 
00278     typedef struct
00279     {
00280        char modName[U_MAXTOKENSIZE];
00281        int  traceLevel;
00282     } modDef_t;
00283 
00284     static const char* m_levelLabels[7]; // initialization in .cpp
00285     static const char* LobTraceProvider::monthName[12];
00286 
00289     modDef_t m_moduleDefs[U_MOD_COUNT];
00290 
00291   public:
00292     LobTraceProvider();
00293     virtual ~LobTraceProvider();
00294 
00295     void initialize(const char * confFilename,
00296                     const char * logFilePrefix);
00297 
00298     virtual void onTrace( LobTraceStream& param );
00299 
00300     void setModuleTraceLevel(int moduleId, int level);
00301 
00302     bool isTraced(int module, int level);
00303     bool isTraced(int module, traceLevels level);
00304 
00305     const char * getLogFilename() { return m_logFilename; }
00306     const bool isXmlTrace()       { return m_isXmlTrace; }
00307 
00308     virtual void dump( );
00309 
00313     friend ostream & operator<< (ostream& os, LobTraceProvider& ltp)
00314     {
00315        os << "LobTraceProvider Instance (" << &ltp << ") tracing to : " << ltp.m_logFilename;
00316        return os;
00317     }
00318 
00319   private:
00320 
00322     typedef struct _v {
00323        char name[U_MAXTOKENSIZE];
00324        char value[U_MAXTOKENSIZE];
00325        struct _v *next;
00326     } variable_t;
00327 
00328     enum lexertokenType { lex_error = 0, lex_variable, lex_equal, lex_value };
00329 
00330     virtual void getFormatedTime(char *buffer);
00331 
00332     int  setVariables(variable_t *myVars);
00333     int  readConfFile(variable_t **tVarList);
00334     int  confFileLexer(char* szToken, lexertokenType *iType);
00335 
00336   private:
00337     // theese members are used by confFileLexer() when parsing the
00338     // configuration file. they are declared here for reentrance purpose.
00339     char m_nextChar;       /* char belong to the next token */
00340     bool m_isNextChar; /* true if next char has already been readen*/
00341     int  m_lexState;
00342 };
00343 
00344 //========================================================================
00351 class  LobTraceStreamEnd {};
00352 
00353 //========================================================================
00366 class LobTraceStream
00367 {
00368   protected:
00369     char  m_bufferString[BUFFERSTRING_SIZE];
00370     LobTraceProvider *m_pTraceProvider;
00371 
00372   private:
00373     char* m_pbuffer;
00374 
00375    struct {
00376       int  module;
00377       int  level;
00378       char file[FILE_PATH_LENGTH];
00379       int  line;
00380    } m_contextInfo;
00381 
00382 
00383 
00384   public:
00385     LobTraceStream(LobTraceProvider *tp = &g_lobTraceProvider);
00386 
00387     const char* getTraceOutput()   { return m_bufferString; }
00388     bool        getXmlTraceOutput(const char* srcString, char* xmlString, const int len);
00389     int         getContextModule() { return m_contextInfo.module; }
00390     int         getContextLevel()  { return m_contextInfo.level; }
00391     int         getContextLine()   { return m_contextInfo.line; }
00392     const char* getContextFile()   { return m_contextInfo.file; }
00393     LobTraceProvider * getTraceProvider() { return m_pTraceProvider; }
00394 
00395     // =====
00396 
00397     void        setContextInfo(int module, int level, const char* file, int line);
00398     void        setTraceProvider(LobTraceProvider *tp);
00399 
00400     LobTraceStream& operator<< ( const int&    param );
00401     LobTraceStream& operator<< ( const char*   param );
00402     LobTraceStream& operator<< ( const double& param );
00403 
00405     LobTraceStream& operator<< ( LobTraceStreamEnd& param );
00406 
00407     void traceException(int type = 0, const char* msg = NULL);
00408 
00409     virtual void dump( );
00410 
00414     friend ostream & operator<< (ostream& os, LobTraceStream& lts)
00415     {
00416        os << "LobTraceStream   Instance (" << &lts << ") sending to LobTraceProvider : " << lts.m_pTraceProvider;
00417        return os;
00418     }
00419 };
00420 
00421 
00422 //========================================================================
00432 class LobTraceException
00433 {
00434   protected:
00435 
00436     const char* m_msg;
00437     const char* m_file;
00438     int         m_line;
00439     int         m_type;
00440 
00441   public:
00442 
00445     enum lte_type { unknown =0, file_io, memory, bad_value, overflow};
00446 
00447     LobTraceException(LobTraceStream& lts = g_lobTraceStream,
00448                       const char* file = NULL, int line = -1,
00449                       lte_type type = unknown, const char* msg = NULL)
00450     {
00452        m_type = type;
00453        m_msg  = msg;
00454        m_file = file;
00455        m_line = line;
00456 
00457        lts.setContextInfo(U_MOD_GENERAL,U_TRA_EXCEPTION, file, line);
00458        lts.traceException(type, msg);
00459     }
00460 
00461     int         getLine()    { return m_line; }
00462     const char* getFile()    { return m_file; }
00463     int         getType()    { return m_type; }
00464     const char* getMessage() { return m_msg;  }
00465 };
00466 
00467 
00468 
00469 
00470 #endif // __LOBTRACE_H__
00471 
00472 
00473 //========================================================================

Generated on Sun Dec 15 23:17:21 2002 for LobTraceStream by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001