libopenraw
trace.hpp
1 /*
2  * libopenraw - trace.h
3  *
4  * Copyright (C) 2006-2015 Hubert Figuiere
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #ifndef OR_INTERNALS_TRACE_H_
23 #define OR_INTERNALS_TRACE_H_
24 
25 #include <string>
26 #include <vector>
27 #include <algorithm>
28 
29 #include <libopenraw/debug.h>
30 
31 namespace Debug {
32 
37 void log(debug_level level, const char* fmt, ...)
38  __attribute__ ((format (printf, 2, 3)));
39 
40 
41 #define LOGWARN(...) \
42  Debug::log(WARNING, ## __VA_ARGS__)
43 
44 #define LOGERR(...) \
45  Debug::log(ERROR, ## __VA_ARGS__)
46 
47 #define LOGDBG1(...) \
48  Debug::log(DEBUG1, ## __VA_ARGS__)
49 
50 #define LOGDBG2(...) \
51  Debug::log(DEBUG2, ## __VA_ARGS__)
52 
54 class Trace
55 {
56 public:
57  Trace(debug_level level)
58  : m_level(level)
59  {
60  }
61  Trace & operator<<(int i);
62  Trace & operator<<(const char * s);
63  Trace & operator<<(void *);
64  Trace & operator<<(const std::string & s);
65 
66  template <class T>
67  Trace & operator<<(const std::vector<T> & v);
68 
69  static void setDebugLevel(debug_level lvl);
70 private:
71  friend void log(debug_level level, const char* fmt, ...);
72  static void print(int i);
73  static int debugLevel; // global debug level
74  int m_level;
75 };
76 
77 
78 template <class T>
79 Trace & Trace::operator<<(const std::vector<T> & v)
80 {
81  if (m_level <= debugLevel) {
82  std::for_each(v.cbegin(), v.cend(),
83  [](T a) {
84  print(a);
85  });
86  }
87  return *this;
88 }
89 
90 }
91 
92 #endif
93 /*
94  Local Variables:
95  mode:c++
96  c-file-style:"stroustrup"
97  c-file-offsets:((innamespace . 0))
98  tab-width:2
99  c-basic-offset:2
100  indent-tabs-mode:nil
101  fill-column:80
102  End:
103 */
Definition: trace.cpp:30
friend void log(debug_level level, const char *fmt,...)
Definition: trace.cpp:34