00001 #ifndef Impala_Basis_LogSystem_h
00002 #define Impala_Basis_LogSystem_h
00003
00004 #include <sstream>
00005
00006 #include "Basis/ILog.h"
00007 #include "Basis/FileExists.h"
00008 #include "Basis/StringList.h"
00009
00015 namespace Impala
00016 {
00017
00018 class LogSystem
00019 {
00020
00021 public:
00022
00023 class LogSystemConfig
00024 {
00025
00026 public:
00027
00028 String mLogProperties;
00029 String mLogFile;
00030 String mLogLayout;
00031 bool mDebug;
00032 int mLogLevel;
00033 String mDebugCat;
00034 bool mNoErrorLog;
00035 String mJobErrorLog;
00036 bool mExitOnError;
00037 bool mLogFlushOutput;
00038
00039 };
00040
00041 static LogSystem&
00042 GetInstance()
00043 {
00044 static LogSystem theLogSystem;
00045 return theLogSystem;
00046 }
00047
00048 virtual
00049 ~LogSystem()
00050 {
00051 #ifdef LOG4CPP_USED
00052 log4cpp::Category::shutdown();
00053 #endif
00054 }
00055
00056
00057 #ifdef LOG4CPP_USED
00058
00059 void
00060 Configure(const LogSystemConfig& config)
00061 {
00062 if (FileExists(config.mLogProperties))
00063 {
00064 const String logPropertiesFile = config.mLogProperties;
00065 log4cpp::PropertyConfigurator::configure(logPropertiesFile);
00066 }
00067 else
00068 {
00069 log4cpp::Appender* appender =
00070 new log4cpp::OstreamAppender("stdout", &std::cout);
00071 AddAppender(appender, config.mLogLayout);
00072 }
00073
00074 if (config.mDebug)
00075 {
00076 log4cpp::Category& root = log4cpp::Category::getRoot();
00077 root.setPriority(log4cpp::Priority::DEBUG);
00078 }
00079
00080 if (!config.mDebugCat.empty())
00081 {
00082 StringList catList(config.mDebugCat, ';');
00083 StringListCI i;
00084 for (i = catList.begin() ; i != catList.end() ; i++)
00085 {
00086 std::cout << "debug cat " << *i << std::endl;
00087 log4cpp::Category& cat = log4cpp::Category::getInstance(*i);
00088 cat.setPriority(log4cpp::Priority::DEBUG);
00089 }
00090 }
00091
00092 if (!config.mLogFile.empty())
00093 {
00094 std::ostringstream oss;
00095 if (Link::Mpi::NrProcs() > 1)
00096 oss << "p" << Link::Mpi::MyId() << "_";
00097 oss << config.mLogFile;
00098 log4cpp::Appender* appender =
00099 new log4cpp::FileAppender("proc_file", oss.str());
00100 AddAppender(appender, config.mLogLayout);
00101 }
00102
00103 if (!config.mNoErrorLog)
00104 {
00105 std::ostringstream oss;
00106 if (Link::Mpi::NrProcs() > 1)
00107 oss << "p" << Link::Mpi::MyId() << "_";
00108 oss << "errors.log";
00109 log4cpp::Appender* appender =
00110 new log4cpp::FileAppender("err_file", oss.str());
00111 appender->setThreshold(log4cpp::Priority::ERROR);
00112 AddAppender(appender, "%d{%a%d %H:%M:%S,%l} - [%c %x] %m%n");
00113 if (! config.mJobErrorLog.empty())
00114 {
00115 log4cpp::Appender* jobAppender =
00116 new log4cpp::FileAppender("err_file", config.mJobErrorLog);
00117 jobAppender->setThreshold(log4cpp::Priority::ERROR);
00118 AddAppender(jobAppender, "%d{%a%d %H:%M:%S,%l} - [%c %x] %m%n");
00119 }
00120 }
00121 log4cpp::StringQueueAppender* ap =
00122 new log4cpp::StringQueueAppender("err_queue");
00123 ap->setThreshold(log4cpp::Priority::ERROR);
00124 AddAppender(ap, "%d{%a%d %H:%M:%S,%l} - [%c %x] %m%n");
00125 ILogErrors::GetInstance().SetAppender(ap);
00126 gExitOnErrorLogged = config.mExitOnError;
00127 gLog4CppFlushOutput = config.mLogFlushOutput;
00128 }
00129
00130 void
00131 AddAppender(log4cpp::Appender* appender, String layoutPattern)
00132 {
00133 log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
00134 layout->setConversionPattern(layoutPattern);
00135 appender->setLayout(layout);
00136 log4cpp::Category& root = log4cpp::Category::getRoot();
00137 root.addAppender(appender);
00138 }
00139
00140 #else
00141
00142 void
00143 Configure(const LogSystemConfig& config)
00144 {
00145 Logger::Configure(config.mLogLevel, config.mLogFile, config.mDebug);
00146 gExitOnErrorLogged = config.mExitOnError;
00147 gLog4CppFlushOutput = config.mLogFlushOutput;
00148 }
00149
00150 #endif //LOG4CPP_USED
00151
00152
00153 private:
00154
00155 LogSystem()
00156 {
00157 }
00158
00159 LogSystem(const LogSystem&)
00160 {
00161 }
00162
00163 LogSystem&
00164 operator=(const LogSystem&)
00165 {
00166 }
00167
00168 };
00169
00170 }
00171
00172 #endif