SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Log.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 /// @todo Documentation Core/Logging/Log.h
29 
30 #ifndef CORE_LOGGING_LOG_H
31 #define CORE_LOGGING_LOG_H
32 
33 #include <string>
34 #include <Core/Logging/LoggerFwd.h>
35 #ifndef Q_MOC_RUN
36 #include <boost/lexical_cast.hpp>
37 #endif
38 #include <Core/Logging/share.h>
39 
40 namespace SCIRun
41 {
42  namespace Core
43  {
44  namespace Logging
45  {
46  //Copied from http://jonblack.org/2013/01/27/wrapping-a-logging-framework-in-cpp/
47 
48  /// Logging levels used by pix. Follows the same as for syslog, taken from
49  /// RFC 5424. Comments added for ease of reading.
50  /// @see http://en.wikipedia.org/wiki/Syslog.
51  enum LogLevel
52  {
53  EMERG, // System is unusable (e.g. multiple parts down)
54  ALERT, // System is unusable (e.g. single part down)
55  CRIT, // Failure in non-primary system (e.g. backup site down)
56  ERROR_LOG, // Non-urgent failures; relay to developers
57  WARN, // Not an error, but indicates error will occur if nothing done.
58  NOTICE, // Events that are unusual, but not error conditions.
59  INFO, // Normal operational messages. No action required.
60  DEBUG_LOG, // Information useful during development for debugging.
62  };
63 
64  class SCISHARE Log /*final*/
65  {
66  public:
67  static Log& get();
68  static Log& get(const std::string& name);
69 
71  {
72  public:
73  explicit Stream(class LogStreamImpl* impl);
74  SCISHARE friend Stream& operator<<(Stream& log, const std::string& msg);
75  void stream(const std::string& msg);
76  void stream(double x);
77  void flush();
78  private:
79  boost::shared_ptr<class LogStreamImpl> impl_;
80  };
81 
82  void log(LogLevel level, const std::string& msg);
83 
84  SCISHARE friend Stream& operator<<(Log& log, LogLevel level);
85 
86  void setVerbose(bool v);
87  bool verbose() const;
88  void flush();
89 
90  private:
91  Log();
92  explicit Log(const std::string& name);
93  Log(const Log&)/* =delete*/;
94  Log(Log&&)/* =delete*/;
95  Log& operator=(const Log&)/* =delete*/;
96  Log& operator=(Log&&)/* =delete*/;
97 
98  private:
99  friend class Stream;
100  boost::shared_ptr<class LogImpl> impl_;
101  };
102 
104  SCISHARE Log::Stream& operator<<(Log::Stream& log, const std::string& msg);
105  SCISHARE Log::Stream& operator<<(Log::Stream& log, double x);
106 
107  template <typename T>
108  Log::Stream& operator<<(Log::Stream& log, const T& t)
109  {
110  return log << boost::lexical_cast<std::string>(t);
111  }
112 
113  SCISHARE Log::Stream& operator<<(Log::Stream& log, std::ostream&(*func)(std::ostream&));
114  }
115  }
116 }
117 
118 /// @todo: log4cpp crashes on Mac more easily, just macro these out on that platform for now.
119 #ifdef _WIN32
120 #define LOG_DEBUG(str) SCIRun::Core::Logging::Log::get() << SCIRun::Core::Logging::DEBUG_LOG << str << std::endl
121 #else
122 #define LOG_DEBUG(str)
123 #endif
124 
125 #define LOG_DEBUG_TO(log, str) log << SCIRun::Core::Logging::DEBUG_LOG << str << std::endl
126 
127 #endif
Definition: Log.h:57
#define msg(m)
Definition: PiecewiseInterp.h:55
LogLevel
Definition: Log.h:51
#define SCISHARE
Definition: share.h:39
Definition: Log.h:59
Definition: Log.h:55
const char * name[]
Definition: BoostGraphExampleTests.cc:87
Definition: Log.h:54
Definition: Log.h:53
v
Definition: readAllFields.py:42
Definition: Log.h:58
SCISHARE Log::Stream & operator<<(Log &log, LogLevel level)
Definition: Log.cc:212
Definition: Log.h:61
friend class Stream
Definition: Log.h:99
Definition: Log.h:64