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