fw4spl
SpyLogger.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifdef _MSC_VER
8 #pragma warning(disable : 4996) // warning for sprintf() in Boost.log
9 #endif // _MSC_VER
10 
11 #ifndef ANDROID
12 #include <boost/log/core.hpp>
13 #include <boost/log/attributes.hpp>
14 #include <boost/log/sinks/sink.hpp>
15 #include <boost/log/sources/global_logger_storage.hpp>
16 #include <boost/log/sources/logger.hpp>
17 #include <boost/log/sources/severity_logger.hpp>
18 #include <boost/log/utility/setup/common_attributes.hpp>
19 #include <boost/log/expressions.hpp>
20 #include <boost/log/trivial.hpp>
21 #include <boost/log/utility/setup/console.hpp>
22 #include <boost/log/utility/setup/file.hpp>
23 #include <boost/log/expressions/formatters/date_time.hpp>
24 #include <boost/log/support/date_time.hpp>
25 #include <boost/log/attributes/current_thread_id.hpp>
26 #include <boost/log/attributes/current_process_id.hpp>
27 #include <boost/log/attributes/timer.hpp>
28 #else
29 #include <android/log.h>
30 #endif
31 
32 #include "fwCore/macros.hpp"
33 #include "fwCore/log/SpyLogger.hpp"
34 
35 namespace fwCore
36 {
37 namespace log
38 {
39 
40 SpyLogger SpyLogger::s_spyLogger;
41 
42 #ifndef ANDROID
43 BOOST_LOG_GLOBAL_LOGGER(lg, ::boost::log::sources::severity_logger_mt< ::boost::log::trivial::severity_level >);
44 BOOST_LOG_GLOBAL_LOGGER_DEFAULT(lg, ::boost::log::sources::severity_logger_mt< ::boost::log::trivial::severity_level >);
45 #else
46 #define LOG_TAG "SpyLogger"
47 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
48 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
49 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
50 #define LOGF(...) __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, __VA_ARGS__)
51 #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
52 #endif
53 
54 //-----------------------------------------------------------------------------
55 
56 SpyLogger::SpyLogger()
57 {
58 #ifndef ANDROID
59  ::boost::log::add_common_attributes();
60  ::boost::log::core::get()
61  ->add_global_attribute("Uptime", ::boost::log::attributes::timer());
62 #endif
63 }
64 
65 //-----------------------------------------------------------------------------
66 
67 void SpyLogger::createBasicConfiguration()
68 {
69 #ifdef _WIN32
70  this->addFileAppender();
71 #else
72  this->addStreamAppender();
73 #endif
74 }
75 
76 //-----------------------------------------------------------------------------
77 
78 void SpyLogger::addStreamAppender( std::ostream &os, LevelType level )
79 {
80 #ifndef ANDROID
81  namespace expr = ::boost::log::expressions;
82  namespace keywords = ::boost::log::keywords;
83 
84  typedef ::boost::posix_time::ptime::time_duration_type DurationType;
85 
86  ::boost::log::add_console_log (
87  os,
88  keywords::format = (
89  expr::stream << "["
90  << expr::attr<unsigned int>("LineID")
91  << "][" << expr::format_date_time< DurationType >("Uptime", "%H:%M:%S.%f")
92  << "][" << expr::attr< ::boost::log::trivial::severity_level >("Severity")
93  << "] " << expr::smessage
94  ),
95  keywords::filter = expr::attr< ::boost::log::trivial::severity_level >("Severity") >=
96  static_cast < ::boost::log::trivial::severity_level > (level),
97  // auto-flush feature of the backend
98  keywords::auto_flush = true
99  );
100 #endif
101 }
102 
103 //-----------------------------------------------------------------------------
104 
105 // void SpyLogger::addSyslogAppender(const std::string & hostName, const std::string & facilityName)
106 // {
107 // }
108 
109 //-----------------------------------------------------------------------------
110 
111 void SpyLogger::addFileAppender(const std::string & logFile, LevelType level)
112 {
113 #ifndef ANDROID
114  namespace expr = ::boost::log::expressions;
115  namespace keywords = ::boost::log::keywords;
116 
117  typedef ::boost::posix_time::ptime::time_duration_type DurationType;
118 
119  ::boost::log::add_file_log (
120  // file name pattern
121  keywords::file_name = logFile,
122  // rotate files every 10 MiB...
123  keywords::rotation_size = 10 * 1024 * 1024,
124  // ...or at midnight
125  keywords::time_based_rotation = ::boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
126  // log record format
127  keywords::format = (
128  expr::stream
129  << "[" << expr::format_date_time< ::boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f")
130  << "][" << expr::format_date_time< DurationType >("Uptime", "%H:%M:%S.%f")
131  << "][" << expr::attr< ::boost::log::attributes::current_process_id::value_type >("ProcessID")
132  << "][" << expr::attr< ::boost::log::attributes::current_thread_id::value_type >("ThreadID")
133  << "][" << expr::attr< ::boost::log::trivial::severity_level >("Severity")
134  << "] " << expr::smessage
135  ),
136  keywords::filter = expr::attr< ::boost::log::trivial::severity_level >("Severity") >=
137  static_cast < ::boost::log::trivial::severity_level > (level),
138  // auto-flush feature of the backend
139  keywords::auto_flush = true
140  );
141 #endif
142 }
143 
144 //-----------------------------------------------------------------------------
145 
146 void SpyLogger::setLevel(LevelType level)
147 {
148 #ifndef ANDROID
149  ::boost::log::core::get()->set_filter
150  (
151  ::boost::log::expressions::attr< ::boost::log::trivial::severity_level >("Severity")
152  >= static_cast < ::boost::log::trivial::severity_level > (level)
153  );
154 #endif
155 }
156 
157 //-----------------------------------------------------------------------------
158 
159 void SpyLogger::trace(const std::string & mes, const char * file, int line)
160 {
161 #ifndef ANDROID
162  BOOST_LOG_SEV(lg::get(), ::boost::log::trivial::trace) << file << ":" << line << ": "<< mes;
163 #else
164  LOGI("t: %s(%d): %s", file, line, mes.c_str());
165 #endif
166 }
167 
168 //-----------------------------------------------------------------------------
169 
170 void SpyLogger::debug(const std::string & mes, const char * file, int line)
171 {
172 #ifndef ANDROID
173  BOOST_LOG_SEV(lg::get(), ::boost::log::trivial::debug) << file << ":" << line << ": "<< mes;
174 #else
175  LOGI("i: %s(%d): %s", file, line, mes.c_str());
176 #endif
177 }
178 
179 //-----------------------------------------------------------------------------
180 
181 void SpyLogger::info(const std::string & mes, const char * file, int line)
182 {
183 #ifndef ANDROID
184  BOOST_LOG_SEV(lg::get(), ::boost::log::trivial::info) << file << ":" << line << ": "<< mes;
185 #else
186  LOGI("i: %s(%d): %s", file, line, mes.c_str());
187 #endif
188 }
189 
190 //-----------------------------------------------------------------------------
191 
192 void SpyLogger::warn(const std::string & mes, const char * file, int line)
193 {
194 #ifndef ANDROID
195  BOOST_LOG_SEV(lg::get(), ::boost::log::trivial::warning) << file << ":" << line << ": "<< mes;
196 #else
197  LOGW("w: %s(%d): %s", file, line, mes.c_str());
198 #endif
199 }
200 
201 //-----------------------------------------------------------------------------
202 
203 void SpyLogger::error(const std::string & mes, const char * file, int line)
204 {
205 #ifndef ANDROID
206  BOOST_LOG_SEV(lg::get(), ::boost::log::trivial::error) << file << ":" << line << ": "<< mes;
207 #else
208  LOGE("e: %s(%d): %s", file, line, mes.c_str());
209 #endif
210 }
211 
212 //-----------------------------------------------------------------------------
213 
214 void SpyLogger::fatal(const std::string & mes, const char * file, int line)
215 {
216 #ifndef ANDROID
217  BOOST_LOG_SEV(lg::get(), ::boost::log::trivial::fatal) << file << ":" << line << ": "<< mes;
218 #else
219  LOGF("f: %s(%d): %s", file, line, mes.c_str());
220 #endif
221 }
222 
223 //-----------------------------------------------------------------------------
224 
225 void SpyLogger::log(const std::string & mes, const char * file, int line)
226 {
227 #ifndef ANDROID
228  BOOST_LOG_SEV(lg::get(), ::boost::log::trivial::error) << file << ":" << line << ": "<< mes;
229 #else
230  LOGI("i: %s(%d): %s", file, line, mes.c_str());
231 #endif
232 }
233 
234 //-----------------------------------------------------------------------------
235 
236 } // namespace log
237 } // namespace fwCore
238 
This file defines fwCore base macros.
This namespace fwCore provides common foundations for FW4SPL.
Definition: BaseObject.hpp:16