fw4spl
fwAtomsPatch/src/fwAtomsPatch/infos/Logger.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
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 #include "fwAtomsPatch/infos/Logger.hpp"
8 
9 #ifndef ANDROID
10 #include <fwRuntime/profile/Profile.hpp>
11 #include <fwTools/Os.hpp>
12 
13 #include <boost/filesystem.hpp>
14 #include <boost/log/attributes.hpp>
15 #include <boost/log/expressions.hpp>
16 #include <boost/log/expressions/formatters/date_time.hpp>
17 #include <boost/log/keywords/channel.hpp>
18 #include <boost/log/sinks.hpp>
19 #include <boost/log/sources/channel_logger.hpp>
20 #include <boost/log/sources/global_logger_storage.hpp>
21 #include <boost/log/support/date_time.hpp>
22 #include <boost/log/trivial.hpp>
23 #include <boost/log/utility/setup/common_attributes.hpp>
24 #include <boost/log/utility/setup/file.hpp>
25 #include <boost/parameter/keyword.hpp>
26 #else
27 #include <android/log.h>
28 #endif
29 
30 
31 namespace fwAtomsPatch
32 {
33 namespace infos
34 {
35 
36 #ifndef ANDROID
37 
38 BOOST_LOG_GLOBAL_LOGGER(lg_channel,
39  ::boost::log::sources::channel_logger<std::string>);
40 
41 BOOST_LOG_GLOBAL_LOGGER_CTOR_ARGS(lg_channel,
42  ::boost::log::sources::channel_logger_mt<std::string>,
43  (std::string("patch")));
44 
45 Logger::StreamPtrType Logger::s_stream = ::boost::make_shared< Logger::StreamType >();
46 
47 #else
48 #define LOG_TAG "Logger"
49 #define LOGSTREAM(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
50 
51 Logger::StreamPtrType Logger::s_stream;
52 
53 #endif
54 
55 Logger Logger::s_logger;
56 
57 Logger::Logger()
58 {
59 
60 #ifndef ANDROID
61  namespace expr = ::boost::log::expressions;
62  namespace keywords = ::boost::log::keywords;
63 
64 
65  namespace bfile = ::boost::filesystem;
66 
67  //Create PATCH.log in a user data dir nammed fw4spl/appName/
68  ::fwRuntime::profile::Profile::sptr profile = ::fwRuntime::profile::getCurrentProfile();
69 
70  //default name of application with no profile.xml
71  std::string appName = "default";
72 
73  if(profile)
74  {
75  appName = profile->getName();
76  }
77 
78  const bfile::path appPrefDir = ::fwTools::os::getUserDataDir("fw4spl", appName, true);
79 
80  FW_RAISE_IF("Unable to define User's data directory", appPrefDir.empty());
81 
82  if (!bfile::exists(appPrefDir))
83  {
84  bfile::create_directories(appPrefDir);
85  }
86 
87  ::boost::log::add_file_log (
88  // file name pattern
89  keywords::file_name = appPrefDir.string() + "/PATCH.log",
90  // rotate files every 10 MiB...
91  keywords::rotation_size = 10 * 1024 * 1024,
92  // ...or at midnight
93  keywords::time_based_rotation = ::boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
94  // log record format
95  keywords::format = (
96  expr::stream
97  << "[" << expr::format_date_time< ::boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f")
98  << "][" << expr::format_date_time< ::boost::posix_time::ptime >("Uptime", "%H:%M:%S.%f")
99  << "][" << expr::attr< std::string >("Channel")
100  << "] " << expr::smessage
101  ),
102  // auto-flush feature of the backend
103  keywords::auto_flush = true,
104  keywords::filter = (expr::attr< std::string >("Channel") == "patch")
105  );
106 
107  // Construct the sink
108  typedef ::boost::log::sinks::synchronous_sink< ::boost::log::sinks::text_ostream_backend > text_sink;
109  ::boost::shared_ptr< text_sink > pSink = ::boost::make_shared< text_sink >();
110 
111  // Add a stream to write log to
112  pSink->locked_backend()->add_stream(s_stream);
113 
114  // Register the sink in the logging core
115  ::boost::log::core::get()->add_sink(pSink);
116 #endif
117 }
118 
119 // ----------------------------------------------------------------------------
120 
122 {
123 }
124 
125 // ----------------------------------------------------------------------------
126 
127 Logger::StreamPtrType Logger::getStream()
128 {
129  return s_stream;
130 }
131 
132 // ----------------------------------------------------------------------------
133 
134 void Logger::error(const std::string& message)
135 {
136 #ifndef ANDROID
137  BOOST_LOG_STREAM(lg_channel::get()) << "PATCH_ERROR" << ": " << message;
138 #else
139  LOGSTREAM("BADCAST: %s", message.c_str());
140 #endif
141 }
142 
143 // ----------------------------------------------------------------------------
144 
145 void Logger::badCast(const std::string& message)
146 {
147 #ifndef ANDROID
148  BOOST_LOG_STREAM(lg_channel::get()) << "BADCAST" << ": " << message;
149 #else
150  LOGSTREAM("BADCAST: %s", message.c_str());
151 #endif
152 }
153 
154 // ----------------------------------------------------------------------------
155 
156 void Logger::outOfRange(const std::string& message)
157 {
158 #ifndef ANDROID
159  BOOST_LOG_STREAM(lg_channel::get()) << "OUTOFRANGE" << ": " << message;
160 #else
161  LOGSTREAM("BADCAST: %s", message.c_str());
162 #endif
163 }
164 
165 // ----------------------------------------------------------------------------
166 
167 void Logger::info(const std::string& message)
168 {
169 #ifndef ANDROID
170  BOOST_LOG_STREAM(lg_channel::get()) << "INFO" << ": " << message;
171 #else
172  LOGSTREAM("BADCAST: %s", message.c_str());
173 #endif
174 }
175 
176 // ----------------------------------------------------------------------------
177 
178 void Logger::addAttribute(const std::string& message)
179 {
180 #ifndef ANDROID
181  BOOST_LOG_STREAM(lg_channel::get()) << "ADD_ATTR" << ": " << message;
182 #else
183  LOGSTREAM("BADCAST: %s", message.c_str());
184 #endif
185 }
186 
187 // ----------------------------------------------------------------------------
188 
189 void Logger::eraseAttribute(const std::string& message)
190 {
191 #ifndef ANDROID
192  BOOST_LOG_STREAM(lg_channel::get()) << "ERASE_ATTR" << ": " << message;
193 #else
194  LOGSTREAM("BADCAST: %s", message.c_str());
195 #endif
196 }
197 
198 // ----------------------------------------------------------------------------
199 
200 void Logger::replaceAttribute(const std::string& message)
201 {
202 #ifndef ANDROID
203  BOOST_LOG_STREAM(lg_channel::get()) << "REPLACE_ATTR" << ": " << message;
204 #else
205  LOGSTREAM("BADCAST: %s", message.c_str());
206 #endif
207 }
208 
209 // ----------------------------------------------------------------------------
210 
211 } //infos
212 } //fwAtomHelper
213 
FWATOMSPATCH_API void badCast(const std::string &message)
Bad cast message.
FWATOMSPATCH_API void eraseAttribute(const std::string &message)
Erase attribute message.
FWATOMSPATCH_API void outOfRange(const std::string &message)
Out of range message.
Contains base functionalities used to transform objects from a version to another.
Definition: Abstract.hpp:16
FWATOMSPATCH_API void info(const std::string &message)
Information message.
FWATOMSPATCH_API void addAttribute(const std::string &message)
New attribute message.
FWATOMSPATCH_API void replaceAttribute(const std::string &message)
Replace attribute message.
FWATOMSPATCH_API void error(const std::string &message)
Error message.