wslog: Minor log_write_do_work() rewrite

This commit is contained in:
João Valverde 2023-09-12 10:51:00 +01:00
parent 962eafafaf
commit bf220c14d4

View File

@ -119,7 +119,9 @@ static FILE *custom_log = NULL;
static enum ws_log_level fatal_log_level = LOG_LEVEL_ERROR;
#ifdef WS_DEBUG
static bool init_complete = false;
#endif
ws_log_console_open_pref ws_log_console_open = LOG_CONSOLE_OPEN_NEVER;
@ -511,8 +513,10 @@ int ws_log_parse_args(int *argc_ptr, char *argv[],
if (argc_ptr == NULL || argv == NULL)
return -1;
#ifdef WS_DEBUG
/* Assert ws_log_init() was called before ws_log_parse_args(). */
ASSERT(init_complete);
#endif
/* Configure from command line. */
@ -917,7 +921,9 @@ void ws_log_init(const char *progname,
if (env != NULL)
ws_log_set_noisy_filter(env);
#ifdef WS_DEBUG
init_complete = true;
#endif
}
@ -990,47 +996,52 @@ static inline const char *color_off(bool enable)
*/
static void log_write_do_work(FILE *fp, bool use_color,
struct tm *when, long nanosecs, intmax_t pid,
const char *domain, enum ws_log_level level,
const char *domain, enum ws_log_level level,
const char *file, long line, const char *func,
const char *user_format, va_list user_ap)
{
fputs(" **", fp);
#ifdef WS_DEBUG
if (!init_complete)
fputs(" ** (noinit)", fp);
fputs(" no init!", fp);
#endif
/* Process */
fprintf(fp, " ** (%s:%"PRIdMAX") ", registered_progname, pid);
fprintf(fp, " (%s:%"PRIdMAX")", registered_progname, pid);
/* Timestamp */
if (when != NULL && nanosecs >= 0)
fprintf(fp, "%02d:%02d:%02d.%06ld ",
when->tm_hour, when->tm_min, when->tm_sec,
nanosecs / NANOSECS_IN_MICROSEC);
else if (when != NULL)
fprintf(fp, "%02d:%02d:%02d ",
if (when != NULL) {
fprintf(fp, " %02d:%02d:%02d",
when->tm_hour, when->tm_min, when->tm_sec);
else
fputs("(notime) ", fp);
if (nanosecs >= 0) {
fprintf(fp, ".%06ld", nanosecs / NANOSECS_IN_MICROSEC);
}
}
/* Domain/level */
fprintf(fp, "[%s %s%s%s] ", domain_to_string(domain),
fprintf(fp, " [%s %s%s%s]", domain_to_string(domain),
level_color_on(use_color, level),
ws_log_level_to_string(level),
color_off(use_color));
/* File/line */
if (file != NULL && line >= 0)
fprintf(fp, "%s:%ld ", file, line);
else if (file != NULL)
fprintf(fp, "%s ", file);
if (file != NULL) {
fprintf(fp, " %s", file);
if (line >= 0) {
fprintf(fp, ":%ld", line);
}
}
/* Any formatting changes here need to be synced with ui/capture.c:capture_input_closed. */
fputs("-- ", fp);
fputs(" --", fp);
/* Function name */
if (func != NULL)
fprintf(fp, "%s(): ", func);
fprintf(fp, " %s():", func);
/* User message */
fputc(' ', fp);
vfprintf(fp, user_format, user_ap);
fputc('\n', fp);
fflush(fp);