libcommonc++  0.7
Log.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Log_hxx
24 #define __ccxx_Log_hxx
25 
26 #include <commonc++/Common.h++>
29 #include <commonc++/FileLogger.h++>
30 #include <commonc++/Logger.h++>
31 #include <commonc++/String.h++>
32 
33 #include <cstdarg>
34 
35 namespace ccxx {
36 
39 class COMMONCPP_API LogFunctor
40 {
41  public:
42 
43  LogFunctor(const char *file, int line, LogLevel level = LogDebug,
44  bool nodebug = false);
45  ~LogFunctor();
46 
47  void operator()(const char *fmt, ...);
48  void operator()(LogLevel level, const char *fmt, ...);
49 
50  private:
51 
52  const char *_file;
53  int _line;
54  LogLevel _level;
55  bool _nodebug;
56 };
66 {
67  friend class LogFunctor;
68 
69  public:
70 
76  static void setFileLogFormat(const String& format);
77 
83  static void setConsoleLogFormat(const String& format);
84 
91  inline static void setUseConsoleLog(bool flag)
92  { _useConsoleLog = flag; }
93 
99  static void setFileLogger(FileLogger* logger);
100 
102  inline static FileLogger* getFileLogger()
103  { return(_fileLog); }
104 
111  static void setConsoleLogger(ConsoleLogger* logger);
112 
115  { return(_consoleLog); }
116 
123  inline static void setUseFileLog(bool flag)
124  { _useFileLog = flag; }
125 
132  static void setLogFile(const String& dir, const String& name);
133 
139  static void setLogFileMaxSize(size_t maxLogSize);
140 
148  static void setLogFileRotateCount(uint_t rotateCount);
149 
151  static void vlogFile(LogLevel level, const char* file, int line,
152  const char* message, va_list args);
153 
155  static void vlogConsole(LogLevel level, const char* file, int line,
156  const char* message, va_list args);
157 
166  static void log(LogLevel level, const char* file, int line,
167  const char* message, ...) ___PRINTF(4, 5);
168 
173  static bool assert_(const char* file, int line, const char* expr);
174 
175  /* temporary hackery */
176  static void disableCleanup();
177 
180  private:
181 
182  static CriticalSection _lock;
183  static ConsoleLogger* _consoleLog;
184  static FileLogger* _fileLog;
185  static bool _useConsoleLog;
186  static bool _useFileLog;
187 
188  Log(); // not supported
190 };
191 
193 #define Log_assert(EXPR) \
194  (void)((EXPR)|| ccxx::Log::assert_(__FILE__, __LINE__, #EXPR))
195 
196 #if (defined CCXX_OS_WINDOWS) && (defined _MSC_VER) && (_MSC_VER < 1400)
197 
198 // no variadic macros; emulate with LogFunctor
199 
200 #if (defined DEBUG) || (defined DEBUG_LOG_MESSAGES)
201 
206 #define Log_debug \
207  ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogDebug)
208 
209 #else // ! (DEBUG || DEBUG_LOG_MESSAGES)
210 
211 #define Log_debug \
212  ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogDebug, true)
213 
214 #endif // DEBUG || DEBUG_LOG_MESSAGES
215 
217 #define Log_info \
218  ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogInfo)
219 
221 #define Log_warning \
222  ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogWarning)
223 
225 #define Log_error \
226  ccxx::LogFunctor(__FILE__, __LINE__, ccxx::LogError)
227 
228 #elif (defined __GNUC__)
229 
230 // gcc-style variadic macros (for compatibility with older versions of GCC)
231 
232 #if (defined DEBUG) || (defined DEBUG_LOG_MESSAGES)
233 
238 #define Log_debug(M, args...) \
239  ccxx::Log::log(ccxx::LogDebug, __FILE__, __LINE__, M, ## args)
240 
241 #else // ! (DEBUG || DEBUG_LOG_MESSAGES)
242 
243 #define Log_debug(M, args...)
244 
245 #endif // DEBUG || DEBUG_LOG_MESSAGES
246 
248 #define Log_info(M, args...) \
249  ccxx::Log::log(ccxx::LogInfo, __FILE__, __LINE__, M, ## args)
250 
252 #define Log_warning(M, args...) \
253  ccxx::Log::log(ccxx::LogWarning, __FILE__, __LINE__, M, ## args)
254 
256 #define Log_error(M, args...) \
257  ccxx::Log::log(ccxx::LogError, __FILE__, __LINE__, M, ## args)
258 
259 #else // assume ANSI compiler with support for C99 variadic macros
260 
261 #if (defined DEBUG) || defined(DEBUG_LOG_MESSAGES)
262 
267 #define Log_debug(M, ...) \
268  ccxx::Log::log(ccxx::LogDebug, __FILE__, __LINE__, M, __VA_ARGS__)
269 
270 #else // ! (DEBUG || DEBUG_LOG_MESSAGES)
271 
272 #define Log_debug(M, ...)
273 
274 #endif // DEBUG || DEBUG_LOG_MESSAGES
275 
277 #define Log_info(M, ...) \
278  ccxx::Log::log(ccxx::LogInfo, __FILE__, __LINE__, M, __VA_ARGS__)
279 
281 #define Log_warning(M, ...) \
282  ccxx::Log::log(ccxx::LogWarning, __FILE__, __LINE__, M, __VA_ARGS__)
283 
285 #define Log_error(M, ...) \
286  ccxx::Log::log(ccxx::LogError, __FILE__, __LINE__, M, __VA_ARGS__)
287 
288 #endif // variadic checks
289 
290 } // namespace ccxx
291 
292 #endif // __ccxx_Log_hxx
static FileLogger * getFileLogger()
Get the FileLogger currently in use for logging to a file.
Definition: Log.h++:102
A logger that writes to a file, and can optionally perform log rotation.
Definition: FileLogger.h++:40
A critical section, a synchronization primitive that is typically more efficient than but roughly sem...
Definition: CriticalSection.h++:51
Logging routines.
Definition: Log.h++:65
static void setUseConsoleLog(bool flag)
Enable or disable logging to the console.
Definition: Log.h++:91
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
LogLevel
Logging levels.
Definition: LogFormat.h++:44
A logger that writes to the console (the standard error stream).
Definition: ConsoleLogger.h++:37
static ConsoleLogger * getConsoleLogger()
Get the ConsoleLogger currently in use for logging to the console.
Definition: Log.h++:114
static void setUseFileLog(bool flag)
Enable or disable logging to a file.
Definition: Log.h++:123
Debugging messages.
Definition: LogFormat.h++:46
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
#define ___PRINTF(M, N)
Definition: Common.h++:182
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
Definition: AllocationMap.c++:25