00001 /*************************************************************************** 00002 LOW_helper_msglog.h - description 00003 ------------------- 00004 begin : Sun Jul 21 2002 00005 copyright : (C) 2002 by Harald Roelle 00006 email : roelle@informatik.uni-muenchen.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef LOW_HELPER_MSGLOG_H 00019 #define LOW_HELPER_MSGLOG_H 00020 00021 00022 #include <stdint.h> 00023 #include <stdarg.h> 00024 #include <stdio.h> 00025 00026 00027 #include "LOW_thread_mutex.h" 00028 00029 00030 /** Static class for message logging. 00031 Distinguishes error/warning/normal and debugging messages. 00032 Debugging support arbitrary debug levels. 00033 00034 This class is thread-safe. 00035 00036 @todo Finish implementation of class, namely additional output streams. 00037 00038 @bug printXXX( "\n") gives "(NULL)" output. 00039 00040 @author Harald Roelle 00041 */ 00042 class LOW_helper_msglog { 00043 00044 //======================================================================================= 00045 public: 00046 00047 //===================================================================================== 00048 // 00049 // type definitions 00050 // 00051 00052 /** Type defining debug levels. */ 00053 typedef enum { 00054 portSerial_dl, /**< Debugging of serial port communication. */ 00055 objSync_getLock_dl, /**< Debugging of successfully obtaining a lock in LOW_objectSynchronizer. */ 00056 objSync_lockFailed_dl, /**< Debugging of failing to obtain a lock in LOW_objectSynchronizer. */ 00057 all_dl /**< This one MUST be the last! */ 00058 } debugLevel_t; 00059 00060 00061 //===================================================================================== 00062 // 00063 // static methods 00064 // 00065 00066 /** Enable/disable logging of debug messages of a certain level. 00067 @param inLevel Debug level to set logging for. 00068 @param isEnabled Whether to enable or disable logging. 00069 */ 00070 static void setDebugLevelEnabled( const debugLevel_t inLevel, const bool isEnabled = true); 00071 00072 00073 /** Get whether logging of debug messages of a certain level is enabled. 00074 @param inLevel Debug level to query. 00075 @return True if logging is enabled. 00076 */ 00077 static bool getDebugLevelEnabled( const debugLevel_t inLevel); 00078 00079 00080 /** Print error message. 00081 00082 @param inErrno OS error number. 00083 @param inFormat Format string as in ANSI-C printf(). 00084 @param ... Variable argument list as in ANSI-C printf(). 00085 */ 00086 static void printPerror( const int inErrno, const char *inFormat, ...); 00087 00088 00089 /** Print error message. 00090 00091 @param inFormat Format string as in ANSI-C printf(). 00092 @param ... Variable argument list as in ANSI-C printf(). 00093 */ 00094 static void printError( const char *inFormat, ...); 00095 00096 00097 /** Print warning message. 00098 00099 @param inFormat Format string as in ANSI-C printf(). 00100 @param ... Variable argument list as in ANSI-C printf(). 00101 */ 00102 static void printWarning( const char *inFormat, ...); 00103 00104 00105 /** Print message. 00106 00107 @param inFormat Format string as in ANSI-C printf(). 00108 @param ... Variable argument list as in ANSI-C printf(). 00109 */ 00110 static void printMessage( const char *inFormat, ...); 00111 00112 00113 /** Print message. 00114 00115 @param inLevel Debug level the message belongs to. 00116 @param inFormat Format string as in ANSI-C printf(). 00117 @param ... Variable argument list as in ANSI-C printf(). 00118 */ 00119 static void printDebug( const debugLevel_t inLevel, const char *inFormat, ...); 00120 00121 00122 //======================================================================================= 00123 private: 00124 00125 //===================================================================================== 00126 // 00127 // locks 00128 // 00129 00130 /** Locking class to ensure exclusive access to output streams. 00131 00132 The class is intended to be used in a "locking is creation" design pattern. 00133 On creation an exclusive lock is optained, and on destruction the lock is released. 00134 00135 Uses a recursive mutex, i.e. does not distinguish reads/writes but my be called 00136 multiple times by the same thread without blocking. 00137 00138 Class is inlined for performance reasons. 00139 00140 @see LOW_thread_mutex 00141 */ 00142 class msgLock { 00143 public: 00144 /** Obtain the lock. 00145 */ 00146 inline msgLock() 00147 { 00148 msgMutex->lock(); 00149 }; 00150 00151 /** Release the lock. 00152 */ 00153 inline ~msgLock() 00154 { 00155 msgMutex->unlock(); 00156 }; 00157 00158 private: 00159 static LOW_thread_mutex *msgMutex; /**< Recursive mutex used for locking. */ 00160 }; 00161 00162 00163 //===================================================================================== 00164 // 00165 // type definitions 00166 // 00167 00168 /** Internal type to mark type of message. */ 00169 typedef enum { msg_log, warn_log, err_log, debug_log} logType_t; 00170 00171 00172 //===================================================================================== 00173 // 00174 // constructors 00175 // 00176 00177 /** Default constructor. 00178 It is private to prevent creating objects from this class as 00179 this is a static helper class. 00180 */ 00181 LOW_helper_msglog(); 00182 00183 /** Destructor. 00184 It is private to prevent creating objects from this class as 00185 this is a static helper class. 00186 */ 00187 ~LOW_helper_msglog(); 00188 00189 00190 //===================================================================================== 00191 // 00192 // static attributes 00193 // 00194 00195 static bool errorOccured; 00196 static bool debugLevels[all_dl]; 00197 00198 static bool useStdMsgStream; 00199 static bool useStdWarnStream; 00200 static bool useStdErrStream; 00201 static bool useStdDebugStream; 00202 static bool useExtraMsgStream; 00203 static bool useExtraWarnStream; 00204 static bool useExtraErrStream; 00205 static bool useExtraDebugStream; 00206 00207 static FILE* stdOutStream; 00208 static FILE* stdWarnStream; 00209 static FILE* stdErrStream; 00210 static FILE* stdDebugStream; 00211 static FILE* extraOutStream; 00212 static FILE* extraWarnStream; 00213 static FILE* extraErrStream; 00214 static FILE* extraDebugStream; 00215 00216 00217 //===================================================================================== 00218 // 00219 // static methods 00220 // 00221 00222 static void va_printToLog( const logType_t inLogType, const char *inFormat, va_list inParamList); 00223 static unsigned int fprintLogHeader( FILE *inExtraStream, FILE *inStdStream); 00224 static void fprintfMulti( FILE *inExtraStream, FILE *inStdStream, const char *inFormat, ...); 00225 static void vfprintfMulti( FILE *inExtraStream, FILE *inStdStream, const char *inFormat, va_list inAp); 00226 static void* callocCheck( const size_t inSize); 00227 00228 }; 00229 00230 #endif 00231