blob: e12446d5011f3fb6ea13f8f25b36d784ac28bfee [file] [log] [blame]
snelson5a364a82010-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>
Souvik Ghosh3c963a42016-07-19 18:48:15 -07005 * Copyright (C) 2011 Carl-Daniel Hailfinger
snelson5a364a82010-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.
snelson5a364a82010-01-07 20:21:58 +000016 */
17
18#include <stdio.h>
19#include <stdarg.h>
Souvik Ghosh3c963a42016-07-19 18:48:15 -070020#include <string.h>
21#include <errno.h>
snelson5a364a82010-01-07 20:21:58 +000022#include "flash.h"
23
Edward O'Callaghan83c77002019-06-04 15:56:19 +100024enum flashrom_log_level verbose_screen = FLASHROM_MSG_INFO;
25enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2;
26
Souvik Ghosh3c963a42016-07-19 18:48:15 -070027#ifndef STANDALONE
28static FILE *logfile = NULL;
29
30int close_logfile(void)
31{
32 if (!logfile)
33 return 0;
34 /* No need to call fflush() explicitly, fclose() already does that. */
35 if (fclose(logfile)) {
36 /* fclose returned an error. Stop writing to be safe. */
37 logfile = NULL;
Patrick Georgidbde2f12017-02-03 18:07:45 +010038 msg_gerr("Closing the log file returned error %s\n", strerror(errno));
Souvik Ghosh3c963a42016-07-19 18:48:15 -070039 return 1;
40 }
41 logfile = NULL;
42 return 0;
43}
44
45int open_logfile(const char * const filename)
46{
47 if (!filename) {
Patrick Georgidbde2f12017-02-03 18:07:45 +010048 msg_gerr("No logfile name specified.\n");
Souvik Ghosh3c963a42016-07-19 18:48:15 -070049 return 1;
50 }
51 if ((logfile = fopen(filename, "w")) == NULL) {
Edward O'Callaghan6592f042019-09-06 13:47:40 +100052 msg_gerr("Error: opening log file \"%s\" failed: %s\n", filename, strerror(errno));
Souvik Ghosh3c963a42016-07-19 18:48:15 -070053 return 1;
54 }
55 return 0;
56}
57
58void start_logging(void)
59{
Edward O'Callaghan6592f042019-09-06 13:47:40 +100060 enum flashrom_log_level oldverbose_screen = verbose_screen;
Souvik Ghosh3c963a42016-07-19 18:48:15 -070061
Patrick Georgidbde2f12017-02-03 18:07:45 +010062 /* Shut up the console. */
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110063 verbose_screen = FLASHROM_MSG_ERROR;
Souvik Ghosh3c963a42016-07-19 18:48:15 -070064 print_version();
65 verbose_screen = oldverbose_screen;
66}
67#endif /* !STANDALONE */
68
Patrick Georgidbde2f12017-02-03 18:07:45 +010069/* Please note that level is the verbosity, not the importance of the message. */
Edward O'Callaghanf0e80772020-12-04 15:36:04 +110070int flashrom_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap)
snelson5a364a82010-01-07 20:21:58 +000071{
Edward O'Callaghan90aaa302019-05-21 14:43:38 +100072 int ret = 0;
73 FILE *output_type = stdout;
uwef6f94d42010-03-13 17:28:29 +000074
Edward O'Callaghanf0e80772020-12-04 15:36:04 +110075 va_list logfile_args;
76 va_copy(logfile_args, ap);
77
78 if (level < FLASHROM_MSG_INFO)
snelson5a364a82010-01-07 20:21:58 +000079 output_type = stderr;
Edward O'Callaghan90aaa302019-05-21 14:43:38 +100080
81 if (level <= verbose_screen) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +100082 ret = vfprintf(output_type, fmt, ap);
Edward O'Callaghanf0e80772020-12-04 15:36:04 +110083 /* msg_*spew often happens inside chip accessors in possibly
84 * time-critical operations. Don't slow them down by flushing. */
Edward O'Callaghan90aaa302019-05-21 14:43:38 +100085 if (level != FLASHROM_MSG_SPEW)
86 fflush(output_type);
snelson5a364a82010-01-07 20:21:58 +000087 }
Souvik Ghosh3c963a42016-07-19 18:48:15 -070088#ifndef STANDALONE
Patrick Georgidbde2f12017-02-03 18:07:45 +010089 if ((level <= verbose_logfile) && logfile) {
Edward O'Callaghanf0e80772020-12-04 15:36:04 +110090 ret = vfprintf(logfile, fmt, logfile_args);
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110091 if (level != FLASHROM_MSG_SPEW)
Souvik Ghosh3c963a42016-07-19 18:48:15 -070092 fflush(logfile);
93 }
94#endif /* !STANDALONE */
Edward O'Callaghanf0e80772020-12-04 15:36:04 +110095 va_end(logfile_args);
snelson5a364a82010-01-07 20:21:58 +000096 return ret;
97}