Mercurial > hg > orthanc
changeset 1489:1389834e130f
basic logger
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 04 Aug 2015 12:22:08 +0200 |
parents | c8763b603b0e |
children | 596927722403 |
files | Core/Logging.cpp Core/Logging.h NEWS OrthancServer/main.cpp |
diffstat | 4 files changed, 207 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/Logging.cpp Tue Aug 04 10:54:13 2015 +0200 +++ b/Core/Logging.cpp Tue Aug 04 12:22:08 2015 +0200 @@ -35,11 +35,17 @@ #if ORTHANC_ENABLE_LOGGING == 1 + +#if ORTHANC_ENABLE_GOOGLE_LOG == 1 + +/********************************************************* + * Wrapper around Google Log + *********************************************************/ + namespace Orthanc { namespace Logging { -#if ORTHANC_ENABLE_GOOGLE_LOG == 1 void Initialize() { // Initialize Google's logging library. @@ -72,8 +78,166 @@ FLAGS_v = 0; } } + + void SetTargetFolder(const std::string& path) + { + FLAGS_logtostderr = false; + FLAGS_log_dir = path; + } + } +} + +#else + +/********************************************************* + * Use internal logger, not Google Log + *********************************************************/ + +#include "OrthancException.h" + +#include <boost/filesystem.hpp> +#include <boost/thread.hpp> + +#if BOOST_HAS_DATE_TIME == 1 +# include <boost/date_time/posix_time/posix_time.hpp> +#else +# error Boost::date_time is required #endif + + +namespace +{ + struct NullStream : public std::ostream + { + NullStream() : std::ios(0), std::ostream(0) + { + } + }; +} + + +static boost::mutex mutex_; +static bool infoEnabled_ = false; +static bool traceEnabled_ = false; +static std::ostream& error_ = std::cerr; +static std::ostream& warning_ = std::cerr; +static std::ostream& info_ = std::cerr; +static std::ostream& trace_ = std::cerr; +static NullStream null_; + +namespace Orthanc +{ + namespace Logging + { + void Initialize() + { + infoEnabled_ = false; + traceEnabled_ = false; + } + + void Finalize() + { + } + + void EnableInfoLevel(bool enabled) + { + boost::mutex::scoped_lock lock(mutex_); + infoEnabled_ = enabled; + } + + void EnableTraceLevel(bool enabled) + { + boost::mutex::scoped_lock lock(mutex_); + traceEnabled_ = enabled; + + if (enabled) + { + // Also enable the "INFO" level when trace-level debugging is + // enabled + infoEnabled_ = true; + } + } + + void SetTargetFolder(const std::string& path) + { + boost::mutex::scoped_lock lock(mutex_); + // TODO + } + + InternalLogger::InternalLogger(const char* level, + const char* file, + int line) : + lock_(mutex_) + { + char c; + + if (strcmp(level, "ERROR") == 0) + { + stream_ = &error_; + c = 'E'; + } + else if (strcmp(level, "WARNING") == 0) + { + stream_ = &warning_; + c = 'W'; + } + else if (strcmp(level, "INFO") == 0) + { + stream_ = infoEnabled_ ? &info_ : &null_; + c = 'I'; + } + else if (strcmp(level, "TRACE") == 0) + { + stream_ = traceEnabled_ ? &trace_ : &null_; + c = 'T'; + } + else + { + // Unknown logging level + throw OrthancException(ErrorCode_InternalError); + } + + if (stream_ != &null_) + { + boost::filesystem::path path(file); + boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); + boost::posix_time::time_duration duration = now.time_of_day(); + + /** + From Google Log documentation: + + Log lines have this form: + + Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... + + where the fields are defined as follows: + + L A single character, representing the log level (eg 'I' for INFO) + mm The month (zero padded; ie May is '05') + dd The day (zero padded) + hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds + threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) + file The file name + line The line number + msg The user-supplied message + **/ + + char date[32]; + sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", c, + now.date().month().as_number(), + now.date().day().as_number(), + duration.hours(), + duration.minutes(), + duration.seconds(), + static_cast<int>(duration.fractional_seconds())); + + *stream_ << date << path.filename().string() << ":" << line << "] "; + } + } } } #endif + + +#endif // ORTHANC_ENABLE_LOGGING
--- a/Core/Logging.h Tue Aug 04 10:54:13 2015 +0200 +++ b/Core/Logging.h Tue Aug 04 12:22:08 2015 +0200 @@ -35,10 +35,16 @@ #if ORTHANC_ENABLE_LOGGING == 1 #if ORTHANC_ENABLE_GOOGLE_LOG == 1 -#include <stdlib.h> // This fixes a problem in glog for recent releases of MinGW -#include <glog/logging.h> +# include <stdlib.h> // This fixes a problem in glog for recent releases of MinGW +# include <glog/logging.h> +#else +# include <iostream> +# include <boost/thread/mutex.hpp> +# define LOG(level) ::Orthanc::Logging::InternalLogger(#level, __FILE__, __LINE__) +# define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__) #endif + namespace Orthanc { namespace Logging @@ -50,6 +56,37 @@ void EnableInfoLevel(bool enabled); void EnableTraceLevel(bool enabled); + + void SetTargetFolder(const std::string& path); + + +#if ORTHANC_ENABLE_GOOGLE_LOG != 1 + class InternalLogger + { + private: + boost::mutex::scoped_lock lock_; + std::ostream* stream_; + + public: + InternalLogger(const char* level, + const char* file, + int line); + + ~InternalLogger() + { +#if defined(_WIN32) + *stream_ << "\r\n"; +#else + *stream_ << "\n"; +#endif + } + + std::ostream& operator<< (const std::string& message) + { + return (*stream_) << message; + } + }; +#endif } }
--- a/NEWS Tue Aug 04 10:54:13 2015 +0200 +++ b/NEWS Tue Aug 04 12:22:08 2015 +0200 @@ -1,6 +1,8 @@ Pending changes in the mainline =============================== +* Orthanc does not use Google Log anymore + Version 0.9.2 (2015/08/02) ==========================
--- a/OrthancServer/main.cpp Tue Aug 04 10:54:13 2015 +0200 +++ b/OrthancServer/main.cpp Tue Aug 04 12:22:08 2015 +0200 @@ -652,8 +652,7 @@ if (boost::starts_with(argv[i], "--logdir=")) { - FLAGS_logtostderr = false; - FLAGS_log_dir = std::string(argv[i]).substr(9); + Logging::SetTargetFolder(std::string(argv[i]).substr(9)); } if (boost::starts_with(argv[i], "--config="))