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_LogFormat_hxx
00024 #define __ccxx_LogFormat_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/String.h++>
00028 #include <commonc++/Buffer.h++>
00029 #include <commonc++/DateTimeFormat.h++>
00030
00031 #include <cstdio>
00032 #include <cstdarg>
00033
00034 #ifdef CCXX_OS_POSIX
00035 #include <unistd.h>
00036 #include <sys/utsname.h>
00037 #endif
00038
00039 #include <vector>
00040
00041 namespace ccxx {
00042
00043 enum LogLevel { LogDebug = 0x01, LogInfo = 0x02, LogWarning = 0x04,
00044 LogError = 0x08 };
00045
00159 class COMMONCPP_API LogFormat
00160 {
00161 private:
00162
00163 class TokenList;
00164
00165 enum TokenCode { TOK_INVALID = -1, TOK_LITERAL = 0, TOK_TIME_LONG,
00166 TOK_TIME_SHORT, TOK_DATE_LONG, TOK_DATE_SHORT, TOK_FILE,
00167 TOK_LINE, TOK_MESSAGE, TOK_THREAD, TOK_PID, TOK_OSNAME,
00168 TOK_ARCH, TOK_OSVER, TOK_USERNAME, TOK_HOST, TOK_LEVEL,
00169 TOK_PERCENT, TOK_BOLD, TOK_UNDERLINE, TOK_INVERSE,
00170 TOK_PLAIN, TOK_DEFAULT, TOK_BLACK, TOK_RED, TOK_GREEN,
00171 TOK_YELLOW, TOK_BLUE, TOK_MAGENTA, TOK_CYAN, TOK_WHITE,
00172 TOK_STYLESOFF, TOK_AUTOCOLOR, TOK_NEWLINE, TOK_APPNAME,
00173 TOK_MAX };
00174
00175 TokenCode lookupToken(char c);
00176 void clearTokens();
00177
00178 struct Token
00179 {
00180 Token(TokenCode token = TOK_LITERAL, const char *text = NULL, int len = 0);
00181 ~Token();
00182
00183 TokenCode _token;
00184 char *_text;
00185 };
00186
00187 String _format;
00188 TokenList *_tokens;
00189
00190 public:
00191
00196 LogFormat(String format = "[%D %T] %F:%L %m");
00197
00199 virtual ~LogFormat() throw();
00200
00202 void setFormat(const String &format);
00203
00215 void format(CharBuffer &buffer, LogLevel level, const char *file,
00216 int line, const char *message, va_list args);
00217
00219 void setShortDateFormat(const String &format);
00220
00222 void setLongDateFormat(const String &format);
00223
00225 void setShortTimeFormat(const String &format);
00226
00228 void setLongTimeFormat(const String &format);
00229
00230 private:
00231
00232 static size_t _eolLen;
00233
00234 DateTimeFormat _shortDateFormat;
00235 DateTimeFormat _longDateFormat;
00236 DateTimeFormat _shortTimeFormat;
00237 DateTimeFormat _longTimeFormat;
00238
00239 CCXX_COPY_DECLS(LogFormat);
00240 };
00241
00242 };
00243
00244 #endif // __ccxx_LogFormat_hxx
00245
00246