comparison Core/Logging.cpp @ 2483:9c54c40eaf25

logging primitives for WebAssembly
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 02 Mar 2018 15:10:37 +0100
parents 878b59270859
children 911e62dbb4ac
comparison
equal deleted inserted replaced
2482:509041cb57db 2483:9c54c40eaf25
103 103
104 InternalLogger::~InternalLogger() 104 InternalLogger::~InternalLogger()
105 { 105 {
106 if (context_ != NULL) 106 if (context_ != NULL)
107 { 107 {
108 if (level_ == "ERROR") 108 switch (level_)
109 { 109 {
110 OrthancPluginLogError(context_, message_.c_str()); 110 case ERROR:
111 } 111 OrthancPluginLogError(context_, message_.c_str());
112 else if (level_ == "WARNING") 112 break;
113 { 113
114 OrthancPluginLogWarning(context_, message_.c_str()); 114 case WARNING:
115 } 115 OrthancPluginLogWarning(context_, message_.c_str());
116 else if (level_ == "INFO") 116 break;
117 { 117
118 OrthancPluginLogInfo(context_, message_.c_str()); 118 case INFO:
119 } 119 OrthancPluginLogInfo(context_, message_.c_str());
120 else 120 break;
121 { 121
122 std::string s = "Unknown log level (" + level_ + ") for message: " + message_; 122 case TRACE:
123 OrthancPluginLogError(context_, s.c_str()); 123 // Not used by plugins
124 break;
125
126 default:
127 {
128 std::string s = ("Unknown log level (" + boost::lexical_cast<std::string>(level_) +
129 ") for message: " + message_);
130 OrthancPluginLogError(context_, s.c_str());
131 break;
132 }
124 } 133 }
125 } 134 }
126 } 135 }
127 136
128 InternalLogger& InternalLogger::operator<< (const std::string& message) 137 InternalLogger& InternalLogger::operator<< (const std::string& message)
144 } 153 }
145 } 154 }
146 } 155 }
147 156
148 157
149 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */ 158 #elif ORTHANC_ENABLE_LOGGING_STDIO == 1
159
160 /*********************************************************
161 * Logger compatible with <stdio.h>
162 *********************************************************/
163
164 #include <stdio.h>
165 #include <boost/lexical_cast.hpp>
166
167 namespace Orthanc
168 {
169 namespace Logging
170 {
171 static bool globalVerbose_ = false;
172 static bool globalTrace_ = false;
173
174 InternalLogger::InternalLogger(Level level,
175 const char* file /* ignored */,
176 int line /* ignored */) :
177 level_(level)
178 {
179 }
180
181 InternalLogger::~InternalLogger()
182 {
183 switch (level_)
184 {
185 case ERROR:
186 fprintf(stderr, "E: %s\n", message_.c_str());
187 break;
188
189 case WARNING:
190 fprintf(stdout, "W: %s\n", message_.c_str());
191 break;
192
193 case INFO:
194 if (globalVerbose_)
195 {
196 fprintf(stdout, "I: %s\n", message_.c_str());
197 }
198 break;
199
200 case TRACE:
201 if (globalTrace_)
202 {
203 fprintf(stdout, "T: %s\n", message_.c_str());
204 }
205 break;
206
207 default:
208 fprintf(stderr, "Unknown log level (%d) for message: %s\n", level_, message_.c_str());
209 }
210 }
211
212 InternalLogger& InternalLogger::operator<< (const std::string& message)
213 {
214 message_ += message;
215 return *this;
216 }
217
218 InternalLogger& InternalLogger::operator<< (const char* message)
219 {
220 message_ += std::string(message);
221 return *this;
222 }
223
224 InternalLogger& InternalLogger::operator<< (int message)
225 {
226 message_ += boost::lexical_cast<std::string>(message);
227 return *this;
228 }
229
230 void EnableInfoLevel(bool enabled)
231 {
232 globalVerbose_ = enabled;
233 }
234
235 void EnableTraceLevel(bool enabled)
236 {
237 globalTrace_ = enabled;
238 }
239 }
240 }
241
242
243 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 &&
244 ORTHANC_ENABLE_LOGGING_STDIO == 0 &&
245 ORTHANC_ENABLE_LOGGING == 1 */
150 246
151 /********************************************************* 247 /*********************************************************
152 * Internal logger of Orthanc, that mimics some 248 * Internal logger of Orthanc, that mimics some
153 * behavior from Google Log. 249 * behavior from Google Log.
154 *********************************************************/ 250 *********************************************************/
155 251
156 #include "OrthancException.h" 252 #include "OrthancException.h"
157 #include "Enumerations.h" 253 #include "Enumerations.h"
158 #include "Toolbox.h" 254 #include "Toolbox.h"
159 #include "SystemToolbox.h" 255
256 #if ORTHANC_SANDBOXED == 1
257 # include <stdio.h>
258 #else
259 # include "SystemToolbox.h"
260 #endif
160 261
161 #include <fstream> 262 #include <fstream>
162 #include <boost/filesystem.hpp> 263 #include <boost/filesystem.hpp>
163 #include <boost/thread.hpp> 264 #include <boost/thread.hpp>
164 #include <boost/date_time/posix_time/posix_time.hpp> 265 #include <boost/date_time/posix_time/posix_time.hpp>
230 char date[64]; 331 char date[64];
231 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d", 332 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d",
232 static_cast<int>(now.date().year()), 333 static_cast<int>(now.date().year()),
233 now.date().month().as_number(), 334 now.date().month().as_number(),
234 now.date().day().as_number(), 335 now.date().day().as_number(),
235 now.time_of_day().hours(), 336 static_cast<int>(now.time_of_day().hours()),
236 now.time_of_day().minutes(), 337 static_cast<int>(now.time_of_day().minutes()),
237 now.time_of_day().seconds(), 338 static_cast<int>(now.time_of_day().seconds()),
238 SystemToolbox::GetProcessId()); 339 SystemToolbox::GetProcessId());
239 340
240 std::string programName = exe.filename().replace_extension("").string(); 341 std::string programName = exe.filename().replace_extension("").string();
241 342
242 log = (root / (programName + ".log" + suffix + "." + std::string(date))); 343 log = (root / (programName + ".log" + suffix + "." + std::string(date)));
434 char date[32]; 535 char date[32];
435 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", 536 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ",
436 level[0], 537 level[0],
437 now.date().month().as_number(), 538 now.date().month().as_number(),
438 now.date().day().as_number(), 539 now.date().day().as_number(),
439 duration.hours(), 540 static_cast<int>(duration.hours()),
440 duration.minutes(), 541 static_cast<int>(duration.minutes()),
441 duration.seconds(), 542 static_cast<int>(duration.seconds()),
442 static_cast<int>(duration.fractional_seconds())); 543 static_cast<int>(duration.fractional_seconds()));
443 544
444 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] "; 545 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] ";
445 } 546 }
446 547