SockIt
|
00001 #ifndef LOGGER_H_ 00002 #define LOGGER_H_ 00003 00004 #if defined (__UNIX__) || defined (__OSX__) 00005 00006 #include <unistd.h> 00007 #include <sys/types.h> 00008 #define PID getpid() 00009 00010 #else 00011 #define WIN32_LEAN_AND_MEAN 00012 #include "process.h" 00013 #include <winsock2.h> 00014 #include <windows.h> 00015 #include <conio.h> 00016 #define PID _getpid() 00017 00018 #endif 00019 00020 #include <stdio.h> 00021 00022 #include <queue> 00023 #include <string> 00024 00025 #include <boost/date_time/local_time/local_time.hpp> 00026 #include <boost/date_time/posix_time/posix_time.hpp> 00027 #include <boost/filesystem.hpp> 00028 #include <boost/lexical_cast.hpp> 00029 #include <boost/thread.hpp> 00030 00031 // apparently std::queue doesn't quite work. 00032 // don't try it. 00033 using std::pair; 00034 using std::string; 00035 00060 class Logger 00061 { 00062 public: 00063 00071 static inline void error(string msg, int port = NO_PORT, string host = "local") 00072 { 00073 if (Logger::is_enabled()) 00074 Logger::queue(host, port, msg, "ERROR : "); 00075 } 00076 00084 static inline void info(string msg, int port = NO_PORT, string host = "local") 00085 { 00086 if (Logger::is_enabled()) 00087 Logger::queue(host, port, msg, "INFO : "); 00088 } 00089 00097 static inline void warn(string msg, int port = NO_PORT, string host = "local") 00098 { 00099 if (Logger::is_enabled()) 00100 Logger::queue(host, port, msg, "WARN : "); 00101 } 00102 00107 static const bool is_enabled() 00108 { 00109 return Logger::enabled; 00110 } 00111 00114 static void shutdown(); 00115 00117 static const int NO_PORT = 0; 00118 00119 private: 00120 00121 /* Disallow instantiating. */ 00122 Logger() 00123 { 00124 } 00125 00127 static const int SLEEP_TIME_MS = 50; 00128 00129 // add a log write request to the queue 00130 static void queue(string host, int port, string msg, string cat); 00131 00133 static string get_date(); 00134 00136 static string get_timestamp(); 00137 00139 static string get_log_base_path(); 00140 00142 static void initialize(); 00143 00145 static void log_writer_run(); 00146 00148 static void handle_write_request(string dir, string line); 00149 00151 static void formatter_run(); 00152 00154 static void handle_format_request(string host, int port, string msg, string cat); 00155 00157 static bool initialized; 00158 00160 static bool enabled; 00161 00163 static boost::mutex log_writer_mtx; 00164 00166 static boost::mutex formatter_mtx; 00167 00169 static boost::mutex shutdown_mtx; 00170 00172 static boost::condition_variable shutdown_cvar; 00173 00175 static boost::condition_variable formatter_cvar; 00176 00178 static boost::condition_variable log_writer_cvar; 00179 00181 static std::queue<pair<pair<string, int> , pair<string, string> > > raw_requests; 00182 00184 static std::queue<pair<string, string> > write_requests; 00185 00187 static string pid; 00188 00190 static boost::thread frm_t; 00191 00193 static boost::thread lw_t; 00194 }; 00195 00196 #endif /* LOGGER_H_ */