blob: 79d92430deb99b9fae08f4edd24c257a4677e036 [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
Souvik Ghosh3c963a42016-07-19 18:48:15 -070025#ifndef STANDALONE
26static FILE *logfile = NULL;
27
28int close_logfile(void)
29{
30 if (!logfile)
31 return 0;
32 /* No need to call fflush() explicitly, fclose() already does that. */
33 if (fclose(logfile)) {
34 /* fclose returned an error. Stop writing to be safe. */
35 logfile = NULL;
Patrick Georgidbde2f12017-02-03 18:07:45 +010036 msg_gerr("Closing the log file returned error %s\n", strerror(errno));
Souvik Ghosh3c963a42016-07-19 18:48:15 -070037 return 1;
38 }
39 logfile = NULL;
40 return 0;
41}
42
43int open_logfile(const char * const filename)
44{
45 if (!filename) {
Patrick Georgidbde2f12017-02-03 18:07:45 +010046 msg_gerr("No logfile name specified.\n");
Souvik Ghosh3c963a42016-07-19 18:48:15 -070047 return 1;
48 }
49 if ((logfile = fopen(filename, "w")) == NULL) {
50 perror(filename);
51 return 1;
52 }
53 return 0;
54}
55
56void start_logging(void)
57{
58 int oldverbose_screen = verbose_screen;
59
Patrick Georgidbde2f12017-02-03 18:07:45 +010060 /* Shut up the console. */
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110061 verbose_screen = FLASHROM_MSG_ERROR;
Souvik Ghosh3c963a42016-07-19 18:48:15 -070062 print_version();
63 verbose_screen = oldverbose_screen;
64}
65#endif /* !STANDALONE */
66
Patrick Georgidbde2f12017-02-03 18:07:45 +010067/* Please note that level is the verbosity, not the importance of the message. */
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110068int print(enum flashrom_log_level level, const char *fmt, ...)
snelson5a364a82010-01-07 20:21:58 +000069{
70 va_list ap;
71 int ret;
72 FILE *output_type;
uwef6f94d42010-03-13 17:28:29 +000073
Patrick Georgidbde2f12017-02-03 18:07:45 +010074 switch (level) {
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110075 case FLASHROM_MSG_ERROR:
snelson5a364a82010-01-07 20:21:58 +000076 output_type = stderr;
77 break;
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110078 case FLASHROM_MSG_SPEW:
Souvik Ghosh3c963a42016-07-19 18:48:15 -070079 if (verbose_screen < 3)
stefanctee850532011-08-04 17:40:25 +000080 return 0;
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110081 case FLASHROM_MSG_DEBUG2:
Souvik Ghosh3c963a42016-07-19 18:48:15 -070082 if (verbose_screen < 2)
uwef6f94d42010-03-13 17:28:29 +000083 return 0;
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110084 case FLASHROM_MSG_DEBUG:
Souvik Ghosh3c963a42016-07-19 18:48:15 -070085 if (verbose_screen < 1)
uwef6f94d42010-03-13 17:28:29 +000086 return 0;
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110087 case FLASHROM_MSG_INFO:
snelson5a364a82010-01-07 20:21:58 +000088 default:
89 output_type = stdout;
90 break;
91 }
uwef6f94d42010-03-13 17:28:29 +000092
snelson5a364a82010-01-07 20:21:58 +000093 va_start(ap, fmt);
94 ret = vfprintf(output_type, fmt, ap);
95 va_end(ap);
uwe7ce825d2011-06-19 17:27:57 +000096 fflush(output_type);
Souvik Ghosh3c963a42016-07-19 18:48:15 -070097
98#ifndef STANDALONE
Patrick Georgidbde2f12017-02-03 18:07:45 +010099 if ((level <= verbose_logfile) && logfile) {
Souvik Ghosh3c963a42016-07-19 18:48:15 -0700100 va_start(ap, fmt);
101 ret = vfprintf(logfile, fmt, ap);
102 va_end(ap);
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +1100103 if (level != FLASHROM_MSG_SPEW)
Souvik Ghosh3c963a42016-07-19 18:48:15 -0700104 fflush(logfile);
105 }
106#endif /* !STANDALONE */
snelson5a364a82010-01-07 20:21:58 +0000107 return ret;
108}