log: introduce log_syntax_parse_error()

This provides generic error message for failures in conf parsers.
Currently this is not used, but will be used later.
This commit is contained in:
Yu Watanabe
2024-09-02 05:45:04 +09:00
parent b043f651c8
commit 1e04eb00f7
2 changed files with 56 additions and 0 deletions
+38
View File
@@ -1689,6 +1689,44 @@ int log_syntax_invalid_utf8_internal(
"String is not UTF-8 clean, ignoring assignment: %s", strna(p));
}
int log_syntax_parse_error_internal(
const char *unit,
const char *config_file,
unsigned config_line,
int error,
bool critical,
const char *file,
int line,
const char *func,
const char *lvalue,
const char *rvalue) {
PROTECT_ERRNO;
_cleanup_free_ char *escaped = NULL;
/* OOM is always handled as critical. */
if (ERRNO_VALUE(error) == ENOMEM)
return log_oom_internal(LOG_ERR, file, line, func);
if (rvalue && !utf8_is_valid(rvalue)) {
escaped = utf8_escape_invalid(rvalue);
if (!escaped)
rvalue = "(oom)";
else
rvalue = " (escaped)";
}
log_syntax_internal(unit, critical ? LOG_ERR : LOG_WARNING, config_file, config_line, error,
file, line, func,
"Failed to parse %s=%s%s%s%s%s",
strna(lvalue), strempty(escaped), strempty(rvalue),
critical ? "" : ", ignoring",
error == 0 ? "." : ": ",
error == 0 ? "" : STRERROR(error));
return critical ? -ERRNO_VALUE(error) : 0;
}
void log_set_upgrade_syslog_to_journal(bool b) {
upgrade_syslog_to_journal = b;