OSDN Git Service

formatted all files with 'astyle -A8 -HUpc -k3 -z2 -r ./*.cpp ./*.c ./*.h'
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / 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) 2009  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 <sstream>
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 <boost/format.hpp>
37 #include <boost/tr1/unordered_map.hpp>
38 #include <boost/foreach.hpp>
39 #include "logger_enum.h"
40 #include "logger_rotation_enum.h"
41 #include "appender_property.h"
42
43 #if !defined(LOGGER_PROCESS_VSD) && !defined(LOGGER_PROCESS_ADM) && !defined(LOGGER_PROCESS_SNM)
44 #define LOGGER_PROCESS_VSD
45 #endif
46
47 #if defined(LOGGER_PROCESS_VSD)
48 #define LOGGER_PROCESS_ID "VSD"
49 #elif defined(LOGGER_PROCESS_ADM)
50 #define LOGGER_PROCESS_ID "ADM"
51 #else
52 #define LOGGER_PROCESS_ID "SNM"
53 #endif
54
55 #define LOGGER_PROCESS_PROTOCOL_MODULE_ID "PRM"
56
57 #define LOGGER_PROCESS_SCHEDULE_MODULE_ID "SCM"
58
59 #define LOGGER_NULL "/dev/null"
60
61 #define LOGGER_LEVEL_NUM (6)
62
63 namespace log4cxx
64 {
65 typedef helpers::ObjectPtrT<RollingFileAppender> RollingFileAppenderPtr;
66 }
67
68 namespace l7vs
69 {
70
71 //! @class LoggerImpl
72 //! @brief Logger implement class.
73 //! @brief this class manage logger setting and logging by log4cxx.
74 class LoggerImpl
75 {
76 protected:
77         //! typedef    category <-> level map
78         typedef    std::map< LOG_CATEGORY_TAG, LOG_LEVEL_TAG>
79         category_level_map_type;
80         //! typedef category <-> level map read only
81         typedef std::tr1::unordered_map< LOG_CATEGORY_TAG, LOG_LEVEL_TAG>
82         category_level_read_map_type;
83         //! typedef    categoryname <-> CATEGORY_TAG map
84         typedef    std::map< std::string, LOG_CATEGORY_TAG>
85         name_category_map_type;
86         //! typedef CATEGORY_TAG <-> categoryname map
87         typedef    std::map< LOG_CATEGORY_TAG, std::string>
88         category_name_map_type;
89 public:
90         //! returns current instance.
91         static LoggerImpl &getInstance();
92
93         //! initialze function
94         virtual bool init();
95
96         //! Configuration function
97         virtual    void loadConf();
98
99         /*!
100          * retrieve category's log level.
101          *
102          * @param   category that want to know
103          * @return  log level
104          */
105         virtual inline LOG_LEVEL_TAG getLogLevel(LOG_CATEGORY_TAG cat) {
106                 return category_level_read_map[cat];
107         }
108
109         /*!
110          * retrieve all category's log level.
111          *
112          * @param[out]   category level list
113          */
114         virtual inline void getLogLevelAll(category_level_list_type &list) {
115                 category_level_read_map.clear();
116                 BOOST_FOREACH(category_level_map_type::value_type const & itr, category_level_map) {
117                         category_level_read_map[itr.first] = itr.second;
118                         list.push_back(std::make_pair(itr.first, itr.second));
119                 }
120         }
121
122         /*!
123          * set category's log level.
124          *
125          * @param   category to set log level
126          * @param   level
127          * @retval  true  succeed
128          * @retval  false failed
129          */
130         virtual inline bool setLogLevel(LOG_CATEGORY_TAG cat, LOG_LEVEL_TAG level) {
131                 category_level_map_type::iterator lv_itr = category_level_map.find(cat);
132                 lv_itr->second = level;
133
134                 category_level_read_map.clear();
135                 BOOST_FOREACH(category_level_map_type::value_type const & itr, category_level_map) {
136                         category_level_read_map[itr.first] = itr.second;
137                 }
138
139                 category_name_map_type::iterator categoryname_itr = category_name_map.find(cat);
140
141                 try {
142                         log4cxx::Logger::getLogger(categoryname_itr->second)->setLevel(log4cxx::Level::toLevel(levelTable[level]));
143                 } catch (const std::exception &ex) {
144                         return false;
145                 }
146                 return true;
147         }
148
149         /*!
150          * set all category's log level.
151          *
152          * @param   category to set log level
153          * @param   level
154          * @retval  true  succeed
155          * @retval  false failed
156          */
157         virtual inline bool setLogLevelAll(LOG_LEVEL_TAG level) {
158                 category_level_read_map.clear();
159                 BOOST_FOREACH(category_level_map_type::value_type & itr, category_level_map) {
160                         itr.second = level;
161                         category_level_read_map[itr.first] = itr.second;
162
163                         category_name_map_type::iterator categoryname_itr = category_name_map.find(itr.first);
164                         try {
165                                 log4cxx::Logger::getLogger(categoryname_itr->second)->setLevel(log4cxx::Level::toLevel(levelTable[level]));
166                         } catch (const std::exception &ex) {
167                                 return false;
168                         }
169                 }
170                 return true;
171         }
172
173         /*!
174          * output fatal log.
175          *
176          * @param   category that logging matter occured
177          * @param   log message id
178          * @param   log message
179          * @param   current file
180          * @param   current line
181          * @retrun  void
182          */
183         virtual inline void putLogFatal(LOG_CATEGORY_TAG cat,
184                                         const unsigned int message_id,
185                                         const std::string &message,
186                                         const char *file,
187                                         int line) {
188                 std::stringstream   buf;
189
190
191                 switch (cat) {
192                 case    LOG_CAT_PROTOCOL:
193
194                         buf << boost::format("%s%d%07d %s %s")
195                             % LOGGER_PROCESS_PROTOCOL_MODULE_ID
196                             % LOG_LV_FATAL
197                             % message_id
198                             % message.c_str()
199                             % hostname;
200
201                         break;
202                 case    LOG_CAT_SCHEDULE:
203
204                         buf << boost::format("%s%d%07d %s %s")
205                             % LOGGER_PROCESS_SCHEDULE_MODULE_ID
206                             % LOG_LV_FATAL
207                             % message_id
208                             % message.c_str()
209                             % hostname;
210
211                         break;
212                 default:
213
214                         buf << boost::format("%s%d%02d%05d %s %s")
215                             % LOGGER_PROCESS_ID
216                             % LOG_LV_FATAL
217                             % cat
218                             % message_id
219                             % message.c_str()
220                             % hostname;
221
222                 }
223
224                 try {
225                         category_name_map_type::iterator categoryname_itr = category_name_map.find(cat);
226                         log4cxx::Logger::getLogger(categoryname_itr->second)->forcedLog(log4cxx::Level::getFatal(),
227                                         buf.str(),
228                                         log4cxx::spi::LocationInfo(file, "", line));
229                         // send_trap
230                         if (snmpSendtrap && (LOG_CAT_L7VSD_SNMPBRIDGE != cat)) {
231                                 snmpSendtrap(buf.str());
232                         }
233                 } catch (const std::exception &ex) {
234                         std::ostringstream oss;
235                         oss << "Logging Error (Fatal Log) : " << ex.what();
236                         errorConf(3, oss.str(), __FILE__, __LINE__);
237                 }
238         }
239         /*!
240          * output error log.
241          *
242          * @param   category that logging matter occured
243          * @param   log message id
244          * @param   log message
245          * @param   current file
246          * @param   current line
247          * @retrun  void
248          */
249         virtual inline void putLogError(LOG_CATEGORY_TAG cat,
250                                         const unsigned int message_id,
251                                         const std::string &message,
252                                         const char *file,
253                                         int line) {
254                 std::stringstream    buf;
255
256                 switch (cat) {
257                 case    LOG_CAT_PROTOCOL:
258
259                         buf << boost::format("%s%d%07d %s %s")
260                             % LOGGER_PROCESS_PROTOCOL_MODULE_ID
261                             % LOG_LV_ERROR
262                             % message_id
263                             % message.c_str()
264                             % hostname;
265
266                         break;
267                 case    LOG_CAT_SCHEDULE:
268
269                         buf << boost::format("%s%d%07d %s %s")
270                             % LOGGER_PROCESS_SCHEDULE_MODULE_ID
271                             % LOG_LV_ERROR
272                             % message_id
273                             % message.c_str()
274                             % hostname;
275
276                         break;
277                 default:
278
279                         buf << boost::format("%s%d%02d%05d %s %s")
280                             % LOGGER_PROCESS_ID
281                             % LOG_LV_ERROR
282                             % cat
283                             % message_id
284                             % message.c_str()
285                             % hostname;
286
287                 }
288
289                 try {
290                         category_name_map_type::iterator categoryname_itr = category_name_map.find(cat);
291                         log4cxx::Logger::getLogger(categoryname_itr->second)->forcedLog(log4cxx::Level::getError(),
292                                         buf.str(),
293                                         log4cxx::spi::LocationInfo(file, "", line));
294                         // send_trap
295                         if (snmpSendtrap && (LOG_CAT_L7VSD_SNMPBRIDGE != cat)) {
296                                 snmpSendtrap(buf.str());
297                         }
298                 } catch (const std::exception &ex) {
299                         std::ostringstream oss;
300                         oss << "Logging Error (Error Log) : " << ex.what();
301                         errorConf(4, oss.str(), __FILE__, __LINE__);
302                 }
303         }
304         /*!
305          * output warn log.
306          *
307          * @param   category that logging matter occured
308          * @param   log message id
309          * @param   log message
310          * @param   current file
311          * @param   current line
312          * @retrun  void
313          */
314         virtual inline void putLogWarn(LOG_CATEGORY_TAG cat,
315                                        const unsigned int message_id,
316                                        const std::string &message,
317                                        const char *file,
318                                        int line) {
319                 std::stringstream buf;
320
321                 switch (cat) {
322                 case    LOG_CAT_PROTOCOL:
323
324                         buf << boost::format("%s%d%07d %s %s")
325                             % LOGGER_PROCESS_PROTOCOL_MODULE_ID
326                             % LOG_LV_WARN
327                             % message_id
328                             % message.c_str()
329                             % hostname;
330
331                         break;
332                 case    LOG_CAT_SCHEDULE:
333
334                         buf << boost::format("%s%d%07d %s %s")
335                             % LOGGER_PROCESS_SCHEDULE_MODULE_ID
336                             % LOG_LV_WARN
337                             % message_id
338                             % message.c_str()
339                             % hostname;
340
341                         break;
342                 default:
343
344                         buf << boost::format("%s%d%02d%05d %s %s")
345                             % LOGGER_PROCESS_ID
346                             % LOG_LV_WARN
347                             % cat
348                             % message_id
349                             % message.c_str()
350                             % hostname;
351
352                 }
353
354                 try {
355                         category_name_map_type::iterator categoryname_itr = category_name_map.find(cat);
356                         log4cxx::Logger::getLogger(categoryname_itr->second)->forcedLog(log4cxx::Level::getWarn(),
357                                         buf.str(),
358                                         log4cxx::spi::LocationInfo(file, "", line));
359                 } catch (const std::exception &ex) {
360                         std::ostringstream oss;
361                         oss << "Logging Error (Warn Log) : " << ex.what();
362                         errorConf(5, oss.str(), __FILE__, __LINE__);
363                 }
364         }
365         /*!
366          * output info log.
367          *
368          * @param   category that logging matter occured
369          * @param   log message id
370          * @param   log message
371          * @param   current file
372          * @param   current line
373          * @retrun  void
374          */
375         virtual inline void putLogInfo(LOG_CATEGORY_TAG cat,
376                                        const unsigned int message_id,
377                                        const std::string &message,
378                                        const char *file,
379                                        int line) {
380                 std::stringstream    buf;
381
382                 switch (cat) {
383                 case    LOG_CAT_PROTOCOL:
384
385                         buf << boost::format("%s%d%07d %s %s")
386                             % LOGGER_PROCESS_PROTOCOL_MODULE_ID
387                             % LOG_LV_INFO
388                             % message_id
389                             % message.c_str()
390                             % hostname;
391
392                         break;
393                 case    LOG_CAT_SCHEDULE:
394
395                         buf << boost::format("%s%d%07d %s %s")
396                             % LOGGER_PROCESS_SCHEDULE_MODULE_ID
397                             % LOG_LV_INFO
398                             % message_id
399                             % message.c_str()
400                             % hostname;
401
402                         break;
403                 default:
404
405                         buf << boost::format("%s%d%02d%05d %s %s")
406                             % LOGGER_PROCESS_ID
407                             % LOG_LV_INFO
408                             % cat
409                             % message_id
410                             % message.c_str()
411                             % hostname;
412
413                 }
414
415                 try {
416                         category_name_map_type::iterator categoryname_itr = category_name_map.find(cat);
417                         log4cxx::Logger::getLogger(categoryname_itr->second)->forcedLog(log4cxx::Level::getInfo(),
418                                         buf.str(),
419                                         log4cxx::spi::LocationInfo(file, "", line));
420                 } catch (const std::exception &ex) {
421                         std::ostringstream oss;
422                         oss << "Logging Error (Info Log) : " << ex.what();
423                         errorConf(6, oss.str(), __FILE__, __LINE__);
424                 }
425         }
426         /*!
427          * output debug log.
428          *
429          * @param   category that logging matter occured
430          * @param   log message id
431          * @param   log message
432          * @param   current file
433          * @param   current line
434          * @retrun  void
435          */
436         virtual inline void putLogDebug(LOG_CATEGORY_TAG cat,
437                                         const unsigned int message_id,
438                                         const std::string &message,
439                                         const char *file,
440                                         int line) {
441                 std::stringstream    buf;
442                 std::string logger_process_id = LOGGER_PROCESS_ID;
443
444                 switch (cat) {
445                 case    LOG_CAT_PROTOCOL:
446
447                         buf << boost::format("%s%d%07d %s %s")
448                             % LOGGER_PROCESS_PROTOCOL_MODULE_ID
449                             % LOG_LV_DEBUG
450                             % message_id
451                             % message.c_str()
452                             % hostname;
453
454                         break;
455                 case    LOG_CAT_SCHEDULE:
456
457                         buf << boost::format("%s%d%07d %s %s")
458                             % LOGGER_PROCESS_SCHEDULE_MODULE_ID
459                             % LOG_LV_DEBUG
460                             % message_id
461                             % message.c_str()
462                             % hostname;
463
464                         break;
465                 default:
466
467                         buf << boost::format("%s%d%02d%05d %s %s")
468                             % LOGGER_PROCESS_ID
469                             % LOG_LV_DEBUG
470                             % cat
471                             % message_id
472                             % message.c_str()
473                             % hostname;
474
475                 }
476
477
478                 try {
479                         category_name_map_type::iterator categoryname_itr = category_name_map.find(cat);
480                         log4cxx::Logger::getLogger(categoryname_itr->second)->forcedLog(log4cxx::Level::getDebug(),
481                                         buf.str(), log4cxx::spi::LocationInfo(file, "", line));
482                 } catch (const std::exception &ex) {
483                         std::ostringstream oss;
484                         oss << "Logging Error (Debug Log) : " << ex.what();
485                         errorConf(7, oss.str(), __FILE__, __LINE__);
486                 }
487         }
488
489         /*!
490          * set snmp sendtrap function
491          *
492          * @param   snmp send trap function object
493          * @retrun  void
494          */
495         void    setSnmpSendtrap(const snmpSendtrapFuncType func) {
496                 snmpSendtrap = func;
497         }
498
499 protected:
500         //! default constructor initialize member variables.
501         LoggerImpl();
502
503         //! cpoy constructor disable
504         LoggerImpl(const LoggerImpl &);
505
506         //! operator= disable
507         LoggerImpl &operator=(const LoggerImpl &);
508
509         //! LOG_LEVEL_TAG to log4cxx::LevelPtr transrator
510         virtual inline const log4cxx::LevelPtr toLevel(LOG_LEVEL_TAG level)    {
511                 return log4cxx::Level::toLevel(levelTable[level]);
512         }
513
514         //! if error occured, switch appenders to syslogappender and fileappender(/dev/console)
515         virtual void errorConf(unsigned int messageId,
516                                const std::string &errorMessage,
517                                const char *file,
518                                int line);
519
520         virtual void logic_error(const unsigned int, const std::string &, const char *, const unsigned int);
521
522         /*
523             //! log4cxx::LevelPtr to LOG_LEVEL_TAG transrator
524             virtual inline LOG_LEVEL_TAG toLevelTag(const log4cxx::LevelPtr level){
525                 int levelInt = level->toInt();
526                 switch (levelInt) {
527                 case log4cxx::Level::DEBUG_INT:
528                     return LOG_LV_DEBUG;
529                 case log4cxx::Level::INFO_INT:
530                     return LOG_LV_INFO;
531                 case log4cxx::Level::WARN_INT:
532                     return LOG_LV_WARN;
533                 case log4cxx::Level::ERROR_INT:
534                     return LOG_LV_ERROR;
535                 case log4cxx::Level::FATAL_INT:
536                     return LOG_LV_FATAL;
537                 default:
538                     return LOG_LV_DEBUG;
539                 }
540             }
541         */
542
543         //! destructor.
544         virtual ~LoggerImpl() {}
545         //! initialized flag
546         bool initialized;
547         //! hostname
548         std::string hostname;
549
550         //log4cxx::Level* levelTable[LOGGER_LEVEL_NUM];
551         int    levelTable[LOGGER_LEVEL_NUM];
552
553         //! category - loglevel hash map
554         category_level_map_type    category_level_map;
555         category_level_read_map_type category_level_read_map;
556         //! category string -> logcateogry hash map
557         name_category_map_type    name_category_map;
558         //! log_category -> category string hash map
559         category_name_map_type    category_name_map;
560
561         appender_property    normal_log_property;
562         appender_property    access_log_property;
563
564         //! snmp trap function
565         snmpSendtrapFuncType    snmpSendtrap;
566
567 };
568
569 }    //namespace l7vs
570 #endif    //__LOGGER_IMPL_H__