diff --git a/vl.c b/vl.c
index e881e45..63c199c 100644
--- a/vl.c
+++ b/vl.c
@@ -4878,6 +4878,9 @@ int main(int argc, char **argv, char **envp)
     CPUState *env;
     int show_vnc_port = 0;

+    /* Prevent unfortunate early crash with debugging fprintf */
+    logfile=stdout;
+
     init_clocks();

     qemu_errors_to_file(stderr);
l

diff --git a/exec.c b/exec.c
index 2b068f5..752e208 100644
--- a/exec.c
+++ b/exec.c
@@ -1494,7 +1494,7 @@ void cpu_single_step(CPUState *env, int enabled)
 void cpu_set_log(int log_flags)
 {
     loglevel = log_flags;
-    if (loglevel && !logfile) {
+    if (loglevel && (!logfile || logfile == stdout)) {
         logfile = fopen(logfilename, log_append ? "a" : "w");
         if (!logfile) {
             perror(logfilename);
@@ -1521,7 +1521,7 @@ void cpu_set_log(int log_flags)
 void cpu_set_log_filename(const char *filename)
 {
     logfilename = strdup(filename);
-    if (logfile) {
+    if (logfile && logfile != stdout) {
         fclose(logfile);
         logfile = NULL;
     }
