Rev 1964 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1964 | Rev 1990 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2012 Mattias Runge |
3 | * Copyright (C) 2012 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "log.h" |
21 | #include "log.h" |
| 22 | 22 | ||
| 23 | #include <iostream> |
23 | #include <iostream> |
| 24 | #include <stdarg.h> |
24 | #include <stdarg.h> |
| 25 | #include <string.h> |
25 | #include <string.h> |
| 26 | 26 | ||
| 27 | #include <boost/date_time.hpp> |
27 | #include <boost/date_time.hpp> |
| 28 | #include <boost/thread/mutex.hpp> |
28 | #include <boost/thread/mutex.hpp> |
| 29 | 29 | ||
| 30 | namespace atom { |
30 | namespace atom { |
| 31 | namespace log { |
31 | namespace log { |
| 32 | 32 | ||
| 33 | static boost::mutex mutex_; |
33 | static boost::mutex mutex_; |
| 34 | static std::ofstream file_; |
34 | static std::ofstream file_; |
| 35 | static Level level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO); |
35 | static Level level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO); |
| 36 | static const uint32_t kMaxBufferSize = 4096; |
36 | static const uint32_t kMaxBufferSize = 4096; |
| 37 | 37 | ||
| 38 | void SetLevel(Level level) |
38 | void SetLevel(Level level) |
| 39 | { |
39 | { |
| 40 | level_ = level; |
40 | level_ = level; |
| 41 | } |
41 | } |
| 42 | 42 | ||
| 43 | void SetLevelByString(std::string level_string) |
43 | void SetLevelByString(std::string level_string) |
| 44 | { |
44 | { |
| 45 | level_ = LOG_LEVEL_NONE; |
45 | level_ = LOG_LEVEL_NONE; |
| 46 | 46 | ||
| 47 | if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos) |
47 | if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos) |
| 48 | { |
48 | { |
| 49 | level_ = (Level)(level_ | LOG_LEVEL_ERROR); |
49 | level_ = (Level)(level_ | LOG_LEVEL_ERROR); |
| 50 | } |
50 | } |
| 51 | 51 | ||
| 52 | if (level_string.find("LOG_LEVEL_EXCEPTION") != std::string::npos) |
52 | if (level_string.find("LOG_LEVEL_EXCEPTION") != std::string::npos) |
| 53 | { |
53 | { |
| 54 | level_ = (Level)(level_ | LOG_LEVEL_EXCEPTION); |
54 | level_ = (Level)(level_ | LOG_LEVEL_EXCEPTION); |
| 55 | } |
55 | } |
| 56 | 56 | ||
| 57 | if (level_string.find("LOG_LEVEL_WARNING") != std::string::npos) |
57 | if (level_string.find("LOG_LEVEL_WARNING") != std::string::npos) |
| 58 | { |
58 | { |
| 59 | level_ = (Level)(level_ | LOG_LEVEL_WARNING); |
59 | level_ = (Level)(level_ | LOG_LEVEL_WARNING); |
| 60 | } |
60 | } |
| 61 | 61 | ||
| 62 | if (level_string.find("LOG_LEVEL_INFO") != std::string::npos) |
62 | if (level_string.find("LOG_LEVEL_INFO") != std::string::npos) |
| 63 | { |
63 | { |
| 64 | level_ = (Level)(level_ | LOG_LEVEL_INFO); |
64 | level_ = (Level)(level_ | LOG_LEVEL_INFO); |
| 65 | } |
65 | } |
| 66 | 66 | ||
| 67 | if (level_string.find("LOG_LEVEL_DEBUG") != std::string::npos) |
67 | if (level_string.find("LOG_LEVEL_DEBUG") != std::string::npos) |
| 68 | { |
68 | { |
| 69 | level_ = (Level)(level_ | LOG_LEVEL_DEBUG); |
69 | level_ = (Level)(level_ | LOG_LEVEL_DEBUG); |
| - | 70 | } |
|
| - | 71 | ||
| - | 72 | if (level_string.find("LOG_LEVEL_EXTREME") != std::string::npos) |
|
| - | 73 | { |
|
| - | 74 | level_ = (Level)(level_ | LOG_LEVEL_EXTREME); |
|
| 70 | } |
75 | } |
| 71 | 76 | ||
| 72 | if (level_string.find("LOG_LEVEL_ALL") != std::string::npos) |
77 | if (level_string.find("LOG_LEVEL_ALL") != std::string::npos) |
| 73 | { |
78 | { |
| 74 | level_ = (Level)(level_ | LOG_LEVEL_ALL); |
79 | level_ = (Level)(level_ | LOG_LEVEL_ALL); |
| 75 | } |
80 | } |
| 76 | } |
81 | } |
| 77 | 82 | ||
| 78 | bool OpenFile(std::string filepath) |
83 | bool OpenFile(std::string filepath) |
| 79 | { |
84 | { |
| 80 | CloseFile(); |
85 | CloseFile(); |
| 81 | 86 | ||
| 82 | file_.open(filepath.data(), std::ios::app); |
87 | file_.open(filepath.data(), std::ios::app); |
| 83 | 88 | ||
| 84 | return file_.is_open(); |
89 | return file_.is_open(); |
| 85 | } |
90 | } |
| 86 | 91 | ||
| 87 | void CloseFile(void) |
92 | void CloseFile(void) |
| 88 | { |
93 | { |
| 89 | if (file_.is_open()) |
94 | if (file_.is_open()) |
| 90 | { |
95 | { |
| 91 | file_.close(); |
96 | file_.close(); |
| 92 | } |
97 | } |
| 93 | } |
98 | } |
| 94 | 99 | ||
| 95 | void Print(Level level, std::string module, std::string message) |
100 | void Print(Level level, std::string module, std::string message) |
| 96 | { |
101 | { |
| 97 | std::string log_string; |
102 | std::string log_string; |
| 98 | std::string level_string; |
103 | std::string level_string; |
| 99 | boost::posix_time::ptime date_and_time(boost::posix_time::second_clock::local_time()); |
104 | boost::posix_time::ptime date_and_time(boost::posix_time::second_clock::local_time()); |
| 100 | 105 | ||
| 101 | 106 | ||
| 102 | /* Check log level */ |
107 | /* Check log level */ |
| 103 | if (0 == (level & level_)) |
108 | if (0 == (level & level_)) |
| 104 | { |
109 | { |
| 105 | return; |
110 | return; |
| 106 | } |
111 | } |
| 107 | 112 | ||
| 108 | 113 | ||
| 109 | /* Convert log level to string */ |
114 | /* Convert log level to string */ |
| 110 | if (0 != (level & LOG_LEVEL_ERROR)) |
115 | if (0 != (level & LOG_LEVEL_ERROR)) |
| 111 | { |
116 | { |
| 112 | level_string = "ERROR"; |
117 | level_string = "ERROR"; |
| 113 | } |
118 | } |
| 114 | else if (0 != (level & LOG_LEVEL_INFO)) |
119 | else if (0 != (level & LOG_LEVEL_INFO)) |
| 115 | { |
120 | { |
| 116 | level_string = "INFO"; |
121 | level_string = "INFO"; |
| 117 | } |
122 | } |
| 118 | else if (0 != (level & LOG_LEVEL_DEBUG)) |
123 | else if (0 != (level & LOG_LEVEL_DEBUG)) |
| 119 | { |
124 | { |
| 120 | level_string = "DEBUG"; |
125 | level_string = "DEBUG"; |
| 121 | } |
126 | } |
| 122 | else if (0 != (level & LOG_LEVEL_EXCEPTION)) |
127 | else if (0 != (level & LOG_LEVEL_EXCEPTION)) |
| 123 | { |
128 | { |
| 124 | level_string = "EXCEPTION"; |
129 | level_string = "EXCEPTION"; |
| 125 | } |
130 | } |
| 126 | else if (0 != (level & LOG_LEVEL_WARNING)) |
131 | else if (0 != (level & LOG_LEVEL_WARNING)) |
| 127 | { |
132 | { |
| 128 | level_string = "WARNING"; |
133 | level_string = "WARNING"; |
| - | 134 | } |
|
| - | 135 | else if (0 != (level & LOG_LEVEL_EXTREME)) |
|
| - | 136 | { |
|
| - | 137 | level_string = "EXTREME"; |
|
| 129 | } |
138 | } |
| 130 | else |
139 | else |
| 131 | { |
140 | { |
| 132 | level_string = "UNKNOWN"; |
141 | level_string = "UNKNOWN"; |
| 133 | } |
142 | } |
| 134 | 143 | ||
| 135 | 144 | ||
| 136 | /* Adjust lengths of sub strings */ |
145 | /* Adjust lengths of sub strings */ |
| 137 | level_string.insert(level_string.end(), 9 - level_string.size(), ' '); |
146 | level_string.insert(level_string.end(), 9 - level_string.size(), ' '); |
| 138 | module.insert(module.end(), 25 - module.size(), ' '); |
147 | module.insert(module.end(), 25 - module.size(), ' '); |
| 139 | 148 | ||
| 140 | 149 | ||
| 141 | /* Create log string */ |
150 | /* Create log string */ |
| 142 | log_string = boost::posix_time::to_simple_string(date_and_time) + " " + level_string + " " + module + " " + message; |
151 | log_string = boost::posix_time::to_simple_string(date_and_time) + " " + level_string + " " + module + " " + message; |
| 143 | 152 | ||
| 144 | 153 | ||
| 145 | /* Print message */ |
154 | /* Print message */ |
| 146 | mutex_.lock(); |
155 | mutex_.lock(); |
| 147 | 156 | ||
| 148 | std::cout << log_string << std::endl; |
157 | std::cout << log_string << std::endl; |
| 149 | 158 | ||
| 150 | 159 | ||
| 151 | if (file_.is_open()) |
160 | if (file_.is_open()) |
| 152 | { |
161 | { |
| 153 | file_ << log_string << std::endl; |
162 | file_ << log_string << std::endl; |
| 154 | } |
163 | } |
| 155 | 164 | ||
| 156 | std::cout.imbue(std::locale::classic()); |
165 | std::cout.imbue(std::locale::classic()); |
| 157 | 166 | ||
| 158 | mutex_.unlock(); |
167 | mutex_.unlock(); |
| 159 | } |
168 | } |
| 160 | 169 | ||
| 161 | void Info(std::string module, char* format, ...) |
170 | void Info(std::string module, char* format, ...) |
| 162 | { |
171 | { |
| - | 172 | char buffer[kMaxBufferSize]; |
|
| - | 173 | va_list parameters; |
|
| - | 174 | ||
| - | 175 | ||
| - | 176 | /* Initialize variables */ |
|
| - | 177 | memset(&buffer[0], 0, sizeof(buffer)); |
|
| - | 178 | ||
| - | 179 | ||
| - | 180 | /* Construct message */ |
|
| - | 181 | va_start(parameters, format); |
|
| - | 182 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
|
| - | 183 | va_end(parameters); |
|
| - | 184 | ||
| - | 185 | Print(LOG_LEVEL_INFO, module, &buffer[0]); |
|
| - | 186 | } |
|
| - | 187 | ||
| - | 188 | void Info(std::string module, std::string message) |
|
| - | 189 | { |
|
| - | 190 | Print(LOG_LEVEL_INFO, module, message); |
|
| - | 191 | } |
|
| - | 192 | ||
| - | 193 | void Debug(std::string module, char* format, ...) |
|
| - | 194 | { |
|
| 163 | char buffer[kMaxBufferSize]; |
195 | char buffer[kMaxBufferSize]; |
| 164 | va_list parameters; |
196 | va_list parameters; |
| 165 | 197 | ||
| 166 | 198 | ||
| 167 | /* Initialize variables */ |
199 | /* Initialize variables */ |
| 168 | memset(&buffer[0], 0, sizeof(buffer)); |
200 | memset(&buffer[0], 0, sizeof(buffer)); |
| 169 | 201 | ||
| 170 | 202 | ||
| 171 | /* Construct message */ |
203 | /* Construct message */ |
| 172 | va_start(parameters, format); |
204 | va_start(parameters, format); |
| 173 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
205 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
| 174 | va_end(parameters); |
206 | va_end(parameters); |
| 175 | 207 | ||
| 176 | Print( |
208 | Print(LOG_LEVEL_DEBUG, module, &buffer[0]); |
| - | 209 | } |
|
| - | 210 | ||
| - | 211 | void Debug(std::string module, std::string message) |
|
| - | 212 | { |
|
| - | 213 | Print(LOG_LEVEL_DEBUG, module, message); |
|
| 177 | } |
214 | } |
| 178 | 215 | ||
| 179 | void Info(std::string module, std::string message) |
- | |
| 180 | { |
- | |
| 181 | Print(LOG_LEVEL_INFO, module, message); |
- | |
| 182 | } |
- | |
| 183 | - | ||
| 184 | void |
216 | void Extreme(std::string module, char* format, ...) |
| 185 | { |
217 | { |
| 186 | char buffer[kMaxBufferSize]; |
218 | char buffer[kMaxBufferSize]; |
| 187 | va_list parameters; |
219 | va_list parameters; |
| 188 | 220 | ||
| 189 | 221 | ||
| 190 | /* Initialize variables */ |
222 | /* Initialize variables */ |
| 191 | memset(&buffer[0], 0, sizeof(buffer)); |
223 | memset(&buffer[0], 0, sizeof(buffer)); |
| 192 | 224 | ||
| 193 | 225 | ||
| 194 | /* Construct message */ |
226 | /* Construct message */ |
| 195 | va_start(parameters, format); |
227 | va_start(parameters, format); |
| 196 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
228 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
| 197 | va_end(parameters); |
229 | va_end(parameters); |
| 198 | 230 | ||
| 199 | Print( |
231 | Print(LOG_LEVEL_EXTREME, module, &buffer[0]); |
| 200 | } |
232 | } |
| 201 | 233 | ||
| 202 | void |
234 | void Extreme(std::string module, std::string message) |
| 203 | { |
235 | { |
| 204 | Print( |
236 | Print(LOG_LEVEL_EXTREME, module, message); |
| 205 | } |
237 | } |
| 206 | 238 | ||
| 207 | void Error(std::string module, char* format, ...) |
239 | void Error(std::string module, char* format, ...) |
| 208 | { |
240 | { |
| 209 | char buffer[kMaxBufferSize]; |
241 | char buffer[kMaxBufferSize]; |
| 210 | va_list parameters; |
242 | va_list parameters; |
| 211 | 243 | ||
| 212 | 244 | ||
| 213 | /* Initialize variables */ |
245 | /* Initialize variables */ |
| 214 | memset(&buffer[0], 0, sizeof(buffer)); |
246 | memset(&buffer[0], 0, sizeof(buffer)); |
| 215 | 247 | ||
| 216 | 248 | ||
| 217 | /* Construct message */ |
249 | /* Construct message */ |
| 218 | va_start(parameters, format); |
250 | va_start(parameters, format); |
| 219 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
251 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
| 220 | va_end(parameters); |
252 | va_end(parameters); |
| 221 | 253 | ||
| 222 | Print(LOG_LEVEL_ERROR, module, &buffer[0]); |
254 | Print(LOG_LEVEL_ERROR, module, &buffer[0]); |
| 223 | } |
255 | } |
| 224 | 256 | ||
| 225 | void Error(std::string module, std::string message) |
257 | void Error(std::string module, std::string message) |
| 226 | { |
258 | { |
| 227 | Print(LOG_LEVEL_ERROR, module, message); |
259 | Print(LOG_LEVEL_ERROR, module, message); |
| 228 | } |
260 | } |
| 229 | 261 | ||
| 230 | void Warning(std::string module, char* format, ...) |
262 | void Warning(std::string module, char* format, ...) |
| 231 | { |
263 | { |
| 232 | char buffer[kMaxBufferSize]; |
264 | char buffer[kMaxBufferSize]; |
| 233 | va_list parameters; |
265 | va_list parameters; |
| 234 | 266 | ||
| 235 | 267 | ||
| 236 | /* Initialize variables */ |
268 | /* Initialize variables */ |
| 237 | memset(&buffer[0], 0, sizeof(buffer)); |
269 | memset(&buffer[0], 0, sizeof(buffer)); |
| 238 | 270 | ||
| 239 | 271 | ||
| 240 | /* Construct message */ |
272 | /* Construct message */ |
| 241 | va_start(parameters, format); |
273 | va_start(parameters, format); |
| 242 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
274 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
| 243 | va_end(parameters); |
275 | va_end(parameters); |
| 244 | 276 | ||
| 245 | Print(LOG_LEVEL_WARNING, module, &buffer[0]); |
277 | Print(LOG_LEVEL_WARNING, module, &buffer[0]); |
| 246 | } |
278 | } |
| 247 | 279 | ||
| 248 | void Warning(std::string module, std::string message) |
280 | void Warning(std::string module, std::string message) |
| 249 | { |
281 | { |
| 250 | Print(LOG_LEVEL_WARNING, module, message); |
282 | Print(LOG_LEVEL_WARNING, module, message); |
| 251 | } |
283 | } |
| 252 | 284 | ||
| 253 | void Exception(std::string module, std::exception& exception) |
285 | void Exception(std::string module, std::exception& exception) |
| 254 | { |
286 | { |
| 255 | Print(LOG_LEVEL_EXCEPTION, module, exception.what()); |
287 | Print(LOG_LEVEL_EXCEPTION, module, exception.what()); |
| 256 | } |
288 | } |
| 257 | 289 | ||
| 258 | 290 | ||
| 259 | }; // namespace log |
291 | }; // namespace log |
| 260 | }; // namespace atom |
292 | }; // namespace atom |
| 261 | 293 | ||