OSDN Git Service

Temporary implementation of insert X-Forwarded-For.
[ultramonkey-l7/sslproxy.git] / logger / logger_impl.h
1 /*
2  * @file  logger_impl.h
3  * @brief logger module implementation class.
4  *
5  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6  * Copyright (C) 2008  NTT COMWARE Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *      
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  **********************************************************************/
24
25 #ifndef __LOGGER_IMPL_H__
26 #define __LOGGER_IMPL_H__
27
28 #include <string>
29 #include <map>
30 #include <log4cxx/logger.h>
31 #include <log4cxx/level.h>
32 #include <log4cxx/net/syslogappender.h>
33 #include <log4cxx/fileappender.h>
34 #include <log4cxx/rollingfileappender.h>
35 #include <log4cxx/patternlayout.h>
36 #include "logger_enum.h"
37
38 #define BUF_LEN (4096)
39 #define LOGGER_LEVEL_NUM (6)
40 #define LOGGER_CATEGORY_NUM (40)
41
42 #define LOGGER_PROCESS_ID "SLP"
43
44 #define LOGGER_NULL "/dev/null"
45
46 #define HOST_NAME_LEN (256)
47
48 namespace log4cxx
49 {
50         typedef helpers::ObjectPtrT<RollingFileAppender> RollingFileAppenderPtr;
51 }
52
53 namespace l7vs
54 {
55         /*!
56          *  Logger implement class.
57          *  operate log4cxx library. 
58          *  this is singleton class. 
59          */
60         class LoggerImpl
61         {
62         public:
63                 //! returns current instance.
64                 static LoggerImpl& getInstance();
65         protected:
66                 //! default constructor initialize member variables.
67                 LoggerImpl() : initialized(false), logFilename(""), rotation(LOG_ROT_SIZE), maxBackupIndex(0), maxFileSize(0), rotationTiming(LOG_TIM_YEAR), rotationTimingValue("")
68                 {
69                         levelTable[LOG_LV_NONE] = log4cxx::Level::getDebug();
70                         levelTable[LOG_LV_DEBUG] = log4cxx::Level::getDebug();
71                         levelTable[LOG_LV_INFO] = log4cxx::Level::getInfo();
72                         levelTable[LOG_LV_WARN] = log4cxx::Level::getWarn();
73                         levelTable[LOG_LV_ERROR] = log4cxx::Level::getError();
74                         levelTable[LOG_LV_FATAL] = log4cxx::Level::getFatal();
75
76                         loggerCategory = LOG_CAT_SSLPROXY_LOGGER;
77                         //set default log level
78                         for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
79                                 categoryLevel[cat] = log4cxx::Level::getError();
80                         }
81                 }
82                 //! cpoy constructor disable
83                 LoggerImpl( const LoggerImpl& );
84                 //! operator= disable
85                 LoggerImpl& operator=( const LoggerImpl& );
86                 //! destructor.
87                 virtual ~LoggerImpl() {}
88                 //! static Logger instance
89                 static LoggerImpl* instance;
90                 //! initialized flag
91                 bool initialized;
92                 //! logger category
93                 LOG_CATEGORY_TAG loggerCategory;
94                 //! hostname
95                 char hostname[HOST_NAME_LEN];
96
97                 //! if error occured, switch appenders to syslogappender and fileappender(/dev/console)
98                 virtual void errorConf(unsigned int messageId, const std::string& errorMessage, const char* file, int line);
99
100                 //! base logFileanme
101                 std::string logFilename;
102                 //! rotation way (size base, date base, or both size and date base)
103                 LOG_ROTATION_TAG rotation;
104                 //! number of backup log file
105                 unsigned int maxBackupIndex;
106                 //! max size of log file
107                 unsigned long long maxFileSize;
108                 /*!
109                  *  rotation timing
110                  *  "year"   = yearly
111                  *  "monthr" = monthly
112                  *  "week"   = weekly
113                  *  "date"   = daily
114                  *  "hour"   = hourly
115                  */
116                 LOG_ROTATION_TIMING_TAG rotationTiming;
117                 /*!
118                  *  rotation timing value 
119                  *
120                  *  rotation timing     value
121                  *  -------------------------------------------------
122                  *  year                "03051500"      (3/5 15:00)
123                  *  month               "051100"        (5 11:00)
124                  *  week                "12000"         (mon 20:00) sun = 0, sat = 6
125                  *  date                "1500"          (15:00)
126                  *  hour                "45"            (45)
127                  */
128                 std::string rotationTimingValue;
129
130                 //! key strings for logger
131                 std::string log_filename_key;
132                 std::string rotation_key;
133                 std::string max_backup_index_key;
134                 std::string max_file_size_key;
135                 std::string rotation_timing_key;
136                 std::string rotation_timing_value_key;
137
138
139                 //! for transration between log4cxx::LevelPtr and LOGER_LEVEL_TAG
140                 static log4cxx::LevelPtr levelTable[LOGGER_LEVEL_NUM];
141                 //! for transration between string and LOGGER_CATEGORY_TAG
142                 static char categoryTable[][LOGGER_CATEGORY_NUM];
143                 //! holds category-loglevel
144                 log4cxx::LevelPtr categoryLevel[LOGGER_CATEGORY_NUM];
145         
146                 //! LOG_LEVEL_TAG to log4cxx::LevelPtr transrator
147                 virtual inline const log4cxx::LevelPtr toLevel(LOG_LEVEL_TAG level)
148                 {
149                         return levelTable[level];
150                 }
151                 //! log4cxx::LevelPtr to LOG_LEVEL_TAG transrator
152                 virtual inline LOG_LEVEL_TAG toLevelTag(const log4cxx::LevelPtr level)
153                 {
154                         int levelInt = level->toInt();
155                         switch (levelInt) {
156                         case log4cxx::Level::DEBUG_INT:
157                                 return LOG_LV_DEBUG;
158                         case log4cxx::Level::INFO_INT:
159                                 return LOG_LV_INFO;
160                         case log4cxx::Level::WARN_INT:
161                                 return LOG_LV_WARN;
162                         case log4cxx::Level::ERROR_INT:
163                                 return LOG_LV_ERROR;
164                         case log4cxx::Level::FATAL_INT:
165                                 return LOG_LV_FATAL;
166                         default: 
167                                 return LOG_LV_DEBUG;
168                         }
169                 }
170         private:
171                 //! loglevel tagle
172                 LOG_LEVEL_TAG loglevel[LOGGER_CATEGORY_NUM];
173
174         public:
175                 //! initialze function
176                 virtual bool init();
177                 //! Configuration function
178                 virtual void loadConf();
179                 //! Category logger configuration function
180                 virtual void loadCategoryLoggerConf(LOG_CATEGORY_TAG cat);
181                 //1 initialize loglevel table
182                 void initLogLevelTable();
183
184                 /*!
185                  * retrieve category's log level.
186                  *
187                  * @param   category that want to know
188                  * @return  log level
189                  */
190                 virtual inline LOG_LEVEL_TAG getLogLevel(LOG_CATEGORY_TAG cat)
191                 {
192                         if (LOG_LV_NONE == loglevel[cat]) {
193                                 this->loglevel[cat] = toLevelTag(log4cxx::Logger::getLogger(categoryTable[cat])->getLevel());
194                         }
195
196                         return loglevel[cat];
197                 }
198
199                 /*!
200                  * set category's log level.
201                  *
202                  * @param   category to set log level
203                  * @param   level
204                  * @retval  true  succeed
205                  * @retval  false failed
206                  */
207                 virtual inline bool setLogLevel(LOG_CATEGORY_TAG cat, LOG_LEVEL_TAG level)
208                 {
209                         try {
210                                 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(toLevel(level));
211                         }
212                         catch (const std::exception& ex) {
213                                 return false;   
214                         }
215
216                         loglevel[cat] = level;
217                         return true;
218                 }
219
220                 /*!
221                  * output fatal log.
222                  *
223                  * @param   category that logging matter occured
224                  * @param   log message id 
225                  * @param   log message 
226                  * @param   current file 
227                  * @param   current line
228                  * @retrun  void
229                  */
230                 virtual inline void putLogFatal(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
231                 {
232                         char buf[BUF_LEN];
233                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_FATAL, cat, message_id, message.c_str(), hostname);
234                         try {
235                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getFatal(), buf, log4cxx::spi::LocationInfo(file, "", line));
236                         }
237                         catch (const std::exception& ex) {
238                                 std::ostringstream oss;
239                                 oss << "Logging Error (Fatal Log) : " << ex.what();
240                                 errorConf(1, oss.str(), __FILE__, __LINE__);
241                         }
242                 }
243                 /*!
244                  * output error log.
245                  *
246                  * @param   category that logging matter occured
247                  * @param   log message id 
248                  * @param   log message 
249                  * @param   current file 
250                  * @param   current line
251                  * @retrun  void
252                  */
253                 //! output fatal log.
254                 virtual inline void putLogError(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
255                 {
256                         char buf[BUF_LEN];
257                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_ERROR, cat, message_id, message.c_str(), hostname);
258                         try {
259                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getError(), buf, log4cxx::spi::LocationInfo(file, "", line));
260                         }
261                         catch (const std::exception& ex) {
262                                 std::ostringstream oss;
263                                 oss << "Logging Error (Error Log) : " << ex.what();
264                                 errorConf(2, oss.str(), __FILE__, __LINE__);
265                         }
266                 }
267                 /*!
268                  * output warn log.
269                  *
270                  * @param   category that logging matter occured
271                  * @param   log message id 
272                  * @param   log message 
273                  * @param   current file 
274                  * @param   current line
275                  * @retrun  void
276                  */
277                 //! output fatal log.
278                 virtual inline void putLogWarn(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
279                 {
280                         char buf[BUF_LEN];
281                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_WARN, cat, message_id, message.c_str(), hostname);
282                         try {
283                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getWarn(), buf, log4cxx::spi::LocationInfo(file, "", line));
284                         }
285                         catch (const std::exception& ex) {
286                                 std::ostringstream oss;
287                                 oss << "Logging Error (Warn Log) : " << ex.what();
288                                 errorConf(3, oss.str(), __FILE__, __LINE__);
289                         }
290                 }
291                 /*!
292                  * output info log.
293                  *
294                  * @param   category that logging matter occured
295                  * @param   log message id 
296                  * @param   log message 
297                  * @param   current file 
298                  * @param   current line
299                  * @retrun  void
300                  */
301                 //! output fatal log.
302                 virtual inline void putLogInfo(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
303                 {
304                         char buf[BUF_LEN];
305                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_INFO, cat, message_id, message.c_str(), hostname);
306                         try {
307                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getInfo(), buf, log4cxx::spi::LocationInfo(file, "", line));
308                         }
309                         catch (const std::exception& ex) {
310                                 std::ostringstream oss;
311                                 oss << "Logging Error (Info Log) : " << ex.what();
312                                 errorConf(4, oss.str(), __FILE__, __LINE__);
313                         }
314                 }
315                 /*!
316                  * output debug log.
317                  *
318                  * @param   category that logging matter occured
319                  * @param   log message id 
320                  * @param   log message 
321                  * @param   current file 
322                  * @param   current line
323                  * @retrun  void
324                  */
325                 virtual inline void putLogDebug(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
326                 {
327                         char buf[BUF_LEN];
328                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_DEBUG, cat, message_id, message.c_str(), hostname);
329                         try {
330                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getDebug(), buf, log4cxx::spi::LocationInfo(file, "", line));
331                         }
332                         catch (const std::exception& ex) {
333                                 std::ostringstream oss;
334                                 oss << "Logging Error (Debug Log) : " << ex.what();
335                                 errorConf(5, oss.str(), __FILE__, __LINE__);
336                         }
337                 }
338         };
339 }       //namespace l7vs
340 #endif  //__LOGGER_IMPL_H__