cppDataPipeline 0.3.0
C++ Implementation of the FAIR Data Pipeline API
Loading...
Searching...
No Matches
logging.hxx
1
11#ifndef __FDP_LOGGING_HXX__
12#define __FDP_LOGGING_HXX__
13#include <memory>
14#include <iostream>
15#include <sstream>
16#include <string>
17#include <vector>
18
19namespace FairDataPipeline {
20 namespace logging {
21
22 enum LOG_LEVEL
23 {
24 TRACE = 0,
25 DEBUG = 1,
26 INFO = 2,
27 WARN = 3,
28 ERROR = 4,
29 CRITICAL = 5,
30 OFF = 6
31 };
32
33 const std::string& to_string( enum LOG_LEVEL lvl );
34
35 struct ISink
36 {
37 typedef std::shared_ptr< ISink > sptr;
38 virtual int log( enum LOG_LEVEL msg_lvl, const std::string& msg ) = 0;
39 virtual ~ISink(){};
40 };
41
42 class Logger;
43
44 class SinkFormatter;
45 typedef std::shared_ptr< SinkFormatter > SinkFormatterPtr;
46
48 {
49 typedef std::shared_ptr< ISinkFormatter > sptr;
50 virtual std::string header(enum LOG_LEVEL msg_lvl, Logger* logger )= 0;
51 virtual ~ISinkFormatter(){;}
52 };
53
54
55 class Sink : public ISink
56 {
57 public:
58 typedef std::shared_ptr< Sink > sptr;
59
60
61 enum LOG_LEVEL log_level();
62 void log_level( enum LOG_LEVEL log_lvl );
63
64 bool should_log( enum LOG_LEVEL msg_lvl );
65
66 void set_formatter( ISinkFormatter::sptr fmtr ){_fmtr = fmtr;}
67
68 int execute( Logger* logger, enum LOG_LEVEL msg_lvl, const std::string& s );
69
70 protected:
71 Sink( enum LOG_LEVEL log_lvl );
72
73 private:
74
75
76 enum LOG_LEVEL _log_lvl;
77 ISinkFormatter::sptr _fmtr;
78 };
79
80
81 class OStreamSink : public Sink
82 {
83 public:
84 typedef std::shared_ptr< OStreamSink > sptr;
85
86 static sptr create( enum LOG_LEVEL lvl, std::ostream& os );
87
88 int log( enum LOG_LEVEL msg_lvl, const std::string& s);
89
90 private:
91
92 OStreamSink(enum LOG_LEVEL log_lvl, std::ostream& os);
93
94 std::ostream& _os;
95 };
96
97 class CompositeSink : public Sink
98 {
99 public:
100 typedef std::shared_ptr< CompositeSink > sptr;
101
102
103 static sptr create( enum LOG_LEVEL lvl ){ return sptr( new CompositeSink( lvl ) ); }
104
105
106 int log( enum LOG_LEVEL msg_lvl, const std::string& s);
107 void add_sink( Sink::sptr sink ){ _sinks.push_back( sink ); }
108
109 private:
110 std::vector< Sink::sptr > _sinks;
111
112 CompositeSink(enum LOG_LEVEL log_lvl) : Sink(log_lvl){ ;}
113 };
114
115
117 {
118 public:
119 typedef std::shared_ptr< SinkFormatter > sptr;
120
121 static sptr create(){ return sptr( new SinkFormatter() ); }
122
124 {
125 }
126
127 std::string header( enum LOG_LEVEL msg_lvl, Logger* logger );
128
129 private:
130 SinkFormatter(){}
131 };
132
133 class Logger
134 {
135 public:
136 typedef std::shared_ptr< Logger > sptr;
138 {
139 public:
140 friend class Logger;
141 typedef std::shared_ptr< MsgBuilder > sptr;
142
143 ~MsgBuilder();
144
145 template< typename T >
146 MsgBuilder& operator<<( const T& s)
147 {
148 if( _logger->sink()->should_log( _msg_lvl ) )
149 {
150 _oss << s;
151 }
152 return *this;
153 }
154
155 private:
156 MsgBuilder( enum LOG_LEVEL msg_lvl, Logger* logger );
157 MsgBuilder( const MsgBuilder& rhs );
158
159 Logger* _logger;
160 enum LOG_LEVEL _msg_lvl;
161
162 std::ostringstream _oss{""};
163 };
164
165 static sptr create( enum LOG_LEVEL lvl, Sink::sptr sink, std::string name="" );
166
167 MsgBuilder info();
168 MsgBuilder debug();
169 MsgBuilder trace();
170 MsgBuilder warn();
171 MsgBuilder critical();
172 MsgBuilder error();
173
174 Sink::sptr sink(){ return _sink; }
175 const std::string& name() const { return _name;}
176
177 void set_level( enum LOG_LEVEL lvl){ _log_lvl = lvl;}
178 enum LOG_LEVEL get_level() const { return _log_lvl;}
179
180 protected:
181 Logger( enum LOG_LEVEL lvl, Sink::sptr sink, std::string name ) ;
182
183 private:
184
185 enum LOG_LEVEL _log_lvl;
186 Sink::sptr _sink;
187 std::string _name;
188 };
189 }
190
191 class logger
192 {
193 public:
194 typedef std::shared_ptr< logging::Logger > logger_sptr;
195
196 static logger_sptr get_logger();
197 private:
198 static logger_sptr _instance;
199 };
200
201} // namespace FairDataPipeline
202
203
204
205#endif
Definition: logging.hxx:192
Definition: logging.hxx:134
Definition: logging.hxx:82
Definition: logging.hxx:117
Definition: logging.hxx:56
Definition: logging.hxx:36