1 /*
2     LinKNX KNX home automation platform
3     Copyright (C) 2007 Jean-François Meessen <linknx@ouaye.net>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 #ifndef LOGGER_H_
21 #define LOGGER_H_
22 
23 #include "config.h"
24 #include "ticpp.h"
25 
26 class Logging
27 {
28 public:
instance()29     static Logging* instance()
30     {
31         if (instance_m == 0)
32             instance_m = new Logging();
33         return instance_m;
34     };
reset()35     static void reset()
36     {
37         if (instance_m)
38             delete instance_m;
39         instance_m = 0;
40     };
41 
42     void importXml(ticpp::Element* pConfig);
43     void exportXml(ticpp::Element* pConfig);
44 
defaultConfig()45     void defaultConfig() { importXml(NULL); };
46 
47 private:
Logging()48     Logging() : maxSize_m(-1), maxIndex_m(0) {};
49 
50     std::string conffile_m;
51     std::string output_m;
52     std::string format_m;
53     std::string level_m;
54     int maxSize_m;
55     int maxIndex_m;
56     static Logging* instance_m;
57 };
58 
59 #ifdef HAVE_LOG4CPP
60 #include    <log4cpp/Category.hh>
61 
62 #define Logger log4cpp::Category
63 #define ErrStream log4cpp::CategoryStream
64 #define WarnStream log4cpp::CategoryStream
65 #define LogStream log4cpp::CategoryStream
66 #define DbgStream log4cpp::CategoryStream
67 #define endlog log4cpp::eol
68 
69 #else
70 
71 #include <iostream>
72 #include <map>
73 
74 #define LOG_SHOW_ERROR 1
75 #define LOG_SHOW_WARN 1
76 #define LOG_SHOW_INFO 1
77 #undef  LOG_SHOW_DEBUG
78 
79 
80 #ifdef LOG_SHOW_ERROR
81 #define ErrStream std::ostream&
82 #else
83 #define ErrStream DummyStream&
84 #endif
85 
86 #ifdef LOG_SHOW_WARN
87 #define WarnStream std::ostream&
88 #else
89 #define WarnStream DummyStream&
90 #endif
91 
92 #ifdef LOG_SHOW_INFO
93 #define LogStream std::ostream&
94 #else
95 #define LogStream DummyStream&
96 #endif
97 
98 #ifdef LOG_SHOW_DEBUG
99 #define DbgStream std::ostream&
100 #else
101 #define DbgStream DummyStream&
102 #endif
103 #define endlog std::endl
104 
105 class DummyStream
106 {
107 public:
108     template<class T>
109     DummyStream& operator<<(T val) { return *this; };
110     DummyStream& operator<< (std::ostream& ( *pf )(std::ostream&)) { return *this; };
111     static DummyStream dummy;
112 };
113 
114 class NullStreamBuf : public std::streambuf
115 {
116 protected:
overflow(int_type c)117     int_type overflow (int_type c) { return c; }
sync()118     int sync () { return 0; }
119 };
120 
121 class Logger
122 {
123 public:
124     static Logger& getInstance(const char* cat);
125     Logger(const char* cat);
126     ErrStream errorStream();
127     WarnStream warnStream();
128     LogStream infoStream();
129     DbgStream debugStream();
130     bool isDebugEnabled();
131     friend class Logging;
132 private:
133     std::string cat_m;
134     typedef std::pair<std::string ,Logger*> LoggerPair_t;
135     typedef std::map<std::string ,Logger*> LoggerMap_t;
136     static LoggerMap_t* getLoggerMap();
137     static int level_m; // 10=DEBUG, 20=INFO, 30=NOTICE, 40=WARN, 50=ERROR,
138     static bool timestamp_m;
139     static std::ostream nullStream_m;
140     static NullStreamBuf nullStreamBuf_m;
141 
142     std::ostream& addPrefix(std::ostream &s, const char* level);
143 };
144 #endif
145 
146 ErrStream errorStream(const char* cat);
147 WarnStream warnStream(const char* cat);
148 LogStream infoStream(const char* cat);
149 DbgStream debugStream(const char* cat);
150 bool isDebugEnabled(const char* cat);
151 
152 class ErrorMessage : public std::stringstream
153 {
154 public:
logAndThrow(Logger & logger)155     void logAndThrow(Logger &logger) {
156         std::string s = str();
157         logger.errorStream() << s << endlog;
158         throw ticpp::Exception(s);
159     };
160 };
161 
162 #endif /*LOGGER_H_*/
163