review: if/else cleanup (2)

This commit is contained in:
staviq 2023-08-29 22:36:47 +02:00
parent 6fa208e157
commit f60f7d3640

View file

@ -352,47 +352,47 @@ inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStat
// with fallback in case something went wrong // with fallback in case something went wrong
return logfile ? logfile : stderr; return logfile ? logfile : stderr;
} }
// do the (re)initialization
if (target != nullptr)
{
if (logfile != nullptr && logfile != stdout && logfile != stderr)
{
fclose(logfile);
}
log_current_filename = LOG_DEFAULT_FILE_NAME;
log_current_target = target;
logfile = target;
}
else else
{ {
if (target != nullptr) if (log_current_filename != filename)
{ {
if (logfile != nullptr && logfile != stdout && logfile != stderr) if (logfile != nullptr && logfile != stdout && logfile != stderr)
{ {
fclose(logfile); fclose(logfile);
} }
log_current_filename = LOG_DEFAULT_FILE_NAME;
log_current_target = target;
logfile = target;
}
else
{
if (log_current_filename != filename)
{
if (logfile != nullptr && logfile != stdout && logfile != stderr)
{
fclose(logfile);
}
}
logfile = fopen(filename.c_str(), "w");
} }
if (!logfile) logfile = fopen(filename.c_str(), "w");
{
// Verify whether the file was opened, otherwise fallback to stderr
logfile = stderr;
fprintf(stderr, "Failed to open logfile '%s' with error '%s'\n", filename.c_str(), std::strerror(errno));
fflush(stderr);
}
// At this point we set init flag to true, and let the target fallback to stderr
// otherwise we would repeatedly fopen() which was already unsuccessful
_initialized = true;
} }
if (!logfile)
{
// Verify whether the file was opened, otherwise fallback to stderr
logfile = stderr;
fprintf(stderr, "Failed to open logfile '%s' with error '%s'\n", filename.c_str(), std::strerror(errno));
fflush(stderr);
// At this point we let the init flag be to true below, and let the target fallback to stderr
// otherwise we would repeatedly fopen() which was already unsuccessful
}
_initialized = true;
return logfile ? logfile : stderr; return logfile ? logfile : stderr;
} }