Mercurial > hg > orthanc
comparison Core/Logging.cpp @ 3786:3801435e34a1 SylvainRouquette/fix-issue169-95b752c
integration Orthanc-1.6.0->SylvainRouquette
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Mar 2020 11:48:30 +0100 |
parents | 2a170a8f1faf |
children | 1f405a3fdeca |
comparison
equal
deleted
inserted
replaced
3785:763533d6dd67 | 3786:3801435e34a1 |
---|---|
1 /** | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
4 * Department, University Hospital of Liege, Belgium | 4 * Department, University Hospital of Liege, Belgium |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | 5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
6 * | 6 * |
7 * This program is free software: you can redistribute it and/or | 7 * This program is free software: you can redistribute it and/or |
8 * modify it under the terms of the GNU General Public License as | 8 * modify it under the terms of the GNU General Public License as |
9 * published by the Free Software Foundation, either version 3 of the | 9 * published by the Free Software Foundation, either version 3 of the |
10 * License, or (at your option) any later version. | 10 * License, or (at your option) any later version. |
313 /********************************************************* | 313 /********************************************************* |
314 * Internal logger of Orthanc, that mimics some | 314 * Internal logger of Orthanc, that mimics some |
315 * behavior from Google Log. | 315 * behavior from Google Log. |
316 *********************************************************/ | 316 *********************************************************/ |
317 | 317 |
318 #include "Compatibility.h" | |
318 #include "OrthancException.h" | 319 #include "OrthancException.h" |
319 #include "Enumerations.h" | 320 #include "Enumerations.h" |
320 #include "Toolbox.h" | 321 #include "Toolbox.h" |
321 | 322 |
322 #if ORTHANC_SANDBOXED == 1 | 323 #if ORTHANC_SANDBOXED == 1 |
342 | 343 |
343 std::ostream* error_; | 344 std::ostream* error_; |
344 std::ostream* warning_; | 345 std::ostream* warning_; |
345 std::ostream* info_; | 346 std::ostream* info_; |
346 | 347 |
347 std::auto_ptr<std::ofstream> file_; | 348 std::unique_ptr<std::ofstream> file_; |
348 | 349 |
349 LoggingContext() : | 350 LoggingContext() : |
350 infoEnabled_(false), | 351 infoEnabled_(false), |
351 traceEnabled_(false), | 352 traceEnabled_(false), |
352 error_(&std::cerr), | 353 error_(&std::cerr), |
370 }; | 371 }; |
371 } | 372 } |
372 | 373 |
373 | 374 |
374 | 375 |
375 static std::auto_ptr<LoggingContext> loggingContext_; | 376 static std::unique_ptr<LoggingContext> loggingContext_; |
376 static boost::mutex loggingMutex_; | 377 static boost::mutex loggingMutex_; |
377 | 378 |
378 | 379 |
379 | 380 |
380 namespace Orthanc | 381 namespace Orthanc |
422 log = (root / (programName + ".log" + suffix + "." + std::string(date))); | 423 log = (root / (programName + ".log" + suffix + "." + std::string(date))); |
423 link = (root / (programName + ".log" + suffix)); | 424 link = (root / (programName + ".log" + suffix)); |
424 } | 425 } |
425 | 426 |
426 | 427 |
427 static void PrepareLogFolder(std::auto_ptr<std::ofstream>& file, | 428 static void PrepareLogFolder(std::unique_ptr<std::ofstream>& file, |
428 const std::string& suffix, | 429 const std::string& suffix, |
429 const std::string& directory) | 430 const std::string& directory) |
430 { | 431 { |
431 boost::filesystem::path log, link; | 432 boost::filesystem::path log, link; |
432 GetLogPath(log, link, suffix, directory); | 433 GetLogPath(log, link, suffix, directory); |
453 } | 454 } |
454 | 455 |
455 void Reset() | 456 void Reset() |
456 { | 457 { |
457 // Recover the old logging context | 458 // Recover the old logging context |
458 std::auto_ptr<LoggingContext> old; | 459 std::unique_ptr<LoggingContext> old; |
459 | 460 |
460 { | 461 { |
461 boost::mutex::scoped_lock lock(loggingMutex_); | 462 boost::mutex::scoped_lock lock(loggingMutex_); |
462 if (loggingContext_.get() == NULL) | 463 if (loggingContext_.get() == NULL) |
463 { | 464 { |
464 return; | 465 return; |
465 } | 466 } |
466 else | 467 else |
467 { | 468 { |
468 old = loggingContext_; | 469 #if __cplusplus < 201103L |
470 old.reset(loggingContext_.release()); | |
471 #else | |
472 old = std::move(loggingContext_); | |
473 #endif | |
469 | 474 |
470 // Create a new logging context, | 475 // Create a new logging context, |
471 loggingContext_.reset(new LoggingContext); | 476 loggingContext_.reset(new LoggingContext); |
472 } | 477 } |
473 } | 478 } |
510 LoggingMementoImpl* memento = | 515 LoggingMementoImpl* memento = |
511 reinterpret_cast<LoggingMementoImpl*>(mementoPtr); | 516 reinterpret_cast<LoggingMementoImpl*>(mementoPtr); |
512 if (!memento->valid_) | 517 if (!memento->valid_) |
513 throw std::runtime_error("Memento already used"); | 518 throw std::runtime_error("Memento already used"); |
514 memento->valid_ = false; | 519 memento->valid_ = false; |
515 std::auto_ptr<LoggingMementoImpl> deleter(memento); | 520 std::unique_ptr<LoggingMementoImpl> deleter(memento); |
516 { | 521 { |
517 boost::mutex::scoped_lock lock(loggingMutex_); | 522 boost::mutex::scoped_lock lock(loggingMutex_); |
518 loggingContext_.reset(new LoggingContext); | 523 loggingContext_.reset(new LoggingContext); |
519 loggingContext_->error_ = memento->error_; | 524 loggingContext_->error_ = memento->error_; |
520 loggingContext_->warning_ = memento->warning_; | 525 loggingContext_->warning_ = memento->warning_; |
551 // Also disable the "TRACE" level when info-level debugging is disabled | 556 // Also disable the "TRACE" level when info-level debugging is disabled |
552 loggingContext_->traceEnabled_ = false; | 557 loggingContext_->traceEnabled_ = false; |
553 } | 558 } |
554 } | 559 } |
555 | 560 |
556 bool IsInfoLevelEnable() | 561 bool IsInfoLevelEnabled() |
557 { | 562 { |
558 boost::mutex::scoped_lock lock(loggingMutex_); | 563 boost::mutex::scoped_lock lock(loggingMutex_); |
559 assert(loggingContext_.get() != NULL); | 564 assert(loggingContext_.get() != NULL); |
560 | 565 |
561 return loggingContext_->infoEnabled_; | 566 return loggingContext_->infoEnabled_; |
573 // Also enable the "INFO" level when trace-level debugging is enabled | 578 // Also enable the "INFO" level when trace-level debugging is enabled |
574 loggingContext_->infoEnabled_ = true; | 579 loggingContext_->infoEnabled_ = true; |
575 } | 580 } |
576 } | 581 } |
577 | 582 |
578 bool IsTraceLevelEnable() | 583 bool IsTraceLevelEnabled() |
579 { | 584 { |
580 boost::mutex::scoped_lock lock(loggingMutex_); | 585 boost::mutex::scoped_lock lock(loggingMutex_); |
581 assert(loggingContext_.get() != NULL); | 586 assert(loggingContext_.get() != NULL); |
582 | 587 |
583 return loggingContext_->traceEnabled_; | 588 return loggingContext_->traceEnabled_; |
584 } | 589 } |
585 | 590 |
586 | 591 |
587 static void CheckFile(std::auto_ptr<std::ofstream>& f) | 592 static void CheckFile(std::unique_ptr<std::ofstream>& f) |
588 { | 593 { |
589 if (loggingContext_->file_.get() == NULL || | 594 if (loggingContext_->file_.get() == NULL || |
590 !loggingContext_->file_->is_open()) | 595 !loggingContext_->file_->is_open()) |
591 { | 596 { |
592 throw OrthancException(ErrorCode_CannotWriteFile); | 597 throw OrthancException(ErrorCode_CannotWriteFile); |
781 void SetErrorWarnInfoLoggingStreams(std::ostream* errorStream, | 786 void SetErrorWarnInfoLoggingStreams(std::ostream* errorStream, |
782 std::ostream* warningStream, | 787 std::ostream* warningStream, |
783 std::ostream* infoStream) | 788 std::ostream* infoStream) |
784 { | 789 { |
785 boost::mutex::scoped_lock lock(loggingMutex_); | 790 boost::mutex::scoped_lock lock(loggingMutex_); |
786 std::auto_ptr<LoggingContext> old = loggingContext_; | 791 std::unique_ptr<LoggingContext> old; |
792 | |
793 #if __cplusplus < 201103L | |
794 old.reset(loggingContext_.release()); | |
795 #else | |
796 old = std::move(loggingContext_); | |
797 #endif | |
798 | |
787 loggingContext_.reset(new LoggingContext); | 799 loggingContext_.reset(new LoggingContext); |
788 loggingContext_->error_ = errorStream; | 800 loggingContext_->error_ = errorStream; |
789 loggingContext_->warning_ = warningStream; | 801 loggingContext_->warning_ = warningStream; |
790 loggingContext_->info_ = infoStream; | 802 loggingContext_->info_ = infoStream; |
791 lock.unlock(); | 803 lock.unlock(); |
795 | 807 |
796 #ifdef __EMSCRIPTEN__ | 808 #ifdef __EMSCRIPTEN__ |
797 | 809 |
798 FuncStreamBuf<decltype(emscripten_console_error)> | 810 FuncStreamBuf<decltype(emscripten_console_error)> |
799 globalEmscriptenErrorStreamBuf(emscripten_console_error); | 811 globalEmscriptenErrorStreamBuf(emscripten_console_error); |
800 std::auto_ptr<std::ostream> globalEmscriptenErrorStream; | 812 std::unique_ptr<std::ostream> globalEmscriptenErrorStream; |
801 | 813 |
802 FuncStreamBuf<decltype(emscripten_console_warn)> | 814 FuncStreamBuf<decltype(emscripten_console_warn)> |
803 globalEmscriptenWarningStreamBuf(emscripten_console_warn); | 815 globalEmscriptenWarningStreamBuf(emscripten_console_warn); |
804 std::auto_ptr<std::ostream> globalEmscriptenWarningStream; | 816 std::unique_ptr<std::ostream> globalEmscriptenWarningStream; |
805 | 817 |
806 FuncStreamBuf<decltype(emscripten_console_log)> | 818 FuncStreamBuf<decltype(emscripten_console_log)> |
807 globalEmscriptenInfoStreamBuf(emscripten_console_log); | 819 globalEmscriptenInfoStreamBuf(emscripten_console_log); |
808 std::auto_ptr<std::ostream> globalEmscriptenInfoStream; | 820 std::unique_ptr<std::ostream> globalEmscriptenInfoStream; |
809 | 821 |
810 void EnableEmscriptenLogging() | 822 void EnableEmscriptenLogging() |
811 { | 823 { |
812 globalEmscriptenErrorStream.reset(new ostream( | 824 globalEmscriptenErrorStream.reset(new ostream( |
813 &globalEmscriptenErrorStreamBuf)); | 825 &globalEmscriptenErrorStreamBuf)); |