snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com> |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 5 | * Copyright (C) 2011 Carl-Daniel Hailfinger |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 6 | * |
| 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 | * |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdarg.h> |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <errno.h> |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 23 | #include "flash.h" |
| 24 | |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 25 | #ifndef STANDALONE |
| 26 | static FILE *logfile = NULL; |
| 27 | |
| 28 | int 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 Georgi | dbde2f1 | 2017-02-03 18:07:45 +0100 | [diff] [blame] | 36 | msg_gerr("Closing the log file returned error %s\n", strerror(errno)); |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 37 | return 1; |
| 38 | } |
| 39 | logfile = NULL; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int open_logfile(const char * const filename) |
| 44 | { |
| 45 | if (!filename) { |
Patrick Georgi | dbde2f1 | 2017-02-03 18:07:45 +0100 | [diff] [blame] | 46 | msg_gerr("No logfile name specified.\n"); |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 47 | return 1; |
| 48 | } |
| 49 | if ((logfile = fopen(filename, "w")) == NULL) { |
| 50 | perror(filename); |
| 51 | return 1; |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void start_logging(void) |
| 57 | { |
| 58 | int oldverbose_screen = verbose_screen; |
| 59 | |
Patrick Georgi | dbde2f1 | 2017-02-03 18:07:45 +0100 | [diff] [blame] | 60 | /* Shut up the console. */ |
Edward O'Callaghan | 8d8d397 | 2019-02-24 20:40:10 +1100 | [diff] [blame] | 61 | verbose_screen = FLASHROM_MSG_ERROR; |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 62 | print_version(); |
| 63 | verbose_screen = oldverbose_screen; |
| 64 | } |
| 65 | #endif /* !STANDALONE */ |
| 66 | |
Patrick Georgi | dbde2f1 | 2017-02-03 18:07:45 +0100 | [diff] [blame] | 67 | /* Please note that level is the verbosity, not the importance of the message. */ |
Edward O'Callaghan | 8d8d397 | 2019-02-24 20:40:10 +1100 | [diff] [blame] | 68 | int print(enum flashrom_log_level level, const char *fmt, ...) |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 69 | { |
| 70 | va_list ap; |
Edward O'Callaghan | 90aaa30 | 2019-05-21 14:43:38 +1000 | [diff] [blame] | 71 | int ret = 0; |
| 72 | FILE *output_type = stdout; |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 73 | |
Edward O'Callaghan | 90aaa30 | 2019-05-21 14:43:38 +1000 | [diff] [blame] | 74 | if (level == FLASHROM_MSG_ERROR) |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 75 | output_type = stderr; |
Edward O'Callaghan | 90aaa30 | 2019-05-21 14:43:38 +1000 | [diff] [blame] | 76 | |
| 77 | if (level <= verbose_screen) { |
| 78 | va_start(ap, fmt); |
| 79 | ret = vfprintf(output_type, fmt, ap); |
| 80 | va_end(ap); |
| 81 | /* msg_*spew usually happens inside chip accessors in possibly |
| 82 | * time-critical operations. Don't slow them down by flushing. |
| 83 | */ |
| 84 | if (level != FLASHROM_MSG_SPEW) |
| 85 | fflush(output_type); |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 86 | } |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 87 | #ifndef STANDALONE |
Patrick Georgi | dbde2f1 | 2017-02-03 18:07:45 +0100 | [diff] [blame] | 88 | if ((level <= verbose_logfile) && logfile) { |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 89 | va_start(ap, fmt); |
| 90 | ret = vfprintf(logfile, fmt, ap); |
| 91 | va_end(ap); |
Edward O'Callaghan | 8d8d397 | 2019-02-24 20:40:10 +1100 | [diff] [blame] | 92 | if (level != FLASHROM_MSG_SPEW) |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 93 | fflush(logfile); |
| 94 | } |
| 95 | #endif /* !STANDALONE */ |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 96 | return ret; |
| 97 | } |