commit c5e180a993b8aba8d80f2bc098eceb27f44bf4d1
parent 85c18bb7a27e89e81540859ae616e80e2544bad3
Author: nibo <nibo@relim.de>
Date: Thu, 10 Oct 2024 09:13:04 +0200
Change general logging to util_log()
Diffstat:
4 files changed, 40 insertions(+), 39 deletions(-)
diff --git a/chordpro.c b/chordpro.c
@@ -292,8 +292,6 @@ static void cho_log(enum LogLevel level, const char *msg, ...)
break;
}
if (g_chordpro_filepath) {
- /* fprintf(stderr, "\033[1m%s:%ld:\033[0m \033[1m\033[%sm%s\033[0m: %s\n",
- g_chordpro_filepath, g_line_number, color, log_level, msg); */
fprintf(stderr, "\033[1m%s:%ld:\033[0m \033[1m\033[%sm%s\033[0m: ",
g_chordpro_filepath, g_line_number, color, log_level);
vfprintf(stderr, msg, va);
diff --git a/config.c b/config.c
@@ -227,8 +227,7 @@ static struct Note **config_notes_load(toml_table_t *notes, const char *system)
toml_array_t *arr = toml_table_array(notes, system);
int arr_len = toml_array_len(arr);
if (arr_len != 7) {
- fprintf(stderr, "ERR: Custom naming system '%s' in [chords.notes] has to have exactly 7 items.\n", system);
- fprintf(stderr, "ERR: For an example see `lorid --print-default-config`\n");
+ util_log(LOG_ERR, "Custom naming system '%s' in [notes] has to have exactly 7 items. For an example see `lorid --print-default-config`.", system);
free(notes);
return NULL;
}
@@ -247,7 +246,7 @@ static struct Note **config_notes_load(toml_table_t *notes, const char *system)
if (value.ok) {
custom_notes[i]->sharp = value.u.s;
if (i == 2 || i == 6) {
- fprintf(stderr, "ERR: Custom naming system '%s' in [chords.notes] can't have sharp value at array index '%d'.\n", system, i);
+ util_log(LOG_ERR, "Custom naming system '%s' in [notes] can't have sharp value at array index '%d'.", system, i);
goto CLEAN;
}
}
@@ -255,7 +254,7 @@ static struct Note **config_notes_load(toml_table_t *notes, const char *system)
if (value.ok) {
custom_notes[i]->flat = value.u.s;
if (i == 0 || i == 3) {
- fprintf(stderr, "ERR: Custom naming system '%s' in [chords.notes] can't have flat value at array index '%d'.\n", system, i);
+ util_log(LOG_ERR, "Custom naming system '%s' in [notes] can't have flat value at array index '%d'.", system, i);
goto CLEAN;
}
}
@@ -383,7 +382,7 @@ static bool config_load_font(struct Font *font, toml_table_t *table, const char
if (family != FF_EMPTY) {
font->family = family;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s.font] family value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s.font] family value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -394,7 +393,7 @@ static bool config_load_font(struct Font *font, toml_table_t *table, const char
if (style != FS_EMPTY) {
font->style = style;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s.font] style value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s.font] style value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -405,7 +404,7 @@ static bool config_load_font(struct Font *font, toml_table_t *table, const char
if (weight != FW_EMPTY) {
font->weight = weight;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s.font] weight value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s.font] weight value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -436,7 +435,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
free(style->foreground_color);
style->foreground_color = color;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] foreground color value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] foreground color value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -448,7 +447,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
free(style->background_color);
style->background_color = color;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] background color value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] background color value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -459,7 +458,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
if (line_style != LS_EMPTY) {
style->underline_style = line_style;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] underline style value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] underline style value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -471,7 +470,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
free(style->underline_color);
style->underline_color = color;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] underline color value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] underline color value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -482,7 +481,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
if (line_style != LS_EMPTY) {
style->overline_style = line_style;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] overline style value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] overline style value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -494,7 +493,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
free(style->overline_color);
style->overline_color = color;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] overline color value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] overline color valeu is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -510,7 +509,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
free(style->strikethrough_color);
style->strikethrough_color = color;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] strikethrough color value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] strikethrough color value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -526,7 +525,7 @@ static bool config_load_style(struct Style *style, toml_table_t *table, const ch
free(style->boxed_color);
style->boxed_color = color;
} else {
- fprintf(stderr, "ERR: Config section [output.styles.%s] boxed color value is invalid.\n", key_name);
+ util_log(LOG_ERR, "Config section [output.styles.%s] boxed color value is invalid.", key_name);
return false;
}
free(value.u.s);
@@ -564,15 +563,14 @@ struct Config *config_load(const char *filepath)
}
FILE *fp = fopen(filepath, "r");
if (!fp) {
- fprintf(stderr, "WARN: Couldn't open config file '%s'. Using default configuration.\n", filepath);
+ util_log(LOG_WARN, "Couldn't open config file '%s'. Using default configuration.", filepath);
return config;
}
char errbuf[200];
toml_table_t *table = toml_parse_file(fp, (char *)&errbuf, sizeof(errbuf));
if (!table) {
LOG_DEBUG("toml_parse_file failed.");
- fprintf(stderr, "ERR: Config file is not a valid toml file.\n");
- fprintf(stderr, "%s\n", (char *)&errbuf);
+ util_log(LOG_ERR, "Config file is not a valid toml file: %s.", (char *)&errbuf);
return NULL;
}
toml_table_t *output = toml_table_table(table, "output");
@@ -585,9 +583,10 @@ struct Config *config_load(const char *filepath)
if (value.ok) {
system = config_naming_system_parse(value.u.s);
if (system == NS_CUSTOM) {
- notes = toml_table_table(output, "notes");
+ notes = toml_table_table(table, "notes");
+ // notes = toml_table_table(output, "notes");
if (!notes) {
- fprintf(stderr, "ERR: Custom notes '%s' has no corresponding definition in [output.notes].\n", value.u.s);
+ util_log(LOG_ERR, "Custom notes '%s' has no corresponding definition in [notes].", value.u.s);
return NULL;
}
custom_notes = config_notes_load(notes, value.u.s);
@@ -596,7 +595,7 @@ struct Config *config_load(const char *filepath)
config->output->notes = custom_notes;
} else {
LOG_DEBUG("config_notes_load failed.");
- fprintf(stderr, "ERR: Couldn't load custom notes '%s' from [notes] section.\n", value.u.s);
+ util_log(LOG_ERR, "Couldn't load custom notes '%s' from [notes] section.", value.u.s);
return NULL;
}
} else {
@@ -642,7 +641,7 @@ struct Config *config_load(const char *filepath)
if (system == NS_CUSTOM) {
notes = toml_table_table(table, "notes");
if (!notes) {
- fprintf(stderr, "ERR: Custom notes '%s' has no corresponding definition in [notes].\n", value.u.s);
+ util_log(LOG_ERR, "Custom notes '%s' has no corresponding definition in [notes].", value.u.s);
return NULL;
}
custom_notes = config_notes_load(notes, value.u.s);
@@ -651,7 +650,7 @@ struct Config *config_load(const char *filepath)
config->parser->notes = custom_notes;
} else {
LOG_DEBUG("config_notes_load failed.");
- fprintf(stderr, "ERR: Couldn't load custom notes '%s' from [notes] section.\n", value.u.s);
+ util_log(LOG_ERR, "Couldn't load custom notes '%s' from [notes] section.", value.u.s);
return NULL;
}
} else {
diff --git a/fontconfig.c b/fontconfig.c
@@ -4,6 +4,7 @@
#include <string.h>
#include <fontconfig/fontconfig.h>
#include "fontconfig.h"
+#include "util.h"
static bool file_extension_is_ttc(const char *filepath)
{
@@ -54,7 +55,7 @@ char *fontconfig_fontpath_find(struct Font *font, enum FontType font_type)
style.u.i = FC_SLANT_ITALIC;
break;
default:
- fprintf(stderr, "ERR: Invalid font style value '%d'.\n", font->style);
+ util_log(LOG_ERR, "Invalid font style value '%d'.", font->style);
return NULL;
}
FcPatternAdd(pattern, FC_SLANT, style, FcFalse);
@@ -68,7 +69,7 @@ char *fontconfig_fontpath_find(struct Font *font, enum FontType font_type)
weight.u.i = FC_WEIGHT_BOLD;
break;
default:
- fprintf(stderr, "ERR: Invalid font weight value '%d'.\n", font->weight);
+ util_log(LOG_ERR, "Invalid font weight value '%d'.", font->weight);
return NULL;
}
FcPatternAdd(pattern, FC_WEIGHT, weight, FcFalse);
diff --git a/out_pdf.c b/out_pdf.c
@@ -159,7 +159,8 @@ static char *out_pdf_filename_generate_from_songs(struct ChoSong **songs)
if (len == 1) {
title = cho_metadata_get(songs[0]->metadata, "title");
if (!title) {
- fprintf(stderr, "ERR: Song has no title.\n");
+ /* INFO: unreachable because the parser already checks the presence of the 'title' directive */
+ util_log(LOG_ERR, "Song has no title.");
return NULL;
}
normalized_title = str_normalize(title);
@@ -175,6 +176,7 @@ static char *out_pdf_filename_create(struct ChoSong **songs, const char *cho_fil
char *pdf_filepath = NULL;
char *pdf_filename;
char *tmp;
+ enum FileType type;
if (cho_filepath) {
pdf_filepath = file_extension_replace_or_add(cho_filepath, "pdf");
pdf_filename = filepath_basename(pdf_filepath);
@@ -186,7 +188,8 @@ static char *out_pdf_filename_create(struct ChoSong **songs, const char *cho_fil
}
}
if (out) {
- switch (file_type(out)) {
+ type = file_type(out);
+ switch (type) {
case F_ERROR:
tmp = filepath_dirname(out);
if (file_type(tmp) == F_FOLDER) {
@@ -196,7 +199,7 @@ static char *out_pdf_filename_create(struct ChoSong **songs, const char *cho_fil
return strdup(out);
} else {
free(tmp);
- fprintf(stderr, "ERR: Invalid argument --output/-o value.\n");
+ util_log(LOG_ERR, "Invalid argument --output/-o value.");
return NULL;
}
break;
@@ -212,10 +215,10 @@ static char *out_pdf_filename_create(struct ChoSong **songs, const char *cho_fil
free(pdf_filepath);
return tmp;
case F_OTHER:
- fprintf(stderr, "ERR: Invalid argument --output/-o value. It doesn't refer to a folder or regular file.\n");
+ util_log(LOG_ERR, "Invalid argument --output/-o value. It doesn't refer to a folder or regular file.");
return NULL;
default:
- fprintf(stderr, "ERR: Invalid enum FileType value.\n");
+ util_log(LOG_ERR, "Invalid enum FileType value '%d'.", type);
return NULL;
}
} else {
@@ -417,7 +420,7 @@ static char *text_find_fitting(const char *str, struct Style *style)
do {
i = find_whitespace((const char *)&tmp, start);
if (i == -1) {
- fprintf(stderr, "ERR: Can't split text because no whitespace was found.\n");
+ util_log(LOG_ERR, "Can' split text because no whitespace was found.");
return NULL;
}
tmp[i] = 0;
@@ -766,7 +769,7 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config)
}
tl++;
} else {
- fprintf(stderr, "WARN: Title doesn't fit on one line.\n");
+ util_log(LOG_WARN, "Title doesn't fit on one line.");
text_lines = text_split_by_whitespace(songs[so]->metadata[m]->value, songs[so]->metadata[m]->style);
for (int i = 0; text_lines[i]; i++) {
width = text_width(text_lines[i]->items[0]);
@@ -818,7 +821,7 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config)
}
tl++;
} else {
- fprintf(stderr, "WARN: Subtitle doesn't fit on one line.\n");
+ util_log(LOG_WARN, "Subtitle doesn't fit on one line.");
text_lines = text_split_by_whitespace(songs[so]->metadata[m]->value, songs[so]->metadata[m]->style);
for (int i = 0; text_lines[i]; i++) {
width = text_width(text_lines[i]->items[0]);
@@ -866,7 +869,7 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config)
}
tl++;
} else {
- fprintf(stderr, "WARN: Section name doesn't fit on one line.\n");
+ util_log(LOG_WARN, "Section name doesn't fit on one line.");
text_lines = text_split_by_whitespace(songs[so]->sections[se]->label->name, songs[so]->sections[se]->label->style);
for (int i = 0; text_lines[i]; i++) {
width = text_width(text_lines[i]->items[0]);
@@ -972,7 +975,7 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config)
return NULL;
}
if (!fits) {
- fprintf(stderr, "WARN: text line (chords/annotations) doesn't fit.\n");
+ util_log(LOG_WARN, "text line (chords/annotations) doesn't fit.");
}
text[t]->lines[tl]->btype = lines[li]->btype;
text[t]->lines[tl]->ftype = SF_CHORD;
@@ -1052,7 +1055,7 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config)
return NULL;
}
if (!fits) {
- fprintf(stderr, "WARN: text line (lyrics) doesn't fit.\n");
+ util_log(LOG_WARN, "text line (lyrics) doesn't fit.");
}
text[t]->lines[tl]->btype = lines[li]->btype;
text[t]->lines[tl]->ftype = SF_TEXT;
@@ -1287,7 +1290,7 @@ char *out_pdf_new(const char *cho_filepath, const char *output_folder_or_file, s
out_pdf_fnt_add(fnt, &g_fonts);
free(fontpath);
} else {
- fprintf(stderr, "ERR: Didn't find font file for following font:\n");
+ util_log(LOG_ERR, "Didn't find font file for following font:");
cho_font_print(needed_fonts[f]);
return NULL;
}