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 linkDS2490_dl, /**< Debugging of DS2490. */ 00058 all_dl /**< This one MUST be the last! */ 00059 } debugLevel_t; 00060 00061 00062 //===================================================================================== 00063 // 00064 // static methods 00065 // 00066 00067 /** Enable/disable logging of debug messages of a certain level. 00068 @param inLevel Debug level to set logging for. 00069 @param isEnabled Whether to enable or disable logging. 00070 */ 00071 static void setDebugLevelEnabled( const debugLevel_t inLevel, const bool isEnabled = true); 00072 00073 00074 /** Get whether logging of debug messages of a certain level is enabled. 00075 @param inLevel Debug level to query. 00076 @return True if logging is enabled. 00077 */ 00078 static bool getDebugLevelEnabled( const debugLevel_t inLevel); 00079 00080 00081 /** Print error message. 00082 00083 @param inErrno OS error number. 00084 @param inFormat Format string as in ANSI-C printf(). 00085 @param ... Variable argument list as in ANSI-C printf(). 00086 */ 00087 static void printPerror( const int inErrno, const char *inFormat, ...); 00088 00089 00090 /** Print error message. 00091 00092 @param inFormat Format string as in ANSI-C printf(). 00093 @param ... Variable argument list as in ANSI-C printf(). 00094 */ 00095 static void printError( const char *inFormat, ...); 00096 00097 00098 /** Print warning message. 00099 00100 @param inFormat Format string as in ANSI-C printf(). 00101 @param ... Variable argument list as in ANSI-C printf(). 00102 */ 00103 static void printWarning( const char *inFormat, ...); 00104 00105 00106 /** Print message. 00107 00108 @param inFormat Format string as in ANSI-C printf(). 00109 @param ... Variable argument list as in ANSI-C printf(). 00110 */ 00111 static void printMessage( const char *inFormat, ...); 00112 00113 00114 /** Print message. 00115 00116 @param inLevel Debug level the message belongs to. 00117 @param inFormat Format string as in ANSI-C printf(). 00118 @param ... Variable argument list as in ANSI-C printf(). 00119 */ 00120 static void printDebug( const debugLevel_t inLevel, const char *inFormat, ...); 00121 00122 00123 //======================================================================================= 00124 private: 00125 00126 //===================================================================================== 00127 // 00128 // locks 00129 // 00130 00131 /** Locking class to ensure exclusive access to output streams. 00132 00133 The class is intended to be used in a "locking is creation" design pattern. 00134 On creation an exclusive lock is optained, and on destruction the lock is released. 00135 00136 Uses a recursive mutex, i.e. does not distinguish reads/writes but my be called 00137 multiple times by the same thread without blocking. 00138 00139 Class is inlined for performance reasons. 00140 00141 @see LOW_thread_mutex 00142 */ 00143 class msgLock { 00144 public: 00145 /** Obtain the lock. 00146 */ 00147 inline msgLock() 00148 { 00149 msgMutex->lock(); 00150 }; 00151 00152 /** Release the lock. 00153 */ 00154 inline ~msgLock() 00155 { 00156 msgMutex->unlock(); 00157 }; 00158 00159 private: 00160 static LOW_thread_mutex *msgMutex; /**< Recursive mutex used for locking. */ 00161 }; 00162 00163 00164 //===================================================================================== 00165 // 00166 // type definitions 00167 // 00168 00169 /** Internal type to mark type of message. */ 00170 typedef enum { msg_log, warn_log, err_log, debug_log} logType_t; 00171 00172 00173 //===================================================================================== 00174 // 00175 // constructors 00176 // 00177 00178 /** Default constructor. 00179 It is private to prevent creating objects from this class as 00180 this is a static helper class. 00181 */ 00182 LOW_helper_msglog(); 00183 00184 /** Destructor. 00185 It is private to prevent creating objects from this class as 00186 this is a static helper class. 00187 */ 00188 ~LOW_helper_msglog(); 00189 00190 00191 //===================================================================================== 00192 // 00193 // static attributes 00194 // 00195 00196 static bool errorOccured; 00197 static bool debugLevels[all_dl]; 00198 00199 static bool useStdMsgStream; 00200 static bool useStdWarnStream; 00201 static bool useStdErrStream; 00202 static bool useStdDebugStream; 00203 static bool useExtraMsgStream; 00204 static bool useExtraWarnStream; 00205 static bool useExtraErrStream; 00206 static bool useExtraDebugStream; 00207 00208 static FILE* stdOutStream; 00209 static FILE* stdWarnStream; 00210 static FILE* stdErrStream; 00211 static FILE* stdDebugStream; 00212 static FILE* extraOutStream; 00213 static FILE* extraWarnStream; 00214 static FILE* extraErrStream; 00215 static FILE* extraDebugStream; 00216 00217 00218 //===================================================================================== 00219 // 00220 // static methods 00221 // 00222 00223 static void va_printToLog( const logType_t inLogType, const char *inFormat, va_list inParamList); 00224 static unsigned int fprintLogHeader( FILE *inExtraStream, FILE *inStdStream); 00225 static void fprintfMulti( FILE *inExtraStream, FILE *inStdStream, const char *inFormat, ...); 00226 static void vfprintfMulti( FILE *inExtraStream, FILE *inStdStream, const char *inFormat, va_list inAp); 00227 static void* callocCheck( const size_t inSize); 00228 00229 }; 00230 00231 #endif 00232