Rev 1961 | Go to most recent revision | 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 | |||
| 72 | if (level_string.find("LOG_LEVEL_ALL") != std::string::npos) |
||
| 73 | { |
||
| 74 | level_ = (Level)(level_ | LOG_LEVEL_ALL); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 1964 | runge | 78 | bool OpenFile(std::string filepath) |
| 79 | { |
||
| 80 | CloseFile(); |
||
| 81 | |||
| 82 | file_.open(filepath.data(), std::ios::app); |
||
| 83 | |||
| 84 | return file_.is_open(); |
||
| 85 | } |
||
| 86 | |||
| 87 | void CloseFile(void) |
||
| 88 | { |
||
| 89 | if (file_.is_open()) |
||
| 90 | { |
||
| 91 | file_.close(); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 1945 | runge | 95 | void Print(Level level, std::string module, std::string message) |
| 96 | { |
||
| 1964 | runge | 97 | std::string log_string; |
| 1945 | runge | 98 | std::string level_string; |
| 99 | boost::posix_time::ptime date_and_time(boost::posix_time::second_clock::local_time()); |
||
| 100 | |||
| 101 | |||
| 102 | /* Check log level */ |
||
| 103 | if (0 == (level & level_)) |
||
| 104 | { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | /* Convert log level to string */ |
||
| 1947 | runge | 110 | if (0 != (level & LOG_LEVEL_ERROR)) |
| 1945 | runge | 111 | { |
| 1947 | runge | 112 | level_string = "ERROR"; |
| 1945 | runge | 113 | } |
| 1947 | runge | 114 | else if (0 != (level & LOG_LEVEL_INFO)) |
| 115 | { |
||
| 116 | level_string = "INFO"; |
||
| 117 | } |
||
| 118 | else if (0 != (level & LOG_LEVEL_DEBUG)) |
||
| 119 | { |
||
| 120 | level_string = "DEBUG"; |
||
| 121 | } |
||
| 122 | else if (0 != (level & LOG_LEVEL_EXCEPTION)) |
||
| 123 | { |
||
| 124 | level_string = "EXCEPTION"; |
||
| 125 | } |
||
| 126 | else if (0 != (level & LOG_LEVEL_WARNING)) |
||
| 127 | { |
||
| 128 | level_string = "WARNING"; |
||
| 129 | } |
||
| 130 | else |
||
| 131 | { |
||
| 132 | level_string = "UNKNOWN"; |
||
| 133 | } |
||
| 1945 | runge | 134 | |
| 135 | |||
| 136 | /* Adjust lengths of sub strings */ |
||
| 137 | level_string.insert(level_string.end(), 9 - level_string.size(), ' '); |
||
| 138 | module.insert(module.end(), 25 - module.size(), ' '); |
||
| 139 | |||
| 140 | |||
| 1964 | runge | 141 | /* Create log string */ |
| 142 | log_string = boost::posix_time::to_simple_string(date_and_time) + " " + level_string + " " + module + " " + message; |
||
| 143 | |||
| 144 | |||
| 1945 | runge | 145 | /* Print message */ |
| 146 | mutex_.lock(); |
||
| 147 | |||
| 1964 | runge | 148 | std::cout << log_string << std::endl; |
| 149 | |||
| 150 | |||
| 151 | if (file_.is_open()) |
||
| 152 | { |
||
| 153 | file_ << log_string << std::endl; |
||
| 154 | } |
||
| 155 | |||
| 1945 | runge | 156 | std::cout.imbue(std::locale::classic()); |
| 157 | |||
| 158 | mutex_.unlock(); |
||
| 159 | } |
||
| 160 | |||
| 161 | void Info(std::string module, char* format, ...) |
||
| 162 | { |
||
| 1950 | runge | 163 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 164 | va_list parameters; |
| 165 | |||
| 166 | |||
| 167 | /* Initialize variables */ |
||
| 168 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 169 | |||
| 170 | |||
| 171 | /* Construct message */ |
||
| 172 | va_start(parameters, format); |
||
| 173 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 174 | va_end(parameters); |
||
| 175 | |||
| 176 | Print(LOG_LEVEL_INFO, module, &buffer[0]); |
||
| 177 | } |
||
| 178 | |||
| 179 | void Info(std::string module, std::string message) |
||
| 180 | { |
||
| 181 | Print(LOG_LEVEL_INFO, module, message); |
||
| 182 | } |
||
| 183 | |||
| 184 | void Debug(std::string module, char* format, ...) |
||
| 185 | { |
||
| 1950 | runge | 186 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 187 | va_list parameters; |
| 188 | |||
| 189 | |||
| 190 | /* Initialize variables */ |
||
| 191 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 192 | |||
| 193 | |||
| 194 | /* Construct message */ |
||
| 195 | va_start(parameters, format); |
||
| 196 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 197 | va_end(parameters); |
||
| 198 | |||
| 199 | Print(LOG_LEVEL_DEBUG, module, &buffer[0]); |
||
| 200 | } |
||
| 201 | |||
| 202 | void Debug(std::string module, std::string message) |
||
| 203 | { |
||
| 204 | Print(LOG_LEVEL_DEBUG, module, message); |
||
| 205 | } |
||
| 206 | |||
| 207 | void Error(std::string module, char* format, ...) |
||
| 208 | { |
||
| 1950 | runge | 209 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 210 | va_list parameters; |
| 211 | |||
| 212 | |||
| 213 | /* Initialize variables */ |
||
| 214 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 215 | |||
| 216 | |||
| 217 | /* Construct message */ |
||
| 218 | va_start(parameters, format); |
||
| 219 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 220 | va_end(parameters); |
||
| 221 | |||
| 222 | Print(LOG_LEVEL_ERROR, module, &buffer[0]); |
||
| 223 | } |
||
| 224 | |||
| 225 | void Error(std::string module, std::string message) |
||
| 226 | { |
||
| 227 | Print(LOG_LEVEL_ERROR, module, message); |
||
| 228 | } |
||
| 229 | |||
| 230 | void Warning(std::string module, char* format, ...) |
||
| 231 | { |
||
| 1950 | runge | 232 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 233 | va_list parameters; |
| 234 | |||
| 235 | |||
| 236 | /* Initialize variables */ |
||
| 237 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 238 | |||
| 239 | |||
| 240 | /* Construct message */ |
||
| 241 | va_start(parameters, format); |
||
| 242 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 243 | va_end(parameters); |
||
| 244 | |||
| 245 | Print(LOG_LEVEL_WARNING, module, &buffer[0]); |
||
| 246 | } |
||
| 247 | |||
| 248 | void Warning(std::string module, std::string message) |
||
| 249 | { |
||
| 250 | Print(LOG_LEVEL_WARNING, module, message); |
||
| 251 | } |
||
| 252 | |||
| 253 | void Exception(std::string module, std::exception& exception) |
||
| 254 | { |
||
| 255 | Print(LOG_LEVEL_EXCEPTION, module, exception.what()); |
||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | }; // namespace log |
||
| 260 | }; // namespace atom |