bioformats  5.1.6
log.h
Go to the documentation of this file.
1 /*
2  * #%L
3  * OME-COMMON C++ library for C++ compatibility/portability
4  * %%
5  * Copyright © 2015 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
46 #ifndef OME_COMMON_LOG_H
47 #define OME_COMMON_LOG_H
48 
49 #include <ostream>
50 #include <string>
51 
52 #include <ome/common/config.h>
53 
54 #ifdef OME_HAVE_BOOST_LOG
55 #define BOOST_LOG_DYN_LINK
56 #include <boost/log/core.hpp>
57 #include <boost/log/trivial.hpp>
58 #include <boost/log/expressions.hpp>
59 #include <boost/log/sources/severity_logger.hpp>
60 #include <boost/log/sources/severity_feature.hpp>
61 #include <boost/log/attributes/constant.hpp>
62 #else // ! OME_HAVE_BOOST_LOG
63 // For std::clog
64 #include <iostream>
65 #endif // OME_HAVE_BOOST_LOG
66 
67 namespace ome
68 {
69 
70 #ifdef OME_HAVE_BOOST_LOG
71  namespace logging = boost::log;
72 #else
73  namespace logging
74  {
75  namespace trivial
76  {
77 
79  enum severity_level
80  {
81  trace,
82  debug,
83  info,
84  warning,
85  error,
86  fatal
87  };
88 
89  }
90  }
91 #endif // OME_HAVE_BOOST_LOG
92 
93  namespace common
94  {
95 
96 #ifdef OME_HAVE_BOOST_LOG
97  typedef logging::sources::severity_logger_mt<logging::trivial::severity_level> Logger;
99 
106  inline
107  Logger
108  createLogger(const std::string& className)
109  {
110  Logger logger;
111  logger.add_attribute("ClassName", logging::attributes::constant<std::string>(className));
112  return logger;
113  }
114 #else // ! OME_HAVE_BOOST_LOG
115  class Logger
116  {
117  private:
119  std::string klass;
120 
121  public:
127  Logger(const std::string& className):
128  klass(className)
129  {}
130 
131  void
132  className(const std::string& className)
133  {
134  this->klass = className;
135  }
136 
137  const std::string&
138  className() const
139  {
140  return this->klass;
141  }
142  };
143 
144  // No switch default to avoid -Wunreachable-code errors.
145  // However, this then makes -Wswitch-default complain. Disable
146  // temporarily.
147 #ifdef __GNUC__
148 # pragma GCC diagnostic push
149 # pragma GCC diagnostic ignored "-Wswitch-default"
150 #endif
151 
152  class LogMessage
153  {
154  private:
155  std::ostream& ostream;
156  logging::trivial::severity_level severity;
157 
158  public:
159  LogMessage(std::ostream& ostream,
160  logging::trivial::severity_level severity):
161  ostream(ostream),
162  severity(severity)
163  {
164  const char * sevstr;
165  switch(severity)
166  {
167  case logging::trivial:: trace:
168  sevstr = "trace";
169  break;
170  case logging::trivial:: debug:
171  sevstr = "debug";
172  break;
173  case logging::trivial:: info:
174  sevstr = "info";
175  break;
176  case logging::trivial:: warning:
177  sevstr = "warning";
178  break;
179  case logging::trivial:: error:
180  sevstr = "error";
181  break;
182  case logging::trivial:: fatal:
183  sevstr = "fatal";
184  break;
185  }
186 
187  ostream << '[' << sevstr << "] ";
188  }
189 
190  ~LogMessage()
191  {
192  try
193  {
194  ostream << '\n';
195  }
196  catch (...)
197  {
198  }
199  }
200 
201  std::ostream&
202  stream()
203  {
204  return ostream;
205  }
206  };
207 
208 #ifdef __GNUC__
209 # pragma GCC diagnostic pop
210 #endif
211 
212  inline
213  Logger
214  createLogger(const std::string& className)
215  {
216  Logger logger(className);
217  return logger;
218  }
219 
220 #endif // OME_HAVE_BOOST_LOG
221 
234  void
235  setLogLevel(logging::trivial::severity_level severity);
236 
242  logging::trivial::severity_level
243  getLogLevel();
244 
245  }
246 }
247 
249 #ifndef OME_HAVE_BOOST_LOG
250 #define BOOST_LOG_SEV(logger, severity)\
251  if (severity >= ome::common::getLogLevel())\
252  ome::common::LogMessage(std::clog, severity).stream() << logger.className() << ": "
253 #endif // !OME_HAVE_BOOST_LOG
254 
255 #endif // OME_COMMON_LOG_H
256 
257 /*
258  * Local Variables:
259  * mode:C++
260  * End:
261  */
logging::sources::severity_logger_mt< logging::trivial::severity_level > Logger
Message logger.
Definition: log.h:98
void setLogLevel(logging::trivial::severity_level severity)
Set global logging level.
Definition: log.cpp:63
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
Logger createLogger(const std::string &className)
Create a message logger for a class.
Definition: log.h:108
logging::trivial::severity_level getLogLevel()
Get global logging level.
Definition: log.cpp:75