Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 dhylands 1
/****************************************************************************
2
*
3
*     Copyright (c) 2003 Dave Hylands
4
*           All Rights Reserved
5
*
6
*   Permission is granted to any individual or institution to use, copy, or
7
*   redistribute this software so long as it is not sold for profit, and that
8
*   this copyright notice is retained.
9
*
10
****************************************************************************/
11
/**
12
*
13
*   @file   LogMessage.h
14
*
15
*   @brief  Implements a mechanism for logging messages
16
*
17
****************************************************************************/
18
/**
19
*   @defgroup   Log Message Logging
20
*
21
*   @brief      Provides classes and macros to encapsulate logging.
22
*
23
****************************************************************************/
24
 
25
#if !defined( LOGMESSAGE_H )
26
#define LOGMESSAGE_H                   ///< Include Guard
27
 
28
// ---- Include Files -------------------------------------------------------
29
 
30
#include <sstream>
31
#include <iomanip>
32
 
33
/**
34
 * @addtogroup Log
35
 * @{
36
 */
37
 
38
/** @def LOG( severity, expr )  Logs a  message.                            */
39
/** @def LOG_INFO( expr )       Logs an informational message.              */
40
/** @def LOG_DEBUG( expr )      Logs a debugging message.                   */
41
/** @def LOG_WARNING( expr )    Logs a warning message.                     */
42
/** @def LOG_ERROR( expr )      Logs an error message.                      */
43
/** @def LOG_ASSERT( expr )     Logs an assertion failure.                  */
44
/** @def LOG_FATAL( expr )      Logs a fatal error.                         */
45
 
46
#define LOG( sev, expr )    do { LogMessage m; m.Stream() << expr; m.Log( sev, __FILE__, __LINE__ ); } while ( 0 )
47
#define LOG_INFO( expr )    LOG( LogMessage::Info,    expr )
48
#define LOG_DEBUG( expr )   LOG( LogMessage::Debug,   expr )
49
#define LOG_WARNING( expr ) LOG( LogMessage::Warning, expr )
50
#define LOG_ERROR( expr )   LOG( LogMessage::Error,   expr )
51
#define LOG_ASSERT( expr )  LOG( LogMessage::Assert,  expr )
52
#define LOG_FATAL( expr )   LOG( LogMessage::Fatal,   expr )
53
 
54
#define HEX(x)  std::setfill( '0' ) << std::hex << (x) << std::dec
55
 
56
//---------------------------------------------------------------------------
57
/**
58
*   The LogMessage class ...
59
*/
60
 
61
class LogMessage
62
{
63
public:
64
 
65
    /**
66
     * The severity describes the severity of the message being logged.
67
     *
68
     * Fatal errors cause the program to be aborted.
69
     */
70
 
71
    enum Severity
72
    {                              
73
        Info,       ///< Informational.
74
        Debug,      ///< Debug.
75
        Warning,    ///< Warning.
76
        Error,      ///< Error.
77
        Assert,     ///< Assertion failure
78
        Fatal       ///< Fatal error. Causes program termination.
79
    };
80
 
81
    /**
82
     * Pointer to a function which does the real logging.
83
     */
84
 
85
    typedef void (*Handler)( Severity sev, const char *msg, const char *file, int lineNum );
86
 
87
    //------------------------------------------------------------------------
88
    // Default Constructor
89
 
90
    LogMessage();
91
 
92
    //------------------------------------------------------------------------
93
    // The compiler generated constructor and destructor are fine.
94
 
95
    //------------------------------------------------------------------------
96
    // Logs a previously collected message
97
 
98
    void Log( Severity sev, const char *file, int lineNum );
99
 
100
    //------------------------------------------------------------------------
101
    // Sets the handler used for logging messages
102
 
103
    static void SetHandler( Handler handler );
104
 
105
    //------------------------------------------------------------------------
106
    // Unlike Log, this function requires the newline to be provided.
107
 
108
    static void PrintStr( Severity sev, const char *msg );
109
 
110
    //------------------------------------------------------------------------
111
    // Returns the stream object that will be used to collect the message.
112
 
113
    std::ostringstream &Stream();
114
 
115
private:
116
 
117
    //------------------------------------------------------------------------
118
    // The copy constructor and assignment operator are not need for this
119
    // class so we declare them private and don't provide an implementation.
120
 
121
    LogMessage( const LogMessage & copy );
122
    LogMessage &operator =( const LogMessage &rhs );
123
 
124
    //------------------------------------------------------------------------
125
 
126
    std::ostringstream  m_stream;   ///< Place where the message gets collected
127
    static  Handler     m_handler;  ///< Handler that does the real logging.
128
};
129
 
130
// ---- Inline Functions ----------------------------------------------------
131
 
132
//***************************************************************************
133
/**
134
*   Default Contstructor
135
*/
136
 
137
inline LogMessage::LogMessage()
138
{
139
    // Currently nothing to do
140
}
141
 
142
//***************************************************************************
143
/**
144
*   Sets the handler used for logging messages.
145
*/
146
 
147
inline void LogMessage::SetHandler
148
(
149
    LogMessage::Handler handler     ///< Pointer to a function that will do the real logging.
150
)
151
{
152
    m_handler = handler;
153
}
154
 
155
//***************************************************************************
156
/**
157
*   Returns the stream object that will be used to collect the message.
158
*/
159
 
160
inline std::ostringstream &LogMessage::Stream()
161
{
162
    return m_stream;
163
}
164
 
165
/** @} */
166
 
167
#endif // LOGMESSAGE_H
168