libosmscout 1.1.1
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_UTIL_LOGGER_H
2#define OSMSCOUT_UTIL_LOGGER_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2015 Tim Teulings
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library 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 GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <string_view>
24
25#include <osmscout/lib/CoreFeatures.h>
27
30
31// Since we have a DEBUG enumeration member
32#ifdef DEBUG
33#undef DEBUG
34#endif
35
36// Since we have a ERROR enumeration member
37#ifdef ERROR
38#undef ERROR
39#endif
40
41namespace osmscout {
42
50 {
51 public:
58
65 {
66 public:
67 Destination() = default;
68 virtual ~Destination() = default;
69
73 virtual void Print(const std::string& value) = 0;
74
78 virtual void Print(const std::string_view& value) = 0;
79
83 virtual void Print(const char* value) = 0;
84
88 virtual void Print(bool value) = 0;
89 virtual void Print(short value) = 0;
90 virtual void Print(unsigned short value) = 0;
91 virtual void Print(int value) = 0;
92 virtual void Print(unsigned int value) = 0;
93 virtual void Print(long value) = 0;
94 virtual void Print(unsigned long value) = 0;
95 virtual void Print(long long value) = 0;
96 virtual void Print(unsigned long long value) = 0;
97
102 virtual void PrintLn() = 0;
103 };
104
113 {
114 private:
115 Destination& destination;
116
117 public:
118 explicit Line(Destination& destination);
119 virtual ~Line();
120
121 Line& operator<<(const std::string& value)
122 {
123 destination.Print(value);
124
125 return *this;
126 }
127
128 Line& operator<<(const std::string_view& value)
129 {
130 destination.Print(value);
131
132 return *this;
133 }
134
135 Line& operator<<(const char* value)
136 {
137 destination.Print(value);
138
139 return *this;
140 }
141
142 Line& operator<<(bool value)
143 {
144 destination.Print(value);
145
146 return *this;
147 }
148
149 Line& operator<<(short value)
150 {
151 destination.Print(value);
152
153 return *this;
154 }
155
156 Line& operator<<(unsigned short value)
157 {
158 destination.Print(value);
159
160 return *this;
161 }
162
163 Line& operator<<(int value)
164 {
165 destination.Print(value);
166
167 return *this;
168 }
169
170 Line& operator<<(unsigned int value)
171 {
172 destination.Print(value);
173
174 return *this;
175 }
176
177 Line& operator<<(long value)
178 {
179 destination.Print(value);
180
181 return *this;
182 }
183
184 Line& operator<<(unsigned long value)
185 {
186 destination.Print(value);
187
188 return *this;
189 }
190
191 Line& operator<<(long long value)
192 {
193 destination.Print(value);
194
195 return *this;
196 }
197
198 Line& operator<<(unsigned long long value)
199 {
200 destination.Print(value);
201
202 return *this;
203 }
204
205 Line& operator<<(float value);
206 Line& operator<<(double value);
207 Line& operator<<(void* value);
208
209 Line& operator<<(const StopClock& value)
210 {
211 destination.Print(value.ResultString());
212
213 return *this;
214 }
215
216 Line& operator<<(const Distance& value)
217 {
218 destination.Print(value.AsString());
219
220 return *this;
221 }
222 };
223
224 protected:
228 virtual Line Log(Level level) = 0;
229
230 public:
231 Logger() = default;
232 virtual ~Logger() = default;
233
238
243
249
254 };
255
261 {
262 private:
267 class OSMSCOUT_API NoOpDestination : public Destination
268 {
269 public:
270 void Print(const std::string& /*value*/) override
271 {
272 // no code
273 }
274
275 void Print(const std::string_view& /*value*/) override
276 {
277 // no code
278 }
279
280 void Print(const char* /*value*/) override
281 {
282 // no code
283 }
284
285 void Print(bool /*value*/) override
286 {
287 // no code
288 }
289
290 void Print(short /*value*/) override
291 {
292 // no code
293 }
294
295 void Print(unsigned short /*value*/) override
296 {
297 // no code
298 }
299
300 void Print(int /*value*/) override
301 {
302 // no code
303 }
304
305 void Print(unsigned int /*value*/) override
306 {
307 // no code
308 }
309
310 void Print(long /*value*/) override
311 {
312 // no code
313 }
314
315 void Print(unsigned long /*value*/) override
316 {
317 // no code
318 }
319
320 void Print(long long /*value*/) override
321 {
322 // no code
323 }
324
325 void Print(unsigned long long /*value*/) override
326 {
327 // no code
328 }
329
330 void PrintLn() override
331 {
332 // no code
333 }
334 };
335
336 private:
337 NoOpDestination destination;
338
339 public:
340 Line Log(Level /*level*/) override
341 {
342 return Line(destination);
343 }
344 };
345
352 {
353 private:
354 std::shared_ptr<Logger> logger;
355 NoOpLogger noOpLogger;
356 bool logDebug=false;
357 bool logInfo=true;
358 bool logWarn=true;
359 bool logError=true;
360
361 public:
363 Log(const Log &log) = default;
364 Log(Log &&log) = default;
365
366 Log& operator=(const Log &log) = default;
367 Log& operator=(Log &&log) = default;
368
369 ~Log() = default;
370
371 void SetLogger(const std::shared_ptr<Logger> &logger);
372
373 Log& Debug(bool state)
374 {
375 logDebug=state;
376
377 return *this;
378 }
379
380 bool IsDebug() const
381 {
382 return logDebug;
383 }
384
385 bool IsInfo() const
386 {
387 return logInfo;
388 }
389
390 bool IsWarn() const
391 {
392 return logWarn;
393 }
394
395 bool IsError() const
396 {
397 return logError;
398 }
399
400 Log& Info(bool state)
401 {
402 logInfo=state;
403
404 return *this;
405 }
406
407 Log& Warn(bool state)
408 {
409 logWarn=state;
410
411 return *this;
412 }
413
414 Log& Error(bool state)
415 {
416 logError=state;
417
418 return *this;
419 }
420
425 };
426
432 extern OSMSCOUT_API Log log;
433}
434
449
450#endif
#define OSMSCOUT_API
Definition CoreImportExport.h:45
Definition Logger.h:352
bool IsDebug() const
Definition Logger.h:380
Log & Info(bool state)
Definition Logger.h:400
bool IsWarn() const
Definition Logger.h:390
bool IsError() const
Definition Logger.h:395
Logger::Line Warn()
Log & operator=(Log &&log)=default
Logger::Line Info()
Logger::Line Debug()
Logger::Line Error()
void SetLogger(const std::shared_ptr< Logger > &logger)
Log & operator=(const Log &log)=default
Log & Warn(bool state)
Definition Logger.h:407
Log & Debug(bool state)
Definition Logger.h:373
Log & Error(bool state)
Definition Logger.h:414
Log(const Log &log)=default
bool IsInfo() const
Definition Logger.h:385
~Log()=default
Log(Log &&log)=default
Definition Logger.h:65
virtual void Print(int value)=0
virtual ~Destination()=default
virtual void Print(const char *value)=0
virtual void Print(unsigned long long value)=0
virtual void Print(unsigned short value)=0
virtual void Print(const std::string &value)=0
virtual void Print(bool value)=0
virtual void Print(long value)=0
virtual void Print(unsigned int value)=0
virtual void Print(long long value)=0
virtual void Print(short value)=0
virtual void Print(const std::string_view &value)=0
virtual void Print(unsigned long value)=0
Definition Logger.h:113
Line & operator<<(bool value)
Definition Logger.h:142
Line & operator<<(unsigned short value)
Definition Logger.h:156
Line & operator<<(unsigned long value)
Definition Logger.h:184
Line & operator<<(const StopClock &value)
Definition Logger.h:209
Line & operator<<(long long value)
Definition Logger.h:191
Line(Destination &destination)
Line & operator<<(const Distance &value)
Definition Logger.h:216
Line & operator<<(const std::string &value)
Definition Logger.h:121
Line & operator<<(const char *value)
Definition Logger.h:135
Line & operator<<(float value)
Line & operator<<(long value)
Definition Logger.h:177
Line & operator<<(unsigned int value)
Definition Logger.h:170
Line & operator<<(double value)
Line & operator<<(int value)
Definition Logger.h:163
Line & operator<<(const std::string_view &value)
Definition Logger.h:128
Line & operator<<(short value)
Definition Logger.h:149
Line & operator<<(void *value)
Line & operator<<(unsigned long long value)
Definition Logger.h:198
Level
Definition Logger.h:52
@ ERROR
Definition Logger.h:56
@ WARN
Definition Logger.h:55
@ DEBUG
Definition Logger.h:53
@ INFO
Definition Logger.h:54
Logger()=default
virtual Line Log(Level level)=0
virtual ~Logger()=default
Definition Logger.h:261
Line Log(Level) override
Definition Logger.h:340
OSMSCOUT_API Log log
Definition LoggerImpl.h:95
Definition Area.h:39