Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 log
33  {
34  extern int flags;
35  extern int verbose_level;
36  extern int srcpath_length; // should be > 3
37  enum // debug flags
38  {
39  DEBUG= 1 << 0, // print any debugs at all
40  SOURCE_INFO= 1 << 1, // show source file and line info prefix
41  NUMBERING= 1 << 2, // number debug outputs
42  COLOR= 1 << 3, // use colors
44  ALL= 0b11111111, // everything
45  };
46  } // log namespace
47 
48  namespace color
49  {
50  class Color
51  {
52  public:
53  Color(const elm::CString& str) : _str(str) { }
54  inline elm::CString operator()() const // Red()
55  { return (log::flags&log::COLOR) ? _str : ""; }
56  inline operator elm::CString() const // (CString)Red
57  { return (*this)(); }
58  inline operator const char*() const // (const char*)Red
59  { return (*this)().chars(); }
60  friend inline elm::io::Output& operator<<(elm::io::Output& out, const Color& color) // elm::cout << Red
61  { return (out << color()); }
62  friend inline std::ostream& operator<<(std::ostream& out, const Color& color) // std::cout << Red
63  { return out << color().chars(); }
64  private:
65  elm::CString _str;
66  }; // Color class
67 
68  // Regular Bold Underline High Intensity Bold+High Intens. Background High Intens. Background
69  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");
70  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");
71  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");
72  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");
73  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");
74  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");
75  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");
76  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");
77 
78  const Color RCol("\e[0m"); // Reset colors
79  const Color Bold("\e[1m"); // Switch bold text ON
80  const Color Dim("\e[2m"); // Switch darker text ON
81  const Color NoBold("\e[21m"); // Switch bold text OFF
82  const Color NoDim("\e[22m"); // Switch darker text OFF
83  // No support for Blink (\e[5m) as it is deprecated
84  } // color namespace
85  namespace log
86  {
88  class Debug {
89  public:
90  static elm::String debugPrefix(const char* file, int line);
91  }; // Debug class
92  } // log namespace
93 } // elm namespace
94 
95 #define ELM_DBG(str) { if(elm::log::flags&elm::log::DEBUG) elm::cout << elm::log::Debug::debugPrefix(__FILE__, __LINE__) << str << elm::color::RCol(); } // standard debug
96 #define ELM_DBGLN(str) { if(elm::log::flags&elm::log::DEBUG) elm::cout << elm::log::Debug::debugPrefix(__FILE__, __LINE__) << str << elm::color::RCol() << elm::io::endl; } // debug with new line
97 #define ELM_DBGV(level, str) { if(level & elm::log::verbose_level) ELM_DBG(str); } // verbose debug
98 
99 // aliases
100 #ifndef ELM_NO_DBG
101  #define DBG ELM_DBG
102  #define DBGLN ELM_DBGLN
103  #define DBGV ELM_DBG
104 #endif
105 
106 #endif
const Color On_IGre("\e[0;102m")
const Color Dim("\e[2m")
const Color Bold("\e[1m")
const Color On_Yel("\e[43m")
Definition: CString.h:17
const Color BRed("\e[1;31m")
int flags
Flags to set for the debugging macros.
Definition: log_Log.cpp:52
const Color IGre("\e[0;92m")
const Color Bla("\e[0;30m")
const Color ICya("\e[0;96m")
const Color BIYel("\e[1;93m")
const Color UWhi("\e[4;37m")
friend elm::io::Output & operator<<(elm::io::Output &out, const Color &color)
Definition: Log.h:60
Definition: Output.h:161
int verbose_level
Verbose bit mask. ELM_DBGV will only be enabled if the provided parameter level & verbose_level is no...
Definition: log_Log.cpp:60
const Color On_IYel("\e[0;103m")
const Color IBla("\e[0;90m")
const Color IBlu("\e[0;94m")
const Color URed("\e[4;31m")
const Color BICya("\e[1;96m")
const Color On_IPur("\e[0;105m")
const Color NoBold("\e[21m")
int srcpath_length
Numeric value that defines the constant length of the source path in the prefix.
Definition: log_Log.cpp:68
const Color BYel("\e[1;33m")
const Color BIBla("\e[1;90m")
const Color BWhi("\e[1;37m")
const Color UBla("\e[4;30m")
const Color UPur("\e[4;35m")
const Color BBla("\e[1;30m")
const Color BGre("\e[1;32m")
Define ANSI colors (including foreground, background, bold and underline) for global use in std C/elm...
Definition: Log.h:50
const Color On_IBla("\e[0;100m")
const Color Pur("\e[0;35m")
Definition: Log.h:40
const Color On_Red("\e[41m")
const Color On_Bla("\e[40m")
const Color BIRed("\e[1;91m")
const Color NoDim("\e[22m")
const Color On_Blu("\e[44m")
const Color On_Gre("\e[42m")
elm::CString operator()() const
Definition: Log.h:54
const Color BIBlu("\e[1;94m")
static elm::String debugPrefix(const char *file, int line)
Definition: log_Log.cpp:106
const Color BCya("\e[1;36m")
const Color Whi("\e[0;37m")
elm::color::Color prefix_color
Color used for printing the prefix message. The default value is elm::color::Yel. ...
Definition: log_Log.cpp:76
const Color BPur("\e[1;35m")
sys::SystemOutStream & out
Definition: system_SystemIO.cpp:101
const Color UGre("\e[4;32m")
const Color UYel("\e[4;33m")
const Color Yel("\e[0;33m")
const Color On_Cya("\e[46m")
const Color On_Whi("\e[47m")
Definition: Log.h:39
Definition: String.h:31
Definition: Log.h:44
Definition: Log.h:42
const Color Blu("\e[0;34m")
const Color BBlu("\e[1;34m")
Definition: Log.h:41
const Color On_ICya("\e[0;106m")
Color(const elm::CString &str)
Definition: Log.h:53
const Color Gre("\e[0;32m")
const Color On_IRed("\e[0;101m")
const Color BIWhi("\e[1;97m")
const Color On_IBlu("\e[0;104m")
const Color On_IWhi("\e[0;107m")
Definition: Log.h:43
const Color On_Pur("\e[45m")
const Color UBlu("\e[4;34m")
Provide necessary static methods for the use of ELM_DBG* macros.
Definition: Log.h:88
const Color UCya("\e[4;36m")
const Color IWhi("\e[0;97m")
const Color BIGre("\e[1;92m")
const Color IPur("\e[0;95m")
const Color RCol("\e[0m")
friend std::ostream & operator<<(std::ostream &out, const Color &color)
Definition: Log.h:62
const Color Cya("\e[0;36m")
const Color Red("\e[0;31m")
const Color IYel("\e[0;93m")
const Color BIPur("\e[1;95m")
const Color IRed("\e[0;91m")