blob: 5c1327829719160e67d86adcd95011685c228310 [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.
16 *
snelson5a364a82010-01-07 20:21:58 +000017 */
18
19#include <stdio.h>
20#include <stdarg.h>
Souvik Ghosh3c963a42016-07-19 18:48:15 -070021#include <string.h>
22#include <errno.h>
snelson5a364a82010-01-07 20:21:58 +000023#include "flash.h"
24
Edward O'Callaghan83c77002019-06-04 15:56:19 +100025enum flashrom_log_level verbose_screen = FLASHROM_MSG_INFO;
26enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2;
27
Souvik Ghosh3c963a42016-07-19 18:48:15 -070028#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;
Patrick Georgidbde2f12017-02-03 18:07:45 +010039 msg_gerr("Closing the log file returned error %s\n", strerror(errno));
Souvik Ghosh3c963a42016-07-19 18:48:15 -070040 return 1;
41 }
42 logfile = NULL;
43 return 0;
44}
45
46int open_logfile(const char * const filename)
47{
48 if (!filename) {
Patrick Georgidbde2f12017-02-03 18:07:45 +010049 msg_gerr("No logfile name specified.\n");
Souvik Ghosh3c963a42016-07-19 18:48:15 -070050 return 1;
51 }
52 if ((logfile = fopen(filename, "w")) == NULL) {
53 perror(filename);
54 return 1;
55 }
56 return 0;
57}
58
59void start_logging(void)
60{
61 int oldverbose_screen = verbose_screen;
62
Patrick Georgidbde2f12017-02-03 18:07:45 +010063 /* Shut up the console. */
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110064 verbose_screen = FLASHROM_MSG_ERROR;
Souvik Ghosh3c963a42016-07-19 18:48:15 -070065 print_version();
66 verbose_screen = oldverbose_screen;
67}
68#endif /* !STANDALONE */
69
Patrick Georgidbde2f12017-02-03 18:07:45 +010070/* Please note that level is the verbosity, not the importance of the message. */
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110071int print(enum flashrom_log_level level, const char *fmt, ...)
snelson5a364a82010-01-07 20:21:58 +000072{
73 va_list ap;
Edward O'Callaghan90aaa302019-05-21 14:43:38 +100074 int ret = 0;
75 FILE *output_type = stdout;
uwef6f94d42010-03-13 17:28:29 +000076
Edward O'Callaghan90aaa302019-05-21 14:43:38 +100077 if (level == FLASHROM_MSG_ERROR)
snelson5a364a82010-01-07 20:21:58 +000078 output_type = stderr;
Edward O'Callaghan90aaa302019-05-21 14:43:38 +100079
80 if (level <= verbose_screen) {
81 va_start(ap, fmt);
82 ret = vfprintf(output_type, fmt, ap);
83 va_end(ap);
84 /* msg_*spew usually happens inside chip accessors in possibly
85 * time-critical operations. Don't slow them down by flushing.
86 */
87 if (level != FLASHROM_MSG_SPEW)
88 fflush(output_type);
snelson5a364a82010-01-07 20:21:58 +000089 }
Souvik Ghosh3c963a42016-07-19 18:48:15 -070090#ifndef STANDALONE
Patrick Georgidbde2f12017-02-03 18:07:45 +010091 if ((level <= verbose_logfile) && logfile) {
Souvik Ghosh3c963a42016-07-19 18:48:15 -070092 va_start(ap, fmt);
93 ret = vfprintf(logfile, fmt, ap);
94 va_end(ap);
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110095 if (level != FLASHROM_MSG_SPEW)
Souvik Ghosh3c963a42016-07-19 18:48:15 -070096 fflush(logfile);
97 }
98#endif /* !STANDALONE */
snelson5a364a82010-01-07 20:21:58 +000099 return ret;
100}