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>
diff --git a/cli_classic.c b/cli_classic.c
index edcef8e..b415076 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -106,7 +106,7 @@
 	         "-z|"
 #endif
 	         "-E|-r <file>|-w <file>|-v <file>]\n"
-	       "       [-c <chipname>] [-l <file>]\n"
+	       "       [-c <chipname>] [-l <file>] [-o <file>]\n"
 	       "       [-i <image>] [-p <programmername>[:<parameters>]]\n\n");
 
 	printf("Please note that the command line interface for flashrom has "
@@ -135,6 +135,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 "
@@ -189,7 +190,7 @@
 	enum programmer prog = PROGRAMMER_INVALID;
 	int ret = 0;
 
-	static const char optstring[] = "r:Rw:v:nVEfc:l:i:p:Lzh";
+	static const char optstring[] = "r:Rw:v:nVEfc:l:i:p:Lzho:";
 	static const struct option long_options[] = {
 		{"read",		1, NULL, 'r'},
 		{"write",		1, NULL, 'w'},
@@ -206,11 +207,13 @@
 		{"programmer",		1, NULL, 'p'},
 		{"help",		0, NULL, 'h'},
 		{"version",		0, NULL, 'R'},
+		{"output",		1, NULL, 'o'},
 		{NULL,			0, NULL, 0},
 	};
 
 	char *filename = NULL;
 	char *layoutfile = NULL;
+	char *logfile = NULL;
 	char *tempstr = NULL;
 	char *pparam = NULL;
 
@@ -272,7 +275,9 @@
 			chip_to_probe = strdup(optarg);
 			break;
 		case 'V':
-			verbose++;
+			verbose_screen++;
+			if (verbose_screen > MSG_DEBUG2)
+				verbose_logfile = verbose_screen;
 			break;
 		case 'E':
 			if (++operation_specified > 1) {
@@ -378,6 +383,18 @@
 			cli_classic_usage(argv[0]);
 			exit(0);
 			break;
+		case 'o':
+#ifdef STANDALONE
+			fprintf(stderr, "Log file not supported in standalone mode. Aborting.\n");
+			cli_classic_abort_usage();
+#else /* STANDALONE */
+			logfile = strdup(optarg);
+			if (logfile[0] == '\0') {
+				fprintf(stderr, "No log filename specified.\n");
+				cli_classic_abort_usage();
+			}
+#endif /* STANDALONE */
+			break;
 		default:
 			cli_classic_abort_usage();
 			break;
@@ -396,6 +413,13 @@
 		cli_classic_abort_usage();
 	}
 
+#ifndef STANDALONE
+	if (logfile && check_filename(logfile, "log"))
+		cli_classic_abort_usage();
+	if (logfile && open_logfile(logfile))
+		return 1;
+#endif /* !STANDALONE */
+
 #if CONFIG_PRINT_WIKI == 1
 	if (list_supported_wiki) {
 		print_supported_wiki();
@@ -410,6 +434,11 @@
 		goto out;
 	}
 
+#ifndef STANDALONE
+	start_logging();
+#endif /* !STANDALONE */
+
+	print_buildinfo();
 	msg_gdbg("Command line (%i args):", argc - 1);
 	for (i = 0; i < argc; i++) {
 		msg_gdbg(" %s", argv[i]);
@@ -552,5 +581,8 @@
 out_shutdown:
 	programmer_shutdown();
 out:
+#ifndef STANDALONE
+	ret |= close_logfile();
+#endif /* !STANDALONE */
 	return ret;
 }