summaryrefslogtreecommitdiff
path: root/cli_output.c
diff options
context:
space:
mode:
authorCarl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>2012-06-06 09:17:06 +0000
committerCarl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>2012-06-06 09:17:06 +0000
commit1c1554888875840f3d64231978aa891154b05f53 (patch)
treec51dcfb54bf8bac3933e42b5e07168e74f7439cb /cli_output.c
parentedab1d2bc08777a8aab639687f220e7dbebd8c1b (diff)
downloadflashrom-git-1c1554888875840f3d64231978aa891154b05f53.tar.gz
Add logfile support
Usage: flashrom --output logfile.txt Logfile output has at least dbg2 verbosity or screen verbosity, whichever is greater. Corresponding to flashrom svn r1540. Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net> Tested on Linux, Windows and FreeBSD. Acked-by: Idwer Vollering <vidwer@gmail.com>
Diffstat (limited to 'cli_output.c')
-rw-r--r--cli_output.c56
1 files changed, 55 insertions, 1 deletions
diff --git a/cli_output.c b/cli_output.c
index 231f2a80..57a0a058 100644
--- a/cli_output.c
+++ b/cli_output.c
@@ -2,6 +2,7 @@
* This file is part of the flashrom project.
*
* Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
+ * Copyright (C) 2011 Carl-Daniel Hailfinger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -20,8 +21,52 @@
#include <stdio.h>
#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
#include "flash.h"
+#ifndef STANDALONE
+static FILE *logfile = NULL;
+
+int close_logfile(void)
+{
+ if (!logfile)
+ return 0;
+ /* No need to call fflush() explicitly, fclose() already does that. */
+ if (fclose(logfile)) {
+ /* fclose returned an error. Stop writing to be safe. */
+ logfile = NULL;
+ msg_perr("Closing the log file returned error %s\n", strerror(errno));
+ return 1;
+ }
+ logfile = NULL;
+ return 0;
+}
+
+int open_logfile(const char * const filename)
+{
+ if (!filename) {
+ msg_gerr("No filename specified.\n");
+ return 1;
+ }
+ if ((logfile = fopen(filename, "w")) == NULL) {
+ perror(filename);
+ return 1;
+ }
+ return 0;
+}
+
+void start_logging(void)
+{
+ enum msglevel oldverbose_screen = verbose_screen;
+
+ /* Shut up the console. */
+ verbose_screen = MSG_ERROR;
+ print_version();
+ verbose_screen = oldverbose_screen;
+}
+#endif /* !STANDALONE */
+
/* Please note that level is the verbosity, not the importance of the message. */
int print(enum msglevel level, const char *fmt, ...)
{
@@ -32,7 +77,7 @@ int print(enum msglevel level, const char *fmt, ...)
if (level == MSG_ERROR)
output_type = stderr;
- if (level <= verbose) {
+ if (level <= verbose_screen) {
va_start(ap, fmt);
ret = vfprintf(output_type, fmt, ap);
va_end(ap);
@@ -42,5 +87,14 @@ int print(enum msglevel level, const char *fmt, ...)
if (level != MSG_SPEW)
fflush(output_type);
}
+#ifndef STANDALONE
+ if ((level <= verbose_logfile) && logfile) {
+ va_start(ap, fmt);
+ ret = vfprintf(logfile, fmt, ap);
+ va_end(ap);
+ if (level != MSG_SPEW)
+ fflush(logfile);
+ }
+#endif /* !STANDALONE */
return ret;
}