Debian Codegraph @

src/qmi-firmware-update/qfu-log.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmi-firmware-update -- Command line tool to update firmware in QMI devices
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2016-2017 Zodiac Inflight Innovations
19 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
20 */
21
22 #include <config.h>
23 #include <errno.h>
24
25 #include <glib.h>
26 #include <glib/gprintf.h>
27
28 #include <libqmi-glib.h>
29
30 #if QMI_MBIM_QMUX_SUPPORTED
31 #include <libmbim-glib.h>
32 #endif
33
34 #include "qfu-log.h"
35
36 static gboolean stdout_silent_flag;
37 static gboolean stdout_verbose_flag;
38 static FILE *verbose_log_file;
39
40 static void
41 log_handler (const gchar *log_domain,
42 GLogLevelFlags log_level,
43 const gchar *message,
44 gpointer user_data)
45 {
46 const gchar *log_level_str;
47 time_t now;
48 gchar time_str[64];
49 struct tm *local_time;
50 gboolean err;
51
52 /* Nothing to do if we're silent */
53 if (stdout_silent_flag && !verbose_log_file)
54 return;
55
56 now = time ((time_t *) NULL);
57 local_time = localtime (&now);
58 strftime (time_str, 64, "%d %b %Y, %H:%M:%S", local_time);
59 err = FALSE;
60
61 switch (log_level) {
62 case G_LOG_LEVEL_WARNING:
63 log_level_str = "-Warning **";
64 break;
65
66 case G_LOG_LEVEL_CRITICAL:
67 case G_LOG_LEVEL_ERROR:
68 log_level_str = "-Error **";
69 err = TRUE;
70 break;
71
72 case G_LOG_LEVEL_DEBUG:
73 log_level_str = "[Debug]";
74 break;
75
76 case G_LOG_LEVEL_MESSAGE:
77 case G_LOG_LEVEL_INFO:
78 log_level_str = "";
79 break;
80
81 case G_LOG_FLAG_FATAL:
82 case G_LOG_LEVEL_MASK:
83 case G_LOG_FLAG_RECURSION:
84 default:
85 g_assert_not_reached ();
86 }
87
88 if (verbose_log_file) {
89 g_fprintf (verbose_log_file,
90 "[%s] %s %s\n",
91 time_str,
92 log_level_str,
93 message);
94 fflush (verbose_log_file);
95 }
96
97 if (stdout_verbose_flag || err)
98 g_fprintf (err ? stderr : stdout,
99 "[%s] %s %s\n",
100 time_str,
101 log_level_str,
102 message);
103 }
104
105 gboolean
106 qfu_log_get_verbose (void)
107 {
108 return (stdout_verbose_flag || verbose_log_file);
109 }
110
111 gboolean
112 qfu_log_get_verbose_stdout (void)
113 {
114 return (stdout_verbose_flag);
115 }
116
117 gboolean
118 qfu_log_init (gboolean stdout_verbose,
119 gboolean stdout_silent,
120 const gchar *verbose_log_path)
121 {
122 if (stdout_verbose && stdout_silent) {
123 g_printerr ("error: cannot specify --verbose and --silent at the same time\n");
124 return FALSE;
125 }
126
127 /* Store flags */
128 stdout_verbose_flag = stdout_verbose;
129 stdout_silent_flag = stdout_silent;
130
131 /* Open verbose log if required */
132 if (verbose_log_path) {
133 verbose_log_file = fopen (verbose_log_path, "w");
134 if (!verbose_log_file) {
135 g_printerr ("error: cannot open verbose log file for writing: %s\n", g_strerror (errno));
136 return FALSE;
137 }
138 }
139
140 /* Default application logging */
141 g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, NULL);
142
143 /* libqmi logging */
144 g_log_set_handler ("Qmi", G_LOG_LEVEL_MASK, log_handler, NULL);
145 if (stdout_verbose_flag || verbose_log_file)
146 qmi_utils_set_traces_enabled (TRUE);
147
148 #if QMI_MBIM_QMUX_SUPPORTED
149 /* libmbim logging */
150 g_log_set_handler ("Mbim", G_LOG_LEVEL_MASK, log_handler, NULL);
151 if (stdout_verbose_flag || verbose_log_file)
152 mbim_utils_set_traces_enabled (TRUE);
153 #endif
154
155 return TRUE;
156 }
157
158 void
159 qfu_log_shutdown (void)
160 {
161 if (verbose_log_file)
162 fclose (verbose_log_file);
163 }