ITK  5.2.0
Insight Toolkit
itkLoggerBase.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 #include "ITKCommonExport.h"
24 #ifdef DEBUG
25 # undef DEBUG // HDF5 publicly exports this define when built in debug mode
26 // That messes up the DEBUG enumeration in PriorityLevelEnum.
27 #endif
28 
29 namespace itk
30 {
31 /***\class LoggerBaseEnums
32  * \brief Contains all enum classes used by LoggerBase class.
33  * \ingroup ITKCommon
34  */
36 {
37 public:
43  enum class PriorityLevel : uint8_t
44  {
45  MUSTFLUSH = 0,
46  FATAL,
47  CRITICAL,
48  WARNING,
49  INFO,
50  DEBUG,
51  NOTSET
52  };
53 
57  enum class TimeStampFormat : uint8_t
58  {
59  REALVALUE = 0,
60  HUMANREADABLE = 1
61  };
62 };
63 
64 // Define how to print enumeration
65 extern ITKCommon_EXPORT std::ostream &
66  operator<<(std::ostream & out, const LoggerBaseEnums::PriorityLevel value);
67 extern ITKCommon_EXPORT std::ostream &
68  operator<<(std::ostream & out, const LoggerBaseEnums::TimeStampFormat value);
69 
81 class ITKCommon_EXPORT LoggerBase : public Object
82 {
83 public:
84  using Self = LoggerBase;
85  using Superclass = Object;
88 
90  itkTypeMacro(LoggerBase, Object);
91 
93 
95 #if !defined(ITK_LEGACY_REMOVE)
96  // We need to expose the enum values at the class level
97  // for backwards compatibility
98  static constexpr PriorityLevelEnum MUSTFLUSH = PriorityLevelEnum::MUSTFLUSH;
99  static constexpr PriorityLevelEnum FATAL = PriorityLevelEnum::FATAL;
100  static constexpr PriorityLevelEnum CRITICAL = PriorityLevelEnum::CRITICAL;
101  static constexpr PriorityLevelEnum WARNING = PriorityLevelEnum::WARNING;
102  static constexpr PriorityLevelEnum INFO = PriorityLevelEnum::INFO;
103  static constexpr PriorityLevelEnum DEBUG = PriorityLevelEnum::DEBUG;
104  static constexpr PriorityLevelEnum NOTSET = PriorityLevelEnum::NOTSET;
105 #endif
106 
107  itkSetStringMacro(Name);
108  itkGetStringMacro(Name);
109 
111 #if !defined(ITK_LEGACY_REMOVE)
112  // We need to expose the enum values at the class level
113  // for backwards compatibility
114  static constexpr TimeStampFormatEnum REALVALUE = TimeStampFormatEnum::REALVALUE;
115  static constexpr TimeStampFormatEnum HUMANREADABLE = TimeStampFormatEnum::HUMANREADABLE;
116 #endif
117 
127  itkSetEnumMacro(TimeStampFormat, TimeStampFormatEnum);
128  itkGetConstReferenceMacro(TimeStampFormat, TimeStampFormatEnum);
130 
139  itkSetStringMacro(HumanReadableFormat);
140  itkGetStringMacro(HumanReadableFormat);
142 
144  virtual std::string
145  BuildFormattedEntry(PriorityLevelEnum level, std::string const & content);
146 
150  virtual void
152  {
153  m_PriorityLevel = level;
154  }
155 
159  virtual PriorityLevelEnum
161  {
162  return m_PriorityLevel;
163  }
164 
165  virtual void
167  {
168  m_LevelForFlushing = level;
169  }
170 
171  virtual PriorityLevelEnum
173  {
174  return m_LevelForFlushing;
175  }
176 
178  virtual void
179  AddLogOutput(OutputType * output);
180 
181  virtual void
182  Write(PriorityLevelEnum level, std::string const & content);
183 
185  void
186  Debug(std::string const & message)
187  {
188  this->Write(LoggerBase::PriorityLevelEnum::DEBUG, message);
189  }
190 
191  void
192  Info(std::string const & message)
193  {
194  this->Write(LoggerBase::PriorityLevelEnum::INFO, message);
195  }
196 
197  void
198  Warning(std::string const & message)
199  {
200  this->Write(LoggerBase::PriorityLevelEnum::WARNING, message);
201  }
202 
203  void
204  Critical(std::string const & message)
205  {
206  this->Write(LoggerBase::PriorityLevelEnum::CRITICAL, message);
207  }
208 
209  void
210  Error(std::string const & message)
211  {
212  this->Write(LoggerBase::PriorityLevelEnum::CRITICAL, message);
213  }
214 
215  void
216  Fatal(std::string const & message)
217  {
218  this->Write(LoggerBase::PriorityLevelEnum::FATAL, message);
219  }
220 
221  virtual void
222  Flush();
223 
224 protected:
226  LoggerBase();
227 
229  ~LoggerBase() override;
230 
232  void
233  PrintSelf(std::ostream & os, Indent indent) const override;
234 
235 protected:
237 
239 
241 
243 
245 
247 
248 private:
249  std::string m_Name;
250 }; // class LoggerBase
251 } // namespace itk
252 
253 #endif // itkLoggerBase_h
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
TimeStampFormat
itk::LoggerBaseEnums::TimeStampFormat
TimeStampFormat
Definition: itkLoggerBase.h:57
itk::LoggerBase::Fatal
void Fatal(std::string const &message)
Definition: itkLoggerBase.h:216
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
itk::LoggerBaseEnums
Definition: itkLoggerBase.h:35
itk::LoggerBase::m_HumanReadableFormat
std::string m_HumanReadableFormat
Definition: itkLoggerBase.h:246
itk::LoggerBaseEnums::TimeStampFormat::HUMANREADABLE
itk::LoggerBaseEnums::PriorityLevel
PriorityLevel
Definition: itkLoggerBase.h:43
itk::LoggerBaseEnums::PriorityLevel::FATAL
itk::LoggerBase::GetLevelForFlushing
virtual PriorityLevelEnum GetLevelForFlushing() const
Definition: itkLoggerBase.h:172
itk::LoggerBase::GetPriorityLevel
virtual PriorityLevelEnum GetPriorityLevel() const
Definition: itkLoggerBase.h:160
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LoggerBase::Critical
void Critical(std::string const &message)
Definition: itkLoggerBase.h:204
itk::LoggerBaseEnums::PriorityLevel::CRITICAL
itk::LoggerBase::m_Clock
RealTimeClock::Pointer m_Clock
Definition: itkLoggerBase.h:242
itkRealTimeClock.h
itk::LoggerBaseEnums::PriorityLevel::DEBUG
itk::LoggerBase::m_Name
std::string m_Name
Definition: itkLoggerBase.h:249
itk::LoggerBase::m_Output
MultipleLogOutput::Pointer m_Output
Definition: itkLoggerBase.h:240
itk::LoggerBase::Info
void Info(std::string const &message)
Definition: itkLoggerBase.h:192
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::LoggerBase::SetLevelForFlushing
virtual void SetLevelForFlushing(PriorityLevelEnum level)
Definition: itkLoggerBase.h:166
PriorityLevel
itk::LogOutput
Represents an output stream.
Definition: itkLogOutput.h:39
itk::LoggerBase::m_PriorityLevel
PriorityLevelEnum m_PriorityLevel
Definition: itkLoggerBase.h:236
itk::LoggerBase::m_TimeStampFormat
TimeStampFormatEnum m_TimeStampFormat
Definition: itkLoggerBase.h:244
itk::LoggerBase::SetPriorityLevel
virtual void SetPriorityLevel(PriorityLevelEnum level)
Definition: itkLoggerBase.h:151
itk::LoggerBase::Debug
void Debug(std::string const &message)
Definition: itkLoggerBase.h:186
itk::MultipleLogOutput::OutputType
LogOutput OutputType
Definition: itkMultipleLogOutput.h:50
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::LoggerBase::Error
void Error(std::string const &message)
Definition: itkLoggerBase.h:210
itkMultipleLogOutput.h
itk::LoggerBase::m_LevelForFlushing
PriorityLevelEnum m_LevelForFlushing
Definition: itkLoggerBase.h:238
itk::LoggerBaseEnums::PriorityLevel::MUSTFLUSH
itk::LoggerBaseEnums::TimeStampFormat::REALVALUE
itk::LoggerBase::Warning
void Warning(std::string const &message)
Definition: itkLoggerBase.h:198
itk::LoggerBaseEnums::PriorityLevel::NOTSET
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::LoggerBase
Used for logging information during a run.
Definition: itkLoggerBase.h:81
itk::LoggerBaseEnums::PriorityLevel::INFO
itk::LoggerBaseEnums::PriorityLevel::WARNING