Mercurial > hg > orthanc
diff Core/Logging.cpp @ 3359:815b81142ff7 emscripten-logging
Enable custom logging functions to redirect to emscripten specific logging calls
in the ORTHANC_ENABLE_LOGGING_STDIO mode.
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 07 May 2019 11:23:11 +0200 |
parents | 54cdad5a7228 |
children | ea07b29c6d0e |
line wrap: on
line diff
--- a/Core/Logging.cpp Wed Apr 24 07:51:48 2019 +0200 +++ b/Core/Logging.cpp Tue May 07 11:23:11 2019 +0200 @@ -63,7 +63,17 @@ void EnableTraceLevel(bool enabled) { } + + bool IsTraceLevelEnabled() + { + return false; + } + bool IsInfoLevelEnabled() + { + return false; + } + void SetTargetFile(const std::string& path) { } @@ -140,12 +150,18 @@ #elif ORTHANC_ENABLE_LOGGING_STDIO == 1 /********************************************************* - * Logger compatible with <stdio.h> + * Logger compatible with <stdio.h> OR logger that sends its + * output to the emscripten html5 api (depending on the + * definition of __EMSCRIPTEN__) *********************************************************/ #include <stdio.h> #include <boost/lexical_cast.hpp> +#ifdef __EMSCRIPTEN__ +#include "emscripten/html5.h" +#endif + namespace Orthanc { namespace Logging @@ -153,6 +169,67 @@ static bool globalVerbose_ = false; static bool globalTrace_ = false; +#ifdef __EMSCRIPTEN__ + void defaultErrorLogFunc(const char* msg) + { + emscripten_console_error(msg); + } + + void defaultWarningLogFunc(const char* msg) + { + emscripten_console_warn(msg); + } + + void defaultInfoLogFunc(const char* msg) + { + emscripten_console_log(msg); + } + + void defaultTraceLogFunc(const char* msg) + { + emscripten_console_log(msg); + } +#else +// __EMSCRIPTEN__ not #defined + void defaultErrorLogFunc(const char* msg) + { + fprintf(stderr, "E: %s\n", msg); + } + + void defaultWarningLogFunc(const char*) + { + fprintf(stdout, "W: %s\n", msg); + } + + void defaultInfoLogFunc(const char*) + { + fprintf(stdout, "I: %s\n", msg); + } + + void defaultTraceLogFunc(const char*) + { + fprintf(stdout, "T: %s\n", msg); + } +#endif +// __EMSCRIPTEN__ + + static LoggingFunction globalErrorLogFunc = defaultErrorLogFunc; + static LoggingFunction globalWarningLogFunc = defaultWarningLogFunc; + static LoggingFunction globalInfoLogFunc = defaultInfoLogFunc; + static LoggingFunction globalTraceLogFunc = defaultTraceLogFunc; + + void SetErrorWarnInfoTraceLoggingFunctions( + LoggingFunction errorLogFunc, + LoggingFunction warningLogfunc, + LoggingFunction infoLogFunc, + LoggingFunction traceLogFunc) + { + globalErrorLogFunc = errorLogFunc; + globalWarningLogFunc = warningLogfunc; + globalInfoLogFunc = infoLogFunc; + globalTraceLogFunc = traceLogFunc; + } + InternalLogger::InternalLogger(InternalLevel level, const char* file /* ignored */, int line /* ignored */) : @@ -165,29 +242,35 @@ switch (level_) { case InternalLevel_ERROR: - fprintf(stderr, "E: %s\n", message_.c_str()); + globalErrorLogFunc(message_.c_str()); break; case InternalLevel_WARNING: - fprintf(stdout, "W: %s\n", message_.c_str()); + globalWarningLogFunc(message_.c_str()); break; case InternalLevel_INFO: if (globalVerbose_) { - fprintf(stdout, "I: %s\n", message_.c_str()); + globalInfoLogFunc(message_.c_str()); + // TODO: stone_console_info(message_.c_str()); } break; case InternalLevel_TRACE: if (globalTrace_) { - fprintf(stdout, "T: %s\n", message_.c_str()); + globalTraceLogFunc(message_.c_str()); } break; default: - fprintf(stderr, "Unknown log level (%d) for message: %s\n", level_, message_.c_str()); + { + std::stringstream ss; + ss << "Unknown log level (" << level_ << ") for message: " << message_; + auto s = ss.str(); + globalErrorLogFunc(s.c_str()); + } } } @@ -196,10 +279,21 @@ globalVerbose_ = enabled; } + bool IsInfoLevelEnabled() + { + return globalVerbose_; + } + void EnableTraceLevel(bool enabled) { globalTrace_ = enabled; } + + bool IsTraceLevelEnabled() + { + return globalTrace_; + } + } } @@ -388,7 +482,7 @@ { LoggingMementoImpl* memento = new LoggingMementoImpl(); - memento->valid_ = TRUE; + memento->valid_ = true; { boost::mutex::scoped_lock lock(loggingMutex_); memento->infoEnabled_ = loggingContext_->infoEnabled_; @@ -409,7 +503,7 @@ reinterpret_cast<LoggingMementoImpl*>(mementoPtr); if (!memento->valid_) throw std::runtime_error("Memento already used"); - memento->valid_ = FALSE; + memento->valid_ = false; { boost::mutex::scoped_lock lock(loggingMutex_); loggingContext_.reset(new LoggingContext); @@ -450,6 +544,14 @@ } } + bool IsInfoLevelEnable() + { + boost::mutex::scoped_lock lock(loggingMutex_); + assert(loggingContext_.get() != NULL); + + return loggingContext_->infoEnabled_; + } + void EnableTraceLevel(bool enabled) { boost::mutex::scoped_lock lock(loggingMutex_); @@ -464,6 +566,14 @@ } } + bool IsTraceLevelEnable() + { + boost::mutex::scoped_lock lock(loggingMutex_); + assert(loggingContext_.get() != NULL); + + return loggingContext_->traceEnabled_; + } + static void CheckFile(std::auto_ptr<std::ofstream>& f) { @@ -664,35 +774,6 @@ } } - - - - /* - struct FunctionCallingStream : std::ostream, std::streambuf - { - template<typename T> - FunctionCallingStream(T func) : std::ostream(this), func_(func) {} - - int overflow(int c) - { - if (c != '\n') - { - currentLine_ - } - else - { - func_(currentLine_.str().c_str()); - currentLine_.str(""); - currentLine_.clear(""); - } - return 0; - } - - private: - std::stringstream currentLine_; - }; - */ - void SetErrorWarnInfoLoggingStreams(std::ostream* errorStream, std::ostream* warningStream, std::ostream* infoStream)