μHAL (v2.6.5)
Part of the IPbus software repository
LogLevels.hpp
Go to the documentation of this file.
1 
2 #ifndef _loglevels_hpp_
3 #define _loglevels_hpp_
4 
5 #include <iostream>
6 #include <stdint.h>
7 
8 
9 
10 template< typename U>
11 void insert ( std::ostream& aStr , const U& aU )
12 {
13  aStr << aU;
14 }
15 
16 // NOTE: the log_insert_warning is defined to bridge between the compilers used on Linux and OSX.
17 // While on linux the 'warning' attribute is defined, it is not in OSX 10.9 clang.
18 // On the other hand clang's 'deprecated' is messageless in g++, while it supports messages in clang.
19 // To make the story short, log_insert_warning uses the most appropriated attribute for the system in use.
20 #ifndef __clang__
21 #define log_insert_warning warning
22 #else
23 #define log_insert_warning deprecated
24 #endif
25 
26 __attribute__ ( ( log_insert_warning ( "Insertion of integer types can result in implicit casts. Consider using the Integer() formatter instead" ) ) )
27 void insert ( std::ostream& aStr , const uint32_t& aUint ) ;
28 
29 __attribute__ ( ( log_insert_warning ( "Insertion of integer types can result in implicit casts. Consider using the Integer() formatter instead" ) ) )
30 void insert ( std::ostream& aStr , const int32_t& aInt );
31 
32 __attribute__ ( ( log_insert_warning ( "Insertion of boolean types can result in implicit casts. Consider using the Boolean() formatter instead" ) ) )
33 void insert ( std::ostream& aStr , const bool& aBool );
34 
35 
36 namespace uhal
37 {
38 
39  template< typename T >
41  {
42  protected:
43  typedef void ( *fPtr ) ( std::ostream& aStr );
44 
45  BaseLogLevel ( std::ostream& aStr , fPtr aHeadFunction , fPtr aTailFunction ) : mStr ( aStr ) , mHeadFunction ( aHeadFunction ) , mTailFunction ( aTailFunction )
46  {}
47 
48  public:
49  T& operator() ()
50  {
51  return static_cast<T&> ( *this );
52  }
53 
54  T& head()
55  {
56  mHeadFunction ( mStr );
57  return static_cast<T&> ( *this );
58  }
59 
60  T& tail()
61  {
62  mTailFunction ( mStr );
63  return static_cast<T&> ( *this );
64  }
65 
66  std::ostream& stream()
67  {
68  return mStr;
69  }
70 
71  private:
72  std::ostream& mStr;
75 
76  };
77 
78 
79 
80 
82  class FatalLevel : public BaseLogLevel< FatalLevel >
83  {
85 
86  public:
87  FatalLevel ( std::ostream& aStr = std::cout , Base::fPtr aHeadFunction = FatalLevel::colour_head, Base::fPtr aTailFunction = FatalLevel::colour_tail );
88 
89  static void colour_head ( std::ostream& aStr );
90  static void colour_tail ( std::ostream& aStr );
91  };
92 
93  extern FatalLevel Fatal;
94 
95 
97  class ErrorLevel : public BaseLogLevel< ErrorLevel >
98  {
100 
101  public:
102  ErrorLevel ( std::ostream& aStr = std::cout , Base::fPtr aHeadFunction = ErrorLevel::colour_head, Base::fPtr aTailFunction = ErrorLevel::colour_tail );
103 
104  static void colour_head ( std::ostream& aStr );
105  static void colour_tail ( std::ostream& aStr );
106  };
107 
108  extern ErrorLevel Error;
109 
110 
111 
113  class WarningLevel : public BaseLogLevel< WarningLevel >
114  {
116 
117  public:
118  WarningLevel ( std::ostream& aStr = std::cout , Base::fPtr aHeadFunction = WarningLevel::colour_head, Base::fPtr aTailFunction = WarningLevel::colour_tail );
119 
120  static void colour_head ( std::ostream& aStr );
121  static void colour_tail ( std::ostream& aStr );
122  };
123 
124  extern WarningLevel Warning;
125 
127  class NoticeLevel : public BaseLogLevel< NoticeLevel >
128  {
129 
131 
132  public:
133  NoticeLevel ( std::ostream& aStr = std::cout , Base::fPtr aHeadFunction = NoticeLevel::colour_head, Base::fPtr aTailFunction = NoticeLevel::colour_tail );
134 
135  static void colour_head ( std::ostream& aStr );
136  static void colour_tail ( std::ostream& aStr );
137  };
138 
139  extern NoticeLevel Notice;
140 
142  class InfoLevel : public BaseLogLevel< InfoLevel >
143  {
145 
146  public:
147  InfoLevel ( std::ostream& aStr = std::cout , Base::fPtr aHeadFunction = InfoLevel::colour_head, Base::fPtr aTailFunction = InfoLevel::colour_tail );
148 
149  static void colour_head ( std::ostream& aStr );
150  static void colour_tail ( std::ostream& aStr );
151  };
152 
153  extern InfoLevel Info;
154 
156  class DebugLevel : public BaseLogLevel< DebugLevel >
157  {
159 
160  public:
161  DebugLevel ( std::ostream& aStr = std::cout , Base::fPtr aHeadFunction = DebugLevel::colour_head, Base::fPtr aTailFunction = DebugLevel::colour_tail );
162 
163  static void colour_head ( std::ostream& aStr );
164  static void colour_tail ( std::ostream& aStr );
165  };
166 
167  extern DebugLevel Debug;
168 
169 
170 
171 }
172 
173 
174 
175 #endif
176 
NoticeLevel Notice
Definition: LogLevels.cpp:96
std::ostream & stream()
Definition: LogLevels.hpp:66
void insert(std::ostream &aStr, const U &aU)
Definition: LogLevels.hpp:11
BaseLogLevel< NoticeLevel > Base
Definition: LogLevels.hpp:130
BaseLogLevel< FatalLevel > Base
Definition: LogLevels.hpp:84
WarningLevel Warning
Definition: LogLevels.cpp:79
Helper struct representing the Info log level to allow us to specialize functions according to their ...
Definition: LogLevels.hpp:142
Helper struct representing the Debug log level to allow us to specialize functions according to their...
Definition: LogLevels.hpp:156
Helper struct representing the Error log level to allow us to specialize functions according to their...
Definition: LogLevels.hpp:97
ErrorLevel Error
Definition: LogLevels.cpp:61
Helper struct representing the Warning log level to allow us to specialize functions according to the...
Definition: LogLevels.hpp:113
#define log_insert_warning
Definition: LogLevels.hpp:21
FatalLevel Fatal
Definition: LogLevels.cpp:42
std::ostream & mStr
Definition: LogLevels.hpp:72
BaseLogLevel< DebugLevel > Base
Definition: LogLevels.hpp:158
Helper struct representing the Fatal log level to allow us to specialize functions according to their...
Definition: LogLevels.hpp:82
BaseLogLevel< InfoLevel > Base
Definition: LogLevels.hpp:144
BaseLogLevel(std::ostream &aStr, fPtr aHeadFunction, fPtr aTailFunction)
Definition: LogLevels.hpp:45
BaseLogLevel< ErrorLevel > Base
Definition: LogLevels.hpp:99
DebugLevel Debug
Definition: LogLevels.cpp:133
Helper struct representing the Notice log level to allow us to specialize functions according to thei...
Definition: LogLevels.hpp:127
BaseLogLevel< WarningLevel > Base
Definition: LogLevels.hpp:115
InfoLevel Info
Definition: LogLevels.cpp:114
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...