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 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdarg.h> |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame^] | 24 | #include <string.h> |
| 25 | #include <errno.h> |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 26 | #include "flash.h" |
| 27 | |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame^] | 28 | #ifndef STANDALONE |
| 29 | static FILE *logfile = NULL; |
| 30 | |
| 31 | int 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; |
| 39 | msg_perr("Closing the log file returned error %s\n", strerror(errno)); |
| 40 | return 1; |
| 41 | } |
| 42 | logfile = NULL; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | int open_logfile(const char * const filename) |
| 47 | { |
| 48 | if (!filename) { |
| 49 | msg_gerr("No filename specified.\n"); |
| 50 | return 1; |
| 51 | } |
| 52 | if ((logfile = fopen(filename, "w")) == NULL) { |
| 53 | perror(filename); |
| 54 | return 1; |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | void start_logging(void) |
| 60 | { |
| 61 | int oldverbose_screen = verbose_screen; |
| 62 | |
| 63 | /* make the console quieter. */ |
| 64 | verbose_screen = MSG_ERROR; |
| 65 | print_version(); |
| 66 | verbose_screen = oldverbose_screen; |
| 67 | } |
| 68 | #endif /* !STANDALONE */ |
| 69 | |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 70 | int print(int type, const char *fmt, ...) |
| 71 | { |
| 72 | va_list ap; |
| 73 | int ret; |
| 74 | FILE *output_type; |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 75 | |
| 76 | switch (type) { |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 77 | case MSG_ERROR: |
| 78 | output_type = stderr; |
| 79 | break; |
| 80 | case MSG_BARF: |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame^] | 81 | if (verbose_screen < 3) |
stefanct | ee85053 | 2011-08-04 17:40:25 +0000 | [diff] [blame] | 82 | return 0; |
| 83 | case MSG_DEBUG2: |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame^] | 84 | if (verbose_screen < 2) |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 85 | return 0; |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 86 | case MSG_DEBUG: |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame^] | 87 | if (verbose_screen < 1) |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 88 | return 0; |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 89 | case MSG_INFO: |
| 90 | default: |
| 91 | output_type = stdout; |
| 92 | break; |
| 93 | } |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 94 | |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 95 | va_start(ap, fmt); |
| 96 | ret = vfprintf(output_type, fmt, ap); |
| 97 | va_end(ap); |
uwe | 7ce825d | 2011-06-19 17:27:57 +0000 | [diff] [blame] | 98 | fflush(output_type); |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame^] | 99 | |
| 100 | #ifndef STANDALONE |
| 101 | if ((type <= verbose_logfile) && logfile) { |
| 102 | va_start(ap, fmt); |
| 103 | ret = vfprintf(logfile, fmt, ap); |
| 104 | va_end(ap); |
| 105 | if (type != MSG_BARF) |
| 106 | fflush(logfile); |
| 107 | } |
| 108 | #endif /* !STANDALONE */ |
| 109 | |
snelson | 5a364a8 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 110 | return ret; |
| 111 | } |