blob: 424721cdb827d64731ea465d5e5e00255d7f0e34 [file] [log] [blame]
snelson5a364a82010-01-07 20:21:58 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
Souvik Ghosh3c963a42016-07-19 18:48:15 -07005 * Copyright (C) 2011 Carl-Daniel Hailfinger
snelson5a364a82010-01-07 20:21:58 +00006 *
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 Ghosh3c963a42016-07-19 18:48:15 -070024#include <string.h>
25#include <errno.h>
snelson5a364a82010-01-07 20:21:58 +000026#include "flash.h"
27
Souvik Ghosh3c963a42016-07-19 18:48:15 -070028#ifndef STANDALONE
29static FILE *logfile = NULL;
30
31int 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
46int 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
59void 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
snelson5a364a82010-01-07 20:21:58 +000070int print(int type, const char *fmt, ...)
71{
72 va_list ap;
73 int ret;
74 FILE *output_type;
uwef6f94d42010-03-13 17:28:29 +000075
76 switch (type) {
snelson5a364a82010-01-07 20:21:58 +000077 case MSG_ERROR:
78 output_type = stderr;
79 break;
80 case MSG_BARF:
Souvik Ghosh3c963a42016-07-19 18:48:15 -070081 if (verbose_screen < 3)
stefanctee850532011-08-04 17:40:25 +000082 return 0;
83 case MSG_DEBUG2:
Souvik Ghosh3c963a42016-07-19 18:48:15 -070084 if (verbose_screen < 2)
uwef6f94d42010-03-13 17:28:29 +000085 return 0;
snelson5a364a82010-01-07 20:21:58 +000086 case MSG_DEBUG:
Souvik Ghosh3c963a42016-07-19 18:48:15 -070087 if (verbose_screen < 1)
uwef6f94d42010-03-13 17:28:29 +000088 return 0;
snelson5a364a82010-01-07 20:21:58 +000089 case MSG_INFO:
90 default:
91 output_type = stdout;
92 break;
93 }
uwef6f94d42010-03-13 17:28:29 +000094
snelson5a364a82010-01-07 20:21:58 +000095 va_start(ap, fmt);
96 ret = vfprintf(output_type, fmt, ap);
97 va_end(ap);
uwe7ce825d2011-06-19 17:27:57 +000098 fflush(output_type);
Souvik Ghosh3c963a42016-07-19 18:48:15 -070099
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
snelson5a364a82010-01-07 20:21:58 +0000110 return ret;
111}