void CacheLogger::tlog_push_message(LogLevel ll, struct timeval *t, const char *component, Exception &e) { if (log_level <= ll ) { MutexLocker lock(mutex); localtime_r(&t->tv_sec, now_s); char *timestr; if (asprintf(×tr, "%02d:%02d:%02d.%06ld", now_s->tm_hour, now_s->tm_min, now_s->tm_sec, t->tv_usec) == -1) { return; } for (Exception::iterator i = e.begin(); i != e.end(); ++i) { CacheEntry e; e.log_level = ll; e.component = component; e.time = *t; e.timestr = timestr; e.message = std::string("[EXCEPTION] ") + *i; __messages.push_front(e); ++__num_entries; } free(timestr); if (__num_entries > __max_num_entries) { __num_entries = __max_num_entries; __messages.resize(__max_num_entries); } } }
void FileLogger::tlog_error(struct timeval *t, const char *component, Exception &e) { if ( log_level <= LL_ERROR ) { mutex->lock(); localtime_r(&t->tv_sec, now_s); for (Exception::iterator i = e.begin(); i != e.end(); ++i) { fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "E", now_s->tm_hour, now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component); fprintf(log_file, "%s", *i); fprintf(log_file, "\n"); } fflush(log_file); mutex->unlock(); } }
void FileLogger::log_warn(const char *component, Exception &e) { if ( log_level <= LL_WARN ) { struct timeval now; gettimeofday(&now, NULL); mutex->lock(); localtime_r(&now.tv_sec, now_s); for (Exception::iterator i = e.begin(); i != e.end(); ++i) { fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "W", now_s->tm_hour, now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component); fprintf(log_file, "%s", *i); fprintf(log_file, "\n"); } fflush(log_file); mutex->unlock(); } }