OSDN Git Service

Fixed chkconfig option
[ultramonkey-l7/ultramonkey-l7-v2.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 #if !defined(LOGGER_PROCESS_VSD) && !defined(LOGGER_PROCESS_ADM) && !defined(LOGGER_PROCESS_SNM)
43 #define LOGGER_PROCESS_VSD
44 #endif
45
46 #if defined(LOGGER_PROCESS_VSD)
47 #define LOGGER_PROCESS_ID "VSD"
48 #elif defined(LOGGER_PROCESS_ADM)
49 #define LOGGER_PROCESS_ID "ADM"
50 #else
51 #define LOGGER_PROCESS_ID "SNM"
52 #endif
53
54 #if defined(LOGGER_PROCESS_VSD)
55 #include "l7vs_snmpbridge.h"
56 #endif
57
58 #define LOGGER_NULL "/dev/null"
59
60 #define HOST_NAME_LEN (256)
61
62 namespace log4cxx
63 {
64         typedef helpers::ObjectPtrT<RollingFileAppender> RollingFileAppenderPtr;
65 }
66
67 namespace l7vs
68 {
69         /*!
70          *  Logger implement class.
71          *  operate log4cxx library. 
72          *  this is singleton class. 
73          */
74         class LoggerImpl
75         {
76         public:
77                 //! returns current instance.
78                 static LoggerImpl& getInstance();
79         protected:
80                 //! default constructor initialize member variables.
81                 LoggerImpl() : initialized(false), logFilename(""), rotation(LOG_ROT_SIZE), maxBackupIndex(0), maxFileSize(0), rotationTiming(LOG_TIM_YEAR), rotationTimingValue("")
82                 {
83                         levelTable[LOG_LV_NONE] = log4cxx::Level::getDebug();
84                         levelTable[LOG_LV_DEBUG] = log4cxx::Level::getDebug();
85                         levelTable[LOG_LV_INFO] = log4cxx::Level::getInfo();
86                         levelTable[LOG_LV_WARN] = log4cxx::Level::getWarn();
87                         levelTable[LOG_LV_ERROR] = log4cxx::Level::getError();
88                         levelTable[LOG_LV_FATAL] = log4cxx::Level::getFatal();
89 #if defined(LOGGER_PROCESS_VSD)
90                         loggerCategory = LOG_CAT_L7VSD_LOGGER;
91                         loggerProcess = LOG_MOD_L7VSD;
92                         //set default log level
93                         for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
94                                 categoryLevel[cat] = log4cxx::Level::getError();
95                         }
96 #elif defined(LOGGER_PROCESS_ADM)
97                         loggerCategory = LOG_CAT_L7VSADM_LOGGER;
98                         loggerProcess = LOG_MOD_L7VSADM;
99                         //set default log level
100                         for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
101                                 categoryLevel[cat] = log4cxx::Level::getError();
102                         }
103 #else   //LOGGER_PROCESS_SNM
104                         loggerCategory = LOG_CAT_SNMPAGENT_LOGGER;
105                         loggerProcess = LOG_MOD_SNMPAGENT;
106                         //set default log level
107                         for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
108                                 categoryLevel[cat] = log4cxx::Level::getError();
109                         }
110 #endif
111                 }
112                 //! cpoy constructor disable
113                 LoggerImpl( const LoggerImpl& );
114                 //! operator= disable
115                 LoggerImpl& operator=( const LoggerImpl& );
116                 //! destructor.
117                 virtual ~LoggerImpl() {}
118                 //! static Logger instance
119                 static LoggerImpl* instance;
120                 //! initialized flag
121                 bool initialized;
122                 //! logger category
123                 LOG_CATEGORY_TAG loggerCategory;
124                 //! logger process
125                 LOG_MODULE_TAG loggerProcess;
126                 //! hostname
127                 char hostname[HOST_NAME_LEN];
128
129                 //! if error occured, switch appenders to syslogappender and fileappender(/dev/console)
130                 virtual void errorConf(unsigned int messageId, const std::string& errorMessage, const char* file, int line);
131
132                 //! base logFileanme
133                 std::string logFilename;
134                 //! rotation way (size base, date base, or both size and date base)
135                 LOG_ROTATION_TAG rotation;
136                 //! number of backup log file
137                 unsigned int maxBackupIndex;
138                 //! max size of log file
139                 unsigned long long maxFileSize;
140                 /*!
141                  *  rotation timing
142                  *  "year"   = yearly
143                  *  "monthr" = monthly
144                  *  "week"   = weekly
145                  *  "date"   = daily
146                  *  "hour"   = hourly
147                  */
148                 LOG_ROTATION_TIMING_TAG rotationTiming;
149                 /*!
150                  *  rotation timing value 
151                  *
152                  *  rotation timing     value
153                  *  -------------------------------------------------
154                  *  year                "03051500"      (3/5 15:00)
155                  *  month               "051100"        (5 11:00)
156                  *  week                "12000"         (mon 20:00) sun = 0, sat = 6
157                  *  date                "1500"          (15:00)
158                  *  hour                "45"            (45)
159                  */
160                 std::string rotationTimingValue;
161
162                 //! for transration between log4cxx::LevelPtr and LOGER_LEVEL_TAG
163                 static log4cxx::LevelPtr levelTable[LOGGER_LEVEL_NUM];
164                 //! for transration between string and LOGGER_CATEGORY_TAG
165                 static char categoryTable[][LOGGER_CATEGORY_NUM];
166                 //! holds category-loglevel
167                 log4cxx::LevelPtr categoryLevel[LOGGER_CATEGORY_NUM];
168         
169                 //! LOG_LEVEL_TAG to log4cxx::LevelPtr transrator
170                 virtual inline const log4cxx::LevelPtr toLevel(LOG_LEVEL_TAG level)
171                 {
172                         return levelTable[level];
173                 }
174                 //! log4cxx::LevelPtr to LOG_LEVEL_TAG transrator
175                 virtual inline LOG_LEVEL_TAG toLevelTag(const log4cxx::LevelPtr level)
176                 {
177                         int levelInt = level->toInt();
178                         switch (levelInt) {
179                         case log4cxx::Level::DEBUG_INT:
180                                 return LOG_LV_DEBUG;
181                         case log4cxx::Level::INFO_INT:
182                                 return LOG_LV_INFO;
183                         case log4cxx::Level::WARN_INT:
184                                 return LOG_LV_WARN;
185                         case log4cxx::Level::ERROR_INT:
186                                 return LOG_LV_ERROR;
187                         case log4cxx::Level::FATAL_INT:
188                                 return LOG_LV_FATAL;
189                         default: 
190                                 return LOG_LV_DEBUG;
191                         }
192                 }
193         private:
194                 //! loglevel tagle
195                 LOG_LEVEL_TAG loglevel[LOGGER_CATEGORY_NUM];
196
197         public:
198                 //! initialze function
199                 virtual bool init();
200                 //! Configuration function
201                 virtual void loadConf();
202                 //1 initialize loglevel table
203                 void initLogLevelTable();
204
205                 /*!
206                  * retrieve category's log level.
207                  *
208                  * @param   category that want to know
209                  * @return  log level
210                  */
211                 virtual inline LOG_LEVEL_TAG getLogLevel(LOG_CATEGORY_TAG cat)
212                 {
213 #if defined(LOGGER_PROCESS_VSD)
214                         if (cat >= getCategoryRangeStart(LOG_MOD_SNMPAGENT) && cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT)) {
215                                 return l7vs_snmpbridge_get_loglevel(cat);
216                         }
217 #endif
218                         if (LOG_LV_NONE == loglevel[cat]) {
219                                 this->loglevel[cat] = toLevelTag(log4cxx::Logger::getLogger(categoryTable[cat])->getLevel());
220                         }
221
222                         return loglevel[cat];
223                         //return toLevelTag(log4cxx::Logger::getLogger(categoryTable[cat])->getLevel());
224                 }
225
226                 /*!
227                  * set category's log level.
228                  *
229                  * @param   category to set log level
230                  * @param   level
231                  * @retval  true  succeed
232                  * @retval  false failed
233                  */
234                 virtual inline bool setLogLevel(LOG_CATEGORY_TAG cat, LOG_LEVEL_TAG level)
235                 {
236                         try {
237 #if defined(LOGGER_PROCESS_VSD)
238                                 if (cat >= getCategoryRangeStart(LOG_MOD_SNMPAGENT) && cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT)) {
239                                         l7vs_snmpbridge_change_loglevel(cat, level);
240                                 }
241 #endif
242                                 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(toLevel(level));
243                         }
244                         catch (const std::exception& ex) {
245                                 return false;   
246                         }
247
248                         loglevel[cat] = level;
249                         return true;
250                 }
251
252                 /*!
253                  * output fatal log.
254                  *
255                  * @param   category that logging matter occured
256                  * @param   log message id 
257                  * @param   log message 
258                  * @param   current file 
259                  * @param   current line
260                  * @retrun  void
261                  */
262                 virtual inline void putLogFatal(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
263                 {
264                         char buf[BUF_LEN];
265                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_FATAL, cat, message_id, message.c_str(), hostname);
266                         try {
267                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getFatal(), buf, log4cxx::spi::LocationInfo(file, "", line));
268                         }
269                         catch (const std::exception& ex) {
270                                 std::ostringstream oss;
271                                 oss << "Logging Error (Fatal Log) : " << ex.what();
272                                 errorConf(1, oss.str(), __FILE__, __LINE__);
273                         }
274                 }
275                 /*!
276                  * output error log.
277                  *
278                  * @param   category that logging matter occured
279                  * @param   log message id 
280                  * @param   log message 
281                  * @param   current file 
282                  * @param   current line
283                  * @retrun  void
284                  */
285                 //! output fatal log.
286                 virtual inline void putLogError(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
287                 {
288                         char buf[BUF_LEN];
289                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_ERROR, cat, message_id, message.c_str(), hostname);
290                         try {
291                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getError(), buf, log4cxx::spi::LocationInfo(file, "", line));
292                         }
293                         catch (const std::exception& ex) {
294                                 std::ostringstream oss;
295                                 oss << "Logging Error (Error Log) : " << ex.what();
296                                 errorConf(2, oss.str(), __FILE__, __LINE__);
297                         }
298                 }
299                 /*!
300                  * output warn log.
301                  *
302                  * @param   category that logging matter occured
303                  * @param   log message id 
304                  * @param   log message 
305                  * @param   current file 
306                  * @param   current line
307                  * @retrun  void
308                  */
309                 //! output fatal log.
310                 virtual inline void putLogWarn(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
311                 {
312                         char buf[BUF_LEN];
313                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_WARN, cat, message_id, message.c_str(), hostname);
314                         try {
315                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getWarn(), buf, log4cxx::spi::LocationInfo(file, "", line));
316                         }
317                         catch (const std::exception& ex) {
318                                 std::ostringstream oss;
319                                 oss << "Logging Error (Warn Log) : " << ex.what();
320                                 errorConf(3, oss.str(), __FILE__, __LINE__);
321                         }
322                 }
323                 /*!
324                  * output info log.
325                  *
326                  * @param   category that logging matter occured
327                  * @param   log message id 
328                  * @param   log message 
329                  * @param   current file 
330                  * @param   current line
331                  * @retrun  void
332                  */
333                 //! output fatal log.
334                 virtual inline void putLogInfo(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
335                 {
336                         char buf[BUF_LEN];
337                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_INFO, cat, message_id, message.c_str(), hostname);
338                         try {
339                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getInfo(), buf, log4cxx::spi::LocationInfo(file, "", line));
340                         }
341                         catch (const std::exception& ex) {
342                                 std::ostringstream oss;
343                                 oss << "Logging Error (Info Log) : " << ex.what();
344                                 errorConf(4, oss.str(), __FILE__, __LINE__);
345                         }
346                 }
347                 /*!
348                  * output debug log.
349                  *
350                  * @param   category that logging matter occured
351                  * @param   log message id 
352                  * @param   log message 
353                  * @param   current file 
354                  * @param   current line
355                  * @retrun  void
356                  */
357                 virtual inline void putLogDebug(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
358                 {
359                         char buf[BUF_LEN];
360                         snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_DEBUG, cat, message_id, message.c_str(), hostname);
361                         try {
362                                 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getDebug(), buf, log4cxx::spi::LocationInfo(file, "", line));
363                         }
364                         catch (const std::exception& ex) {
365                                 std::ostringstream oss;
366                                 oss << "Logging Error (Debug Log) : " << ex.what();
367                                 errorConf(5, oss.str(), __FILE__, __LINE__);
368                         }
369                 }
370
371                 /*!
372                  * return start category by using module.
373                  *
374                  * @param   module
375                  * @retrun  start category
376                  */
377                 virtual inline LOG_CATEGORY_TAG getCategoryRangeStart(LOG_MODULE_TAG mod)
378                 {
379                         switch (mod) {
380                                 case LOG_MOD_L7VSADM: return LOG_CAT_L7VSADM_PARSE;
381                                 case LOG_MOD_SNMPAGENT: return LOG_CAT_SNMPAGENT_START_STOP;
382                                 default: return LOG_CAT_L7VSD_NETWORK;
383                         }
384                 }
385                 /*!
386                  * return end category by using module.
387                  *
388                  * @param   module
389                  * @retrun  end category
390                  */
391                 virtual inline LOG_CATEGORY_TAG getCategoryRangeEnd(LOG_MODULE_TAG mod)
392                 {
393                         switch (mod) {
394                                 case LOG_MOD_L7VSADM: return LOG_CAT_L7VSADM_MODULE;
395                                 case LOG_MOD_SNMPAGENT: return LOG_CAT_SNMPAGENT_PARAMETER;
396                                 default: return LOG_CAT_L7VSD_MODULE;
397                         }
398                 }
399         };
400 }       //namespace l7vs
401 #endif  //__LOGGER_IMPL_H__