Sean Nelson | 74aa772 | 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> |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 5 | * Copyright (C) 2011 Carl-Daniel Hailfinger |
Sean Nelson | 74aa772 | 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. |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdarg.h> |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <errno.h> |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 22 | #include "flash.h" |
| 23 | |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 24 | enum flashrom_log_level verbose_screen = FLASHROM_MSG_INFO; |
| 25 | enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2; |
Stefan Tauner | 9b32de9 | 2014-08-08 23:52:33 +0000 | [diff] [blame] | 26 | |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 27 | #ifndef STANDALONE |
| 28 | static FILE *logfile = NULL; |
| 29 | |
| 30 | int 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; |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 38 | msg_gerr("Closing the log file returned error %s\n", strerror(errno)); |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 39 | return 1; |
| 40 | } |
| 41 | logfile = NULL; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int open_logfile(const char * const filename) |
| 46 | { |
| 47 | if (!filename) { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 48 | msg_gerr("No logfile name specified.\n"); |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 49 | return 1; |
| 50 | } |
| 51 | if ((logfile = fopen(filename, "w")) == NULL) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 52 | msg_gerr("Error: opening log file \"%s\" failed: %s\n", filename, strerror(errno)); |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 53 | return 1; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | void start_logging(void) |
| 59 | { |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 60 | enum flashrom_log_level oldverbose_screen = verbose_screen; |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 61 | |
| 62 | /* Shut up the console. */ |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 63 | verbose_screen = FLASHROM_MSG_ERROR; |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 64 | print_version(); |
| 65 | verbose_screen = oldverbose_screen; |
| 66 | } |
| 67 | #endif /* !STANDALONE */ |
| 68 | |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 69 | /* Please note that level is the verbosity, not the importance of the message. */ |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 70 | int flashrom_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap) |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 71 | { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 72 | int ret = 0; |
| 73 | FILE *output_type = stdout; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 74 | |
Nico Huber | 1878110 | 2012-12-10 13:34:12 +0000 | [diff] [blame] | 75 | va_list logfile_args; |
| 76 | va_copy(logfile_args, ap); |
| 77 | |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 78 | if (level < FLASHROM_MSG_INFO) |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 79 | output_type = stderr; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 80 | |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 81 | if (level <= verbose_screen) { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 82 | ret = vfprintf(output_type, fmt, ap); |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 83 | /* msg_*spew often happens inside chip accessors in possibly |
| 84 | * time-critical operations. Don't slow them down by flushing. */ |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 85 | if (level != FLASHROM_MSG_SPEW) |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 86 | fflush(output_type); |
| 87 | } |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 88 | #ifndef STANDALONE |
| 89 | if ((level <= verbose_logfile) && logfile) { |
Nico Huber | 1878110 | 2012-12-10 13:34:12 +0000 | [diff] [blame] | 90 | ret = vfprintf(logfile, fmt, logfile_args); |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 91 | if (level != FLASHROM_MSG_SPEW) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 92 | fflush(logfile); |
| 93 | } |
| 94 | #endif /* !STANDALONE */ |
Nico Huber | 1878110 | 2012-12-10 13:34:12 +0000 | [diff] [blame] | 95 | va_end(logfile_args); |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 96 | return ret; |
| 97 | } |