util/log: Hoist the eval of is_daemonized in qemu_set_log_internal

Only call is_daemonized once.
We require the result on all paths after this point.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20220417183019.755276-36-richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2022-04-17 11:30:15 -07:00
parent 702979f736
commit beab3447db

View File

@ -123,6 +123,7 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
int log_flags, Error **errp)
{
bool need_to_open_file;
bool daemonized;
QemuLogFile *logfile;
QEMU_LOCK_GUARD(&global_mutex);
@ -178,7 +179,8 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
* or to a file (if there is a filename).
* If we are daemonized, we will only log if there is a filename.
*/
need_to_open_file = log_flags && (!is_daemonized() || filename);
daemonized = is_daemonized();
need_to_open_file = log_flags && (!daemonized || filename);
if (logfile && !need_to_open_file) {
qatomic_rcu_set(&global_file, NULL);
@ -196,7 +198,7 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
return false;
}
/* In case we are a daemon redirect stderr to logfile */
if (is_daemonized()) {
if (daemonized) {
dup2(fileno(fd), STDERR_FILENO);
fclose(fd);
/* This will skip closing logfile in qemu_logfile_free. */
@ -204,7 +206,7 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
}
} else {
/* Default to stderr if no log file specified */
assert(!is_daemonized());
assert(!daemonized);
fd = stderr;
}