Log.hh

00001 /*
00002 ** TowBowlTactics, a turn-based strategy football game.
00003 **
00004 ** Copyright (C) 2006-2010 The TBT Team.
00005 **
00006 ** This program is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU General Public License
00008 ** as published by the Free Software Foundation; either version 2
00009 ** of the License, or (at your option) any later version.
00010 **
00011 ** The complete GNU General Public Licence Notice can be found as the
00012 ** `NOTICE' file in the root directory.
00013 **
00014 ** The TBT Team consists of people listed in the `AUTHORS' file.
00015 */
00016 
00017 #ifndef LOG_HH_
00018 # define LOG_HH_
00019 
00020 # include "misc/Compose.hh"
00021 
00022 # include <cstring>
00023 # include <fstream>
00024 # include <iostream>
00025 # include <map>
00026 
00034 class Log
00035 {
00036 public:
00037   Log();
00038   ~Log();
00039 
00041   static Log* getDefaultInst();
00042 
00045   void setVerboseLevel(int level);
00049   void setVerboseLevel(int level, const std::string& modname);
00054   void setVerboseMask(int mask);
00055 
00057   void setOutputFile(const std::string &filename);
00058   
00061   void setPrintModule(bool enable = true);
00062 
00064   void setPrintFunc(bool enable = true);
00065 
00067   //         (s: seconds, m: milliseconds)
00068   void setPrintTimestamp(bool enable = true);
00069   
00071   void setUseColor(bool enable = true);
00072 
00073  
00075   std::ostream& getStream();
00077   void logPrefix(const char *modname, const char *modcolor,
00078                  const char *funcname, int level);
00080   int getVerboseMask(const char* modname) const;
00081 
00082 private:
00083   bool use_stderr_;
00084   bool print_func_;
00085   bool print_module_;
00086   bool print_timestamp_;
00087   bool use_color_;
00088   std::ofstream out_file_;
00089   
00091   int verbose_mask_;
00092 
00093   typedef std::map<std::string, int> ModuleMaskMap;
00095   ModuleMaskMap module_masks_;
00096 
00097   static Log* default_inst_;
00098 };
00099 
00100 inline std::ostream& Log::getStream()
00101 {
00102   if (use_stderr_)
00103     return std::cerr;
00104   return out_file_;
00105 }
00106 
00107 inline int Log::getVerboseMask(const char* modname) const
00108 {
00109   ModuleMaskMap::const_iterator it = module_masks_.find(modname);
00110   if (it == module_masks_.end())
00111     return verbose_mask_;
00112   else
00113     return it->second;
00114 }
00115 
00116 inline Log* Log::getDefaultInst()
00117 {
00118   return default_inst_;
00119 }
00120 
00121 
00124 
00125 # ifndef MODULE_NAME
00126 #  define MODULE_NAME "unset"
00127 # endif
00128 # ifndef MODULE_COLOR
00129 #  define MODULE_COLOR NULL
00130 # endif
00131 
00132 # define C_NONE        "\e[0m"
00133 # define C_RED         "\e[1;31m"
00134 # define C_GREEN       "\e[1;32m"
00135 # define C_BROWN       "\e[1;33m"
00136 # define C_BLUE        "\e[1;34m"
00137 # define C_PURPLE      "\e[1;35m"
00138 # define C_CYAN        "\e[1;36m"
00139 # define C_GRAY        "\e[0;30m"
00140 # define C_BRED        "\e[0;31m"
00141 # define C_BGREEN      "\e[0;32m"
00142 # define C_YELLOW      "\e[0;33m"
00143 # define C_BBLUE       "\e[0;34m"
00144 # define C_BPURPLE     "\e[0;35m"
00145 # define C_BCYAN       "\e[0;36m"
00146 
00147 # define TLOG(Log, Level, Msg...)               \
00148 do {                                            \
00149   if ((Log)->getVerboseMask(MODULE_NAME)        \
00150         & (1 << (Level)))                       \
00151     {                                           \
00152       (Log)->logPrefix(MODULE_NAME,             \
00153                        MODULE_COLOR,            \
00154                        __func__,                \
00155                        Level);                  \
00156       tlog((Log)->getStream(), Msg);            \
00157     }                                           \
00158 } while (0)
00159 
00160 /*
00161  * Use default logger
00162  */
00163 # define ERR(Msg...)  TLOG(Log::getDefaultInst(), 0, Msg)
00164 # define WARN(Msg...) TLOG(Log::getDefaultInst(), 1, Msg)
00165 # define LOG1(Msg...) TLOG(Log::getDefaultInst(), 2, Msg)
00166 # define LOG2(Msg...) TLOG(Log::getDefaultInst(), 3, Msg)
00167 # define LOG3(Msg...) TLOG(Log::getDefaultInst(), 4, Msg)
00168 # define LOG4(Msg...) TLOG(Log::getDefaultInst(), 5, Msg)
00169 # define LOG5(Msg...) TLOG(Log::getDefaultInst(), 6, Msg)
00170 # define LOG6(Msg...) TLOG(Log::getDefaultInst(), 7, Msg)
00171 
00172 /*
00173  * Use supplied log object
00174  */
00175 # define xERR(Log, Msg...)  TLOG(Log, 0, Msg)
00176 # define xWARN(Log, Msg...) TLOG(Log, 1, Msg)
00177 # define xLOG1(Log, Msg...) TLOG(Log, 2, Msg)
00178 # define xLOG2(Log, Msg...) TLOG(Log, 3, Msg)
00179 # define xLOG3(Log, Msg...) TLOG(Log, 4, Msg)
00180 # define xLOG4(Log, Msg...) TLOG(Log, 5, Msg)
00181 # define xLOG5(Log, Msg...) TLOG(Log, 6, Msg)
00182 # define xLOG6(Log, Msg...) TLOG(Log, 7, Msg)
00183 
00184   
00186 
00187 #endif /* !LOG_H_ */
Generated on Mon Apr 5 21:17:12 2010 for Stechec/TBT by  doxygen 1.6.3