NeoMutt  2023-03-22-27-g3cb248
Teaching an old dog new tricks
DOXYGEN
logging.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <errno.h>
31#include <stdarg.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <string.h>
35#include <time.h>
36#include <unistd.h>
37#include "logging.h"
38#include "date.h"
39#include "file.h"
40#include "memory.h"
41#include "message.h"
42#include "queue.h"
43#include "string2.h"
44
45const char *LevelAbbr = "PEWM12345N";
46
53
54FILE *LogFileFP = NULL;
55char *LogFileName = NULL;
56int LogFileLevel = 0;
57char *LogFileVersion = NULL;
58
62static struct LogLineList LogQueue = STAILQ_HEAD_INITIALIZER(LogQueue);
63
65int LogQueueMax = 0;
66
77static const char *timestamp(time_t stamp)
78{
79 static char buf[23] = { 0 };
80 static time_t last = 0;
81
82 if (stamp == 0)
83 stamp = mutt_date_now();
84
85 if (stamp != last)
86 {
87 mutt_date_localtime_format(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", stamp);
88 last = stamp;
89 }
90
91 return buf;
92}
93
98void log_file_close(bool verbose)
99{
100 if (!LogFileFP)
101 return;
102
103 fprintf(LogFileFP, "[%s] Closing log.\n", timestamp(0));
104 fprintf(LogFileFP, "# vim: syntax=neomuttlog\n");
106 if (verbose)
107 mutt_message(_("Closed log file: %s"), LogFileName);
108}
109
119int log_file_open(bool verbose)
120{
121 if (!LogFileName)
122 return -1;
123
124 if (LogFileFP)
125 log_file_close(false);
126
128 return -1;
129
131 if (!LogFileFP)
132 return -1;
133 setvbuf(LogFileFP, NULL, _IOLBF, 0);
134
135 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
137 if (verbose)
138 mutt_message(_("Debugging at level %d to file '%s'"), LogFileLevel, LogFileName);
139 return 0;
140}
141
149int log_file_set_filename(const char *file, bool verbose)
150{
151 if (!file)
152 return -1;
153
154 /* also handles both being NULL */
155 if (mutt_str_equal(LogFileName, file))
156 return 0;
157
159
160 if (!LogFileName)
161 log_file_close(verbose);
162
163 return log_file_open(verbose);
164}
165
175int log_file_set_level(enum LogLevel level, bool verbose)
176{
177 if ((level < LL_MESSAGE) || (level >= LL_MAX))
178 return -1;
179
180 if (level == LogFileLevel)
181 return 0;
182
183 LogFileLevel = level;
184
185 if (level == LL_MESSAGE)
186 {
187 log_file_close(verbose);
188 }
189 else if (LogFileFP)
190 {
191 if (verbose)
192 mutt_message(_("Logging at level %d to file '%s'"), LogFileLevel, LogFileName);
193 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
195 }
196 else
197 {
198 log_file_open(verbose);
199 }
200
201 if (LogFileLevel >= LL_DEBUG5)
202 {
203 fprintf(LogFileFP, "\n"
204 "WARNING:\n"
205 " Logging at this level can reveal personal information.\n"
206 " Review the log carefully before posting in bug reports.\n"
207 "\n");
208 }
209
210 return 0;
211}
212
220void log_file_set_version(const char *version)
221{
223}
224
230{
231 return LogFileFP;
232}
233
245int log_disp_file(time_t stamp, const char *file, int line,
246 const char *function, enum LogLevel level, ...)
247{
248 if (!LogFileFP || (level < LL_PERROR) || (level > LogFileLevel))
249 return 0;
250
251 int rc = 0;
252 int err = errno;
253
254 if (!function)
255 function = "UNKNOWN";
256
257 rc += fprintf(LogFileFP, "[%s]<%c> %s() ", timestamp(stamp), LevelAbbr[level + 3], function);
258
259 va_list ap;
260 va_start(ap, level);
261 const char *fmt = va_arg(ap, const char *);
262 rc += vfprintf(LogFileFP, fmt, ap);
263 va_end(ap);
264
265 if (level == LL_PERROR)
266 {
267 fprintf(LogFileFP, ": %s\n", strerror(err));
268 }
269 else if (level <= LL_MESSAGE)
270 {
271 fputs("\n", LogFileFP);
272 rc++;
273 }
274
275 return rc;
276}
277
285int log_queue_add(struct LogLine *ll)
286{
287 if (!ll)
288 return -1;
289
290 STAILQ_INSERT_TAIL(&LogQueue, ll, entries);
291
292 if ((LogQueueMax > 0) && (LogQueueCount >= LogQueueMax))
293 {
294 ll = STAILQ_FIRST(&LogQueue);
295 STAILQ_REMOVE_HEAD(&LogQueue, entries);
296 FREE(&ll->message);
297 FREE(&ll);
298 }
299 else
300 {
302 }
303 return LogQueueCount;
304}
305
313{
314 if (size < 0)
315 size = 0;
316 LogQueueMax = size;
317}
318
325{
326 struct LogLine *ll = NULL;
327 struct LogLine *tmp = NULL;
328
329 STAILQ_FOREACH_SAFE(ll, &LogQueue, entries, tmp)
330 {
331 STAILQ_REMOVE(&LogQueue, ll, LogLine, entries);
332 FREE(&ll->message);
333 FREE(&ll);
334 }
335
336 LogQueueCount = 0;
337}
338
347{
348 struct LogLine *ll = NULL;
349 STAILQ_FOREACH(ll, &LogQueue, entries)
350 {
351 disp(ll->time, ll->file, ll->line, ll->function, ll->level, "%s", ll->message);
352 }
353
355}
356
367int log_queue_save(FILE *fp)
368{
369 if (!fp)
370 return 0;
371
372 char buf[32] = { 0 };
373 int count = 0;
374 struct LogLine *ll = NULL;
375 STAILQ_FOREACH(ll, &LogQueue, entries)
376 {
377 mutt_date_localtime_format(buf, sizeof(buf), "%H:%M:%S", ll->time);
378 fprintf(fp, "[%s]<%c> %s", buf, LevelAbbr[ll->level + 3], ll->message);
379 if (ll->level <= LL_MESSAGE)
380 fputs("\n", fp);
381 count++;
382 }
383
384 return count;
385}
386
398int log_disp_queue(time_t stamp, const char *file, int line,
399 const char *function, enum LogLevel level, ...)
400{
401 char buf[1024] = { 0 };
402 int err = errno;
403
404 va_list ap;
405 va_start(ap, level);
406 const char *fmt = va_arg(ap, const char *);
407 int rc = vsnprintf(buf, sizeof(buf), fmt, ap);
408 va_end(ap);
409
410 if (level == LL_PERROR)
411 {
412 if ((rc >= 0) && (rc < sizeof(buf)))
413 rc += snprintf(buf + rc, sizeof(buf) - rc, ": %s", strerror(err));
414 level = LL_ERROR;
415 }
416
417 struct LogLine *ll = mutt_mem_calloc(1, sizeof(*ll));
418 ll->time = (stamp != 0) ? stamp : mutt_date_now();
419 ll->file = file;
420 ll->line = line;
421 ll->function = function;
422 ll->level = level;
423 ll->message = mutt_str_dup(buf);
424
425 log_queue_add(ll);
426
427 return rc;
428}
429
440int log_disp_terminal(time_t stamp, const char *file, int line,
441 const char *function, enum LogLevel level, ...)
442{
443 char buf[1024] = { 0 };
444
445 va_list ap;
446 va_start(ap, level);
447 const char *fmt = va_arg(ap, const char *);
448 int rc = vsnprintf(buf, sizeof(buf), fmt, ap);
449 va_end(ap);
450
451 log_disp_file(stamp, file, line, function, level, "%s", buf);
452
453 if ((level < LL_PERROR) || (level > LL_MESSAGE))
454 return 0;
455
456 FILE *fp = (level < LL_MESSAGE) ? stderr : stdout;
457 int err = errno;
458 int colour = 0;
459 bool tty = (isatty(fileno(fp)) == 1);
460
461 if (tty)
462 {
463 switch (level)
464 {
465 case LL_PERROR:
466 case LL_ERROR:
467 colour = 31;
468 break;
469 case LL_WARNING:
470 colour = 33;
471 break;
472 case LL_MESSAGE:
473 // colour = 36;
474 break;
475 default:
476 break;
477 }
478 }
479
480 if (colour > 0)
481 rc += fprintf(fp, "\033[1;%dm", colour); // Escape
482
483 fputs(buf, fp);
484
485 if (level == LL_PERROR)
486 rc += fprintf(fp, ": %s", strerror(err));
487
488 if (colour > 0)
489 rc += fprintf(fp, "\033[0m"); // Escape
490
491 rc += fprintf(fp, "\n");
492
493 return rc;
494}
495
499int log_disp_null(time_t stamp, const char *file, int line,
500 const char *function, enum LogLevel level, ...)
501{
502 return 0;
503}
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:702
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:432
Time and date handling routines.
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:149
File management functions.
int log_disp_file(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Save a log line to a file - Implements log_dispatcher_t -.
Definition: logging.c:245
int log_disp_terminal(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Save a log line to the terminal - Implements log_dispatcher_t -.
Definition: logging.c:440
log_dispatcher_t MuttLogger
The log dispatcher -.
Definition: logging.c:52
int log_disp_queue(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Save a log line to an internal queue - Implements log_dispatcher_t -.
Definition: logging.c:398
#define mutt_message(...)
Definition: logging.h:86
int log_disp_null(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Discard log lines - Implements log_dispatcher_t -.
Definition: logging.c:499
int log_file_open(bool verbose)
Start logging to a file.
Definition: logging.c:119
FILE * LogFileFP
Log file handle.
Definition: logging.c:54
void log_queue_empty(void)
Free the contents of the queue.
Definition: logging.c:324
void log_queue_set_max_size(int size)
Set a upper limit for the queue length.
Definition: logging.c:312
int log_file_set_level(enum LogLevel level, bool verbose)
Set the logging level.
Definition: logging.c:175
static struct LogLineList LogQueue
In-memory list of log lines.
Definition: logging.c:62
bool log_file_running(void)
Is the log file running?
Definition: logging.c:229
char * LogFileVersion
Program version.
Definition: logging.c:57
int LogQueueMax
Maximum number of entries in the log queue.
Definition: logging.c:65
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:77
const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:45
void log_queue_flush(log_dispatcher_t disp)
Replay the log queue.
Definition: logging.c:346
int LogQueueCount
Number of entries currently in the log queue.
Definition: logging.c:64
int LogFileLevel
Log file level.
Definition: logging.c:56
int log_queue_save(FILE *fp)
Save the contents of the queue to a temporary file.
Definition: logging.c:367
char * LogFileName
Log file name.
Definition: logging.c:55
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
int log_file_set_filename(const char *file, bool verbose)
Set the filename for the log.
Definition: logging.c:149
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
Definition: logging.c:285
void log_file_set_version(const char *version)
Set the program's version number.
Definition: logging.c:220
Logging Dispatcher.
int(* log_dispatcher_t)(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Definition: logging.h:65
LogLevel
Names for the Logging Levels.
Definition: logging.h:35
@ LL_ERROR
Log error.
Definition: logging.h:37
@ LL_PERROR
Log perror (using errno)
Definition: logging.h:36
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_WARNING
Log warning.
Definition: logging.h:38
@ LL_MESSAGE
Log informational message.
Definition: logging.h:39
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
@ LL_MAX
Definition: logging.h:47
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Memory management wrappers.
#define FREE(x)
Definition: memory.h:43
Message logging.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
String manipulation functions.
#define NONULL(x)
Definition: string2.h:37
A Log line.
Definition: logging.h:73
const char * file
Source file.
Definition: logging.h:75
char * message
Message to be logged.
Definition: logging.h:79
const char * function
C function.
Definition: logging.h:77
int line
Line number in source file.
Definition: logging.h:76
enum LogLevel level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:78
time_t time
Timestamp of the message.
Definition: logging.h:74