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 | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 24 | enum flashprog_log_level verbose_screen = FLASHPROG_MSG_INFO; |
| 25 | enum flashprog_log_level verbose_logfile = FLASHPROG_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 | static FILE *logfile = NULL; |
| 28 | |
| 29 | int close_logfile(void) |
| 30 | { |
| 31 | if (!logfile) |
| 32 | return 0; |
| 33 | /* No need to call fflush() explicitly, fclose() already does that. */ |
| 34 | if (fclose(logfile)) { |
| 35 | /* fclose returned an error. Stop writing to be safe. */ |
| 36 | logfile = NULL; |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 37 | 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] | 38 | return 1; |
| 39 | } |
| 40 | logfile = NULL; |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | int open_logfile(const char * const filename) |
| 45 | { |
| 46 | if (!filename) { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 47 | msg_gerr("No logfile name specified.\n"); |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 48 | return 1; |
| 49 | } |
| 50 | if ((logfile = fopen(filename, "w")) == NULL) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 51 | 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] | 52 | return 1; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | void start_logging(void) |
| 58 | { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 59 | enum flashprog_log_level oldverbose_screen = verbose_screen; |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 60 | |
| 61 | /* Shut up the console. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 62 | verbose_screen = FLASHPROG_MSG_ERROR; |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 63 | print_version(); |
| 64 | verbose_screen = oldverbose_screen; |
| 65 | } |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 66 | |
Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 67 | static const char *flashprog_progress_stage_to_string(enum flashprog_progress_stage stage) |
| 68 | { |
| 69 | if (stage == FLASHPROG_PROGRESS_READ) |
Nico Huber | 806509b | 2023-04-23 00:02:57 +0100 | [diff] [blame] | 70 | return "Reading"; |
Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 71 | if (stage == FLASHPROG_PROGRESS_WRITE) |
Nico Huber | 806509b | 2023-04-23 00:02:57 +0100 | [diff] [blame] | 72 | return "Writing"; |
Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 73 | if (stage == FLASHPROG_PROGRESS_ERASE) |
Nico Huber | 806509b | 2023-04-23 00:02:57 +0100 | [diff] [blame] | 74 | return "Erasing"; |
| 75 | return "Progress"; |
| 76 | } |
| 77 | |
| 78 | static void print_progress_bar(enum flashprog_progress_stage stage, unsigned int pc) |
| 79 | { |
| 80 | char progress_line[73], *bar; |
| 81 | unsigned int i; |
| 82 | |
| 83 | const int bar_start = snprintf(progress_line, sizeof(progress_line), "%s... [", |
| 84 | flashprog_progress_stage_to_string(stage)); |
| 85 | |
| 86 | for (bar = progress_line + bar_start, i = 0; i < pc; i += 2) |
| 87 | *bar++ = '='; |
| 88 | if (i < 100) |
| 89 | *bar++ = '>', i += 2; |
| 90 | for (; i < 100; i += 2) |
| 91 | *bar++ = ' '; |
| 92 | |
| 93 | snprintf(bar, sizeof(progress_line) - (bar - progress_line), "] %3u%% ", pc); |
| 94 | |
| 95 | printf("\r%s", progress_line); |
Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void flashprog_progress_cb(enum flashprog_progress_stage stage, size_t current, size_t total, void *user_data) |
| 99 | { |
| 100 | static enum flashprog_progress_stage last_stage = (enum flashprog_progress_stage)-1; |
| 101 | static unsigned int last_pc = (unsigned int)-1; |
| 102 | |
| 103 | const unsigned int pc = total ? (current * 100ull) / total : 100; |
| 104 | |
| 105 | if (last_stage == stage && last_pc == pc) |
| 106 | return; |
| 107 | |
Nico Huber | 806509b | 2023-04-23 00:02:57 +0100 | [diff] [blame] | 108 | if (last_stage != stage || pc == 0) |
| 109 | printf("\n"); |
| 110 | |
| 111 | print_progress_bar(stage, pc); |
Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 112 | last_stage = stage; |
| 113 | last_pc = pc; |
| 114 | } |
| 115 | |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 116 | /* Please note that level is the verbosity, not the importance of the message. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 117 | int flashprog_print_cb(enum flashprog_log_level level, const char *fmt, va_list ap) |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 118 | { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 119 | int ret = 0; |
| 120 | FILE *output_type = stdout; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 121 | |
Nico Huber | 1878110 | 2012-12-10 13:34:12 +0000 | [diff] [blame] | 122 | va_list logfile_args; |
| 123 | va_copy(logfile_args, ap); |
| 124 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 125 | if (level < FLASHPROG_MSG_INFO) |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 126 | output_type = stderr; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 127 | |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 128 | if (level <= verbose_screen) { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 129 | ret = vfprintf(output_type, fmt, ap); |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 130 | /* msg_*spew often happens inside chip accessors in possibly |
| 131 | * time-critical operations. Don't slow them down by flushing. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 132 | if (level != FLASHPROG_MSG_SPEW) |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 133 | fflush(output_type); |
| 134 | } |
Thomas Heijligen | e276765 | 2022-04-07 17:48:53 +0200 | [diff] [blame] | 135 | |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 136 | if ((level <= verbose_logfile) && logfile) { |
Nico Huber | 1878110 | 2012-12-10 13:34:12 +0000 | [diff] [blame] | 137 | ret = vfprintf(logfile, fmt, logfile_args); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 138 | if (level != FLASHPROG_MSG_SPEW) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 139 | fflush(logfile); |
| 140 | } |
Thomas Heijligen | e276765 | 2022-04-07 17:48:53 +0200 | [diff] [blame] | 141 | |
Nico Huber | 1878110 | 2012-12-10 13:34:12 +0000 | [diff] [blame] | 142 | va_end(logfile_args); |
Sean Nelson | 74aa772 | 2010-01-07 20:21:58 +0000 | [diff] [blame] | 143 | return ret; |
| 144 | } |