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