Rev 1959 | Rev 1964 | 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_; |
| 1961 | runge | 34 | static Level level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO); |
| 1950 | runge | 35 | static const uint32_t kMaxBufferSize = 4096; |
| 1945 | runge | 36 | |
| 37 | void SetLogLevel(Level level) |
||
| 38 | { |
||
| 39 | level_ = level; |
||
| 40 | } |
||
| 41 | |||
| 1961 | runge | 42 | void SetLogLevelByString(std::string level_string) |
| 43 | { |
||
| 44 | level_ = LOG_LEVEL_NONE; |
||
| 45 | |||
| 46 | if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos) |
||
| 47 | { |
||
| 48 | level_ = (Level)(level_ | LOG_LEVEL_ERROR); |
||
| 49 | } |
||
| 50 | |||
| 51 | if (level_string.find("LOG_LEVEL_EXCEPTION") != std::string::npos) |
||
| 52 | { |
||
| 53 | level_ = (Level)(level_ | LOG_LEVEL_EXCEPTION); |
||
| 54 | } |
||
| 55 | |||
| 56 | if (level_string.find("LOG_LEVEL_WARNING") != std::string::npos) |
||
| 57 | { |
||
| 58 | level_ = (Level)(level_ | LOG_LEVEL_WARNING); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (level_string.find("LOG_LEVEL_INFO") != std::string::npos) |
||
| 62 | { |
||
| 63 | level_ = (Level)(level_ | LOG_LEVEL_INFO); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (level_string.find("LOG_LEVEL_DEBUG") != std::string::npos) |
||
| 67 | { |
||
| 68 | level_ = (Level)(level_ | LOG_LEVEL_DEBUG); |
||
| 69 | } |
||
| 70 | |||
| 71 | if (level_string.find("LOG_LEVEL_ALL") != std::string::npos) |
||
| 72 | { |
||
| 73 | level_ = (Level)(level_ | LOG_LEVEL_ALL); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 1945 | runge | 77 | void Print(Level level, std::string module, std::string message) |
| 78 | { |
||
| 79 | std::string level_string; |
||
| 80 | boost::posix_time::ptime date_and_time(boost::posix_time::second_clock::local_time()); |
||
| 81 | |||
| 82 | |||
| 83 | /* Check log level */ |
||
| 84 | if (0 == (level & level_)) |
||
| 85 | { |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | |||
| 90 | /* Convert log level to string */ |
||
| 1947 | runge | 91 | if (0 != (level & LOG_LEVEL_ERROR)) |
| 1945 | runge | 92 | { |
| 1947 | runge | 93 | level_string = "ERROR"; |
| 1945 | runge | 94 | } |
| 1947 | runge | 95 | else if (0 != (level & LOG_LEVEL_INFO)) |
| 96 | { |
||
| 97 | level_string = "INFO"; |
||
| 98 | } |
||
| 99 | else if (0 != (level & LOG_LEVEL_DEBUG)) |
||
| 100 | { |
||
| 101 | level_string = "DEBUG"; |
||
| 102 | } |
||
| 103 | else if (0 != (level & LOG_LEVEL_EXCEPTION)) |
||
| 104 | { |
||
| 105 | level_string = "EXCEPTION"; |
||
| 106 | } |
||
| 107 | else if (0 != (level & LOG_LEVEL_WARNING)) |
||
| 108 | { |
||
| 109 | level_string = "WARNING"; |
||
| 110 | } |
||
| 111 | else |
||
| 112 | { |
||
| 113 | level_string = "UNKNOWN"; |
||
| 114 | } |
||
| 1945 | runge | 115 | |
| 116 | |||
| 117 | /* Adjust lengths of sub strings */ |
||
| 118 | level_string.insert(level_string.end(), 9 - level_string.size(), ' '); |
||
| 119 | module.insert(module.end(), 25 - module.size(), ' '); |
||
| 120 | |||
| 121 | |||
| 122 | /* Print message */ |
||
| 123 | mutex_.lock(); |
||
| 124 | |||
| 1959 | runge | 125 | std::cout << boost::posix_time::to_simple_string(date_and_time) << " " << level_string << " " << module << " " << message << std::endl; |
| 1945 | runge | 126 | std::cout.imbue(std::locale::classic()); |
| 127 | |||
| 128 | mutex_.unlock(); |
||
| 129 | } |
||
| 130 | |||
| 131 | void Info(std::string module, char* format, ...) |
||
| 132 | { |
||
| 1950 | runge | 133 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 134 | va_list parameters; |
| 135 | |||
| 136 | |||
| 137 | /* Initialize variables */ |
||
| 138 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 139 | |||
| 140 | |||
| 141 | /* Construct message */ |
||
| 142 | va_start(parameters, format); |
||
| 143 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 144 | va_end(parameters); |
||
| 145 | |||
| 146 | Print(LOG_LEVEL_INFO, module, &buffer[0]); |
||
| 147 | } |
||
| 148 | |||
| 149 | void Info(std::string module, std::string message) |
||
| 150 | { |
||
| 151 | Print(LOG_LEVEL_INFO, module, message); |
||
| 152 | } |
||
| 153 | |||
| 154 | void Debug(std::string module, char* format, ...) |
||
| 155 | { |
||
| 1950 | runge | 156 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 157 | va_list parameters; |
| 158 | |||
| 159 | |||
| 160 | /* Initialize variables */ |
||
| 161 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 162 | |||
| 163 | |||
| 164 | /* Construct message */ |
||
| 165 | va_start(parameters, format); |
||
| 166 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 167 | va_end(parameters); |
||
| 168 | |||
| 169 | Print(LOG_LEVEL_DEBUG, module, &buffer[0]); |
||
| 170 | } |
||
| 171 | |||
| 172 | void Debug(std::string module, std::string message) |
||
| 173 | { |
||
| 174 | Print(LOG_LEVEL_DEBUG, module, message); |
||
| 175 | } |
||
| 176 | |||
| 177 | void Error(std::string module, char* format, ...) |
||
| 178 | { |
||
| 1950 | runge | 179 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 180 | va_list parameters; |
| 181 | |||
| 182 | |||
| 183 | /* Initialize variables */ |
||
| 184 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 185 | |||
| 186 | |||
| 187 | /* Construct message */ |
||
| 188 | va_start(parameters, format); |
||
| 189 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 190 | va_end(parameters); |
||
| 191 | |||
| 192 | Print(LOG_LEVEL_ERROR, module, &buffer[0]); |
||
| 193 | } |
||
| 194 | |||
| 195 | void Error(std::string module, std::string message) |
||
| 196 | { |
||
| 197 | Print(LOG_LEVEL_ERROR, module, message); |
||
| 198 | } |
||
| 199 | |||
| 200 | void Warning(std::string module, char* format, ...) |
||
| 201 | { |
||
| 1950 | runge | 202 | char buffer[kMaxBufferSize]; |
| 1945 | runge | 203 | va_list parameters; |
| 204 | |||
| 205 | |||
| 206 | /* Initialize variables */ |
||
| 207 | memset(&buffer[0], 0, sizeof(buffer)); |
||
| 208 | |||
| 209 | |||
| 210 | /* Construct message */ |
||
| 211 | va_start(parameters, format); |
||
| 212 | vsnprintf(&buffer[0], sizeof(buffer), format, parameters); |
||
| 213 | va_end(parameters); |
||
| 214 | |||
| 215 | Print(LOG_LEVEL_WARNING, module, &buffer[0]); |
||
| 216 | } |
||
| 217 | |||
| 218 | void Warning(std::string module, std::string message) |
||
| 219 | { |
||
| 220 | Print(LOG_LEVEL_WARNING, module, message); |
||
| 221 | } |
||
| 222 | |||
| 223 | void Exception(std::string module, std::exception& exception) |
||
| 224 | { |
||
| 225 | Print(LOG_LEVEL_EXCEPTION, module, exception.what()); |
||
| 226 | } |
||
| 227 | |||
| 228 | |||
| 229 | }; // namespace log |
||
| 230 | }; // namespace atom |