Add log file support to flashrom.

The log file will always contain all verbose messages even if the user
doesn't specify -V. If the user specifies -VV, SPEW messages will be
logged as well.

Proof of concept, comments welcome.

Signed-off-by: Carl-Daniel Hailfinger <[email protected]>

Index: flashrom-logfile/flash.h
===================================================================
--- flashrom-logfile/flash.h    (Revision 1326)
+++ flashrom-logfile/flash.h    (Arbeitskopie)
@@ -229,6 +229,8 @@
 #define ERROR_NONFATAL 0x100
 
 /* cli_output.c */
+int open_logfile(char *filename);
+int msg_log(const char *fmt, ...);
 /* Let gcc and clang check for correct printf-style format strings. */
 int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 
3)));
 #define MSG_ERROR      0
Index: flashrom-logfile/cli_output.c
===================================================================
--- flashrom-logfile/cli_output.c       (Revision 1326)
+++ flashrom-logfile/cli_output.c       (Arbeitskopie)
@@ -2,6 +2,7 @@
  * This file is part of the flashrom project.
  *
  * Copyright (C) 2009 Sean Nelson <[email protected]>
+ * 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
@@ -22,30 +23,70 @@
 #include <stdarg.h>
 #include "flash.h"
 
-int print(int type, const char *fmt, ...)
+static FILE *logfile = NULL;
+
+int open_logfile(char *filename)
 {
+       if (!filename) {
+               msg_gerr("No filename specified.\n");
+               return 1;
+       }
+       if ((logfile = fopen(filename, "w")) == NULL) {
+               perror(filename);
+               return 1;
+       }
+       return 0;
+}
+
+int msg_log(const char *fmt, ...)
+{
        va_list ap;
        int ret;
-       FILE *output_type;
 
+       if (!logfile)
+               return -1;
+       va_start(ap, fmt);
+       ret = vfprintf(logfile, fmt, ap);
+       va_end(ap);
+       return ret;
+}
+
+int print(int type, const char *fmt, ...)
+{
+       va_list ap;
+       int ret = 0;
+       int want_screen = 1;
+       int want_file = 1;
+       FILE *output_type = stdout;
+
        switch (type) {
        case MSG_ERROR:
                output_type = stderr;
                break;
        case MSG_BARF:
-               if (verbose < 2)
-                       return 0;
+               if (verbose < 2) {
+                       want_screen = 0;
+                       want_file = 0;
+               }
+               break;
        case MSG_DEBUG:
                if (verbose < 1)
-                       return 0;
+                       want_screen = 0;
+               break;
        case MSG_INFO:
        default:
-               output_type = stdout;
                break;
        }
 
-       va_start(ap, fmt);
-       ret = vfprintf(output_type, fmt, ap);
-       va_end(ap);
+       if (want_screen) {
+               va_start(ap, fmt);
+               ret = vfprintf(output_type, fmt, ap);
+               va_end(ap);
+       }
+       if (want_file && logfile) {
+               va_start(ap, fmt);
+               ret = vfprintf(logfile, fmt, ap);
+               va_end(ap);
+       }
        return ret;
 }
Index: flashrom-logfile/cli_classic.c
===================================================================
--- flashrom-logfile/cli_classic.c      (Revision 1326)
+++ flashrom-logfile/cli_classic.c      (Arbeitskopie)
@@ -38,7 +38,8 @@
                 "-z|"
 #endif
                 "-E|-r <file>|-w <file>|-v <file>]\n"
-              "       [-c <chipname>] [-m [<vendor>:]<part>] [-l <file>]\n"
+              "       [-c <chipname>] [-m [<vendor>:]<part>] [-l <file>] "
+                "[-o <file>]\n"
               "       [-i <image>] [-p <programmername>[:<parameters>]]\n\n");
 
        printf("Please note that the command line interface for flashrom has "
@@ -72,6 +73,7 @@
                 "<file>\n"
               "   -i | --image <name>               only flash image <name> "
                 "from flash layout\n"
+              "   -o | --output <name>              log to file <name>\n"
               "   -L | --list-supported             print supported devices\n"
 #if CONFIG_PRINT_WIKI == 1
               "   -z | --list-supported-wiki        print supported devices "
@@ -118,7 +120,7 @@
        int operation_specified = 0;
        int i;
 
-       static const char optstring[] = "r:Rw:v:nVEfc:m:l:i:p:Lzh";
+       static const char optstring[] = "r:Rw:v:nVEfc:m:l:i:p:Lzho:";
        static const struct option long_options[] = {
                {"read", 1, NULL, 'r'},
                {"write", 1, NULL, 'w'},
@@ -136,6 +138,7 @@
                {"programmer", 1, NULL, 'p'},
                {"help", 0, NULL, 'h'},
                {"version", 0, NULL, 'R'},
+               {"output", 1, NULL, 'o'},
                {NULL, 0, NULL, 0}
        };
 
@@ -307,14 +310,17 @@
                        cli_classic_usage(argv[0]);
                        exit(0);
                        break;
+               case 'o':
+                       tempstr = strdup(optarg);
+                       if (open_logfile(tempstr))
+                               cli_classic_abort_usage();
+                       break;
                default:
                        cli_classic_abort_usage();
                        break;
                }
        }
 
-       /* FIXME: Print the actions flashrom will take. */
-
        if (list_supported) {
                print_supported();
                exit(0);
@@ -355,6 +361,10 @@
                flash = NULL;
        }
 
+       /* FIXME: Print the actions flashrom will take. */
+       /* For the log. FIXME: Print this only to the log file. */
+       print_version();
+
        /* FIXME: Delay calibration should happen in programmer code. */
        myusec_calibrate_delay();
 


-- 
http://www.hailfinger.org/


_______________________________________________
flashrom mailing list
[email protected]
http://www.flashrom.org/mailman/listinfo/flashrom

Reply via email to