Fix leaks and remove some non-standard functions

This commit is contained in:
Tobias Reisinger 2020-11-15 16:36:44 +01:00
parent c7cafa94c4
commit 97a19135ce
6 changed files with 63 additions and 27 deletions

View file

@ -21,6 +21,7 @@ logger_log(int level, const char *filename, int line, const char *func, const ch
{
return;
}
va_list args;
const char *level_str;
const char *color;
@ -62,23 +63,28 @@ logger_log(int level, const char *filename, int line, const char *func, const ch
time_t rawtime;
time(&rawtime);
strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
size_t timestamp_len = strlen(timestamp_str);
char *buffer = malloc(sizeof(char) * (128 + strlen(msg)));
sprintf(buffer, "%s[%5s] %s:%d:%s " COLOR_NONE "%s", color, level_str, filename, line, func, msg);
char *buffer = malloc(sizeof(char) * (128 + strlen(msg) + timestamp_len));
sprintf(buffer, "%s %s[%5s] %s:%d:%s " COLOR_NONE "%s", timestamp_str, color, level_str, filename, line, func, msg);
va_list args;
// start arg va_list and find log_len
va_start(args, msg);
vsyslog(level, buffer, args);
size_t log_len = vsnprintf(NULL, 0, buffer, args); // NOLINT(clang-analyzer-valist.Uninitialized): clang-tidy bug
va_end(args);
char *buffer_timed = malloc(sizeof(char) * (strlen(timestamp_str) + strlen(buffer) + 2));
sprintf(buffer_timed, "%s %s", timestamp_str, buffer);
char *log_line = malloc(sizeof(char) * (log_len + 1));
// start arg va_list again and write log_line
va_start(args, msg);
vfprintf(global_config->log_file, buffer_timed, args); // NOLINT(clang-analyzer-valist.Uninitialized): clang-tidy bug
vsprintf(log_line, buffer, args); // NOLINT(clang-analyzer-valist.Uninitialized): clang-tidy bug
va_end(args);
syslog(level, "%s", log_line + timestamp_len + 1);
fprintf(global_config->log_file, "%s", log_line);
fflush(global_config->log_file);
va_end(args);
free(buffer);
free(buffer_timed);
free(log_line);
}