25#include <core/threading/mutex.h>
26#include <logging/file.h>
29#include <utils/system/file.h>
57 now_s = (
struct tm *)malloc(
sizeof(
struct tm));
59 gettimeofday(&now, NULL);
60 localtime_r(&now.tv_sec, now_s);
62 if (asprintf(&start_time,
63 "%04d-%02d-%02d_%02d-%02d-%02d",
64 1900 + now_s->tm_year,
71 throw Exception(
"Failed to print current time");
73 std::string pattern(filename_pattern);
74 std::string time_var =
"$time";
75 size_t pos = pattern.find(time_var);
76 if (pos != std::string::npos) {
77 pattern.replace(pos, time_var.length(), std::string(start_time));
80 const char *filename = pattern.c_str();
81 int fd = open(filename, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
83 throw Exception(errno,
"Failed to open log file %s", filename);
85 log_file = fdopen(fd,
"a");
87 setvbuf(log_file, NULL, _IOLBF, 0);
90 if (pos != std::string::npos) {
91 std::string latest_filename(filename_pattern);
92 latest_filename.replace(pos, time_var.length(),
"latest");
93 int link_res = symlink(filename, latest_filename.c_str());
95 if (errno == EEXIST) {
96 int unlink_res = unlink(latest_filename.c_str());
97 if (unlink_res == -1) {
98 throw Exception(errno,
"Failed to update symlink at %s", latest_filename.c_str());
100 link_res = symlink(filename, latest_filename.c_str());
101 if (link_res == -1) {
103 "Failed ot create symlink from %s to %s",
105 latest_filename.c_str());
109 "Failed ot create symlink from %s to %s",
111 latest_filename.c_str());
131 va_start(arg, format);
140 va_start(arg, format);
149 va_start(arg, format);
158 va_start(arg, format);
168 gettimeofday(&now, NULL);
170 localtime_r(&now.tv_sec, now_s);
173 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
180 fprintf(log_file,
"%s", *i);
181 fprintf(log_file,
"\n");
193 gettimeofday(&now, NULL);
195 localtime_r(&now.tv_sec, now_s);
198 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
205 fprintf(log_file,
"%s", *i);
206 fprintf(log_file,
"\n");
218 gettimeofday(&now, NULL);
220 localtime_r(&now.tv_sec, now_s);
223 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
230 fprintf(log_file,
"%s", *i);
231 fprintf(log_file,
"\n");
243 gettimeofday(&now, NULL);
245 localtime_r(&now.tv_sec, now_s);
248 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
255 fprintf(log_file,
"%s", *i);
256 fprintf(log_file,
"\n");
268 gettimeofday(&now, NULL);
270 localtime_r(&now.tv_sec, now_s);
272 "%s %02d:%02d:%02d.%06ld %s: ",
279 vfprintf(log_file, format, va);
280 fprintf(log_file,
"\n");
291 gettimeofday(&now, NULL);
293 localtime_r(&now.tv_sec, now_s);
295 "%s %02d:%02d:%02d.%06ld %s: ",
302 vfprintf(log_file, format, va);
303 fprintf(log_file,
"\n");
314 gettimeofday(&now, NULL);
316 localtime_r(&now.tv_sec, now_s);
318 "%s %02d:%02d:%02d.%06ld %s: ",
325 vfprintf(log_file, format, va);
326 fprintf(log_file,
"\n");
337 gettimeofday(&now, NULL);
339 localtime_r(&now.tv_sec, now_s);
341 "%s %02d:%02d:%02d.%06ld %s: ",
348 vfprintf(log_file, format, va);
349 fprintf(log_file,
"\n");
359 va_start(arg, format);
368 va_start(arg, format);
377 va_start(arg, format);
386 va_start(arg, format);
396 localtime_r(&t->tv_sec, now_s);
399 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
406 fprintf(log_file,
"%s", *i);
407 fprintf(log_file,
"\n");
419 localtime_r(&t->tv_sec, now_s);
422 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
429 fprintf(log_file,
"%s", *i);
430 fprintf(log_file,
"\n");
442 localtime_r(&t->tv_sec, now_s);
445 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
452 fprintf(log_file,
"%s", *i);
453 fprintf(log_file,
"\n");
465 localtime_r(&t->tv_sec, now_s);
468 "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ",
475 fprintf(log_file,
"%s", *i);
476 fprintf(log_file,
"\n");
488 localtime_r(&t->tv_sec, now_s);
490 "%s %02d:%02d:%02d.%06ld %s: ",
497 vfprintf(log_file, format, va);
498 fprintf(log_file,
"\n");
509 localtime_r(&t->tv_sec, now_s);
511 "%s %02d:%02d:%02d.%06ld %s: ",
518 vfprintf(log_file, format, va);
519 fprintf(log_file,
"\n");
530 localtime_r(&t->tv_sec, now_s);
532 "%s %02d:%02d:%02d.%06ld %s: ",
539 vfprintf(log_file, format, va);
540 fprintf(log_file,
"\n");
551 localtime_r(&t->tv_sec, now_s);
553 "%s %02d:%02d:%02d.%06ld %s: ",
560 vfprintf(log_file, format, va);
561 fprintf(log_file,
"\n");
Message iterator for exceptions.
Base class for exceptions in Fawkes.
iterator end() noexcept
Get end iterator for messages.
iterator begin() noexcept
Get iterator for messages.
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Log debug message for specific time.
virtual void log_error(const char *component, const char *format,...)
Log error message.
virtual void vlog_warn(const char *component, const char *format, va_list va)
Log warning message.
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Log informational message for specific time.
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
virtual void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Log debug message for specific time.
virtual void log_debug(const char *component, const char *format,...)
Log debug message.
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Log warning message for specific time.
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Log informational message for specific time.
virtual void vlog_error(const char *component, const char *format, va_list va)
Log error message.
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Log error message for specific time.
FileLogger(const char *filename, LogLevel min_level=LL_DEBUG)
Constructor.
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Log warning message for specific time.
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Log error message for specific time.
virtual void log_info(const char *component, const char *format,...)
Log informational message.
virtual ~FileLogger()
Destructor.
virtual void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
@ LL_INFO
informational output about normal procedures
@ LL_WARN
warning, should be investigated but software still functions, an example is that something was reques...
@ LL_ERROR
error, may be recoverable (software still running) or not (software has to terminate).
@ LL_DEBUG
debug output, relevant only when tracking down problems
LogLevel log_level
Minimum log level.
Mutex mutual exclusion lock.
void lock()
Lock this mutex.
void unlock()
Unlock the mutex.
Fawkes library namespace.