comparison Resources/Orthanc/Core/Logging.cpp @ 130:4f3945a2b725

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 22 Mar 2018 17:32:05 +0100
parents a18bfe1fdd62
children c5c54d5c52e5
comparison
equal deleted inserted replaced
129:806d1bb56918 130:4f3945a2b725
92 void Initialize(OrthancPluginContext* context) 92 void Initialize(OrthancPluginContext* context)
93 { 93 {
94 context_ = context; 94 context_ = context;
95 } 95 }
96 96
97 InternalLogger::InternalLogger(const char* level, 97 InternalLogger::InternalLogger(Level level,
98 const char* file /* ignored */, 98 const char* file /* ignored */,
99 int line /* ignored */) : 99 int line /* ignored */) :
100 level_(level) 100 level_(level)
101 { 101 {
102 } 102 }
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 } 124 break;
125 } 125
126 } 126 default:
127 127 {
128 InternalLogger& InternalLogger::operator<< (const std::string& message) 128 std::string s = ("Unknown log level (" + boost::lexical_cast<std::string>(level_) +
129 { 129 ") for message: " + message_);
130 message_ += message; 130 OrthancPluginLogError(context_, s.c_str());
131 return *this; 131 break;
132 } 132 }
133 133 }
134 InternalLogger& InternalLogger::operator<< (const char* message) 134 }
135 {
136 message_ += std::string(message);
137 return *this;
138 }
139
140 InternalLogger& InternalLogger::operator<< (int message)
141 {
142 message_ += boost::lexical_cast<std::string>(message);
143 return *this;
144 } 135 }
145 } 136 }
146 } 137 }
147 138
148 139
149 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */ 140 #elif ORTHANC_ENABLE_LOGGING_STDIO == 1
141
142 /*********************************************************
143 * Logger compatible with <stdio.h>
144 *********************************************************/
145
146 #include <stdio.h>
147 #include <boost/lexical_cast.hpp>
148
149 namespace Orthanc
150 {
151 namespace Logging
152 {
153 static bool globalVerbose_ = false;
154 static bool globalTrace_ = false;
155
156 InternalLogger::InternalLogger(Level level,
157 const char* file /* ignored */,
158 int line /* ignored */) :
159 level_(level)
160 {
161 }
162
163 InternalLogger::~InternalLogger()
164 {
165 switch (level_)
166 {
167 case ERROR:
168 fprintf(stderr, "E: %s\n", message_.c_str());
169 break;
170
171 case WARNING:
172 fprintf(stdout, "W: %s\n", message_.c_str());
173 break;
174
175 case INFO:
176 if (globalVerbose_)
177 {
178 fprintf(stdout, "I: %s\n", message_.c_str());
179 }
180 break;
181
182 case TRACE:
183 if (globalTrace_)
184 {
185 fprintf(stdout, "T: %s\n", message_.c_str());
186 }
187 break;
188
189 default:
190 fprintf(stderr, "Unknown log level (%d) for message: %s\n", level_, message_.c_str());
191 }
192 }
193
194 void EnableInfoLevel(bool enabled)
195 {
196 globalVerbose_ = enabled;
197 }
198
199 void EnableTraceLevel(bool enabled)
200 {
201 globalTrace_ = enabled;
202 }
203 }
204 }
205
206
207 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 &&
208 ORTHANC_ENABLE_LOGGING_STDIO == 0 &&
209 ORTHANC_ENABLE_LOGGING == 1 */
150 210
151 /********************************************************* 211 /*********************************************************
152 * Internal logger of Orthanc, that mimics some 212 * Internal logger of Orthanc, that mimics some
153 * behavior from Google Log. 213 * behavior from Google Log.
154 *********************************************************/ 214 *********************************************************/
155 215
156 #include "OrthancException.h" 216 #include "OrthancException.h"
157 #include "Enumerations.h" 217 #include "Enumerations.h"
158 #include "Toolbox.h" 218 #include "Toolbox.h"
159 #include "SystemToolbox.h" 219
220 #if ORTHANC_SANDBOXED == 1
221 # include <stdio.h>
222 #else
223 # include "SystemToolbox.h"
224 #endif
160 225
161 #include <fstream> 226 #include <fstream>
162 #include <boost/filesystem.hpp> 227 #include <boost/filesystem.hpp>
163 #include <boost/thread.hpp> 228 #include <boost/thread.hpp>
164 #include <boost/date_time/posix_time/posix_time.hpp> 229 #include <boost/date_time/posix_time/posix_time.hpp>
230 char date[64]; 295 char date[64];
231 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d", 296 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d",
232 static_cast<int>(now.date().year()), 297 static_cast<int>(now.date().year()),
233 now.date().month().as_number(), 298 now.date().month().as_number(),
234 now.date().day().as_number(), 299 now.date().day().as_number(),
235 now.time_of_day().hours(), 300 static_cast<int>(now.time_of_day().hours()),
236 now.time_of_day().minutes(), 301 static_cast<int>(now.time_of_day().minutes()),
237 now.time_of_day().seconds(), 302 static_cast<int>(now.time_of_day().seconds()),
238 SystemToolbox::GetProcessId()); 303 SystemToolbox::GetProcessId());
239 304
240 std::string programName = exe.filename().replace_extension("").string(); 305 std::string programName = exe.filename().replace_extension("").string();
241 306
242 log = (root / (programName + ".log" + suffix + "." + std::string(date))); 307 log = (root / (programName + ".log" + suffix + "." + std::string(date)));
434 char date[32]; 499 char date[32];
435 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", 500 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ",
436 level[0], 501 level[0],
437 now.date().month().as_number(), 502 now.date().month().as_number(),
438 now.date().day().as_number(), 503 now.date().day().as_number(),
439 duration.hours(), 504 static_cast<int>(duration.hours()),
440 duration.minutes(), 505 static_cast<int>(duration.minutes()),
441 duration.seconds(), 506 static_cast<int>(duration.seconds()),
442 static_cast<int>(duration.fractional_seconds())); 507 static_cast<int>(duration.fractional_seconds()));
443 508
444 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] "; 509 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] ";
445 } 510 }
446 511