blob: 476b2bf00e180087e513fc072ca1f95f1d9663c4 [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
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000028#ifndef STANDALONE
29static FILE *logfile = NULL;
30
31int close_logfile(void)
32{
33 if (!logfile)
34 return 0;
35 /* No need to call fflush() explicitly, fclose() already does that. */
36 if (fclose(logfile)) {
37 /* fclose returned an error. Stop writing to be safe. */
38 logfile = NULL;
39 msg_perr("Closing the log file returned error %s\n", strerror(errno));
40 return 1;
41 }
42 logfile = NULL;
43 return 0;
44}
45
46int open_logfile(const char * const filename)
47{
48 if (!filename) {
49 msg_gerr("No filename specified.\n");
50 return 1;
51 }
52 if ((logfile = fopen(filename, "w")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +000053 msg_gerr("Error: opening log file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000054 return 1;
55 }
56 return 0;
57}
58
59void start_logging(void)
60{
61 enum msglevel oldverbose_screen = verbose_screen;
62
63 /* Shut up the console. */
64 verbose_screen = MSG_ERROR;
65 print_version();
66 verbose_screen = oldverbose_screen;
67}
68#endif /* !STANDALONE */
69
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000070/* Please note that level is the verbosity, not the importance of the message. */
71int print(enum msglevel level, const char *fmt, ...)
Sean Nelson74aa7722010-01-07 20:21:58 +000072{
73 va_list ap;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000074 int ret = 0;
75 FILE *output_type = stdout;
Uwe Hermann43959702010-03-13 17:28:29 +000076
Stefan Taunerc6fa32d2013-01-04 22:54:07 +000077 if (level < MSG_INFO)
Sean Nelson74aa7722010-01-07 20:21:58 +000078 output_type = stderr;
Uwe Hermann43959702010-03-13 17:28:29 +000079
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000080 if (level <= verbose_screen) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000081 va_start(ap, fmt);
82 ret = vfprintf(output_type, fmt, ap);
83 va_end(ap);
Stefan Taunerc6fa32d2013-01-04 22:54:07 +000084 /* msg_*spew often happens inside chip accessors in possibly
85 * time-critical operations. Don't slow them down by flushing. */
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000086 if (level != MSG_SPEW)
87 fflush(output_type);
88 }
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000089#ifndef STANDALONE
90 if ((level <= verbose_logfile) && logfile) {
91 va_start(ap, fmt);
92 ret = vfprintf(logfile, fmt, ap);
93 va_end(ap);
94 if (level != MSG_SPEW)
95 fflush(logfile);
96 }
97#endif /* !STANDALONE */
Sean Nelson74aa7722010-01-07 20:21:58 +000098 return ret;
99}