NeoMutt  2024-04-25-97-g7d2481
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
logging.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <errno.h>
32#include <stdarg.h> // IWYU pragma: keep
33#include <stdbool.h>
34#include <stdio.h>
35#include <string.h>
36#include <time.h>
37#include <unistd.h>
38#include "date.h"
39#include "file.h"
40#include "logging2.h"
41#include "memory.h"
42#include "message.h"
43#include "queue.h"
44#include "string2.h"
45
46const char *LevelAbbr = "PEWM12345N";
47
54
55static FILE *LogFileFP = NULL;
56static char *LogFileName = NULL;
57static int LogFileLevel = 0;
58static char *LogFileVersion = NULL;
59
63static struct LogLineList LogQueue = STAILQ_HEAD_INITIALIZER(LogQueue);
64
65static int LogQueueCount = 0;
66static int LogQueueMax = 0;
67
78static const char *timestamp(time_t stamp)
79{
80 static char buf[23] = { 0 };
81 static time_t last = 0;
82
83 if (stamp == 0)
84 stamp = mutt_date_now();
85
86 if (stamp != last)
87 {
88 mutt_date_localtime_format(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", stamp);
89 last = stamp;
90 }
91
92 return buf;
93}
94
99void log_file_close(bool verbose)
100{
101 if (!LogFileFP)
102 return;
103
104 fprintf(LogFileFP, "[%s] Closing log.\n", timestamp(0));
105 fprintf(LogFileFP, "# vim: syntax=neomuttlog\n");
107 if (verbose)
108 mutt_message(_("Closed log file: %s"), LogFileName);
109}
110
120int log_file_open(bool verbose)
121{
122 if (!LogFileName)
123 return -1;
124
125 if (LogFileFP)
126 log_file_close(false);
127
129 return -1;
130
132 if (!LogFileFP)
133 return -1;
134 setvbuf(LogFileFP, NULL, _IOLBF, 0);
135
136 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
138 if (verbose)
139 mutt_message(_("Debugging at level %d to file '%s'"), LogFileLevel, LogFileName);
140 return 0;
141}
142
150int log_file_set_filename(const char *file, bool verbose)
151{
152 if (!file)
153 return -1;
154
155 /* also handles both being NULL */
156 if (mutt_str_equal(LogFileName, file))
157 return 0;
158
160
161 if (!LogFileName)
162 log_file_close(verbose);
163
164 return log_file_open(verbose);
165}
166
176int log_file_set_level(enum LogLevel level, bool verbose)
177{
178 if ((level < LL_MESSAGE) || (level >= LL_MAX))
179 return -1;
180
181 if (level == LogFileLevel)
182 return 0;
183
184 LogFileLevel = level;
185
186 if (level == LL_MESSAGE)
187 {
188 log_file_close(verbose);
189 }
190 else if (LogFileFP)
191 {
192 if (verbose)
193 mutt_message(_("Logging at level %d to file '%s'"), LogFileLevel, LogFileName);
194 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
196 }
197 else
198 {
199 log_file_open(verbose);
200 }
201
202 if (LogFileLevel >= LL_DEBUG5)
203 {
204 fprintf(LogFileFP, "\n"
205 "WARNING:\n"
206 " Logging at this level can reveal personal information.\n"
207 " Review the log carefully before posting in bug reports.\n"
208 "\n");
209 }
210
211 return 0;
212}
213
221void log_file_set_version(const char *version)
222{
224}
225
231{
232 return LogFileFP;
233}
234
246int log_disp_file(time_t stamp, const char *file, int line, const char *function,
247 enum LogLevel level, const char *format, ...)
248{
249 if (!LogFileFP || (level < LL_PERROR) || (level > LogFileLevel))
250 return 0;
251
252 int rc = 0;
253 int err = errno;
254
255 if (!function)
256 function = "UNKNOWN";
257
258 rc += fprintf(LogFileFP, "[%s]<%c> %s() ", timestamp(stamp), LevelAbbr[level + 3], function);
259
260 va_list ap;
261 va_start(ap, format);
262 rc += vfprintf(LogFileFP, format, 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, const char *function,
399 enum LogLevel level, const char *format, ...)
400{
401 char buf[LOG_LINE_MAX_LEN] = { 0 };
402 int err = errno;
403
404 va_list ap;
405 va_start(ap, format);
406 int rc = vsnprintf(buf, sizeof(buf), format, ap);
407 va_end(ap);
408
409 if (level == LL_PERROR)
410 {
411 if ((rc >= 0) && (rc < sizeof(buf)))
412 rc += snprintf(buf + rc, sizeof(buf) - rc, ": %s", strerror(err));
413 level = LL_ERROR;
414 }
415
416 struct LogLine *ll = mutt_mem_calloc(1, sizeof(*ll));
417 ll->time = (stamp != 0) ? stamp : mutt_date_now();
418 ll->file = file;
419 ll->line = line;
420 ll->function = function;
421 ll->level = level;
422 ll->message = mutt_str_dup(buf);
423
424 log_queue_add(ll);
425
426 return rc;
427}
428
441int log_disp_terminal(time_t stamp, const char *file, int line, const char *function,
442 enum LogLevel level, const char *format, ...)
443{
444 char buf[LOG_LINE_MAX_LEN] = { 0 };
445
446 va_list ap;
447 va_start(ap, format);
448 int rc = vsnprintf(buf, sizeof(buf), format, 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 color = 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 color = 31;
468 break;
469 case LL_WARNING:
470 color = 33;
471 break;
472 case LL_MESSAGE:
473 default:
474 break;
475 }
476 }
477
478 if (color > 0)
479 rc += fprintf(fp, "\033[1;%dm", color); // Escape
480
481 fputs(buf, fp);
482
483 if (level == LL_PERROR)
484 rc += fprintf(fp, ": %s", strerror(err));
485
486 if (color > 0)
487 rc += fprintf(fp, "\033[0m"); // Escape
488
489 rc += fprintf(fp, "\n");
490
491 return rc;
492}
493
502void log_multiline_full(enum LogLevel level, const char *str, const char *file,
503 int line, const char *func)
504{
505 while (str && (str[0] != '\0'))
506 {
507 const char *end = strchr(str, '\n');
508 if (end)
509 {
510 int len = end - str;
511 MuttLogger(0, file, line, func, level, "%.*s\n", len, str);
512 str = end + 1;
513 }
514 else
515 {
516 MuttLogger(0, file, line, func, level, "%s\n", str);
517 break;
518 }
519 }
520}
Time and date handling routines.
const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:46
File management functions.
#define mutt_file_fclose(FP)
Definition: file.h:149
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:148
int log_disp_queue(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
Save a log line to an internal queue - Implements log_dispatcher_t -.
Definition: logging.c:398
int log_disp_file(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
Save a log line to a file - Implements log_dispatcher_t -.
Definition: logging.c:246
int log_disp_terminal(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
Save a log line to the terminal - Implements log_dispatcher_t -.
Definition: logging.c:441
log_dispatcher_t MuttLogger
The log dispatcher -.
Definition: logging.c:53
#define mutt_message(...)
Definition: logging2.h:91
Logging Dispatcher.
int(* log_dispatcher_t)(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...) __attribute__((__format__(__printf__
Definition: logging2.h:69
LogLevel
Names for the Logging Levels.
Definition: logging2.h:38
@ LL_ERROR
Log error.
Definition: logging2.h:40
@ LL_PERROR
Log perror (using errno)
Definition: logging2.h:39
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_WARNING
Log warning.
Definition: logging2.h:41
@ LL_MESSAGE
Log informational message.
Definition: logging2.h:42
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
@ LL_MAX
Definition: logging2.h:50
#define LOG_LINE_MAX_LEN
Log lines longer than this will be truncated.
Definition: logging2.h:32
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
Memory management wrappers.
#define FREE(x)
Definition: memory.h:45
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:951
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:456
int log_file_open(bool verbose)
Start logging to a file.
Definition: logging.c:120
static FILE * LogFileFP
Log file handle.
Definition: logging.c:55
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:176
static struct LogLineList LogQueue
In-memory list of log lines.
Definition: logging.c:63
bool log_file_running(void)
Is the log file running?
Definition: logging.c:230
static char * LogFileVersion
Program version.
Definition: logging.c:58
static int LogQueueMax
Maximum number of entries in the log queue.
Definition: logging.c:66
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:78
void log_multiline_full(enum LogLevel level, const char *str, const char *file, int line, const char *func)
Helper to dump multiline text to the log.
Definition: logging.c:502
void log_queue_flush(log_dispatcher_t disp)
Replay the log queue.
Definition: logging.c:346
static int LogQueueCount
Number of entries currently in the log queue.
Definition: logging.c:65
static int LogFileLevel
Log file level.
Definition: logging.c:57
int log_queue_save(FILE *fp)
Save the contents of the queue to a temporary file.
Definition: logging.c:367
static char * LogFileName
Log file name.
Definition: logging.c:56
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:99
int log_file_set_filename(const char *file, bool verbose)
Set the filename for the log.
Definition: logging.c:150
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:221
Message logging.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
#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: logging2.h:78
const char * file
Source file.
Definition: logging2.h:80
char * message
Message to be logged.
Definition: logging2.h:84
const char * function
C function.
Definition: logging2.h:82
int line
Line number in source file.
Definition: logging2.h:81
enum LogLevel level
Log level, e.g. LL_DEBUG1.
Definition: logging2.h:83
time_t time
Timestamp of the message.
Definition: logging2.h:79