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) {
102 throw Exception(errno,
103 "Failed ot create symlink from %s to %s",
105 latest_filename.c_str());
108 throw Exception(errno,
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");