libcommonc++  0.7
LogFormat.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_LogFormat_hxx
24 #define __ccxx_LogFormat_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/Buffer.h++>
29 #include <commonc++/String.h++>
30 
31 #include <cstdio>
32 #include <cstdarg>
33 
34 #ifdef CCXX_OS_POSIX
35 #include <unistd.h>
36 #include <sys/utsname.h>
37 #endif
38 
39 #include <vector>
40 
41 namespace ccxx {
42 
44 enum LogLevel {
46  LogDebug = 1 << 0,
48  LogInfo = 1 << 1,
50  LogWarning = 1 << 2,
52  LogError = 1 << 3
53 };
54 
169 {
170  private:
171 
172  class TokenList; // fwd decl
173 
174  enum TokenCode { TOK_INVALID = -1, TOK_LITERAL = 0, TOK_TIME_LONG,
175  TOK_TIME_SHORT, TOK_DATE_LONG, TOK_DATE_SHORT, TOK_FILE,
176  TOK_LINE, TOK_MESSAGE, TOK_THREAD, TOK_PID, TOK_OSNAME,
177  TOK_ARCH, TOK_OSVER, TOK_USERNAME, TOK_HOST, TOK_LEVEL,
178  TOK_PERCENT, TOK_BOLD, TOK_UNDERLINE, TOK_INVERSE,
179  TOK_PLAIN, TOK_DEFAULT, TOK_BLACK, TOK_RED, TOK_GREEN,
180  TOK_YELLOW, TOK_BLUE, TOK_MAGENTA, TOK_CYAN, TOK_WHITE,
181  TOK_STYLESOFF, TOK_AUTOCOLOR, TOK_NEWLINE, TOK_APPNAME,
182  TOK_MAX };
183 
184  TokenCode lookupToken(char c);
185  void clearTokens();
186 
187  struct Token
188  {
189  Token(TokenCode token = TOK_LITERAL, const char* text = NULL, int len = 0);
190  ~Token();
191 
192  TokenCode _token;
193  char* _text;
194  };
195 
196  String _format;
197  TokenList* _tokens;
198 
199  public:
200 
206  LogFormat(String format = "[%D %T] %F:%L %m");
207 
209  virtual ~LogFormat();
210 
212  void setFormat(const String& format);
213 
226  void format(CharBuffer& buffer, LogLevel level, const char* file,
227  int line, const char* message, va_list args);
228 
230  void setShortDateFormat(const String& format);
231 
233  void setLongDateFormat(const String& format);
234 
236  void setShortTimeFormat(const String& format);
237 
239  void setLongTimeFormat(const String& format);
240 
241  private:
242 
243  static size_t _eolLen;
244 
245  DateTimeFormat _shortDateFormat;
246  DateTimeFormat _longDateFormat;
247  DateTimeFormat _shortTimeFormat;
248  DateTimeFormat _longTimeFormat;
249 
251 };
252 
253 } // namespace ccxx
254 
255 #endif // __ccxx_LogFormat_hxx
A formatter for dates and times.
Definition: DateTimeFormat.h++:97
Informational messages.
Definition: LogFormat.h++:48
A log message formatter.
Definition: LogFormat.h++:168
Error messages.
Definition: LogFormat.h++:52
LogLevel
Logging levels.
Definition: LogFormat.h++:44
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 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
Warning messages.
Definition: LogFormat.h++:50