# HG changeset patch # User Sebastien Jodogne # Date 1604333757 -3600 # Node ID c7bd2f21ccc32638eabd3a8a3ac50d7a75430f10 # Parent 0ae2ca2100774f2d99e542a69d8c3ee9ce6cf239 new macro CLOG, and sharing more code between logging engines diff -r 0ae2ca210077 -r c7bd2f21ccc3 OrthancFramework/Sources/Logging.cpp --- a/OrthancFramework/Sources/Logging.cpp Mon Nov 02 14:48:15 2020 +0100 +++ b/OrthancFramework/Sources/Logging.cpp Mon Nov 02 17:15:57 2020 +0100 @@ -26,10 +26,17 @@ #include "OrthancException.h" +/********************************************************* + * Common section + *********************************************************/ + namespace Orthanc { namespace Logging { + static bool infoEnabled_ = false; + static bool traceEnabled_ = false; + const char* EnumerationToString(LogLevel level) { switch (level) @@ -75,12 +82,71 @@ throw OrthancException(ErrorCode_InternalError); } } + + void EnableInfoLevel(bool enabled) + { + infoEnabled_ = enabled; + + if (!enabled) + { + // Also disable the "TRACE" level when info-level debugging is disabled + traceEnabled_ = false; + } + } + + bool IsInfoLevelEnabled() + { + return infoEnabled_; + } + + void EnableTraceLevel(bool enabled) + { + traceEnabled_ = enabled; + + if (enabled) + { + // Also enable the "INFO" level when trace-level debugging is enabled + infoEnabled_ = true; + } + } + + bool IsTraceLevelEnabled() + { + return traceEnabled_; + } + + static bool IsLoggingEnabled(LogLevel level, + LogCategory category) + { + if (level == LogLevel_ERROR || + level == LogLevel_WARNING) + { + return true; + } + else if (level == LogLevel_INFO) + { + return infoEnabled_; + } + else if (level == LogLevel_TRACE) + { + return traceEnabled_; + } + else + { + return false; + } + } } } + #if ORTHANC_ENABLE_LOGGING != 1 +/********************************************************* + * Section if logging is disabled + *********************************************************/ + namespace Orthanc { namespace Logging @@ -105,24 +171,6 @@ { } - void EnableInfoLevel(bool enabled) - { - } - - void EnableTraceLevel(bool enabled) - { - } - - bool IsTraceLevelEnabled() - { - return false; - } - - bool IsInfoLevelEnabled() - { - return false; - } - void SetTargetFile(const std::string& path) { } @@ -152,9 +200,6 @@ { namespace Logging { - static bool infoEnabled_ = false; - static bool traceEnabled_ = false; - #ifdef __EMSCRIPTEN__ static void ErrorLogFunc(const char* msg) { @@ -202,38 +247,34 @@ { std::string message = messageStream_.str(); - switch (level_) + if (IsLoggingEnabled(level_, category_)) { - case LogLevel_ERROR: - ErrorLogFunc(message.c_str()); - break; + switch (level_) + { + case LogLevel_ERROR: + ErrorLogFunc(message.c_str()); + break; - case LogLevel_WARNING: - WarningLogFunc(message.c_str()); - break; + case LogLevel_WARNING: + WarningLogFunc(message.c_str()); + break; - case LogLevel_INFO: - if (infoEnabled_) - { + case LogLevel_INFO: InfoLogFunc(message.c_str()); // TODO: stone_console_info(message_.c_str()); - } - break; + break; + + case LogLevel_TRACE: + TraceLogFunc(message.c_str()); + break; - case LogLevel_TRACE: - // TODO - Check trace category - if (traceEnabled_) + default: { - TraceLogFunc(message.c_str()); + std::stringstream ss; + ss << "Unknown log level (" << level_ << ") for message: " << message; + std::string s = ss.str(); + ErrorLogFunc(s.c_str()); } - break; - - default: - { - std::stringstream ss; - ss << "Unknown log level (" << level_ << ") for message: " << message; - std::string s = ss.str(); - ErrorLogFunc(s.c_str()); } } } @@ -258,32 +299,6 @@ { } - void EnableInfoLevel(bool enabled) - { - infoEnabled_ = enabled; - - if (!enabled) - { - // Also disable the "TRACE" level when info-level debugging is disabled - traceEnabled_ = false; - } - } - - bool IsInfoLevelEnabled() - { - return infoEnabled_; - } - - void EnableTraceLevel(bool enabled) - { - traceEnabled_ = enabled; - } - - bool IsTraceLevelEnabled() - { - return traceEnabled_; - } - void SetTargetFile(const std::string& path) { } @@ -368,8 +383,6 @@ static boost::mutex loggingStreamsMutex_; static Orthanc::Logging::NullStream nullStream_; static OrthancPluginContext* pluginContext_ = NULL; -static bool infoEnabled_ = false; -static bool traceEnabled_ = false; namespace Orthanc @@ -496,7 +509,8 @@ break; default: - throw OrthancException(ErrorCode_InternalError); + c = '?'; + break; } char date[64]; @@ -575,39 +589,6 @@ } - void EnableInfoLevel(bool enabled) - { - infoEnabled_ = enabled; - - if (!enabled) - { - // Also disable the "TRACE" level when info-level debugging is disabled - traceEnabled_ = false; - } - } - - bool IsInfoLevelEnabled() - { - return infoEnabled_; - } - - void EnableTraceLevel(bool enabled) - { - traceEnabled_ = enabled; - - if (enabled) - { - // Also enable the "INFO" level when trace-level debugging is enabled - infoEnabled_ = true; - } - } - - bool IsTraceLevelEnabled() - { - return traceEnabled_; - } - - void SetTargetFolder(const std::string& path) { boost::mutex::scoped_lock lock(loggingStreamsMutex_); @@ -644,19 +625,19 @@ InternalLogger::InternalLogger(LogLevel level, - TraceCategory category, + LogCategory category, const char* file, int line) : lock_(loggingStreamsMutex_, boost::defer_lock_t()), level_(level), - category_(category), stream_(&nullStream_) // By default, logging to "/dev/null" is simulated { if (pluginContext_ != NULL) { // We are logging using the Orthanc plugin SDK - if (level == LogLevel_TRACE) + if (level == LogLevel_TRACE || + !IsLoggingEnabled(level, category)) { // No trace level in plugins, directly exit as the stream is // set to "/dev/null" @@ -672,8 +653,7 @@ { // We are logging in a standalone application, not inside an Orthanc plugin - if ((level == LogLevel_INFO && !infoEnabled_) || - (level == LogLevel_TRACE && !traceEnabled_)) // TODO - Check trace category + if (!IsLoggingEnabled(level, category)) { // This logging level is disabled, directly exit as the // stream is set to "/dev/null" @@ -710,8 +690,9 @@ stream_ = loggingStreamsContext_->info_; break; - default: - throw OrthancException(ErrorCode_InternalError); + default: // Should not occur + stream_ = loggingStreamsContext_->error_; + break; } if (stream_ == &nullStream_) diff -r 0ae2ca210077 -r c7bd2f21ccc3 OrthancFramework/Sources/Logging.h --- a/OrthancFramework/Sources/Logging.h Mon Nov 02 14:48:15 2020 +0100 +++ b/OrthancFramework/Sources/Logging.h Mon Nov 02 17:15:57 2020 +0100 @@ -52,11 +52,11 @@ LogLevel_TRACE }; - enum TraceCategory + enum LogCategory { - TraceCategory_GENERIC, - TraceCategory_SQLITE, - TraceCategory_DICOM + LogCategory_GENERIC, + LogCategory_SQLITE, + LogCategory_DICOM }; ORTHANC_PUBLIC const char* EnumerationToString(LogLevel level); @@ -106,24 +106,38 @@ /** - * NB: The macro "VLOG(unused)" is for backward compatibility with - * Orthanc <= 1.8.0. + * NB: + * - The "VLOG(unused)" macro is for backward compatibility with + * Orthanc <= 1.8.0. + * - The "CLOG()" macro stands for "category log" (new in Orthanc 1.8.1) **/ +#if defined(LOG) +# error The macro LOG cannot be defined beforehand +#endif + +#if defined(VLOG) +# error The macro VLOG cannot be defined beforehand +#endif + +#if defined(CLOG) +# error The macro CLOG cannot be defined beforehand +#endif + #if ORTHANC_ENABLE_LOGGING != 1 -# define LOG(level) ::Orthanc::Logging::NullStream() -# define VLOG(unused) ::Orthanc::Logging::NullStream() -# define TLOG(category) ::Orthanc::Logging::NullStream() +# define LOG(level) ::Orthanc::Logging::NullStream() +# define VLOG(unused) ::Orthanc::Logging::NullStream() +# define CLOG(level, category) ::Orthanc::Logging::NullStream() #else /* ORTHANC_ENABLE_LOGGING == 1 */ # define LOG(level) ::Orthanc::Logging::InternalLogger \ (::Orthanc::Logging::LogLevel_ ## level, \ - ::Orthanc::Logging::TraceCategory_GENERIC, __FILE__, __LINE__) + ::Orthanc::Logging::LogCategory_GENERIC, __FILE__, __LINE__) # define VLOG(unused) ::Orthanc::Logging::InternalLogger \ (::Orthanc::Logging::LogLevel_TRACE, \ - ::Orthanc::Logging::TraceCategory_GENERIC, __FILE__, __LINE__) -# define TLOG(category) ::Orthanc::Logging::InternalLogger \ - (::Orthanc::Logging::LogLevel_TRACE, \ - ::Orthanc::Logging::TraceCategory_ ## category, __FILE__, __LINE__) + ::Orthanc::Logging::LogCategory_GENERIC, __FILE__, __LINE__) +# define CLOG(level, category) ::Orthanc::Logging::InternalLogger \ + (::Orthanc::Logging::LogLevel_ ## level, \ + ::Orthanc::Logging::LogCategory_ ## category, __FILE__, __LINE__) #endif @@ -144,12 +158,12 @@ { private: LogLevel level_; - TraceCategory category_; + LogCategory category_; std::stringstream messageStream_; public: InternalLogger(LogLevel level, - TraceCategory category, + LogCategory category, const char* file /* ignored */, int line /* ignored */) : level_(level), @@ -191,13 +205,12 @@ private: boost::mutex::scoped_lock lock_; LogLevel level_; - TraceCategory category_; std::unique_ptr pluginStream_; std::ostream* stream_; public: InternalLogger(LogLevel level, - TraceCategory category, + LogCategory category, const char* file, int line); diff -r 0ae2ca210077 -r c7bd2f21ccc3 OrthancFramework/Sources/SQLite/Connection.cpp --- a/OrthancFramework/Sources/SQLite/Connection.cpp Mon Nov 02 14:48:15 2020 +0100 +++ b/OrthancFramework/Sources/SQLite/Connection.cpp Mon Nov 02 17:15:57 2020 +0100 @@ -154,7 +154,7 @@ bool Connection::Execute(const char* sql) { #if ORTHANC_SQLITE_STANDALONE != 1 - TLOG(SQLITE) << "SQLite::Connection::Execute " << sql; + CLOG(TRACE, SQLITE) << "SQLite::Connection::Execute " << sql; #endif CheckIsOpen(); @@ -385,7 +385,7 @@ void Connection::FlushToDisk() { #if ORTHANC_SQLITE_STANDALONE != 1 - TLOG(SQLITE) << "SQLite::Connection::FlushToDisk"; + CLOG(TRACE, SQLITE) << "SQLite::Connection::FlushToDisk"; #endif int err = sqlite3_wal_checkpoint(db_, NULL); diff -r 0ae2ca210077 -r c7bd2f21ccc3 OrthancFramework/Sources/SQLite/Statement.cpp --- a/OrthancFramework/Sources/SQLite/Statement.cpp Mon Nov 02 14:48:15 2020 +0100 +++ b/OrthancFramework/Sources/SQLite/Statement.cpp Mon Nov 02 17:15:57 2020 +0100 @@ -59,8 +59,8 @@ #else // Trace logging is enabled in debug builds # include "../Logging.h" -# define LOG_CREATE(message) TLOG(SQLITE) << "SQLite::Statement create: " << message; -# define LOG_APPLY(message); // TLOG(SQLITE) << "SQLite::Statement apply: " << message; +# define LOG_CREATE(message) CLOG(TRACE, SQLITE) << "SQLite::Statement create: " << message; +# define LOG_APPLY(message); // CLOG(TRACE, SQLITE) << "SQLite::Statement apply: " << message; #endif #include "sqlite3.h" @@ -168,7 +168,7 @@ // spurious error callback. if (clear_bound_vars) sqlite3_clear_bindings(GetStatement()); - //TLOG(SQLITE) << "SQLite::Statement::Reset"; + //CLOG(TRACE, SQLITE) << "SQLite::Statement::Reset"; sqlite3_reset(GetStatement()); } diff -r 0ae2ca210077 -r c7bd2f21ccc3 OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp --- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp Mon Nov 02 14:48:15 2020 +0100 +++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp Mon Nov 02 17:15:57 2020 +0100 @@ -155,9 +155,9 @@ virtual void Compute(SQLite::FunctionContext& context) { - TLOG(SQLITE) << "There exists a remaining ancestor with public ID \"" - << context.GetStringValue(0) << "\" of type " - << context.GetIntValue(1); + CLOG(TRACE, SQLITE) << "There exists a remaining ancestor with public ID \"" + << context.GetStringValue(0) << "\" of type " + << context.GetIntValue(1); if (!hasRemainingAncestor_ || remainingType_ >= context.GetIntValue(1)) diff -r 0ae2ca210077 -r c7bd2f21ccc3 OrthancServer/Sources/OrthancGetRequestHandler.cpp --- a/OrthancServer/Sources/OrthancGetRequestHandler.cpp Mon Nov 02 14:48:15 2020 +0100 +++ b/OrthancServer/Sources/OrthancGetRequestHandler.cpp Mon Nov 02 17:15:57 2020 +0100 @@ -151,8 +151,8 @@ if (pc->result == ASC_P_ACCEPTANCE && LookupTransferSyntax(transferSyntax, pc->acceptedTransferSyntax)) { - TLOG(DICOM) << "C-GET SCP accepted: SOP class " << sopClassUid - << " with transfer syntax " << GetTransferSyntaxUid(transferSyntax); + CLOG(TRACE, DICOM) << "C-GET SCP accepted: SOP class " << sopClassUid + << " with transfer syntax " << GetTransferSyntaxUid(transferSyntax); if (std::string(pc->abstractSyntax) == sopClassUid) { accepted[transferSyntax] = pc->presentationContextID;