00001 #ifndef Impala_Basis_ILog_h
00002 #define Impala_Basis_ILog_h
00003
00004
00005 #include "Basis/LibConfig.h"
00006 #include "Basis/String.h"
00007 #include "Basis/StringList.h"
00008
00009
00010
00011
00012
00013 #ifndef LOG4CPP_USED
00014 #ifndef STDLOG_USED
00015 #define BASISLOG_USED
00016 #endif
00017 #endif
00018
00019 #ifdef __CUDACC__
00020 #undef LOG4CPP_USED
00021 #undef BASISLOG_USED
00022 #define STDLOG_USED
00023 #endif // __CUDACC__
00024
00025
00026
00027 static int ILOG_EXIT_CODE = 42;
00028 static bool gExitOnErrorLogged = false;
00029 static bool gLog4CppFlushOutput = false;
00030
00032 #ifdef LOG4CPP_USED
00033 #include <log4cpp/PropertyConfigurator.hh>
00034 #include <log4cpp/Category.hh>
00035 #include <log4cpp/NDC.hh>
00036 #include <log4cpp/FileAppender.hh>
00037 #include <log4cpp/OstreamAppender.hh>
00038 #include <log4cpp/BasicLayout.hh>
00039 #include <log4cpp/PatternLayout.hh>
00040 #include <log4cpp/StringQueueAppender.hh>
00041 #include "Basis/FileExists.h"
00042 #include "Link/Mpi/MpiFuncs.h"
00043 #include <strstream>
00044
00045 #define ILOG_VAR_DEC static log4cpp::Category& sLog
00046
00047 #define ILOG_VAR_INIT(classstring,pathstring) \
00048 log4cpp::Category& classstring::sLog( \
00049 log4cpp::Category::getInstance( #pathstring "." #classstring))
00050
00051
00052
00053 #define ILOG_VAR_INIT_TEMPL_1(classstring,templ,pathstring) \
00054 template <class templ> \
00055 log4cpp::Category& classstring<templ>::sLog( \
00056 log4cpp::Category::getInstance( #pathstring "." #classstring))
00057
00058 #define ILOG_VAR_INIT_TEMPL_2(classstring,templ1,templ2,pathstring) \
00059 template <class templ1, class templ2> \
00060 log4cpp::Category& classstring<templ1,templ2>::sLog( \
00061 log4cpp::Category::getInstance( #pathstring "." #classstring))
00062
00063 #define ILOG_VAR_INIT_TEMPL_4(classstring,templ1,templ2,templ3,templ4,pathstring) \
00064 template <class templ1, class templ2, class templ3, class templ4> \
00065 log4cpp::Category& classstring<templ1,templ2,templ3,templ4>::sLog( \
00066 log4cpp::Category::getInstance( #pathstring "." #classstring))
00067
00068 #define ILOG_VAR(funcstring) \
00069 static log4cpp::Category& sLog = log4cpp::Category::getInstance( #funcstring )
00070
00071 #define ILOG_DEBUG(message) \
00072 { \
00073 if (sLog.isDebugEnabled()) \
00074 { \
00075 sLog.debugStream() << message; \
00076 if (gLog4CppFlushOutput) \
00077 { \
00078 sLog.debugStream().flush(); \
00079 std::cout.flush(); \
00080 } \
00081 } \
00082 }
00083
00084 #define ILOG_INFO(message) \
00085 { \
00086 if (sLog.isInfoEnabled()) \
00087 { \
00088 sLog.infoStream() << message; \
00089 if (gLog4CppFlushOutput) \
00090 { \
00091 sLog.infoStream().flush(); \
00092 std::cout.flush(); \
00093 } \
00094 } \
00095 }
00096
00097 #define ILOG_WARN(message) \
00098 { \
00099 if (sLog.isWarnEnabled()) \
00100 { \
00101 sLog.warnStream() << message; \
00102 if (gLog4CppFlushOutput) \
00103 { \
00104 sLog.warnStream().flush(); \
00105 std::cout.flush(); \
00106 } \
00107 } \
00108 }
00109
00110 #define ILOG_ERROR(message) \
00111 { \
00112 if (sLog.isErrorEnabled()) \
00113 { \
00114 sLog.errorStream() << message; \
00115 if (gLog4CppFlushOutput) \
00116 { \
00117 sLog.errorStream().flush(); \
00118 std::cout.flush(); \
00119 std::cerr.flush(); \
00120 } \
00121 } \
00122 if (gExitOnErrorLogged) \
00123 exit(ILOG_EXIT_CODE); \
00124 }
00125
00126
00127 #define ILOG_STATS_VAR_DECL static log4cpp::Category& sStatsLog
00128 #define ILOG_STATS_VAR_INIT(classstring,pathstring) log4cpp::Category& classstring::sStatsLog(log4cpp::Category::getInstance( #pathstring "." #classstring))
00129 #define ILOG_STATS_PRE(message) { sStatsLog.infoStream() << "PRE : " << message; }
00130 #define ILOG_STATS_POST(message) { sStatsLog.infoStream() << "POST: " << message; }
00131
00132
00133 #define ILOG_USER(message) { if (sLog.isInfoEnabled()) sLog.infoStream() << "USER:" << message; }
00134 #define ILOG_SYSTEM(message) { if (sLog.isInfoEnabled()) sLog.infoStream() << "USER:" << message; }
00135
00136
00137 #define ILOG_STARTACTION(actionname, level) { sLog.infoStream() << "START: " << level << " " << actionname; }
00138 #define ILOG_ENDACTION(actionname) { sLog.infoStream() << "STOP: " << actionname; }
00139 #define ILOG_LEVEL_DEBUG 1
00140 #define ILOG_LEVEL_INFO 2
00141 #define ILOG_LEVEL_SYSTEM 6
00142 #define ILOG_LEVEL_USER 7
00143
00144 inline void
00145 ILOG_START_DEBUG()
00146 {
00147 log4cpp::Category& root = log4cpp::Category::getRoot();
00148 root.setPriority(log4cpp::Priority::DEBUG);
00149 }
00150
00151 inline void
00152 ILOG_STOP_DEBUG()
00153 {
00154 log4cpp::Category& root = log4cpp::Category::getRoot();
00155 root.setPriority(log4cpp::Priority::INFO);
00156 }
00157
00158 #define ILOG_NDC_PUSH(message) { \
00159 std::ostringstream oss; \
00160 oss << message; \
00161 log4cpp::NDC::push(oss.str()); }
00162 #define ILOG_NDC_POP log4cpp::NDC::pop()
00163
00164 #endif // LOG4CPP_USED
00165
00167 #ifdef BASISLOG_USED
00168 #include "Basis/Logger.h"
00169
00170 #define ILOG_VAR_DEC static Impala::String sLog
00171 #define ILOG_VAR_INIT(classstring,pathstring) Impala::String classstring::sLog = #pathstring "." #classstring
00172 #define ILOG_VAR_INIT_TEMPL_1(classstring,templ,pathstring) \
00173 template <class templ> \
00174 Impala::String classstring<templ>::sLog = #pathstring "." #classstring
00175 #define ILOG_VAR_INIT_TEMPL_2(classstring,templ1,templ2,pathstring) \
00176 template <class templ1,class templ2> \
00177 Impala::String classstring<templ1,templ2>::sLog = #pathstring "." #classstring
00178 #define ILOG_VAR_INIT_TEMPL_4(classstring,templ1,templ2,templ3,templ4,pathstring) \
00179 template <class templ1,class templ2,class templ3,class templ4> \
00180 Impala::String classstring<templ1,templ2,templ3,templ4>::sLog = #pathstring "." #classstring
00181 #define ILOG_VAR(funcstring) static Impala::String sLog = #funcstring
00182 #define ILOG_DEBUG(message) { Impala::Logger::Debug() << sLog << " " << message << std::endl; }
00183 #define ILOG_INFO(message) { Impala::Logger::Information() << sLog << " " << message << std::endl; }
00184 #define ILOG_WARN(message) { Impala::Logger::Warning() << sLog << " " << message << std::endl; }
00185 #define ILOG_ERROR(message) { Impala::Logger::Error() << sLog << " " << message << std::endl; if (gExitOnErrorLogged) exit(ILOG_EXIT_CODE); }
00186 #define ILOG_USER(message) { Impala::Logger::User() << sLog << " " << message << std::endl; }
00187 #define ILOG_SYSTEM(message) { Impala::Logger::System() << sLog << " " << message << std::endl; }
00188
00189
00190 #define ILOG_STARTACTION(actionname, level) { Impala::Logger::StartAction(actionname, level); }
00191 #define ILOG_ENDACTION(actionname) { Impala::Logger::EndAction(actionname); }
00192 #define ILOG_LEVEL_DEBUG 1
00193 #define ILOG_LEVEL_INFO 2
00194 #define ILOG_LEVEL_SYSTEM 6
00195 #define ILOG_LEVEL_USER 7
00196
00197 #define ILOG_NDC_PUSH(message)
00198 #define ILOG_NDC_POP
00199
00200 #endif // BASISLOG_USED
00201
00203 #ifdef STDLOG_USED
00204 #define ILOG_DEBUG(message) { }
00205 #define ILOG_WARN(message) { std::cout << "[WARN] " << message << std::endl; }
00206 #define ILOG_INFO(message) { std::cout << "[INFO] " << message << std::endl; }
00207 #define ILOG_ERROR(message) { std::cerr << "[ERROR] " << message << std::endl; }
00208 #define ILOG_VAR_DEC
00209 #define ILOG_VAR_DECL
00210 #define ILOG_VAR_INIT(message, msg2)
00211 #define ILOG_VAR_INIT_TEMPL_1(a1, a2, a3)
00212 #define ILOG_VAR_INIT_TEMPL_2(a1, a2, a3, a4)
00213 #define ILOG_VAR_INIT_TEMPL_4(a1, a2, a3, a4, a5, a6)
00214 #define ILOG_VAR(message)
00215 #endif // STDLOG_USED
00216
00217
00219 #define ILOG_VAR_DECL ILOG_VAR_DEC
00220 #define ILOG_WARNING ILOG_WARN
00221
00222 #define ILOG_FUNCTION ILOG_VAR
00223 #define ILOG_CLASS ILOG_VAR_DEC
00224 #define ILOG_CLASS_INIT ILOG_VAR_INIT
00225
00226
00227
00228
00229
00230
00231
00232
00233 #define ILOG_PROGRESS(message, timeout) \
00234 { \
00235 static double sLastTime=-0.; \
00236 static Timer sTimer(0); \
00237 double now = sTimer.SplitTime(); \
00238 if(now - sLastTime > timeout) \
00239 { \
00240 sLastTime = now; \
00241 ILOG_INFO(message); \
00242 } \
00243 }
00244
00245
00246
00247 #define ILOG_PROGRESS_DONE(message) \
00248 ILOG_INFO(message)
00249
00252 #define ILOG_WARNING_ONCE(message) \
00253 { \
00254 ILOG_WARNING_ONCE_IMPL(message, sDid ## __LINE__ ## __FILE__ ) \
00255 }
00256
00257 #define ILOG_WARNING_ONCE_IMPL(message, varname) \
00258 static bool varname = false; \
00259 if(! varname ) \
00260 { \
00261 varname = true; \
00262 ILOG_WARNING(message); \
00263 }
00264
00265
00266
00267
00268 namespace Impala
00269 {
00270
00271 class ILogErrors
00272 {
00273
00274 public:
00275
00276 static ILogErrors&
00277 GetInstance()
00278 {
00279 static ILogErrors theILog;
00280 return theILog;
00281 }
00282
00283 void
00284 SetAppender(void* appender)
00285 {
00286 mAppender = appender;
00287 }
00288
00289 int
00290 GetTotalNrErrors()
00291 {
00292 Sync();
00293 return mErrors.size();
00294 }
00295
00296 void
00297 Mark()
00298 {
00299 Sync();
00300 mMark = mErrors.size();
00301 }
00302
00303 int
00304 GetNrErrorsSinceMark()
00305 {
00306 Sync();
00307 return mErrors.size() - mMark;
00308 }
00309
00310 std::vector<String>
00311 GetErrorsSinceMark()
00312 {
00313 Sync();
00314 std::vector<String> res;
00315 for (int i=mMark ; i<mErrors.size() ; i++)
00316 res.push_back(mErrors[i]);
00317 }
00318
00319 String
00320 GetLastErrorSinceMark()
00321 {
00322 Sync();
00323 return mErrors[mErrors.size() - 1];
00324 }
00325
00326 void
00327 Print(bool fromMark)
00328 {
00329 Sync();
00330 int start = (fromMark) ? mMark : 0;
00331 for (int i=start ; i<mErrors.size() ; i++)
00332 std::cout << "ILog: " << mErrors[i] << std::endl;
00333 }
00334
00335 private:
00336
00337 void
00338 Sync()
00339 {
00340 #ifdef LOG4CPP_USED
00341 log4cpp::StringQueueAppender* appender =
00342 (log4cpp::StringQueueAppender*) mAppender;
00343 while (appender->queueSize() > 0)
00344 mErrors.push_back(appender->popMessage());
00345 #endif
00346 }
00347
00348 ILogErrors()
00349 {
00350 mMark = 0;
00351 }
00352
00353 ILogErrors(const ILogErrors&)
00354 {
00355 }
00356
00357 ILogErrors&
00358 operator=(const ILogErrors&)
00359 {
00360 }
00361
00362 int mMark;
00363 std::vector<String> mErrors;
00364 void* mAppender;
00365
00366 };
00367
00368 #define ILOG_ERROR_COUNT ILogErrors::GetInstance().GetTotalNrErrors()
00369
00370 }
00371
00372 #endif