Rev 1964 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1945 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2012 Mattias Runge |
||
| 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 along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "log.h" |
||
| 22 | |||
| 23 | #include <iostream> |
||
| 24 | #include <stdarg.h> |
||
| 25 | #include <string.h> |
||
| 26 | |||
| 27 | #include <boost/date_time.hpp> |
||
| 28 | #include <boost/thread/mutex.hpp> |
||
| 29 | |||
| 30 | namespace atom { |
||
| 31 | namespace log { |
||
| 32 | |||
| 1950 | runge | 33 | static boost::mutex mutex_; |
| 1964 | runge | 34 | static std::ofstream file_; |
| 1961 | runge | 35 | static Level level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO); |
| 1950 | runge | 36 | static const uint32_t kMaxBufferSize = 4096; |
| 1945 | runge | 37 | |
| 1964 | runge | 38 | void SetLevel(Level level) |
| 1945 | runge | 39 | { |
| 40 | level_ = level; |
||
| 41 | } |
||
| 42 | |||
| 1964 | runge | 43 | void SetLevelByString(std::string level_string) |
| 1961 | runge | 44 | { |
| 45 | level_ = LOG_LEVEL_NONE; |
||
| 46 | |||
| 47 | if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos) |
||
| 48 | { |
||
| 49 | level_ = (Level)(level_ | LOG_LEVEL_ERROR); |
||
| 50 | } |
||
| 51 | |||
| 52 | if (level_string.find("LOG_LEVEL_EXCEPTION") != std::string::npos) |
||
| 53 | { |
||
| 54 | level_ = (Level)(level_ | LOG_LEVEL_EXCEPTION); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (level_string.find("LOG_LEVEL_WARNING") != std::string::npos) |
||
| 58 | { |
||
| 59 | level_ = (Level)(level_ | LOG_LEVEL_WARNING); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (level_string.find("LOG_LEVEL_INFO") != std::string::npos) |
||
| 63 | { |
||
| 64 | level_ = (Level)(level_ | LOG_LEVEL_INFO); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (level_string.find("LOG_LEVEL_DEBUG") != std::string::npos) |
||
| 68 | { |
||
| 69 | level_ = (Level)(level_ | LOG_LEVEL_DEBUG); |
||
| 70 | } |
||
| 71 | |||
| 1990 | runge | 72 | if (level_string.find("LOG_LEVEL_EXTREME") != std::string::npos) |
| 73 | { |
||
| 74 | level_ = (Level)(level_ | LOG_LEVEL_EXTREME); |
||
| 75 | } |
||
| 76 | |||
| 1961 | runge | 77 | if (level_string.find("LOG_LEVEL_ALL") != std::string::npos) |
| 78 | { |
||
| 79 | level_ = (Level)(level_ | LOG_LEVEL_ALL); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 1964 | runge | 83 | bool OpenFile(std::string filepath) |
| 84 | { |
||
| 85 | CloseFile(); |
||
| 86 | |||
| 87 | file_.open(filepath.data(), std::ios::app); |
||
| 88 | |||
| 89 | return file_.is_open(); |
||
| 90 | } |
||
| 91 | |||
| 92 | void CloseFile(void) |
||
| 93 | { |
||
| 94 | if (file_.is_open()) |
||
| 95 | { |
||
| 96 | file_.close(); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 1945 | runge | 100 | void Print(Level level, std::string module, std::string message) |
| 101 | { |
||
| 1964 | runge | 102 | std::string log_string; |
| 1945 | runge | 103 | std::string level_string; |
| 104 | boost::posix_time::ptime date_and_time(boost::posix_time::second_clock::local_time()); |
||
| 105 | |||
| 106 | |||
| 107 | /* Check log level */ |
||
| 108 | if (0 == (level & level_)) |
||
| 109 | { |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /* Convert log level to string */ |
||
| 1947 | runge | 115 | if (0 != (level & LOG_LEVEL_ERROR)) |
| 1945 | runge | 116 | { |
| 1947 | runge | 117 | level_string = "ERROR"; |
| 1945 | runge | 118 | } |
| 1947 | runge | 119 | else if (0 != (level & LOG_LEVEL_INFO)) |
| 120 | { |
||
| 121 | level_string = "INFO"; |
||
| 122 | } |
||
| 123 | else if (0 != (level & LOG_LEVEL_DEBUG)) |
||
| 124 | { |
||
| 125 | level_string = "DEBUG"; |
||
| 126 | } |
||
| 127 | else if (0 != (level & LOG_LEVEL_EXCEPTION)) |
||
| 128 | { |
||
| 129 | level_string = "EXCEPTION"; |
||
| 130 | } |
||
| 131 | else if (0 != (level & LOG_LEVEL_WARNING)) |
||
| 132 | { |
||
| 133 | level_string = "WARNING"; |
||
| 134 | } |
||
| 1990 | runge | 135 | else if (0 != (level & LOG_LEVEL_EXTREME)) |
| 136 | { |
||
| 137 | level_string = "EXTREME"; |
||
| 138 | } |
||
| 1947 | runge | 139 | else |
| 140 | { |
||
| 141 | level_string = "UNKNOWN"; |
||
| 142 | } |
||
| 1945 | runge | 143 | |
| 144 | |||
| 145 | /* Adjust lengths of sub strings */ |
||
| 146 | level_string.insert(level_string.end(), 9 - level_string.size(), ' '); |
||
| 147 | module.insert(module.end(), 25 - module.size(), ' '); |
||
| 148 | |||
| 149 | |||
| 1964 | runge | 150 | /* Create log string */ |
| 151 | log_string = boost::posix_time::to_simple_string(date_and_time) + " " + level_string + " " + module + " " + message; |
||
| 152 | |||
| 153 | |||
| 1945 | runge | 154 | /* Print message */ |
| 155 | mutex_.lock(); |
||
| 156 | |||
| 1964 | runge | 157 | std::cout << log_string << std::endl; |
| 158 | |||
| 159 | |||
| 160 | if (file_.is_open()) |
||
| 161 | { |
||
| 162 | file_ << log_string << std::endl; |
||
| 163 | } |
||
| 164 | |||
| 1945 | runge | 165 | std::cout.imbue(std::locale::classic()); |
| 166 | |||
| 167 | mutex_.unlock(); |
||
| 168 | } |
||
| 169 | |||
| 170 | void Info(std::string module, char* format, ...) |
||
| 171 | { |
||
| 1950 | runge | 172 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 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 | { |
||
| 1950 | runge | 195 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 196 | va_list parameters; |
| 197 | |||
| 198 | |||
| 199 | /* Initialize variables */ |
||
| 200 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 201 | |||
| 202 | |||
| 203 | /* Construct message */ |
||
| 204 | va_start(parameters, format); |
||
| 205 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 206 | va_end(parameters); |
||
| 207 | |||
| 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); |
||
| 214 | } |
||
| 215 | |||
| 1990 | runge | 216 | void Extreme(std::string module, char* format, ...) |
| 217 | { |
||
| 218 | char buffer[kMaxBufferSize]; |
||
| 219 | va_list parameters; |
||
| 220 | |||
| 221 | |||
| 222 | /* Initialize variables */ |
||
| 223 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 224 | |||
| 225 | |||
| 226 | /* Construct message */ |
||
| 227 | va_start(parameters, format); |
||
| 228 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 229 | va_end(parameters); |
||
| 230 | |||
| 231 | Print(LOG_LEVEL_EXTREME, module, &buffer[0]); |
||
| 232 | } |
||
| 233 | |||
| 234 | void Extreme(std::string module, std::string message) |
||
| 235 | { |
||
| 236 | Print(LOG_LEVEL_EXTREME, module, message); |
||
| 237 | } |
||
| 238 | |||
| 1945 | runge | 239 | void Error(std::string module, char* format, ...) |
| 240 | { |
||
| 1950 | runge | 241 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 242 | va_list parameters; |
| 243 | |||
| 244 | |||
| 245 | /* Initialize variables */ |
||
| 246 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 247 | |||
| 248 | |||
| 249 | /* Construct message */ |
||
| 250 | va_start(parameters, format); |
||
| 251 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 252 | va_end(parameters); |
||
| 253 | |||
| 254 | Print(LOG_LEVEL_ERROR, module, &buffer[0]); |
||
| 255 | } |
||
| 256 | |||
| 257 | void Error(std::string module, std::string message) |
||
| 258 | { |
||
| 259 | Print(LOG_LEVEL_ERROR, module, message); |
||
| 260 | } |
||
| 261 | |||
| 262 | void Warning(std::string module, char* format, ...) |
||
| 263 | { |
||
| 1950 | runge | 264 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 265 | va_list parameters; |
| 266 | |||
| 267 | |||
| 268 | /* Initialize variables */ |
||
| 269 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 270 | |||
| 271 | |||
| 272 | /* Construct message */ |
||
| 273 | va_start(parameters, format); |
||
| 274 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 275 | va_end(parameters); |
||
| 276 | |||
| 277 | Print(LOG_LEVEL_WARNING, module, &buffer[0]); |
||
| 278 | } |
||
| 279 | |||
| 280 | void Warning(std::string module, std::string message) |
||
| 281 | { |
||
| 282 | Print(LOG_LEVEL_WARNING, module, message); |
||
| 283 | } |
||
| 284 | |||
| 285 | void Exception(std::string module, std::exception& exception) |
||
| 286 | { |
||
| 287 | Print(LOG_LEVEL_EXCEPTION, module, exception.what()); |
||
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | }; // namespace log |
||
| 292 | }; // namespace atom |