LogFormat.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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; // fwd decl
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 }; // namespace ccxx
00243 
00244 #endif // __ccxx_LogFormat_hxx
00245 
00246 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3