1 /*
2  *
3  *  Copyright (C) 2009-2019, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module:  oflog
15  *
16  *  Author:  Uli Schlachter
17  *
18  *  Purpose: Simplify the usage of log4cplus to other modules (Header)
19  *
20  */
21 
22 
23 #ifndef OFLOG_H
24 #define OFLOG_H
25 
26 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
27 
28 #include "dcmtk/oflog/logger.h"
29 #include "dcmtk/oflog/logmacro.h"
30 
31 #define OFLOG_TRACE(logger, msg) DCMTK_LOG4CPLUS_TRACE(logger, msg)
32 #define OFLOG_DEBUG(logger, msg) DCMTK_LOG4CPLUS_DEBUG(logger, msg)
33 #define OFLOG_INFO(logger, msg)  DCMTK_LOG4CPLUS_INFO(logger, msg)
34 #define OFLOG_WARN(logger, msg)  DCMTK_LOG4CPLUS_WARN(logger, msg)
35 #define OFLOG_ERROR(logger, msg) DCMTK_LOG4CPLUS_ERROR(logger, msg)
36 #define OFLOG_FATAL(logger, msg) DCMTK_LOG4CPLUS_FATAL(logger, msg)
37 
38 class OFCommandLine;
39 class OFConsoleApplication;
40 
41 /** simple wrapper around the "low-level" Logger object to make it easier to
42  *  switch to a different system
43  */
44 class DCMTK_LOG4CPLUS_EXPORT OFLogger : public dcmtk::log4cplus::Logger
45 {
46 public:
47     /** copy constructor
48      *  @param base object to be copied
49      */
50     OFLogger(const dcmtk::log4cplus::Logger &base);
51 
52     /// these are the log levels that you can feed to isEnabledFor()
53     enum LogLevel {
54         /// trace: output more details on the internal application state, a kind of "verbose debug"
55         TRACE_LOG_LEVEL = dcmtk::log4cplus::TRACE_LOG_LEVEL,
56         /// debug: fine-grained informational events that are most useful to debug an application
57         DEBUG_LOG_LEVEL = dcmtk::log4cplus::DEBUG_LOG_LEVEL,
58         /// info: informational messages that highlight the progress of the application at coarse-grained level
59         INFO_LOG_LEVEL  = dcmtk::log4cplus::INFO_LOG_LEVEL,
60         /// warn: potentially harmful situations
61         WARN_LOG_LEVEL  = dcmtk::log4cplus::WARN_LOG_LEVEL,
62         /// error: events that might still allow the application to continue running
63         ERROR_LOG_LEVEL = dcmtk::log4cplus::ERROR_LOG_LEVEL,
64         /// fatal: very severe error events that will presumably lead the application to abort
65         FATAL_LOG_LEVEL = dcmtk::log4cplus::FATAL_LOG_LEVEL,
66         /// internal: turn off logging completely
67         OFF_LOG_LEVEL = dcmtk::log4cplus::OFF_LOG_LEVEL
68     };
69 
70     /** Get the logger's log level.
71      *  One of the checks that isEnabledFor() does looks like this:
72      *    if (getChainedLogLevel() < level)
73      *        return false;
74      *  @return the log level to which this logger is set.
75      */
getChainedLogLevel()76     LogLevel getChainedLogLevel() const {
77         return OFstatic_cast(LogLevel, Logger::getChainedLogLevel());
78     }
79 };
80 
81 /** functions for initializing the logging system
82  */
83 class DCMTK_LOG4CPLUS_EXPORT OFLog
84 {
85  private:
86 
87     /** private constructor, don't create instances of this class
88      */
OFLog()89     OFLog() { }
90 
91     /** set up the logging and enable it
92      *  @param level the verbosity that you want
93      */
94     static void configureLogger(dcmtk::log4cplus::LogLevel level);
95 
96  public:
97 
98     /** create a new logger object.
99      *  Logger objects have a reference counting copy-constructor, so returning by-value is cheap.
100      *  @param name the name of the logger
101      *  @return requested logger object
102      */
103     static OFLogger getLogger(const char *name);
104 
105     /** set up the logging and enable it
106      *  @param level the verbosity that you want
107      */
108     static void configure(OFLogger::LogLevel level = OFLogger::WARN_LOG_LEVEL);
109 
110     /** handle the command line options used for logging
111      *  @param cmd the command line whose options are handled
112      *  @param app the console application which is used for console output and error checking
113      *  @param defaultLevel default log level that is used if not specified on the command line
114      */
115     static void configureFromCommandLine(OFCommandLine &cmd,
116                                          OFConsoleApplication &app,
117                                          OFLogger::LogLevel defaultLevel = OFLogger::WARN_LOG_LEVEL);
118 
119     /** add the command line options which configureFromCommandLine() checks for
120      *  @param cmd the command line to which these options should be added
121      */
122     static void addOptions(OFCommandLine &cmd);
123 
124     /** if there was a config file loaded in configureFromCommandLine(), the
125      *  in-memory copy will be parsed again, but all variables will be updated
126      *  first (current time, process id, ....)
127      *  @param cmd command line from which the application name will be retrieved
128      */
129     static void reconfigure(OFCommandLine *cmd = NULL);
130 
131  private:
132 
133     /// If we loaded a config file in configureFromCommandLine(), this is it
134     static OFunique_ptr<dcmtk::log4cplus::helpers::Properties> configProperties_;
135 };
136 
137 #endif
138