00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef EDOC_LOGGER_H
00020 #define EDOC_LOGGER_H
00021
00022 #include "EDoc/LogLevels.h"
00023
00024 #include <sstream>
00025 #include <iostream>
00026 #include <fstream>
00027
00028
00029 #ifndef EDOC_DEFAULT_LOG_LEVEL
00030
00031
00032 #define EDOC_DEFAULT_LOG_LEVEL EDOC_LOG_LEVEL_INFO
00033
00034
00035
00036
00037 #endif
00038
00039 namespace EDoc
00040 {
00041
00042
00043
00044 class Logger
00045 {
00046 public:
00047
00048
00049
00050
00051 static inline Logger* Instance()
00052 {
00053 if (instance == NULL)
00054 {
00055 instance = new Logger();
00056 }
00057
00058 return instance;
00059 }
00060
00061
00062
00063
00064 static void Destroy()
00065 {
00066 if (instance)
00067 {
00068 delete instance;
00069 instance = NULL;
00070 }
00071 }
00072
00073
00074
00075
00076 ~Logger();
00077
00078
00079
00080
00081 bool use_stdout;
00082
00083
00084
00085
00086 bool use_stderr;
00087
00088
00089
00090
00091 void ClearUseFile();
00092
00093
00094
00095
00096 bool GetUseFile() const;
00097
00098
00099
00100
00101
00102 bool GetLogFileName(std::string& filename_out) const;
00103
00104
00105
00106
00107 bool SetLogFilename(std::string filename_in);
00108
00109
00110
00111
00112 bool SetLogFilenameFullyQualified(std::string filename_in);
00113
00114
00115
00116
00117 inline void LogMessage(const std::string& message)
00118 {
00119 if (use_file)
00120 {
00121
00122
00123 if (!file_initialised)
00124 {
00125 InitialiseFile();
00126 }
00127
00128 file << message << ::std::endl;
00129 }
00130
00131 if (use_stdout)
00132 {
00133 std::cout << message << ::std::endl;
00134 }
00135
00136 if (use_stderr)
00137 {
00138 std::cerr << message << ::std::endl;
00139 }
00140 }
00141
00142
00143
00144 private:
00145
00146
00147
00148
00149 Logger();
00150
00151
00152
00153
00154 void InitialiseFile();
00155
00156
00157
00158
00159 bool use_file;
00160
00161
00162
00163
00164 std::string filename;
00165
00166
00167
00168
00169 std::ofstream file;
00170
00171
00172
00173
00174
00175 bool file_initialised;
00176
00177
00178
00179
00180 static Logger* instance;
00181
00182
00183
00184 };
00185
00186 }
00187
00188 #endif // EDOC_LOGGER_H
00189
00190
00191
00192
00193
00194 #undef EDOC_LOG_MESSAGE
00195 #undef EDOC_IMPL_LOG_LEVEL
00196 #undef EDOC_Fatal
00197 #undef EDOC_Error
00198 #undef EDOC_Warning
00199 #undef EDOC_Info
00200 #undef EDOC_Debug
00201 #undef EDOC_Fine
00202 #undef EDOC_Finer
00203 #undef EDOC_Finest
00204
00205
00206
00207
00208
00209
00210
00211
00212 #ifndef EDOC_LOG_WRITE_LEVEL
00213 #define EDOC_LOG_WRITE_LEVEL(Level, LevelString) << LevelString << ":"
00214 #endif
00215
00216
00217
00218
00219
00220 #ifndef EDOC_LOG_WRITE_FILENAME
00221 #define EDOC_LOG_WRITE_FILENAME << __FILE__ << ":"
00222 #endif
00223
00224
00225
00226
00227
00228 #ifndef EDOC_LOG_WRITE_LINE
00229 #define EDOC_LOG_WRITE_LINE << __LINE__ << ":"
00230 #endif
00231
00232
00233
00234
00235 #define EDOC_LOG_WRITE_FUNCTION
00236
00237 #ifndef EDOC_LOG_WRITE_FUNCTION
00238 #define EDOC_LOG_WRITE_FUNCTION << __FUNCTION__ << "():"
00239 #endif
00240
00241
00242
00243
00244
00245 #ifndef EDOC_LOG_WRITE_MESSAGE
00246 #define EDOC_LOG_WRITE_MESSAGE(Message) << " " << Message
00247 #endif
00248
00249
00250
00251
00252
00253 #define EDOC_LOG_MESSAGE(Level, LevelString, Message) \
00254 { \
00255 ::std::ostringstream stream_jh8732rhb; \
00256 stream_jh8732rhb \
00257 EDOC_LOG_WRITE_LEVEL(Level, LevelString) \
00258 EDOC_LOG_WRITE_FILENAME \
00259 EDOC_LOG_WRITE_LINE \
00260 EDOC_LOG_WRITE_FUNCTION \
00261 EDOC_LOG_WRITE_MESSAGE(Message) \
00262 << std::flush; \
00263 EDoc::Logger::Instance()->LogMessage(stream_jh8732rhb.str()); \
00264 }
00265
00266
00267
00268
00269 #ifndef EDOC_LOG_LEVEL
00270 #define EDOC_IMPL_LOG_LEVEL EDOC_DEFAULT_LOG_LEVEL
00271 #else
00272
00273 #define EDOC_IMPL_LOG_LEVEL EDOC_LOG_LEVEL
00274 #endif
00275
00276
00277
00278
00279
00280
00281 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FATAL
00282 #define EDOC_Fatal(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FATAL, "FATAL ", Message)
00283 #else
00284 #define EDOC_Fatal(Message)
00285 #endif
00286
00287 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_ERROR
00288 #define EDOC_Error(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_ERROR, "ERROR ", Message)
00289 #else
00290 #define EDOC_Error(Message)
00291 #endif
00292
00293 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_WARNING
00294 #define EDOC_Warning(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_WARNING, "WARNING", Message)
00295 #else
00296 #define EDOC_Warning(Message)
00297 #endif
00298
00299 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_INFO
00300 #define EDOC_Info(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_INFO, "INFO ", Message)
00301 #else
00302 #define EDOC_Info(Message)
00303 #endif
00304
00305 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_DEBUG
00306 #define EDOC_Debug(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_DEBUG, "DEBUG ", Message)
00307 #else
00308 #define EDOC_Debug(Message)
00309 #endif
00310
00311 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FINE
00312 #define EDOC_Fine(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FINE, "FINE ", Message)
00313 #else
00314 #define EDOC_Fine(Message)
00315 #endif
00316
00317 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FINER
00318 #define EDOC_Finer(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FINER, "FINER ", Message)
00319 #else
00320 #define EDOC_Finer(Message)
00321 #endif
00322
00323 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FINEST
00324 #define EDOC_Finest(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FINEST, "FINEST ", Message)
00325 #else
00326 #define EDOC_Finest(Message)
00327 #endif