Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Log.h
1 /*
2  * $Id$
3  * Debugging, colorful logging tools
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2008, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef _ELM_LOG_H
23 #define _ELM_LOG_H
24 
25 #include <ostream> // std::ostream
26 #include <elm/io/Output.h>
27 #include <elm/string/CString.h>
28 #include <elm/string/AutoString.h>
29 
30 namespace elm
31 {
32  namespace color{
33  class Color;
34  }
35  namespace log
36  {
37  class Debug {
38  public:
39  static elm::String debugPrefixWrapped(const char* file, int line);
40  static elm::String debugPrefix(const char* file, int line, const char* functionName="");
41  static elm::String debugPrefixFunction(const char* functionName);
42 
43  static inline bool getDebugFlag() { return _flags&DEBUG; }
44  static inline void setDebugFlag(bool set = true) { _flags = _flags-(_flags&DEBUG)+(set*DEBUG); }
45  static inline bool getSourceInfoFlag() { return _flags&SOURCE_INFO; }
46  static inline void setSourceInfoFlag(bool set = true) { _flags = _flags-(_flags&SOURCE_INFO)+(set*SOURCE_INFO); }
47  static inline bool getNumberingFlag() { return _flags&NUMBERING; }
48  static inline void setNumberingFlag(bool set = true) { _flags = _flags-(_flags&NUMBERING)+(set*NUMBERING); }
49  static inline bool getColorFlag() { return _flags&COLOR; }
50  static inline void setColorFlag(bool set = true) { _flags = _flags-(_flags&COLOR)+(set*COLOR); }
51 
52  static inline int getVerboseLevel() { return _verbose_level; }
53  static inline void setVerboseLevel(int verbose_level) { _verbose_level = verbose_level; }
54  static inline int getSourcePathLength() { return _srcpath_length; }
55  static inline void setSourcePathLength(int srcpath_length) { if(srcpath_length > 3) _srcpath_length = srcpath_length; }
56  static inline int getFunctionNameLength() { return _function_name_length; }
57  static inline void setFunctionNameLength(int function_name_length) { if(function_name_length > 3) _function_name_length = function_name_length; }
59  static void setPrefixColor(const color::Color& prefix_color);
60 
61  private:
62  static int _flags;
63  static int _verbose_level;
64  static int _srcpath_length; // should be > 3
65  static int _function_name_length; // should be > 3
66  static color::Color _prefix_color;
67 
68  enum // debug flags
69  {
70  DEBUG= 1 << 0, // print any debugs at all
71  SOURCE_INFO= 1 << 1, // show source file and line info prefix
72  NUMBERING= 1 << 2, // number debug outputs
73  COLOR= 1 << 3, // use colors
74  // ALL= 0b11111111, // everything
75  }; // enum
76  }; // Debug class
77  } // log namespace
78 
79  namespace color
80  {
81  class Color
82  {
83  public:
84  Color(const elm::CString& str) : _str(str) { }
85  inline elm::CString operator()() const // Red()
86  { return log::Debug::getColorFlag() ? _str : ""; }
87  inline operator elm::CString() const // (CString)Red
88  { return (*this)(); }
89  inline operator const char*() const // (const char*)Red
90  { return (*this)().chars(); }
91  friend inline elm::io::Output& operator<<(elm::io::Output& out, const Color& color) // elm::cout << Red
92  { return (out << color()); }
93  friend inline std::ostream& operator<<(std::ostream& out, const Color& color) // std::cout << Red
94  { return out << color().chars(); }
95  private:
96  elm::CString _str;
97  }; // Color class
98 
99  // Regular Bold Underline High Intensity Bold+High Intens. Background High Intens. Background
100  const Color Bla("\e[0;30m"), BBla("\e[1;30m"), UBla("\e[4;30m"), IBla("\e[0;90m"), BIBla("\e[1;90m"), On_Bla("\e[40m"), On_IBla("\e[0;100m");
101  const Color Red("\e[0;31m"), BRed("\e[1;31m"), URed("\e[4;31m"), IRed("\e[0;91m"), BIRed("\e[1;91m"), On_Red("\e[41m"), On_IRed("\e[0;101m");
102  const Color Gre("\e[0;32m"), BGre("\e[1;32m"), UGre("\e[4;32m"), IGre("\e[0;92m"), BIGre("\e[1;92m"), On_Gre("\e[42m"), On_IGre("\e[0;102m");
103  const Color Yel("\e[0;33m"), BYel("\e[1;33m"), UYel("\e[4;33m"), IYel("\e[0;93m"), BIYel("\e[1;93m"), On_Yel("\e[43m"), On_IYel("\e[0;103m");
104  const Color Blu("\e[0;34m"), BBlu("\e[1;34m"), UBlu("\e[4;34m"), IBlu("\e[0;94m"), BIBlu("\e[1;94m"), On_Blu("\e[44m"), On_IBlu("\e[0;104m");
105  const Color Pur("\e[0;35m"), BPur("\e[1;35m"), UPur("\e[4;35m"), IPur("\e[0;95m"), BIPur("\e[1;95m"), On_Pur("\e[45m"), On_IPur("\e[0;105m");
106  const Color Cya("\e[0;36m"), BCya("\e[1;36m"), UCya("\e[4;36m"), ICya("\e[0;96m"), BICya("\e[1;96m"), On_Cya("\e[46m"), On_ICya("\e[0;106m");
107  const Color Whi("\e[0;37m"), BWhi("\e[1;37m"), UWhi("\e[4;37m"), IWhi("\e[0;97m"), BIWhi("\e[1;97m"), On_Whi("\e[47m"), On_IWhi("\e[0;107m");
108 
109  const Color RCol("\e[0m"); // Reset colors
110  const Color Bold("\e[1m"); // Switch bold text ON
111  const Color Dim("\e[2m"); // Switch darker text ON
112  const Color NoBold("\e[21m"); // Switch bold text OFF
113  const Color NoDim("\e[22m"); // Switch darker text OFF
114  // No support for Blink (\e[5m) as it is deprecated
115  } // color namespace
116 } // elm namespace
117 
118 #define ELM_DBG(str) { if(elm::log::Debug::getDebugFlag()) elm::cout << elm::log::Debug::debugPrefix(__FILE__, __LINE__) << str << elm::color::RCol(); } // standard debug
119 #define ELM_DBGLN(str) { if(elm::log::Debug::getDebugFlag()) elm::cout << elm::log::Debug::debugPrefix(__FILE__, __LINE__) << str << elm::color::RCol() << elm::io::endl; } // debug with new line
120 #define ELM_DBGV(level, str) { if(level & elm::log::Debug::getVerboseLevel()) ELM_DBG(str); } // verbose debug
121 
122 // aliases
123 #ifndef ELM_NO_DBG
124  #define DBG ELM_DBG
125  #define DBGLN ELM_DBGLN
126  #define DBGV ELM_DBG
127 #endif
128 
129 #endif
elm::color::BIPur
const Color BIPur("\e[1;95m")
elm::color::NoBold
const Color NoBold("\e[21m")
elm::color::UBla
const Color UBla("\e[4;30m")
elm::color::BWhi
const Color BWhi("\e[1;37m")
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::color::IGre
const Color IGre("\e[0;92m")
elm::log::Debug::setFunctionNameLength
static void setFunctionNameLength(int function_name_length)
Definition: Log.h:57
elm::color::BRed
const Color BRed("\e[1;31m")
elm::log::Debug::debugPrefixWrapped
static elm::String debugPrefixWrapped(const char *file, int line)
Display prefix of log line, which may include the source path and line number of file,...
Definition: log_Log.cpp:193
elm::color::BICya
const Color BICya("\e[1;96m")
elm::color::On_IWhi
const Color On_IWhi("\e[0;107m")
elm::color::Bla
const Color Bla("\e[0;30m")
elm::color::BIYel
const Color BIYel("\e[1;93m")
elm::color::UWhi
const Color UWhi("\e[4;37m")
elm::log::Debug::getPrefixColor
static color::Color getPrefixColor()
Get the current color of the prefix of logs.
Definition: log_Log.cpp:267
elm::color::On_ICya
const Color On_ICya("\e[0;106m")
elm::color::UCya
const Color UCya("\e[4;36m")
elm::color::BGre
const Color BGre("\e[1;32m")
elm::color::BIGre
const Color BIGre("\e[1;92m")
elm::color::IBla
const Color IBla("\e[0;90m")
elm::log::Debug::getSourcePathLength
static int getSourcePathLength()
Retrieve the current length set for the source path in the prefix of logs.
Definition: Log.h:54
elm::color::IBlu
const Color IBlu("\e[0;94m")
elm::color::BPur
const Color BPur("\e[1;35m")
elm::color::BYel
const Color BYel("\e[1;33m")
elm::color::Color::operator<<
friend std::ostream & operator<<(std::ostream &out, const Color &color)
Definition: Log.h:93
elm::color::On_IPur
const Color On_IPur("\e[0;105m")
elm::log::Debug::setSourceInfoFlag
static void setSourceInfoFlag(bool set=true)
Enable/Disable printing source info in the prefix of logs.
Definition: Log.h:46
elm::color::Color::operator()
elm::CString operator()() const
Definition: Log.h:85
elm::log::Debug::getColorFlag
static bool getColorFlag()
Check if colors are enabled for the logs.
Definition: Log.h:49
elm::CString
Definition: CString.h:17
elm::color::On_IBla
const Color On_IBla("\e[0;100m")
elm::log::Debug::debugPrefixFunction
static elm::String debugPrefixFunction(const char *functionName)
Definition: log_Log.cpp:241
elm::color::On_IYel
const Color On_IYel("\e[0;103m")
elm::color::BIRed
const Color BIRed("\e[1;91m")
elm::color::URed
const Color URed("\e[4;31m")
elm::color::Pur
const Color Pur("\e[0;35m")
elm::color::Color::operator<<
friend elm::io::Output & operator<<(elm::io::Output &out, const Color &color)
Definition: Log.h:91
elm::log::Debug::getDebugFlag
static bool getDebugFlag()
Check if debugging messages are enabled.
Definition: Log.h:43
elm::color::Color
Define ANSI colors (including foreground, background, bold and underline) for global use in std C/elm...
Definition: Log.h:81
elm::color::UBlu
const Color UBlu("\e[4;34m")
elm::color::BBlu
const Color BBlu("\e[1;34m")
elm::color::UGre
const Color UGre("\e[4;32m")
elm::color::UPur
const Color UPur("\e[4;35m")
elm::color::BBla
const Color BBla("\e[1;30m")
elm::log::Debug::setNumberingFlag
static void setNumberingFlag(bool set=true)
Enable/Disable numbering of the logs.
Definition: Log.h:48
elm::color::On_Gre
const Color On_Gre("\e[42m")
elm
Definition: adapter.h:26
elm::log::Debug::debugPrefix
static elm::String debugPrefix(const char *file, int line, const char *functionName="")
Definition: log_Log.cpp:226
elm::color::RCol
const Color RCol("\e[0m")
elm::log::Debug::setPrefixColor
static void setPrefixColor(const color::Color &prefix_color)
Set a new color for the prefix of logs.
Definition: log_Log.cpp:270
elm::color::On_Red
const Color On_Red("\e[41m")
elm::color::On_Blu
const Color On_Blu("\e[44m")
elm::color::Blu
const Color Blu("\e[0;34m")
elm::color::On_Whi
const Color On_Whi("\e[47m")
elm::color::Dim
const Color Dim("\e[2m")
elm::color::Bold
const Color Bold("\e[1m")
elm::color::BIBlu
const Color BIBlu("\e[1;94m")
elm::color::IPur
const Color IPur("\e[0;95m")
elm::log::Debug::getFunctionNameLength
static int getFunctionNameLength()
Definition: Log.h:56
elm::color::BCya
const Color BCya("\e[1;36m")
elm::color::Whi
const Color Whi("\e[0;37m")
elm::color::On_Bla
const Color On_Bla("\e[40m")
elm::log::Debug::setColorFlag
static void setColorFlag(bool set=true)
Enable/Disable the use of colors in logs.
Definition: Log.h:50
elm::log::Debug
Provide necessary static methods to configure debugging for the use of the ELM_DBG* macros.
Definition: Log.h:37
elm::log::Debug::setDebugFlag
static void setDebugFlag(bool set=true)
Enable/Disable debugging messages.
Definition: Log.h:44
elm::log::Debug::getSourceInfoFlag
static bool getSourceInfoFlag()
Check if printing source info in the prefix of logs is enabled.
Definition: Log.h:45
elm::color::UYel
const Color UYel("\e[4;33m")
elm::String
Definition: String.h:30
elm::color::On_IRed
const Color On_IRed("\e[0;101m")
elm::color::Red
const Color Red("\e[0;31m")
elm::color::Gre
const Color Gre("\e[0;32m")
elm::color::ICya
const Color ICya("\e[0;96m")
elm::color::NoDim
const Color NoDim("\e[22m")
elm::log::Debug::setVerboseLevel
static void setVerboseLevel(int verbose_level)
Set the verbose level to use.
Definition: Log.h:53
elm::color::BIWhi
const Color BIWhi("\e[1;97m")
elm::color::IWhi
const Color IWhi("\e[0;97m")
elm::str
string str(const char *s)
Definition: String.h:150
elm::log::Debug::getVerboseLevel
static int getVerboseLevel()
Retrieve the current verbose level.
Definition: Log.h:52
elm::color::On_Pur
const Color On_Pur("\e[45m")
elm::log::Debug::getNumberingFlag
static bool getNumberingFlag()
Check if the numbering of the logs is enabled.
Definition: Log.h:47
elm::color::On_Yel
const Color On_Yel("\e[43m")
elm::color::Cya
const Color Cya("\e[0;36m")
elm::color::On_Cya
const Color On_Cya("\e[46m")
elm::color::Yel
const Color Yel("\e[0;33m")
elm::color::BIBla
const Color BIBla("\e[1;90m")
elm::io::Output
Definition: Output.h:179
elm::color::Color::Color
Color(const elm::CString &str)
Definition: Log.h:84
elm::color::On_IBlu
const Color On_IBlu("\e[0;104m")
elm::log::Debug::setSourcePathLength
static void setSourcePathLength(int srcpath_length)
Set a new source path length to use in the prefix of logs.
Definition: Log.h:55
elm::color::IRed
const Color IRed("\e[0;91m")
elm::color::On_IGre
const Color On_IGre("\e[0;102m")
elm::color::IYel
const Color IYel("\e[0;93m")