# HG changeset patch # User Sebastien Jodogne # Date 1519999837 -3600 # Node ID 9c54c40eaf25a4cf4d8bd464ce522c65162b146c # Parent 509041cb57db4332af5b3ec84410b6c9fd9aee2e logging primitives for WebAssembly diff -r 509041cb57db -r 9c54c40eaf25 Core/Logging.cpp --- a/Core/Logging.cpp Thu Mar 01 09:29:06 2018 +0100 +++ b/Core/Logging.cpp Fri Mar 02 15:10:37 2018 +0100 @@ -105,22 +105,31 @@ { if (context_ != NULL) { - if (level_ == "ERROR") - { - OrthancPluginLogError(context_, message_.c_str()); - } - else if (level_ == "WARNING") + switch (level_) { - OrthancPluginLogWarning(context_, message_.c_str()); - } - else if (level_ == "INFO") - { - OrthancPluginLogInfo(context_, message_.c_str()); - } - else - { - std::string s = "Unknown log level (" + level_ + ") for message: " + message_; - OrthancPluginLogError(context_, s.c_str()); + case ERROR: + OrthancPluginLogError(context_, message_.c_str()); + break; + + case WARNING: + OrthancPluginLogWarning(context_, message_.c_str()); + break; + + case INFO: + OrthancPluginLogInfo(context_, message_.c_str()); + break; + + case TRACE: + // Not used by plugins + break; + + default: + { + std::string s = ("Unknown log level (" + boost::lexical_cast(level_) + + ") for message: " + message_); + OrthancPluginLogError(context_, s.c_str()); + break; + } } } } @@ -146,7 +155,94 @@ } -#else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */ +#elif ORTHANC_ENABLE_LOGGING_STDIO == 1 + +/********************************************************* + * Logger compatible with + *********************************************************/ + +#include +#include + +namespace Orthanc +{ + namespace Logging + { + static bool globalVerbose_ = false; + static bool globalTrace_ = false; + + InternalLogger::InternalLogger(Level level, + const char* file /* ignored */, + int line /* ignored */) : + level_(level) + { + } + + InternalLogger::~InternalLogger() + { + switch (level_) + { + case ERROR: + fprintf(stderr, "E: %s\n", message_.c_str()); + break; + + case WARNING: + fprintf(stdout, "W: %s\n", message_.c_str()); + break; + + case INFO: + if (globalVerbose_) + { + fprintf(stdout, "I: %s\n", message_.c_str()); + } + break; + + case TRACE: + if (globalTrace_) + { + fprintf(stdout, "T: %s\n", message_.c_str()); + } + break; + + default: + fprintf(stderr, "Unknown log level (%d) for message: %s\n", level_, 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; + } + + void EnableInfoLevel(bool enabled) + { + globalVerbose_ = enabled; + } + + void EnableTraceLevel(bool enabled) + { + globalTrace_ = enabled; + } + } +} + + +#else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && + ORTHANC_ENABLE_LOGGING_STDIO == 0 && + ORTHANC_ENABLE_LOGGING == 1 */ /********************************************************* * Internal logger of Orthanc, that mimics some @@ -156,7 +252,12 @@ #include "OrthancException.h" #include "Enumerations.h" #include "Toolbox.h" -#include "SystemToolbox.h" + +#if ORTHANC_SANDBOXED == 1 +# include +#else +# include "SystemToolbox.h" +#endif #include #include @@ -232,9 +333,9 @@ static_cast(now.date().year()), now.date().month().as_number(), now.date().day().as_number(), - now.time_of_day().hours(), - now.time_of_day().minutes(), - now.time_of_day().seconds(), + static_cast(now.time_of_day().hours()), + static_cast(now.time_of_day().minutes()), + static_cast(now.time_of_day().seconds()), SystemToolbox::GetProcessId()); std::string programName = exe.filename().replace_extension("").string(); @@ -436,9 +537,9 @@ level[0], now.date().month().as_number(), now.date().day().as_number(), - duration.hours(), - duration.minutes(), - duration.seconds(), + static_cast(duration.hours()), + static_cast(duration.minutes()), + static_cast(duration.seconds()), static_cast(duration.fractional_seconds())); header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast(line) + "] "; diff -r 509041cb57db -r 9c54c40eaf25 Core/Logging.h --- a/Core/Logging.h Thu Mar 01 09:29:06 2018 +0100 +++ b/Core/Logging.h Fri Mar 02 15:10:37 2018 +0100 @@ -47,6 +47,14 @@ # endif #endif +#if !defined(ORTHANC_ENABLE_LOGGING_STDIO) +# if ORTHANC_ENABLE_LOGGING == 1 +# error The macro ORTHANC_ENABLE_LOGGING_STDIO must be defined +# else +# define ORTHANC_ENABLE_LOGGING_STDIO 0 +# endif +#endif + #if ORTHANC_ENABLE_LOGGING_PLUGIN == 1 # include #endif @@ -104,24 +112,35 @@ # define VLOG(level) ::Orthanc::Logging::NullStream() -#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1 +#elif (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 || \ + ORTHANC_ENABLE_LOGGING_STDIO == 1) # include -# define LOG(level) ::Orthanc::Logging::InternalLogger(#level, __FILE__, __LINE__) -# define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__) +# define LOG(level) ::Orthanc::Logging::InternalLogger \ + (::Orthanc::Logging::level, __FILE__, __LINE__) +# define VLOG(level) ::Orthanc::Logging::InternalLogger \ + (::Orthanc::Logging::TRACE, __FILE__, __LINE__) namespace Orthanc { namespace Logging { + enum Level + { + ERROR, + WARNING, + INFO, + TRACE + }; + class InternalLogger : public boost::noncopyable { private: - std::string level_; + Level level_; std::string message_; public: - InternalLogger(const char* level, + InternalLogger(Level level, const char* file, int line); @@ -137,7 +156,11 @@ } -#else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */ + + +#else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && + ORTHANC_ENABLE_LOGGING_STDIO == 0 && + ORTHANC_ENABLE_LOGGING == 1 */ # include # define LOG(level) ::Orthanc::Logging::InternalLogger(#level, __FILE__, __LINE__) diff -r 509041cb57db -r 9c54c40eaf25 Resources/CMake/OrthancFrameworkConfiguration.cmake --- a/Resources/CMake/OrthancFrameworkConfiguration.cmake Thu Mar 01 09:29:06 2018 +0100 +++ b/Resources/CMake/OrthancFrameworkConfiguration.cmake Fri Mar 02 15:10:37 2018 +0100 @@ -428,15 +428,26 @@ if (ORTHANC_SANDBOXED) add_definitions( -DORTHANC_SANDBOXED=1 - -DORTHANC_ENABLE_LOGGING=0 -DORTHANC_ENABLE_LOGGING_PLUGIN=0 ) + + if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten") + add_definitions( + -DORTHANC_ENABLE_LOGGING=1 + -DORTHANC_ENABLE_LOGGING_STDIO=1 + ) + else() + add_definitions( + -DORTHANC_ENABLE_LOGGING=0 + ) + endif() else() add_definitions( -DORTHANC_SANDBOXED=0 -DORTHANC_ENABLE_LOGGING=1 -DORTHANC_ENABLE_LOGGING_PLUGIN=0 + -DORTHANC_ENABLE_LOGGING_STDIO=0 ) list(APPEND ORTHANC_CORE_SOURCES_INTERNAL