Mercurial > hg > orthanc
comparison Core/Logging.cpp @ 4006:55710d73780f
reorganizing Logging.cpp
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 06 Jun 2020 11:21:39 +0200 |
parents | 7f9909062d9c |
children | f0ee3f1db775 |
comparison
equal
deleted
inserted
replaced
4005:7f9909062d9c | 4006:55710d73780f |
---|---|
529 | 529 |
530 file.reset(new std::ofstream(log.string().c_str())); | 530 file.reset(new std::ofstream(log.string().c_str())); |
531 } | 531 } |
532 | 532 |
533 | 533 |
534 static void CheckFile(std::unique_ptr<std::ofstream>& f) | |
535 { | |
536 if (loggingContext_->file_.get() == NULL || | |
537 !loggingContext_->file_->is_open()) | |
538 { | |
539 throw OrthancException(ErrorCode_CannotWriteFile); | |
540 } | |
541 } | |
542 | |
543 | |
544 static void GetLinePrefix(std::string& prefix, | |
545 LogLevel level, | |
546 const char* file, | |
547 int line) | |
548 { | |
549 boost::filesystem::path path(file); | |
550 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); | |
551 boost::posix_time::time_duration duration = now.time_of_day(); | |
552 | |
553 /** | |
554 From Google Log documentation: | |
555 | |
556 "Log lines have this form: | |
557 | |
558 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... | |
559 | |
560 where the fields are defined as follows: | |
561 | |
562 L A single character, representing the log level (eg 'I' for INFO) | |
563 mm The month (zero padded; ie May is '05') | |
564 dd The day (zero padded) | |
565 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds | |
566 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) | |
567 file The file name | |
568 line The line number | |
569 msg The user-supplied message" | |
570 | |
571 In this implementation, "threadid" is not printed. | |
572 **/ | |
573 | |
574 char c; | |
575 switch (level) | |
576 { | |
577 case LogLevel_ERROR: | |
578 c = 'E'; | |
579 break; | |
580 | |
581 case LogLevel_WARNING: | |
582 c = 'W'; | |
583 break; | |
584 | |
585 case LogLevel_INFO: | |
586 c = 'I'; | |
587 break; | |
588 | |
589 case LogLevel_TRACE: | |
590 c = 'T'; | |
591 break; | |
592 | |
593 default: | |
594 throw OrthancException(ErrorCode_InternalError); | |
595 } | |
596 | |
597 char date[64]; | |
598 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", | |
599 c, | |
600 now.date().month().as_number(), | |
601 now.date().day().as_number(), | |
602 static_cast<int>(duration.hours()), | |
603 static_cast<int>(duration.minutes()), | |
604 static_cast<int>(duration.seconds()), | |
605 static_cast<int>(duration.fractional_seconds())); | |
606 | |
607 prefix = (std::string(date) + path.filename().string() + ":" + | |
608 boost::lexical_cast<std::string>(line) + "] "); | |
609 } | |
610 | |
611 | |
534 void Initialize() | 612 void Initialize() |
535 { | 613 { |
536 boost::mutex::scoped_lock lock(loggingMutex_); | 614 boost::mutex::scoped_lock lock(loggingMutex_); |
537 loggingContext_.reset(new LoggingContext); | 615 loggingContext_.reset(new LoggingContext); |
538 } | 616 } |
624 | 702 |
625 return loggingContext_->traceEnabled_; | 703 return loggingContext_->traceEnabled_; |
626 } | 704 } |
627 | 705 |
628 | 706 |
629 static void CheckFile(std::unique_ptr<std::ofstream>& f) | |
630 { | |
631 if (loggingContext_->file_.get() == NULL || | |
632 !loggingContext_->file_->is_open()) | |
633 { | |
634 throw OrthancException(ErrorCode_CannotWriteFile); | |
635 } | |
636 } | |
637 | |
638 void SetTargetFolder(const std::string& path) | 707 void SetTargetFolder(const std::string& path) |
639 { | 708 { |
640 boost::mutex::scoped_lock lock(loggingMutex_); | 709 boost::mutex::scoped_lock lock(loggingMutex_); |
641 assert(loggingContext_.get() != NULL); | 710 assert(loggingContext_.get() != NULL); |
642 | 711 |
688 // the mutex to speed-up things. The stream is set to "/dev/null" | 757 // the mutex to speed-up things. The stream is set to "/dev/null" |
689 lock_.unlock(); | 758 lock_.unlock(); |
690 return; | 759 return; |
691 } | 760 } |
692 | 761 |
693 // Compute the header of the line, temporary release the lock as | 762 // Compute the prefix of the line, temporary release the lock as |
694 // this is a time-consuming operation | 763 // this is a time-consuming operation |
695 lock_.unlock(); | 764 lock_.unlock(); |
696 std::string header; | 765 std::string prefix; |
697 | 766 GetLinePrefix(prefix, level, file, line); |
698 { | 767 |
699 boost::filesystem::path path(file); | 768 // The prefix is computed, we now re-lock the mutex to access |
700 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); | |
701 boost::posix_time::time_duration duration = now.time_of_day(); | |
702 | |
703 /** | |
704 From Google Log documentation: | |
705 | |
706 "Log lines have this form: | |
707 | |
708 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... | |
709 | |
710 where the fields are defined as follows: | |
711 | |
712 L A single character, representing the log level (eg 'I' for INFO) | |
713 mm The month (zero padded; ie May is '05') | |
714 dd The day (zero padded) | |
715 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds | |
716 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) | |
717 file The file name | |
718 line The line number | |
719 msg The user-supplied message" | |
720 | |
721 In this implementation, "threadid" is not printed. | |
722 **/ | |
723 | |
724 char prefix; | |
725 switch (level) | |
726 { | |
727 case LogLevel_ERROR: | |
728 prefix = 'E'; | |
729 break; | |
730 | |
731 case LogLevel_WARNING: | |
732 prefix = 'W'; | |
733 break; | |
734 | |
735 case LogLevel_INFO: | |
736 prefix = 'I'; | |
737 break; | |
738 | |
739 case LogLevel_TRACE: | |
740 prefix = 'T'; | |
741 break; | |
742 | |
743 default: | |
744 throw OrthancException(ErrorCode_InternalError); | |
745 } | |
746 | |
747 char date[64]; | |
748 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", | |
749 prefix, | |
750 now.date().month().as_number(), | |
751 now.date().day().as_number(), | |
752 static_cast<int>(duration.hours()), | |
753 static_cast<int>(duration.minutes()), | |
754 static_cast<int>(duration.seconds()), | |
755 static_cast<int>(duration.fractional_seconds())); | |
756 | |
757 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] "; | |
758 } | |
759 | |
760 | |
761 // The header is computed, we now re-lock the mutex to access | |
762 // the stream objects. Pay attention that "loggingContext_", | 769 // the stream objects. Pay attention that "loggingContext_", |
763 // "infoEnabled_" or "traceEnabled_" might have changed while | 770 // "infoEnabled_" or "traceEnabled_" might have changed while |
764 // the mutex was unlocked. | 771 // the mutex was unlocked. |
765 lock_.lock(); | 772 lock_.lock(); |
766 | 773 |
806 // "null_" member of this object, so we can release the global | 813 // "null_" member of this object, so we can release the global |
807 // mutex. | 814 // mutex. |
808 lock_.unlock(); | 815 lock_.unlock(); |
809 } | 816 } |
810 | 817 |
811 (*stream_) << header; | 818 (*stream_) << prefix; |
812 } | 819 } |
813 catch (...) | 820 catch (...) |
814 { | 821 { |
815 // Something is going really wrong, probably running out of | 822 // Something is going really wrong, probably running out of |
816 // memory. Fallback to a degraded mode. | 823 // memory. Fallback to a degraded mode. |