forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutt_logging.c
296 lines (250 loc) · 6.73 KB
/
mutt_logging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* @file
* Mutt Logging
*
* @authors
* Copyright (C) 2018 Richard Russon <[email protected]>
*
* @copyright
* 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.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page mutt_logging Mutt Logging
*
* Mutt Logging
*/
#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "mutt/mutt.h"
#include "globals.h"
#include "mutt_curses.h"
#include "protos.h"
struct timeval LastError = { 0 };
short DebugLevel = 0; /**< Log file logging level */
char *DebugFile = NULL; /**< Log file name */
const int NumOfLogs = 5; /**< How many log files to rotate */
bool LogAllowDebugSet = false;
/**
* micro_elapsed - Number of microseconds between two timevals
* @param begin Begin time
* @param end End time
* @retval num Microseconds elapsed
* @retval LONG_MAX Begin time was zero
*/
static long micro_elapsed(const struct timeval *begin, const struct timeval *end)
{
if ((begin->tv_sec == 0) && (end->tv_sec != 0))
return LONG_MAX;
return ((end->tv_sec - begin->tv_sec) * 1000000) + (end->tv_usec - begin->tv_usec);
}
/**
* error_pause - Wait for an error message to be read
*
* If a second hasn't elapsed since LastError, then wait.
*/
static void error_pause(void)
{
struct timeval now = { 0 };
if (gettimeofday(&now, NULL) < 0)
{
mutt_debug(1, "gettimeofday failed: %d\n", errno);
return;
}
long micro = micro_elapsed(&LastError, &now);
if (micro >= 1000000)
return;
struct timespec wait = { 0 };
wait.tv_nsec = 1000000000 - (micro * 10);
mutt_refresh();
nanosleep(&wait, NULL);
}
/**
* rotate_logs - Rotate a set of numbered files
* @param file Template filename
* @param count Maximum number of files
* @retval ptr Name of the 0'th file
*
* Given a template 'temp', rename files numbered 0 to (count-1).
*
* Rename:
* - ...
* - temp1 -> temp2
* - temp0 -> temp1
*/
static const char *rotate_logs(const char *file, int count)
{
if (!file)
return NULL;
char old[PATH_MAX];
char new[PATH_MAX];
/* rotate the old debug logs */
for (count -= 2; count >= 0; count--)
{
snprintf(old, sizeof(old), "%s%d", file, count);
snprintf(new, sizeof(new), "%s%d", file, count + 1);
mutt_expand_path(old, sizeof(old));
mutt_expand_path(new, sizeof(new));
rename(old, new);
}
return mutt_str_strdup(old);
}
/**
* mutt_clear_error - Clear the message line (bottom line of screen)
*/
void mutt_clear_error(void)
{
/* Make sure the error message has had time to be read */
if (OPT_MSG_ERR)
error_pause();
ErrorBuf[0] = 0;
if (!OPT_NO_CURSES)
mutt_window_clearline(MuttMessageWindow, 0);
}
/**
* log_disp_curses - Display a log line in the message line
* @param stamp Unix time
* @param file Source file
* @param line Source line
* @param function Source function
* @param level Logging level, e.g. #LL_WARNING
* @param ... Format string and parameters, like printf()
* @retval >0 Success, number of characters written
*/
int log_disp_curses(time_t stamp, const char *file, int line,
const char *function, int level, ...)
{
if (level > DebugLevel)
return 0;
char buf[LONG_STRING];
va_list ap;
va_start(ap, level);
const char *fmt = va_arg(ap, const char *);
int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (level == LL_PERROR)
{
char *buf2 = buf + ret;
int len = sizeof(buf) - ret;
char *p = strerror(errno);
if (!p)
p = _("unknown error");
ret += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
}
log_disp_file(stamp, file, line, function, level, "%s", buf);
if (stamp == 0)
log_disp_queue(stamp, file, line, function, level, "%s", buf);
/* Don't display debugging message on screen */
if (level > LL_MESSAGE)
return 0;
/* Only pause if this is a message following an error */
if ((level > LL_ERROR) && OPT_MSG_ERR)
error_pause();
mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, MuttMessageWindow->cols,
FMT_LEFT, 0, buf, sizeof(buf), 0);
if (!OPT_KEEP_QUIET)
{
if (level == LL_ERROR)
BEEP();
SETCOLOR((level == LL_ERROR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
mutt_window_mvaddstr(MuttMessageWindow, 0, 0, ErrorBuf);
NORMAL_COLOR;
mutt_window_clrtoeol(MuttMessageWindow);
mutt_refresh();
}
if (level <= LL_ERROR)
{
OPT_MSG_ERR = true;
if (gettimeofday(&LastError, NULL) < 0)
mutt_debug(1, "gettimeofday failed: %d\n", errno);
}
else
{
OPT_MSG_ERR = false;
LastError.tv_sec = 0;
}
return ret;
}
/**
* mutt_log_start - Enable file logging
*
* This also handles file rotation.
*/
int mutt_log_start(void)
{
if (DebugLevel < 1)
return 0;
if (log_file_running())
return 0;
char ver[64];
snprintf(ver, sizeof(ver), "-%s%s", PACKAGE_VERSION, GitVer);
log_file_set_version(ver);
const char *name = rotate_logs(DebugFile, NumOfLogs);
if (!name)
return -1;
log_file_set_filename(name, false);
/* This will trigger the file creation */
if (log_file_set_level(DebugLevel, true) < 1)
return -1;
return 0;
}
/**
* mutt_log_stop - Close the log file
*/
void mutt_log_stop(void)
{
log_file_close(false);
}
/**
* mutt_log_set_level - Change the logging level
* @param level Logging level
* @param verbose If true, then log the event
* @retval 0 Success
* @retval -1 Error, level is out of range
*/
int mutt_log_set_level(int level, bool verbose)
{
if (log_file_set_level(level, verbose) != 0)
return -1;
if (!LogAllowDebugSet)
return 0;
DebugLevel = level;
return 0;
}
/**
* mutt_log_set_file - Change the logging file
* @param file Name to use
* @param verbose If true, then log the event
* @retval 0 Success, file opened
* @retval -1 Error, see errno
*
* Close the old log, rotate the new logs and open the new log.
*/
int mutt_log_set_file(const char *file, bool verbose)
{
if (mutt_str_strcmp(DebugFile, file) == 0)
return 0;
if (!LogAllowDebugSet)
return 0;
const char *name = rotate_logs(file, NumOfLogs);
if (!name)
return 0;
log_file_set_filename(name, verbose);
mutt_str_replace(&DebugFile, file);
return 0;
}