libosmscout  1.1.1
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 <osmscout/CoreFeatures.h>
24 
26 
27 #include <ostream>
28 #include <sstream>
29 
31 #include <osmscout/util/Distance.h>
32 
33 // Since we have a DEBUG enumeration member
34 #ifdef DEBUG
35 #undef DEBUG
36 #endif
37 
38 // Since we have a ERROR enumeration member
39 #ifdef ERROR
40 #undef ERROR
41 #endif
42 
43 namespace osmscout {
44 
52  {
53  public:
54  enum Level {
58  ERROR
59  };
60 
67  {
68  public:
69  Destination() = default;
70  virtual ~Destination() = default;
71 
75  virtual void Print(const std::string& value) = 0;
76 
80  virtual void Print(const std::string_view& value) = 0;
81 
85  virtual void Print(const char* value) = 0;
86 
90  virtual void Print(bool value) = 0;
91  virtual void Print(short value) = 0;
92  virtual void Print(unsigned short value) = 0;
93  virtual void Print(int value) = 0;
94  virtual void Print(unsigned int value) = 0;
95  virtual void Print(long value) = 0;
96  virtual void Print(unsigned long value) = 0;
97  virtual void Print(long long value) = 0;
98  virtual void Print(unsigned long long value) = 0;
99 
104  virtual void PrintLn() = 0;
105  };
106 
115  {
116  private:
117  Destination& destination;
118 
119  public:
120  explicit Line(Destination& destination);
121  virtual ~Line();
122 
123  Line& operator<<(const std::string& value)
124  {
125  destination.Print(value);
126 
127  return *this;
128  }
129 
130  Line& operator<<(const std::string_view& value)
131  {
132  destination.Print(value);
133 
134  return *this;
135  }
136 
137  Line& operator<<(const char* value)
138  {
139  destination.Print(value);
140 
141  return *this;
142  }
143 
144  Line& operator<<(bool value)
145  {
146  destination.Print(value);
147 
148  return *this;
149  }
150 
151  Line& operator<<(short value)
152  {
153  destination.Print(value);
154 
155  return *this;
156  }
157 
158  Line& operator<<(unsigned short value)
159  {
160  destination.Print(value);
161 
162  return *this;
163  }
164 
165  Line& operator<<(int value)
166  {
167  destination.Print(value);
168 
169  return *this;
170  }
171 
172  Line& operator<<(unsigned int value)
173  {
174  destination.Print(value);
175 
176  return *this;
177  }
178 
179  Line& operator<<(long value)
180  {
181  destination.Print(value);
182 
183  return *this;
184  }
185 
186  Line& operator<<(unsigned long value)
187  {
188  destination.Print(value);
189 
190  return *this;
191  }
192 
193  Line& operator<<(long long value)
194  {
195  destination.Print(value);
196 
197  return *this;
198  }
199 
200  Line& operator<<(unsigned long long value)
201  {
202  destination.Print(value);
203 
204  return *this;
205  }
206 
207  Line& operator<<(float value);
208  Line& operator<<(double value);
209  Line& operator<<(void* value);
210 
211  Line& operator<<(const StopClock& value)
212  {
213  destination.Print(value.ResultString());
214 
215  return *this;
216  }
217 
218  Line& operator<<(const Distance& value)
219  {
220  destination.Print(value.AsString());
221 
222  return *this;
223  }
224  };
225 
226  protected:
230  virtual Line Log(Level level) = 0;
231 
232  public:
233  Logger() = default;
234  virtual ~Logger() = default;
235 
239  Line Debug();
240 
244  Line Info();
245 
250  Line Warn();
251 
255  Line Error();
256  };
257 
263  {
264  private:
269  class OSMSCOUT_API NoOpDestination : public Destination
270  {
271  public:
272  void Print(const std::string& /*value*/) override
273  {
274  // no code
275  }
276 
277  void Print(const std::string_view& /*value*/) override
278  {
279  // no code
280  }
281 
282  void Print(const char* /*value*/) override
283  {
284  // no code
285  }
286 
287  void Print(bool /*value*/) override
288  {
289  // no code
290  }
291 
292  void Print(short /*value*/) override
293  {
294  // no code
295  }
296 
297  void Print(unsigned short /*value*/) override
298  {
299  // no code
300  }
301 
302  void Print(int /*value*/) override
303  {
304  // no code
305  }
306 
307  void Print(unsigned int /*value*/) override
308  {
309  // no code
310  }
311 
312  void Print(long /*value*/) override
313  {
314  // no code
315  }
316 
317  void Print(unsigned long /*value*/) override
318  {
319  // no code
320  }
321 
322  void Print(long long /*value*/) override
323  {
324  // no code
325  }
326 
327  void Print(unsigned long long /*value*/) override
328  {
329  // no code
330  }
331 
332  void PrintLn() override
333  {
334  // no code
335  }
336  };
337 
338  private:
339  NoOpDestination destination;
340 
341  public:
342  Line Log(Level /*level*/) override
343  {
344  return Line(destination);
345  }
346  };
347 
355  {
356  private:
362  class OSMSCOUT_API StreamDestination : public Destination
363  {
364  private:
365  std::ostream& stream;
366 
367  public:
368  explicit StreamDestination(std::ostream& stream);
369 
370  void Print(const std::string& value) override;
371  void Print(const std::string_view& value) override;
372  void Print(const char* value) override;
373  void Print(bool value) override;
374  void Print(short value) override;
375  void Print(unsigned short value) override;
376  void Print(int value) override;
377  void Print(unsigned int value) override;
378  void Print(long value) override;
379  void Print(unsigned long value) override;
380  void Print(long long value) override;
381  void Print(unsigned long long value) override;
382  void PrintLn() override;
383  };
384 
385  private:
386  StreamDestination infoDestination;
387  StreamDestination errorDestination;
388 
389  public:
390  StreamLogger(std::ostream& infoStream,
391  std::ostream& errorStream);
392 
393  Line Log(Level level) override;
394  };
395 
402  {
403  public:
404  ConsoleLogger();
405  };
406 
413  {
414  private:
415  Logger* logger;
416  NoOpLogger noOpLogger;
417  bool logDebug;
418  bool logInfo;
419  bool logWarn;
420  bool logError;
421 
422  public:
423  Log();
424  ~Log();
425 
426  void SetLogger(Logger* logger);
427 
428  Log& Debug(bool state)
429  {
430  logDebug=state;
431 
432  return *this;
433  }
434 
435  bool IsDebug() const
436  {
437  return logDebug;
438  }
439 
440  bool IsInfo() const
441  {
442  return logInfo;
443  }
444 
445  bool IsWarn() const
446  {
447  return logWarn;
448  }
449 
450  bool IsError() const
451  {
452  return logError;
453  }
454 
455  Log& Info(bool state)
456  {
457  logInfo=state;
458 
459  return *this;
460  }
461 
462  Log& Warn(bool state)
463  {
464  logWarn=state;
465 
466  return *this;
467  }
468 
469  Log& Error(bool state)
470  {
471  logError=state;
472 
473  return *this;
474  }
475 
476  Logger::Line Debug();
477  Logger::Line Info();
478  Logger::Line Warn();
479  Logger::Line Error();
480  };
481 
487  extern OSMSCOUT_API Log log;
488 }
489 
505 #endif
OSMSCOUT_API Log log
Line & operator<<(const Distance &value)
Definition: Logger.h:218
Line & operator<<(unsigned int value)
Definition: Logger.h:172
Definition: Logger.h:55
bool IsError() const
Definition: Logger.h:450
bool IsInfo() const
Definition: Logger.h:440
Line & operator<<(const StopClock &value)
Definition: Logger.h:211
Line & operator<<(const char *value)
Definition: Logger.h:137
Log & Info(bool state)
Definition: Logger.h:455
std::ostream & operator<<(std::ostream &stream, const DBId &o)
Definition: DBFileOffset.h:80
Line & operator<<(long value)
Definition: Logger.h:179
Log & Error(bool state)
Definition: Logger.h:469
Line & operator<<(unsigned short value)
Definition: Logger.h:158
Level
Definition: Logger.h:54
Line Log(Level) override
Definition: Logger.h:342
Definition: Logger.h:51
Line & operator<<(bool value)
Definition: Logger.h:144
Definition: Area.h:38
bool IsWarn() const
Definition: Logger.h:445
Definition: Logger.h:57
Definition: Logger.h:66
Log & Debug(bool state)
Definition: Logger.h:428
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
Line & operator<<(short value)
Definition: Logger.h:151
virtual void Print(const std::string &value)=0
Definition: Logger.h:114
bool IsDebug() const
Definition: Logger.h:435
Definition: Logger.h:401
Line & operator<<(long long value)
Definition: Logger.h:193
Line & operator<<(const std::string_view &value)
Definition: Logger.h:130
Log & Warn(bool state)
Definition: Logger.h:462
Definition: Logger.h:412
Definition: Logger.h:354
Definition: Logger.h:262
Line & operator<<(unsigned long long value)
Definition: Logger.h:200
Line & operator<<(const std::string &value)
Definition: Logger.h:123
Line & operator<<(unsigned long value)
Definition: Logger.h:186
Definition: Logger.h:56
Line & operator<<(int value)
Definition: Logger.h:165