blob: 61a9af6ed0b30e1be7969879e3e0459faff3a1b0 [file] [log] [blame]
Sean Nelson74aa7722010-01-07 20:21:58 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00005 * Copyright (C) 2011 Carl-Daniel Hailfinger
Sean Nelson74aa7722010-01-07 20:21:58 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdarg.h>
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000024#include <string.h>
25#include <errno.h>
Sean Nelson74aa7722010-01-07 20:21:58 +000026#include "flash.h"
27
Nico Huberd152fb92017-06-19 12:57:10 +020028enum flashrom_log_level verbose_screen = FLASHROM_MSG_INFO;
29enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2;
Stefan Tauner9b32de92014-08-08 23:52:33 +000030
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000031#ifndef STANDALONE
32static FILE *logfile = NULL;
33
34int close_logfile(void)
35{
36 if (!logfile)
37 return 0;
38 /* No need to call fflush() explicitly, fclose() already does that. */
39 if (fclose(logfile)) {
40 /* fclose returned an error. Stop writing to be safe. */
41 logfile = NULL;
Stefan Taunerc2eec2c2014-05-03 21:33:01 +000042 msg_gerr("Closing the log file returned error %s\n", strerror(errno));
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000043 return 1;
44 }
45 logfile = NULL;
46 return 0;
47}
48
49int open_logfile(const char * const filename)
50{
51 if (!filename) {
Stefan Tauner0554ca52013-07-25 22:54:25 +000052 msg_gerr("No logfile name specified.\n");
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000053 return 1;
54 }
55 if ((logfile = fopen(filename, "w")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +000056 msg_gerr("Error: opening log file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000057 return 1;
58 }
59 return 0;
60}
61
62void start_logging(void)
63{
Nico Huberd152fb92017-06-19 12:57:10 +020064 enum flashrom_log_level oldverbose_screen = verbose_screen;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000065
66 /* Shut up the console. */
Nico Huberd152fb92017-06-19 12:57:10 +020067 verbose_screen = FLASHROM_MSG_ERROR;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000068 print_version();
69 verbose_screen = oldverbose_screen;
70}
71#endif /* !STANDALONE */
72
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000073/* Please note that level is the verbosity, not the importance of the message. */
Nico Huberd152fb92017-06-19 12:57:10 +020074int flashrom_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap)
Sean Nelson74aa7722010-01-07 20:21:58 +000075{
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000076 int ret = 0;
77 FILE *output_type = stdout;
Uwe Hermann43959702010-03-13 17:28:29 +000078
Nico Huber18781102012-12-10 13:34:12 +000079 va_list logfile_args;
80 va_copy(logfile_args, ap);
81
Nico Huberd152fb92017-06-19 12:57:10 +020082 if (level < FLASHROM_MSG_INFO)
Sean Nelson74aa7722010-01-07 20:21:58 +000083 output_type = stderr;
Uwe Hermann43959702010-03-13 17:28:29 +000084
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000085 if (level <= verbose_screen) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000086 ret = vfprintf(output_type, fmt, ap);
Stefan Taunerc6fa32d2013-01-04 22:54:07 +000087 /* msg_*spew often happens inside chip accessors in possibly
88 * time-critical operations. Don't slow them down by flushing. */
Nico Huberd152fb92017-06-19 12:57:10 +020089 if (level != FLASHROM_MSG_SPEW)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000090 fflush(output_type);
91 }
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000092#ifndef STANDALONE
93 if ((level <= verbose_logfile) && logfile) {
Nico Huber18781102012-12-10 13:34:12 +000094 ret = vfprintf(logfile, fmt, logfile_args);
Nico Huberd152fb92017-06-19 12:57:10 +020095 if (level != FLASHROM_MSG_SPEW)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000096 fflush(logfile);
97 }
98#endif /* !STANDALONE */
Nico Huber18781102012-12-10 13:34:12 +000099 va_end(logfile_args);
Sean Nelson74aa7722010-01-07 20:21:58 +0000100 return ret;
101}