Slice Tools libFoundation |
Logger.hhGo to the documentation of this file.00001 // $Id: Logger.hh,v 1.1 2005/07/29 02:55:15 mschatz Exp $ 00002 00005 00006 #ifndef LOGGER_HH 00007 #define LOGGER_HH 1 00008 00009 #include <map> 00010 #include <list> 00011 #include <string> 00012 #include <fstream> 00013 00014 #include <time.h> 00015 #include <pwd.h> 00016 00017 #include "FileSystem.hh" 00018 #include "MessageLevel.hh" 00019 00020 class LogMsg; 00021 #include "LogMsg.hh" 00022 00023 class CategoryInformation; 00024 #include "CategoryInformation.hh" 00025 00026 class LogCategory; 00027 #include "LogCategory.hh" 00028 00029 typedef std::list<CategoryInformation *> CategoryStack; 00030 typedef std::list<std::string>::const_iterator StringListCIter; 00031 typedef std::map<std::string, CategoryInformation *> CategoryMap; 00032 typedef CategoryMap::const_iterator CategoryMapCIter; 00033 00034 00036 00039 class Logger 00040 { 00041 public: 00042 Logger(const char * logfile, 00043 const char * errorfile, 00044 const std::string & programName, 00045 const std::string & invocation); 00046 ~Logger(); 00047 00048 void addLogfile(const char * filename, bool isLogFile=true); 00049 void addOutputStream(std::ostream * os); 00050 00051 // Defaults 00052 void setFilterLevels(const std::string & filterLevels); 00053 bool hasFilterLevel() const; 00054 00055 MessageLevel getDefaultFilterLevel() const; 00056 void setDefaultFilterLevel(MessageLevel filterLevel, 00057 bool forceFirstTime = false); 00058 void setDefaultFilterLevel(int filterLevel, 00059 bool forceFirstTime = false); 00060 00061 MessageLevel getDefaultMessageLevel() const; 00062 void setDefaultMessageLevel(MessageLevel messageLevel); 00063 void setDefaultMessageLevel(int messageLevel); 00064 00065 00066 // Category Management 00067 LogCategory getCategory(std::string categoryName, 00068 MessageLevel messageLevel = MESSAGE_LEVEL_DEFAULT, 00069 MessageLevel filterLevel = MESSAGE_LEVEL_DEFAULT); 00070 00071 CategoryInformation * getCategoryInformation(std::string categoryName, 00072 MessageLevel messageLevel = MESSAGE_LEVEL_DEFAULT, 00073 MessageLevel filterLevel = MESSAGE_LEVEL_DEFAULT); 00074 00075 LogCategory getLog(); 00076 00077 // Logging Master Control 00078 void enable(); 00079 void disable(); 00080 bool isEnabled() const; 00081 00082 // Main logging function 00083 void writeLog(LogMsg * logMsg, 00084 bool incrementCount = true, 00085 std::ostream * os = NULL); 00086 00087 private: 00088 void writeFatal(LogMsg * logMsg); 00089 00090 MessageLevel convertFilterLevel(const std::string & filterLevel) const; 00091 void setFilterLevelPair(const std::string & filterPair, 00092 bool moreThanOnePair); 00093 00094 bool hasCategory(std::string category) const; 00095 bool doLog() const; 00096 00097 void openErrorFile(); 00098 void openLogFiles(); 00099 00100 void writeOpenLogSequence(std::ostream * os); 00101 std::string getOpenLogStr(); 00102 std::string getUsernameStr() const; 00103 std::string getHostnameStr() const; 00104 00105 std::string getCloseLogStr() const; 00106 00108 bool m_logEnabled; 00109 00111 bool m_firstTimeEnabled; 00112 00114 CategoryInformation * m_defaultCategory; 00115 00117 CategoryMap m_categoryMap; 00118 00120 int m_msgCount; 00121 00123 int m_pid; 00124 00126 std::list<std::ostream *> m_outputStreamList; 00127 00129 std::list<std::ofstream *> m_logfileList; 00130 00132 std::list<std::string> m_logfileNameList; 00133 00135 std::string m_errorfileName; 00136 00138 std::ofstream * m_errorfile; 00139 00141 time_t m_startTime; 00142 00144 std::string m_invocation; 00145 00147 std::string m_programName; 00148 }; 00149 00150 #endif |