# HG changeset patch # User Sebastien Jodogne # Date 1482505320 -3600 # Node ID 2dbfdafc25124c6cf9ee47eba4c3540d9231e52b # Parent 4e8e0ad2001cbb8ae48758687c73ab8b8947e400 Logger compatible with the Orthanc plugin SDK diff -r 4e8e0ad2001c -r 2dbfdafc2512 CMakeLists.txt --- a/CMakeLists.txt Mon Dec 19 15:31:01 2016 +0100 +++ b/CMakeLists.txt Fri Dec 23 16:02:00 2016 +0100 @@ -412,6 +412,7 @@ -DORTHANC_BUILD_UNIT_TESTS=1 -DORTHANC_ENABLE_BASE64=1 -DORTHANC_ENABLE_LOGGING=1 + -DORTHANC_ENABLE_LOGGING_PLUGIN=0 -DORTHANC_ENABLE_LUA=1 -DORTHANC_ENABLE_MD5=1 -DORTHANC_MAXIMUM_TAG_LENGTH=256 diff -r 4e8e0ad2001c -r 2dbfdafc2512 Core/Logging.cpp --- a/Core/Logging.cpp Mon Dec 19 15:31:01 2016 +0100 +++ b/Core/Logging.cpp Fri Dec 23 16:02:00 2016 +0100 @@ -73,7 +73,74 @@ } } -#else + +#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1 + +/********************************************************* + * Logger compatible with the Orthanc plugin SDK + *********************************************************/ + +#include + +namespace Orthanc +{ + namespace Logging + { + static OrthancPluginContext* context_ = NULL; + + void Initialize(OrthancPluginContext* context) + { + context_ = context_; + } + + InternalLogger::InternalLogger(const char* level, + const char* file /* ignored */, + int line /* ignored */) : + level_(level) + { + } + + InternalLogger::~InternalLogger() + { + if (context_ != NULL) + { + if (level_ == "ERROR") + { + OrthancPluginLogError(context_, message_.c_str()); + } + else if (level_ == "WARNING") + { + OrthancPluginLogWarning(context_, message_.c_str()); + } + else if (level_ == "INFO") + { + OrthancPluginLogInfo(context_, message_.c_str()); + } + } + } + + InternalLogger& InternalLogger::operator<< (const std::string& message) + { + message_ += message; + return *this; + } + + InternalLogger& InternalLogger::operator<< (const char* message) + { + message_ += std::string(message); + return *this; + } + + InternalLogger& InternalLogger::operator<< (int message) + { + message_ += boost::lexical_cast(message); + return *this; + } + } +} + + +#else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */ /********************************************************* * Internal logger of Orthanc, that mimics some diff -r 4e8e0ad2001c -r 2dbfdafc2512 Core/Logging.h --- a/Core/Logging.h Mon Dec 19 15:31:01 2016 +0100 +++ b/Core/Logging.h Fri Dec 23 16:02:00 2016 +0100 @@ -38,11 +38,27 @@ # error The macro ORTHANC_ENABLE_LOGGING must be defined #endif +#if !defined(ORTHANC_ENABLE_LOGGING_PLUGIN) +# if ORTHANC_ENABLE_LOGGING == 1 +# error The macro ORTHANC_ENABLE_LOGGING_PLUGIN must be defined +# else +# define ORTHANC_ENABLE_LOGGING_PLUGIN 0 +# endif +#endif + +#if ORTHANC_ENABLE_LOGGING_PLUGIN == 1 +# include +#endif + namespace Orthanc { namespace Logging { +#if ORTHANC_ENABLE_LOGGING_PLUGIN == 1 + void Initialize(OrthancPluginContext* context); +#else void Initialize(); +#endif void Finalize(); @@ -86,7 +102,41 @@ # define LOG(level) ::Orthanc::Logging::NullStream() # define VLOG(level) ::Orthanc::Logging::NullStream() -#else /* ORTHANC_ENABLE_LOGGING == 1 */ + +#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1 + +# include +# define LOG(level) ::Orthanc::Logging::InternalLogger(#level, __FILE__, __LINE__) +# define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__) + +namespace Orthanc +{ + namespace Logging + { + class InternalLogger : public boost::noncopyable + { + private: + std::string level_; + std::string message_; + + public: + InternalLogger(const char* level, + const char* file, + int line); + + ~InternalLogger(); + + InternalLogger& operator<< (const std::string& message); + + InternalLogger& operator<< (const char* message); + + InternalLogger& operator<< (int message); + }; + } +} + + +#else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */ # include # define LOG(level) ::Orthanc::Logging::InternalLogger(#level, __FILE__, __LINE__)