char *basedir; /* Base output directory */
char *dumpdir; /* Dumps */
char *logdir; /* Log files */
+ bool isatty; /* is stdout a tty */
} LogOpts;
void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2) pg_attribute_noreturn();
void end_progress_output(void);
void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
+void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
void check_ok(void);
unsigned int str2uint(const char *str);
}
-/* force blank output for progress display */
void
end_progress_output(void)
{
/*
- * In case nothing printed; pass a space so gcc doesn't complain about
- * empty format string.
+ * For output to a tty, erase prior contents of progress line. When either
+ * tty or verbose, indent so that report_status() output will align
+ * nicely.
*/
- prep_status(" ");
+ if (log_opts.isatty)
+ pg_log(PG_REPORT, "\r%-*s", MESSAGE_WIDTH, "");
+ else if (log_opts.verbose)
+ pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, "");
}
vsnprintf(message, sizeof(message), fmt, args);
va_end(args);
- if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
- pg_log(PG_REPORT, "%s", message);
+ /* trim strings */
+ pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
+}
+
+/*
+ * prep_status_progress
+ *
+ * Like prep_status(), but for potentially longer running operations.
+ * Details about what item is currently being processed can be displayed
+ * with pg_log(PG_STATUS, ...). A typical sequence would look like this:
+ *
+ * prep_status_progress("copying files");
+ * for (...)
+ * pg_log(PG_STATUS, "%s", filename);
+ * end_progress_output();
+ * report_status(PG_REPORT, "ok");
+ */
+void
+prep_status_progress(const char *fmt,...)
+{
+ va_list args;
+ char message[MAX_STRING];
+
+ va_start(args, fmt);
+ vsnprintf(message, sizeof(message), fmt, args);
+ va_end(args);
+
+ /*
+ * If outputting to a tty or in verbose, append newline. pg_log_v() will
+ * put the individual progress items onto the next line.
+ */
+ if (log_opts.isatty || log_opts.verbose)
+ pg_log(PG_REPORT, "%-*s\n", MESSAGE_WIDTH, message);
else
- /* trim strings that don't end in a newline */
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
}
-
static void
pg_log_v(eLogType type, const char *fmt, va_list ap)
{
break;
case PG_STATUS:
- /* for output to a display, do leading truncation and append \r */
- if (isatty(fileno(stdout)))
+ /*
+ * For output to a display, do leading truncation. Append \r so
+ * that the next message is output at the start of the line.
+ *
+ * If going to non-interactive output, only display progress if
+ * verbose is enabled. Otherwise the output gets unreasonably
+ * large by default.
+ */
+ if (log_opts.isatty)
/* -2 because we use a 2-space indent */
printf(" %s%-*.*s\r",
/* prefix with "..." if we do leading truncation */
/* optional leading truncation */
strlen(message) <= MESSAGE_WIDTH - 2 ? message :
message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
- else
+ else if (log_opts.verbose)
printf(" %s\n", message);
break;