00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <core/threading/mutex.h>
00025 #include <utils/logging/console.h>
00026
00027 #include <utils/system/console_colors.h>
00028
00029 #include <cstdlib>
00030 #include <sys/time.h>
00031 #include <ctime>
00032 #include <cstdio>
00033
00034 namespace fawkes {
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 ConsoleLogger::ConsoleLogger(LogLevel log_level)
00050 : Logger(log_level)
00051 {
00052 now_s = (struct ::tm *)malloc(sizeof(struct ::tm));
00053 mutex = new Mutex();
00054 }
00055
00056
00057
00058 ConsoleLogger::~ConsoleLogger()
00059 {
00060 free(now_s);
00061 delete mutex;
00062 }
00063
00064
00065 void
00066 ConsoleLogger::vlog_debug(const char *component, const char *format, va_list va)
00067 {
00068 if (log_level <= LL_DEBUG ) {
00069 struct timeval now;
00070 gettimeofday(&now, NULL);
00071 mutex->lock();
00072 localtime_r(&now.tv_sec, now_s);
00073 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_darkgray, now_s->tm_hour,
00074 now_s->tm_min, now_s->tm_sec, now.tv_usec, component);
00075 vfprintf(stderr, format, va);
00076 fprintf(stderr, "%s\n", c_normal);
00077 mutex->unlock();
00078 }
00079 }
00080
00081
00082 void
00083 ConsoleLogger::vlog_info(const char *component, const char *format, va_list va)
00084 {
00085 if (log_level <= LL_INFO ) {
00086 struct timeval now;
00087 gettimeofday(&now, NULL);
00088 mutex->lock();
00089 localtime_r(&now.tv_sec, now_s);
00090 fprintf(stderr, "%02d:%02d:%02d.%06ld %s: ", now_s->tm_hour, now_s->tm_min,
00091 now_s->tm_sec, now.tv_usec, component);
00092 vfprintf(stderr, format, va);
00093 fprintf(stderr, "\n");
00094 mutex->unlock();
00095 }
00096 }
00097
00098
00099 void
00100 ConsoleLogger::vlog_warn(const char *component, const char *format, va_list va)
00101 {
00102 if ( log_level <= LL_WARN ) {
00103 struct timeval now;
00104 gettimeofday(&now, NULL);
00105 mutex->lock();
00106 localtime_r(&now.tv_sec, now_s);
00107 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_brown, now_s->tm_hour,
00108 now_s->tm_min, now_s->tm_sec, now.tv_usec, component);
00109 vfprintf(stderr, format, va);
00110 fprintf(stderr, "%s\n", c_normal);
00111 mutex->unlock();
00112 }
00113 }
00114
00115
00116 void
00117 ConsoleLogger::vlog_error(const char *component, const char *format, va_list va)
00118 {
00119 if ( log_level <= LL_ERROR ) {
00120 struct timeval now;
00121 gettimeofday(&now, NULL);
00122 mutex->lock();
00123 localtime_r(&now.tv_sec, now_s);
00124 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_red, now_s->tm_hour,
00125 now_s->tm_min, now_s->tm_sec, now.tv_usec, component);
00126 vfprintf(stderr, format, va);
00127 fprintf(stderr, "%s\n", c_normal);
00128 mutex->unlock();
00129 }
00130 }
00131
00132
00133 void
00134 ConsoleLogger::log_debug(const char *component, const char *format, ...)
00135 {
00136 va_list arg;
00137 va_start(arg, format);
00138 vlog_debug(component, format, arg);
00139 va_end(arg);
00140 }
00141
00142
00143 void
00144 ConsoleLogger::log_info(const char *component, const char *format, ...)
00145 {
00146 va_list arg;
00147 va_start(arg, format);
00148 vlog_info(component, format, arg);
00149 va_end(arg);
00150 }
00151
00152
00153 void
00154 ConsoleLogger::log_warn(const char *component, const char *format, ...)
00155 {
00156 va_list arg;
00157 va_start(arg, format);
00158 vlog_warn(component, format, arg);
00159 va_end(arg);
00160 }
00161
00162
00163 void
00164 ConsoleLogger::log_error(const char *component, const char *format, ...)
00165 {
00166 va_list arg;
00167 va_start(arg, format);
00168 vlog_error(component, format, arg);
00169 va_end(arg);
00170 }
00171
00172
00173 void
00174 ConsoleLogger::log_debug(const char *component, Exception &e)
00175 {
00176 if (log_level <= LL_DEBUG ) {
00177 struct timeval now;
00178 gettimeofday(&now, NULL);
00179 mutex->lock();
00180 localtime_r(&now.tv_sec, now_s);
00181 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00182 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_darkgray, now_s->tm_hour,
00183 now_s->tm_min, now_s->tm_sec, now.tv_usec, component);
00184 fprintf(stderr, "%s", *i);
00185 fprintf(stderr, "%s\n", c_normal);
00186 }
00187 mutex->unlock();
00188 }
00189 }
00190
00191
00192 void
00193 ConsoleLogger::log_info(const char *component, Exception &e)
00194 {
00195 if (log_level <= LL_INFO ) {
00196 struct timeval now;
00197 gettimeofday(&now, NULL);
00198 mutex->lock();
00199 localtime_r(&now.tv_sec, now_s);
00200 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00201 fprintf(stderr, "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", now_s->tm_hour,
00202 now_s->tm_min, now_s->tm_sec, now.tv_usec, component);
00203 fprintf(stderr, "%s", *i);
00204 fprintf(stderr, "%s\n", c_normal);
00205 }
00206 mutex->unlock();
00207 }
00208 }
00209
00210
00211 void
00212 ConsoleLogger::log_warn(const char *component, Exception &e)
00213 {
00214 if (log_level <= LL_WARN ) {
00215 struct timeval now;
00216 gettimeofday(&now, NULL);
00217 mutex->lock();
00218 localtime_r(&now.tv_sec, now_s);
00219 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00220 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_brown, now_s->tm_hour,
00221 now_s->tm_min, now_s->tm_sec, now.tv_usec, component);
00222 fprintf(stderr, "%s", *i);
00223 fprintf(stderr, "%s\n", c_normal);
00224 }
00225 mutex->unlock();
00226 }
00227 }
00228
00229
00230 void
00231 ConsoleLogger::log_error(const char *component, Exception &e)
00232 {
00233 if (log_level <= LL_DEBUG ) {
00234 struct timeval now;
00235 gettimeofday(&now, NULL);
00236 mutex->lock();
00237 localtime_r(&now.tv_sec, now_s);
00238 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00239 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_red, now_s->tm_hour,
00240 now_s->tm_min, now_s->tm_sec, now.tv_usec, component);
00241 fprintf(stderr, "%s", *i);
00242 fprintf(stderr, "%s\n", c_normal);
00243 }
00244 mutex->unlock();
00245 }
00246 }
00247
00248
00249 void
00250 ConsoleLogger::tlog_debug(struct timeval *t, const char *component, const char *format, ...)
00251 {
00252 va_list arg;
00253 va_start(arg, format);
00254 vtlog_debug(t, component, format, arg);
00255 va_end(arg);
00256 }
00257
00258
00259 void
00260 ConsoleLogger::tlog_info(struct timeval *t, const char *component, const char *format, ...)
00261 {
00262 va_list arg;
00263 va_start(arg, format);
00264 vtlog_info(t, component, format, arg);
00265 va_end(arg);
00266 }
00267
00268
00269 void
00270 ConsoleLogger::tlog_warn(struct timeval *t, const char *component, const char *format, ...)
00271 {
00272 va_list arg;
00273 va_start(arg, format);
00274 vtlog_warn(t, component, format, arg);
00275 va_end(arg);
00276 }
00277
00278
00279 void
00280 ConsoleLogger::tlog_error(struct timeval *t, const char *component, const char *format, ...)
00281 {
00282 va_list arg;
00283 va_start(arg, format);
00284 vtlog_error(t, component, format, arg);
00285 va_end(arg);
00286 }
00287
00288
00289 void
00290 ConsoleLogger::tlog_debug(struct timeval *t, const char *component, Exception &e)
00291 {
00292 if (log_level <= LL_DEBUG ) {
00293 mutex->lock();
00294 localtime_r(&t->tv_sec, now_s);
00295 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00296 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_darkgray, now_s->tm_hour,
00297 now_s->tm_min, now_s->tm_sec, t->tv_usec, component);
00298 fprintf(stderr, "%s", *i);
00299 fprintf(stderr, "%s\n", c_normal);
00300 }
00301 mutex->unlock();
00302 }
00303 }
00304
00305
00306 void
00307 ConsoleLogger::tlog_info(struct timeval *t, const char *component, Exception &e)
00308 {
00309 if (log_level <= LL_INFO ) {
00310 mutex->lock();
00311 localtime_r(&t->tv_sec, now_s);
00312 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00313 fprintf(stderr, "%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", now_s->tm_hour,
00314 now_s->tm_min, now_s->tm_sec, t->tv_usec, component);
00315 fprintf(stderr, "%s", *i);
00316 fprintf(stderr, "%s\n", c_normal);
00317 }
00318 mutex->unlock();
00319 }
00320 }
00321
00322
00323 void
00324 ConsoleLogger::tlog_warn(struct timeval *t, const char *component, Exception &e)
00325 {
00326 if (log_level <= LL_WARN ) {
00327 mutex->lock();
00328 localtime_r(&t->tv_sec, now_s);
00329 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00330 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_brown, now_s->tm_hour,
00331 now_s->tm_min, now_s->tm_sec, t->tv_usec, component);
00332 fprintf(stderr, "%s", *i);
00333 fprintf(stderr, "%s\n", c_normal);
00334 }
00335 mutex->unlock();
00336 }
00337 }
00338
00339
00340 void
00341 ConsoleLogger::tlog_error(struct timeval *t, const char *component, Exception &e)
00342 {
00343 if (log_level <= LL_DEBUG ) {
00344 mutex->lock();
00345 localtime_r(&t->tv_sec, now_s);
00346 for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00347 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: [EXCEPTION] ", c_red, now_s->tm_hour,
00348 now_s->tm_min, now_s->tm_sec, t->tv_usec, component);
00349 fprintf(stderr, "%s", *i);
00350 fprintf(stderr, "%s\n", c_normal);
00351 }
00352 mutex->unlock();
00353 }
00354 }
00355
00356
00357
00358
00359 void
00360 ConsoleLogger::vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
00361 {
00362 if (log_level <= LL_DEBUG ) {
00363 mutex->lock();
00364 localtime_r(&t->tv_sec, now_s);
00365 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_darkgray, now_s->tm_hour,
00366 now_s->tm_min, now_s->tm_sec, t->tv_usec, component);
00367 vfprintf(stderr, format, va);
00368 fprintf(stderr, "%s\n", c_normal);
00369 mutex->unlock();
00370 }
00371 }
00372
00373
00374 void
00375 ConsoleLogger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
00376 {
00377 if (log_level <= LL_INFO ) {
00378 mutex->lock();
00379 localtime_r(&t->tv_sec, now_s);
00380 fprintf(stderr, "%02d:%02d:%02d.%06ld %s: ", now_s->tm_hour, now_s->tm_min,
00381 now_s->tm_sec, t->tv_usec, component);
00382 vfprintf(stderr, format, va);
00383 fprintf(stderr, "\n");
00384 mutex->unlock();
00385 }
00386 }
00387
00388
00389 void
00390 ConsoleLogger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
00391 {
00392 if ( log_level <= LL_WARN ) {
00393 mutex->lock();
00394 localtime_r(&t->tv_sec, now_s);
00395 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_brown, now_s->tm_hour,
00396 now_s->tm_min, now_s->tm_sec, t->tv_usec, component);
00397 vfprintf(stderr, format, va);
00398 fprintf(stderr, "%s\n", c_normal);
00399 mutex->unlock();
00400 }
00401 }
00402
00403
00404 void
00405 ConsoleLogger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
00406 {
00407 if ( log_level <= LL_ERROR ) {
00408 mutex->lock();
00409 localtime_r(&t->tv_sec, now_s);
00410 fprintf(stderr, "%s%02d:%02d:%02d.%06ld %s: ", c_red, now_s->tm_hour,
00411 now_s->tm_min, now_s->tm_sec, t->tv_usec, component);
00412 vfprintf(stderr, format, va);
00413 fprintf(stderr, "%s\n", c_normal);
00414 mutex->unlock();
00415 }
00416 }
00417
00418
00419 }