00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_Log_hxx
00024 #define __ccxx_Log_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/ConsoleLogger.h++>
00028 #include <commonc++/CriticalSection.h++>
00029 #include <commonc++/FileLogger.h++>
00030 #include <commonc++/Logger.h++>
00031 #include <commonc++/String.h++>
00032
00033 #include <cstdarg>
00034
00035 namespace ccxx {
00036
00039 class COMMONCPP_API LogFunctor
00040 {
00041 public:
00042
00043 LogFunctor(const char *file, int line, LogLevel level = LogDebug,
00044 bool nodebug = false);
00045 ~LogFunctor();
00046
00047 void operator()(const char *fmt, ...);
00048 void operator()(LogLevel level, const char *fmt, ...);
00049
00050 private:
00051
00052 const char *_file;
00053 int _line;
00054 LogLevel _level;
00055 bool _nodebug;
00056 };
00065 class COMMONCPP_API Log
00066 {
00067 friend class LogFunctor;
00068 public:
00069
00074 static void setFileLogFormat(const String &format);
00075
00080 static void setConsoleLogFormat(const String &format);
00081
00087 inline static void setUseConsoleLog(bool flag)
00088 { _useConsoleLog = flag; }
00089
00094 static void setFileLogger(FileLogger *logger);
00095
00097 inline static FileLogger *getFileLogger()
00098 { return(_fileLog); }
00099
00105 static void setConsoleLogger(ConsoleLogger *logger);
00106
00108 inline static ConsoleLogger *getConsoleLogger()
00109 { return(_consoleLog); }
00110
00116 inline static void setUseFileLog(bool flag)
00117 { _useFileLog = flag; }
00118
00124 static void setLogFile(const String& dir, const String& name);
00125
00130 static void setLogFileMaxSize(size_t maxLogSize);
00131
00138 static void setLogFileRotateCount(uint_t rotateCount);
00139
00141 static void vlogFile(LogLevel level, const char *file, int line,
00142 const char *message, va_list args);
00143
00145 static void vlogConsole(LogLevel level, const char *file, int line,
00146 const char *message, va_list args);
00147
00156 static void log(LogLevel level, const char *file, int line,
00157 const char *message, ...) ___PRINTF(4, 5);
00158
00162 static bool assert_(const char *file, int line, const char *expr);
00163
00164
00165 static void disableCleanup();
00166
00169 private:
00170
00171 static CriticalSection _lock;
00172 static ConsoleLogger *_consoleLog;
00173 static FileLogger *_fileLog;
00174 static bool _useConsoleLog;
00175 static bool _useFileLog;
00176
00177 Log();
00178 CCXX_COPY_DECLS(Log);
00179 };
00180
00182 #define Log_assert(EXPR) \
00183 (void)((EXPR)|| ccxx::Log::assert_(__FILE__, __LINE__, #EXPR))
00184
00185
00186 #if (defined CCXX_OS_WINDOWS) && (defined _MSC_VER) && (_MSC_VER < 1400)
00187
00188
00189
00190 #if (defined DEBUG) || (defined DEBUG_LOG_MESSAGES)
00191
00195 #define Log_debug \
00196 ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogDebug)
00197
00198 #else // ! (DEBUG || DEBUG_LOG_MESSAGES)
00199
00200 #define Log_debug \
00201 ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogDebug, true)
00202
00203 #endif // DEBUG || DEBUG_LOG_MESSAGES
00204
00206 #define Log_info \
00207 ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogInfo)
00208
00210 #define Log_warning \
00211 ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogWarning)
00212
00214 #define Log_error \
00215 ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogError)
00216
00217 #elif (defined __GNUC__)
00218
00219
00220
00221 #if (defined DEBUG) || (defined DEBUG_LOG_MESSAGES)
00222
00226 #define Log_debug(M, args...) \
00227 ccxx::Log::log(ccxx::LogDebug, __FILE__, __LINE__, M, ## args)
00228
00229 #else // ! (DEBUG || DEBUG_LOG_MESSAGES)
00230
00231 #define Log_debug(M, args...)
00232
00233 #endif // DEBUG || DEBUG_LOG_MESSAGES
00234
00236 #define Log_info(M, args...) \
00237 ccxx::Log::log(ccxx::LogInfo, __FILE__, __LINE__, M, ## args)
00238
00240 #define Log_warning(M, args...) \
00241 ccxx::Log::log(ccxx::LogWarning, __FILE__, __LINE__, M, ## args)
00242
00244 #define Log_error(M, args...) \
00245 ccxx::Log::log(ccxx::LogError, __FILE__, __LINE__, M, ## args)
00246
00247 #else // assume ANSI compiler with support for C99 variadic macros
00248
00249 #if (defined DEBUG) || defined(DEBUG_LOG_MESSAGES)
00250
00254 #define Log_debug(M, ...) \
00255 ccxx::Log::log(ccxx::LogDebug, __FILE__, __LINE__, M, __VA_ARGS__)
00256
00257 #else // ! (DEBUG || DEBUG_LOG_MESSAGES)
00258
00259 #define Log_debug(M, ...)
00260
00261 #endif // DEBUG || DEBUG_LOG_MESSAGES
00262
00264 #define Log_info(M, ...) \
00265 ccxx::Log::log(ccxx::LogInfo, __FILE__, __LINE__, M, __VA_ARGS__)
00266
00268 #define Log_warning(M, ...) \
00269 ccxx::Log::log(ccxx::LogWarning, __FILE__, __LINE__, M, __VA_ARGS__)
00270
00272 #define Log_error(M, ...) \
00273 ccxx::Log::log(ccxx::LogError, __FILE__, __LINE__, M, __VA_ARGS__)
00274
00275 #endif // variadic checks
00276
00277 };
00278
00279 #endif // __ccxx_Log_hxx
00280
00281