Mercurial > hg > orthanc
comparison Core/Logging.cpp @ 3353:54cdad5a7228 emscripten-logging
Added a public function that will use emscripten-specific logging functions when using its SDK + scaffolding work. Build and UT OK on msvc15 x64. Not actually tested under *nix or emscripten yet
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 23 Apr 2019 21:40:57 +0200 |
parents | 4e43e67f8ecf |
children | 815b81142ff7 |
comparison
equal
deleted
inserted
replaced
3350:ba051f674f4b | 3353:54cdad5a7228 |
---|---|
251 warning_(&std::cerr), | 251 warning_(&std::cerr), |
252 info_(&std::cerr) | 252 info_(&std::cerr) |
253 { | 253 { |
254 } | 254 } |
255 }; | 255 }; |
256 | |
257 struct LoggingMementoImpl | |
258 { | |
259 bool valid_; | |
260 bool infoEnabled_; | |
261 bool traceEnabled_; | |
262 std::string targetFile_; | |
263 std::string targetFolder_; | |
264 | |
265 std::ostream* error_; | |
266 std::ostream* warning_; | |
267 std::ostream* info_; | |
268 }; | |
256 } | 269 } |
257 | 270 |
258 | 271 |
259 | 272 |
260 static std::auto_ptr<LoggingContext> loggingContext_; | 273 static std::auto_ptr<LoggingContext> loggingContext_; |
368 { | 381 { |
369 SetTargetFile(old->targetFile_); | 382 SetTargetFile(old->targetFile_); |
370 } | 383 } |
371 } | 384 } |
372 | 385 |
386 | |
387 LoggingMemento CreateLoggingMemento() | |
388 { | |
389 LoggingMementoImpl* memento = new LoggingMementoImpl(); | |
390 | |
391 memento->valid_ = TRUE; | |
392 { | |
393 boost::mutex::scoped_lock lock(loggingMutex_); | |
394 memento->infoEnabled_ = loggingContext_->infoEnabled_; | |
395 memento->traceEnabled_ = loggingContext_->traceEnabled_; | |
396 memento->targetFile_ = loggingContext_->targetFile_; | |
397 memento->targetFolder_ = loggingContext_->targetFolder_; | |
398 | |
399 memento->error_ = loggingContext_->error_; | |
400 memento->warning_ = loggingContext_->warning_; | |
401 memento->info_ = loggingContext_->info_; | |
402 } | |
403 return reinterpret_cast<void*>(memento); | |
404 } | |
405 | |
406 void RestoreLoggingMemento(LoggingMemento mementoPtr) | |
407 { | |
408 LoggingMementoImpl* memento = | |
409 reinterpret_cast<LoggingMementoImpl*>(mementoPtr); | |
410 if (!memento->valid_) | |
411 throw std::runtime_error("Memento already used"); | |
412 memento->valid_ = FALSE; | |
413 { | |
414 boost::mutex::scoped_lock lock(loggingMutex_); | |
415 loggingContext_.reset(new LoggingContext); | |
416 loggingContext_->error_ = memento->error_; | |
417 loggingContext_->warning_ = memento->warning_; | |
418 loggingContext_->info_ = memento->info_; | |
419 } | |
420 EnableInfoLevel(memento->infoEnabled_); | |
421 EnableTraceLevel(memento->traceEnabled_); | |
422 if (!memento->targetFolder_.empty()) | |
423 { | |
424 SetTargetFolder(memento->targetFolder_); | |
425 } | |
426 else if (!memento->targetFile_.empty()) | |
427 { | |
428 SetTargetFile(memento->targetFile_); | |
429 } | |
430 } | |
431 | |
432 void DiscardLoggingMemento(LoggingMemento mementoPtr) | |
433 { | |
434 LoggingMementoImpl* memento = | |
435 reinterpret_cast<LoggingMementoImpl*>(mementoPtr); | |
436 delete memento; | |
437 } | |
438 | |
373 void EnableInfoLevel(bool enabled) | 439 void EnableInfoLevel(bool enabled) |
374 { | 440 { |
375 boost::mutex::scoped_lock lock(loggingMutex_); | 441 boost::mutex::scoped_lock lock(loggingMutex_); |
376 assert(loggingContext_.get() != NULL); | 442 assert(loggingContext_.get() != NULL); |
377 | 443 |
595 loggingContext_->file_.get() != NULL) | 661 loggingContext_->file_.get() != NULL) |
596 { | 662 { |
597 loggingContext_->file_->flush(); | 663 loggingContext_->file_->flush(); |
598 } | 664 } |
599 } | 665 } |
666 | |
667 | |
668 | |
669 | |
670 /* | |
671 struct FunctionCallingStream : std::ostream, std::streambuf | |
672 { | |
673 template<typename T> | |
674 FunctionCallingStream(T func) : std::ostream(this), func_(func) {} | |
675 | |
676 int overflow(int c) | |
677 { | |
678 if (c != '\n') | |
679 { | |
680 currentLine_ | |
681 } | |
682 else | |
683 { | |
684 func_(currentLine_.str().c_str()); | |
685 currentLine_.str(""); | |
686 currentLine_.clear(""); | |
687 } | |
688 return 0; | |
689 } | |
690 | |
691 private: | |
692 std::stringstream currentLine_; | |
693 }; | |
694 */ | |
695 | |
696 void SetErrorWarnInfoLoggingStreams(std::ostream* errorStream, | |
697 std::ostream* warningStream, | |
698 std::ostream* infoStream) | |
699 { | |
700 boost::mutex::scoped_lock lock(loggingMutex_); | |
701 std::auto_ptr<LoggingContext> old = loggingContext_; | |
702 loggingContext_.reset(new LoggingContext); | |
703 loggingContext_->error_ = errorStream; | |
704 loggingContext_->warning_ = warningStream; | |
705 loggingContext_->info_ = infoStream; | |
706 lock.unlock(); | |
707 EnableInfoLevel(old->infoEnabled_); | |
708 EnableTraceLevel(old->traceEnabled_); | |
709 } | |
710 | |
711 #ifdef __EMSCRIPTEN__ | |
712 | |
713 FuncStreamBuf<decltype(emscripten_console_error)> | |
714 globalEmscriptenErrorStreamBuf(emscripten_console_error); | |
715 std::auto_ptr<std::ostream> globalEmscriptenErrorStream; | |
716 | |
717 FuncStreamBuf<decltype(emscripten_console_warn)> | |
718 globalEmscriptenWarningStreamBuf(emscripten_console_warn); | |
719 std::auto_ptr<std::ostream> globalEmscriptenWarningStream; | |
720 | |
721 FuncStreamBuf<decltype(emscripten_console_log)> | |
722 globalEmscriptenInfoStreamBuf(emscripten_console_log); | |
723 std::auto_ptr<std::ostream> globalEmscriptenInfoStream; | |
724 | |
725 void EnableEmscriptenLogging() | |
726 { | |
727 globalEmscriptenErrorStream.reset(new ostream( | |
728 &globalEmscriptenErrorStreamBuf)); | |
729 | |
730 globalEmscriptenWarningStream.reset(new ostream( | |
731 &globalEmscriptenWarningStreamBuf)); | |
732 | |
733 globalEmscriptenInfoStream.reset(new ostream( | |
734 &globalEmscriptenInfoStreamBuf)); | |
735 | |
736 SetErrorWarnInfoLoggingStreams( | |
737 &globalEmscriptenErrorStream, | |
738 &globalEmscriptenWarningStream | |
739 &globalEmscriptenInfoStream); | |
740 } | |
741 #endif | |
600 } | 742 } |
601 } | 743 } |
602 | 744 |
745 | |
603 #endif // ORTHANC_ENABLE_LOGGING | 746 #endif // ORTHANC_ENABLE_LOGGING |