1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkLoggerBase_h
19 #define itkLoggerBase_h
20 
21 #include "itkMultipleLogOutput.h"
22 #include "itkRealTimeClock.h"
23 
24 #ifdef DEBUG
25 #undef DEBUG // HDF5 publicly exports this define when built in debug mode
26 // That messes up the DEBUG enumeration in PriorityLevelType.
27 #endif
28 
29 namespace itk
30 {
31 /** \class LoggerBase
32  *  \brief Used for logging information during a run.
33  *
34  * \author Hee-Su Kim, Compute Science Dept. Kyungpook National University,
35  *                     ISIS Center, Georgetown University.
36  *
37  *
38  * \ingroup OSSystemObjects LoggingObjects
39  * \ingroup ITKCommon
40  */
41 
42 class ITKCommon_EXPORT LoggerBase:public Object
43 {
44 public:
45 
46   using Self = LoggerBase;
47   using Superclass = Object;
48   using Pointer = SmartPointer< Self >;
49   using ConstPointer = SmartPointer< const Self >;
50 
51   /** Run-time type information (and related methods). */
52   itkTypeMacro(LoggerBase, Object);
53 
54   using OutputType = MultipleLogOutput::OutputType;
55 
56   /** Definition of types of messages. These codes will be used to regulate
57     * the level of detail of messages reported to the final outputs */
58   typedef enum {
59     MUSTFLUSH = 0,
60     FATAL,
61     CRITICAL,
62     WARNING,
63     INFO,
64     DEBUG,
65     NOTSET
66     } PriorityLevelType;
67 
68   itkSetStringMacro(Name);
69   itkGetStringMacro(Name);
70 
71   /** Select the type of format for reporting time stamps */
72   typedef enum {
73     REALVALUE = 0,
74     HUMANREADABLE
75     } TimeStampFormatType;
76 
77   /** Set/Get the type of format used for reporting the time stamp of a given
78    * log message. The main options are REALVALUE and HUMANREADABLE.
79    * REALVALUE will report the time in seconds as a double number.
80    * HUMANREADABLE will report the time in formatted text such as '2007 May 7
81    * 09:23:14'
82    *
83    * \sa SetHumanReadableFormat()
84    *
85    */
86   itkSetMacro(TimeStampFormat, TimeStampFormatType);
87   itkGetConstReferenceMacro(TimeStampFormat, TimeStampFormatType);
88 
89   /** Set/Get the specific text format to use when the time stamp format type
90    * is set to HUMANREADABLE. For a description of the acceptable formats
91    * please look at the man page of the strftime() method. The default is set
92    * to  "%Y %b %d %H:%M:%S"
93    *
94    * \sa SetTimeStampFormat
95    *
96    */
97   itkSetStringMacro(HumanReadableFormat);
98   itkGetStringMacro(HumanReadableFormat);
99 
100   /** Provides a default formatted log entry */
101   virtual std::string BuildFormattedEntry(PriorityLevelType level,
102                                           std::string const & content);
103 
104   /** Set the priority level for the current logger. Only messages that have
105     * priorities equal or greater than the one set here will be posted to the
106     * current outputs */
SetPriorityLevel(PriorityLevelType level)107   virtual void SetPriorityLevel(PriorityLevelType level)
108   {
109     m_PriorityLevel = level;
110   }
111 
112   /** Get the priority level for the current logger. Only messages that have
113    * priorities equal or greater than the one set here will be posted to the
114    * current outputs */
GetPriorityLevel()115   virtual PriorityLevelType GetPriorityLevel() const
116   {
117     return m_PriorityLevel;
118   }
119 
SetLevelForFlushing(PriorityLevelType level)120   virtual void SetLevelForFlushing(PriorityLevelType level)
121   {
122     m_LevelForFlushing = level;
123   }
124 
GetLevelForFlushing()125   virtual PriorityLevelType GetLevelForFlushing() const
126   {
127     return m_LevelForFlushing;
128   }
129 
130   /** Registers another output stream with the multiple output. */
131   virtual void AddLogOutput(OutputType *output);
132 
133   virtual void Write(PriorityLevelType level, std::string const & content);
134 
135   /** Helper methods */
Debug(std::string const & message)136   void Debug(std::string const & message)
137   {
138     this->Write (LoggerBase::DEBUG, message);
139   }
140 
Info(std::string const & message)141   void Info(std::string const & message)
142   {
143     this->Write (LoggerBase::INFO, message);
144   }
145 
Warning(std::string const & message)146   void Warning(std::string const & message)
147   {
148     this->Write (LoggerBase::WARNING, message);
149   }
150 
Critical(std::string const & message)151   void Critical(std::string const & message)
152   {
153     this->Write (LoggerBase::CRITICAL, message);
154   }
155 
Error(std::string const & message)156   void Error(std::string const & message)
157   {
158     this->Write (LoggerBase::CRITICAL, message);
159   }
160 
Fatal(std::string const & message)161   void Fatal(std::string const & message)
162   {
163     this->Write (LoggerBase::FATAL, message);
164   }
165 
166   virtual void Flush();
167 
168 protected:
169 
170   /** Constructor */
171   LoggerBase();
172 
173   /** Destructor */
174   ~LoggerBase() override;
175 
176   /** Print contents of a LoggerBase */
177   void PrintSelf(std::ostream & os, Indent indent) const override;
178 
179 protected:
180 
181   PriorityLevelType m_PriorityLevel;
182 
183   PriorityLevelType m_LevelForFlushing;
184 
185   MultipleLogOutput::Pointer m_Output;
186 
187   RealTimeClock::Pointer m_Clock;
188 
189   TimeStampFormatType m_TimeStampFormat;
190 
191   std::string m_HumanReadableFormat;
192 
193 private:
194 
195   std::string m_Name;
196 };  // class LoggerBase
197 } // namespace itk
198 
199 #endif  // itkLoggerBase_h
200