00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _log_h_
00018 #define _log_h_
00019
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 #include <spl/DateTime.h>
00023 #include <spl/Exception.h>
00024 #include <spl/collection/List.h>
00025 #include <spl/String.h>
00026 #include <spl/net/UdpSocket.h>
00027
00034 class Log;
00035 typedef RefCountPtr<Log> LogPtr;
00036 typedef WeakReference<Log, LogPtr> LogRef;
00037
00041 class Log : public IMemoryValidate
00042 {
00043 public:
00044 class OutputConfig
00045 {
00046 public:
00047 unsigned m_doLogEmergency : 1;
00048 unsigned m_doLogAlert : 1;
00049 unsigned m_doLogCritical : 1;
00050 unsigned m_doLogError : 1;
00051 unsigned m_doLogWarning : 1;
00052 unsigned m_doLogNotice : 1;
00053 unsigned m_doLogInfo : 1;
00054 unsigned m_doLogDebug : 1;
00055
00056 inline OutputConfig()
00057 : m_doLogEmergency(1),
00058 m_doLogAlert(1),
00059 m_doLogCritical(1),
00060 m_doLogError(1),
00061 m_doLogWarning(1),
00062 m_doLogNotice(1),
00063 m_doLogInfo(1),
00064 m_doLogDebug(1)
00065 {
00066 }
00067 };
00068
00069 typedef enum _Severity
00070 {
00071 SevEmergency = 0,
00072 SevAlert = 1,
00073 SevCritical = 2,
00074 SevError = 3,
00075 SevWarning = 4,
00076 SevNotice = 5,
00077 SevInfo = 6,
00078 SevDebug = 7
00079 } Severity;
00080
00081 typedef enum _Facility
00082 {
00083 FacKernal = 0,
00084 FacUser = 1,
00085 FacMail = 2,
00086 FacDaemon = 3,
00087 FacAuthentication = 4,
00088 FacSyslog = 5,
00089 FacPrinter = 6,
00090 FacNews = 7,
00091 FacUUCP = 8,
00092 FacCron1 = 9,
00093 FacAuthorization = 10,
00094 FacFTP = 11,
00095 FacNTP = 12,
00096 FacLogAudit = 13,
00097 FacLogAlert = 14,
00098 FacCron2 = 15,
00099 FacLocal0 = 16,
00100 FacLocal1 = 17,
00101 FacLocal2 = 18,
00102 FacLocal3 = 19,
00103 FacLocal4 = 20,
00104 FacLocal5 = 21,
00105 FacLocal6 = 22,
00106 FacLocal7 = 23
00107 } Facility;
00108
00109 typedef enum _LogType
00110 {
00111 LogToNone = 0,
00112 LogToConsole = 1<<1,
00113 LogToFile = 1<<2,
00114 LogToServer = 1<<3
00115 } LogType;
00116
00117 class LogEntry : public IMemoryValidate
00118 {
00119 private:
00120 Facility m_facility;
00121 Severity m_severity;
00122 DateTime m_dts;
00123 String m_host;
00124 String m_process;
00125 int m_pid;
00126 String m_message;
00127
00128 public:
00129 LogEntry();
00130 LogEntry(const LogEntry& le);
00131 LogEntry(Log::Facility facility, Log::Severity severity, const String& host, const String& process, int pid, const String& message);
00132 LogEntry(Log::Facility facility, Log::Severity severity, const String& message);
00133 virtual ~LogEntry();
00134
00135 LogEntry& operator =(const LogEntry& le);
00136
00137 inline Facility& GetFacility()
00138 {
00139 return m_facility;
00140 }
00141
00142 inline Severity& GetSeverity()
00143 {
00144 return m_severity;
00145 }
00146
00147 inline const Severity GetSeverity() const
00148 {
00149 return m_severity;
00150 }
00151
00152 inline int Priority() const
00153 {
00154 return m_facility * 8 + m_severity;
00155 }
00156
00157 inline DateTime& GetDateTime()
00158 {
00159 return m_dts;
00160 }
00161
00162 inline String& GetHost()
00163 {
00164 return m_host;
00165 }
00166
00167 inline String& GetProcess()
00168 {
00169 return m_process;
00170 }
00171
00172 inline int& GetPID()
00173 {
00174 return m_pid;
00175 }
00176
00177 inline String& GetMessage()
00178 {
00179 return m_message;
00180 }
00181
00182 StringPtr ToString() const;
00183 StringPtr ToShortString() const;
00184
00185 static LogEntry Parse(const String& msg, unsigned long sourceIp);
00186
00187 #if defined(DEBUG)
00188 void CheckMem() const;
00189 void ValidateMem() const;
00190 #endif
00191 };
00192
00193 class LogServerInfo : public IMemoryValidate
00194 {
00195 private:
00196 String m_server;
00197 int m_port;
00198 OutputConfig m_logMsgTypes;
00199 UdpSocketPtr m_sock;
00200
00201 public:
00202 LogServerInfo();
00203 LogServerInfo(const String& server, int port, OutputConfig logMsgTypes);
00204 LogServerInfo(const String& server, int port);
00205 virtual ~LogServerInfo();
00206
00207 LogServerInfo& operator =(const LogServerInfo& lsi);
00208
00209 OutputConfig& GetWhereToLog() { return m_logMsgTypes; }
00210 void Send(const LogEntry& le);
00211 void Send(const String& line);
00212
00213 inline void Close()
00214 {
00215 m_sock->Close();
00216 }
00217
00218 #if defined(DEBUG)
00219 void CheckMem() const;
00220 void ValidateMem() const;
00221 #endif
00222 };
00223
00224 protected:
00225 static bool _LogDoLog(const Log::LogEntry& le, Log::OutputConfig m_msgToLog);
00226
00227 protected:
00228 Facility m_facility;
00229 int m_logType;
00230 int m_messageCount;
00231 String m_fileName;
00232 List<LogServerInfo> m_servers;
00233 OutputConfig m_msgToLog;
00234
00235 static char m_staticFileName[512];
00236 static char m_staticServerName[128];
00237 static int m_staticServerPort;
00238 static volatile int m_staticMessageCount;
00239 static volatile int m_staticOldMsgCount;
00240 static int m_staticLogType;
00241
00242 static void SWrite(const LogEntry& le);
00243
00244 bool DoLog(const LogEntry& le);
00245
00246 public:
00247 Log();
00248 Log(Facility facility, LogType logType);
00249 Log(const Log& l);
00250 virtual ~Log();
00251 Log& operator =(const Log& l);
00252
00253 inline void AddServer(const String& hostName, int port = 514) { m_servers.Add(LogServerInfo(hostName, port)); }
00254 inline void AddServer(const String& hostName, OutputConfig cf, int port = 514) { m_servers.Add(LogServerInfo(hostName, port, cf)); }
00255
00256 inline void DoLogEmergency() { m_msgToLog.m_doLogEmergency = 1; }
00257 inline void DoLogAlert() { m_msgToLog.m_doLogAlert = 1; }
00258 inline void DoLogCritical() { m_msgToLog.m_doLogCritical = 1; }
00259 inline void DoLogError() { m_msgToLog.m_doLogError = 1; }
00260 inline void DoLogWarning() { m_msgToLog.m_doLogWarning = 1; }
00261 inline void DoLogNotice() { m_msgToLog.m_doLogNotice = 1; }
00262 inline void DoLogInfo() { m_msgToLog.m_doLogInfo = 1; }
00263 inline void DoLogDebug() { m_msgToLog.m_doLogDebug = 1; }
00264
00265 inline void DontLogEmergency() { m_msgToLog.m_doLogEmergency = 0; }
00266 inline void DontLogAlert() { m_msgToLog.m_doLogAlert = 0; }
00267 inline void DontLogCritical() { m_msgToLog.m_doLogCritical = 0; }
00268 inline void DontLogError() { m_msgToLog.m_doLogError = 0; }
00269 inline void DontLogWarning() { m_msgToLog.m_doLogWarning = 0; }
00270 inline void DontLogNotice() { m_msgToLog.m_doLogNotice = 0; }
00271 inline void DontLogInfo() { m_msgToLog.m_doLogInfo = 0; }
00272 inline void DontLogDebug() { m_msgToLog.m_doLogDebug = 0; }
00273
00274 inline void SetLogType(LogType lt) { m_logType = lt; }
00275 inline void SetFacility(Facility facility) { m_facility = facility; }
00276
00277 virtual void Write(const LogEntry& le);
00278 void Write(const OutOfMemoryException& mex);
00279 void Write(Exception *ex);
00280 void Write(Severity severity, const String& message);
00281 void Write(Severity severity, Facility facility, const String& process, const String& message);
00282 void Write(Severity severity, Facility facility, const String& process, int pid, const String& message);
00283 void Write(Severity severity, Facility facility, const String& host, const String& process, int pid, const String& message);
00284
00285 void WriteError(const String& message);
00286 void WriteWarn(const String& message);
00287 void WriteInfo(const String& msg);
00288
00289 inline int GetMessageCount() const { return m_messageCount; }
00290
00291 static void SSetLogType(LogType lt);
00292 static void SSetLogFileName(const String& filename);
00293 static void SSetSyslogServer(const String& server);
00294 static void SSetStaticSyslogPort(int port);
00295
00296 static void SWrite(const OutOfMemoryException& mex);
00297 static void SWrite(Exception *ex);
00298 static void SWrite(Severity severity, const String& message);
00299 static void SWrite(Severity severity, Facility facility, const String& process, const String& message);
00300 static void SWrite(Severity severity, Facility facility, const String& process, int pid, const String& message);
00301 static void SWrite(Severity severity, Facility facility, const String& host, const String& process, int pid, const String& message);
00302
00303 static void SWriteError(const String& message);
00304 static void SWriteWarn(const String& message);
00305 static void SWriteInfo(const String& msg);
00306
00307 static int SMessageCount();
00308
00309 static void SWriteOkFail( const String& msg );
00310 static void SWriteEndOfRunTotal();
00311
00312 #if defined(DEBUG)
00313 void CheckMem() const;
00314 void ValidateMem() const;
00315 #endif
00316 };
00317
00320 #endif
00321
00322