2010-01-07 20:21:58 +00:00
|
|
|
/*
|
|
|
|
|
* This file is part of the flashrom project.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
|
2012-06-06 09:17:06 +00:00
|
|
|
* Copyright (C) 2011 Carl-Daniel Hailfinger
|
2010-01-07 20:21:58 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
2012-06-06 09:17:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
2010-01-07 20:21:58 +00:00
|
|
|
#include "flash.h"
|
|
|
|
|
|
2017-06-19 12:57:10 +02:00
|
|
|
enum flashrom_log_level verbose_screen = FLASHROM_MSG_INFO;
|
|
|
|
|
enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2;
|
2014-08-08 23:52:33 +00:00
|
|
|
|
2012-06-06 09:17:06 +00:00
|
|
|
#ifndef STANDALONE
|
|
|
|
|
static FILE *logfile = NULL;
|
|
|
|
|
|
|
|
|
|
int close_logfile(void)
|
|
|
|
|
{
|
|
|
|
|
if (!logfile)
|
|
|
|
|
return 0;
|
|
|
|
|
/* No need to call fflush() explicitly, fclose() already does that. */
|
|
|
|
|
if (fclose(logfile)) {
|
|
|
|
|
/* fclose returned an error. Stop writing to be safe. */
|
|
|
|
|
logfile = NULL;
|
2014-05-03 21:33:01 +00:00
|
|
|
msg_gerr("Closing the log file returned error %s\n", strerror(errno));
|
2012-06-06 09:17:06 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
logfile = NULL;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int open_logfile(const char * const filename)
|
|
|
|
|
{
|
|
|
|
|
if (!filename) {
|
2013-07-25 22:54:25 +00:00
|
|
|
msg_gerr("No logfile name specified.\n");
|
2012-06-06 09:17:06 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if ((logfile = fopen(filename, "w")) == NULL) {
|
2013-04-07 13:08:30 +00:00
|
|
|
msg_gerr("Error: opening log file \"%s\" failed: %s\n", filename, strerror(errno));
|
2012-06-06 09:17:06 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void start_logging(void)
|
|
|
|
|
{
|
2017-06-19 12:57:10 +02:00
|
|
|
enum flashrom_log_level oldverbose_screen = verbose_screen;
|
2012-06-06 09:17:06 +00:00
|
|
|
|
|
|
|
|
/* Shut up the console. */
|
2017-06-19 12:57:10 +02:00
|
|
|
verbose_screen = FLASHROM_MSG_ERROR;
|
2012-06-06 09:17:06 +00:00
|
|
|
print_version();
|
|
|
|
|
verbose_screen = oldverbose_screen;
|
|
|
|
|
}
|
|
|
|
|
#endif /* !STANDALONE */
|
|
|
|
|
|
2012-05-14 22:54:58 +00:00
|
|
|
/* Please note that level is the verbosity, not the importance of the message. */
|
2017-06-19 12:57:10 +02:00
|
|
|
int flashrom_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap)
|
2010-01-07 20:21:58 +00:00
|
|
|
{
|
2012-05-14 22:54:58 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
FILE *output_type = stdout;
|
2010-03-13 17:28:29 +00:00
|
|
|
|
2012-12-10 13:34:12 +00:00
|
|
|
va_list logfile_args;
|
|
|
|
|
va_copy(logfile_args, ap);
|
|
|
|
|
|
2017-06-19 12:57:10 +02:00
|
|
|
if (level < FLASHROM_MSG_INFO)
|
2010-01-07 20:21:58 +00:00
|
|
|
output_type = stderr;
|
2010-03-13 17:28:29 +00:00
|
|
|
|
2012-06-06 09:17:06 +00:00
|
|
|
if (level <= verbose_screen) {
|
2012-05-14 22:54:58 +00:00
|
|
|
ret = vfprintf(output_type, fmt, ap);
|
2013-01-04 22:54:07 +00:00
|
|
|
/* msg_*spew often happens inside chip accessors in possibly
|
|
|
|
|
* time-critical operations. Don't slow them down by flushing. */
|
2017-06-19 12:57:10 +02:00
|
|
|
if (level != FLASHROM_MSG_SPEW)
|
2012-05-14 22:54:58 +00:00
|
|
|
fflush(output_type);
|
|
|
|
|
}
|
2012-06-06 09:17:06 +00:00
|
|
|
#ifndef STANDALONE
|
|
|
|
|
if ((level <= verbose_logfile) && logfile) {
|
2012-12-10 13:34:12 +00:00
|
|
|
ret = vfprintf(logfile, fmt, logfile_args);
|
2017-06-19 12:57:10 +02:00
|
|
|
if (level != FLASHROM_MSG_SPEW)
|
2012-06-06 09:17:06 +00:00
|
|
|
fflush(logfile);
|
|
|
|
|
}
|
|
|
|
|
#endif /* !STANDALONE */
|
2012-12-10 13:34:12 +00:00
|
|
|
va_end(logfile_args);
|
2010-01-07 20:21:58 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|