NeoMutt  2025-12-11-177-g48e272
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
logging.c
Go to the documentation of this file.
1
24
30
31#include "config.h"
32#include <errno.h>
33#include <stdarg.h> // IWYU pragma: keep
34#include <stdbool.h>
35#include <stdio.h>
36#include <string.h>
37#include <time.h>
38#include <unistd.h>
39#include "date.h"
40#include "file.h"
41#include "logging2.h"
42#include "memory.h"
43#include "message.h"
44#include "queue.h"
45#include "string2.h"
46
47const char *LogLevelAbbr = "PEWM12345N";
48
55
56static FILE *LogFileFP = NULL;
57static char *LogFileName = NULL;
58static int LogFileLevel = 0;
59static char *LogFileVersion = NULL;
60
64static struct LogLineList LogQueue = STAILQ_HEAD_INITIALIZER(LogQueue);
65
66static int LogQueueCount = 0;
67static int LogQueueMax = 0;
68
79static const char *timestamp(time_t stamp)
80{
81 static char buf[23] = { 0 };
82 static time_t last = 0;
83
84 if (stamp == 0)
85 stamp = mutt_date_now();
86
87 if (stamp != last)
88 {
89 mutt_date_localtime_format(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", stamp);
90 last = stamp;
91 }
92
93 return buf;
94}
95
100void log_file_close(bool verbose)
101{
102 if (!LogFileFP)
103 return;
104
105 fprintf(LogFileFP, "[%s] Closing log.\n", timestamp(0));
106 fprintf(LogFileFP, "# vim: syntax=neomuttlog\n");
108 if (verbose)
109 mutt_message(_("Closed log file: %s"), LogFileName);
110}
111
121int log_file_open(bool verbose)
122{
123 if (!LogFileName)
124 return -1;
125
126 if (LogFileFP)
127 log_file_close(false);
128
130 return -1;
131
133 if (!LogFileFP)
134 return -1;
135 setvbuf(LogFileFP, NULL, _IOLBF, 0);
136
137 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
139 if (verbose)
140 mutt_message(_("Debugging at level %d to file '%s'"), LogFileLevel, LogFileName);
141 return 0;
142}
143
151int log_file_set_filename(const char *file, bool verbose)
152{
153 if (!file)
154 return -1;
155
156 /* also handles both being NULL */
157 if (mutt_str_equal(LogFileName, file))
158 return 0;
159
161
162 if (!LogFileName)
163 log_file_close(verbose);
164
165 return log_file_open(verbose);
166}
167
177int log_file_set_level(enum LogLevel level, bool verbose)
178{
179 if ((level < LL_MESSAGE) || (level >= LL_MAX))
180 return -1;
181
182 if (level == LogFileLevel)
183 return 0;
184
185 LogFileLevel = level;
186
187 if (level == LL_MESSAGE)
188 {
189 log_file_close(verbose);
190 }
191 else if (LogFileFP)
192 {
193 if (verbose)
194 mutt_message(_("Logging at level %d to file '%s'"), LogFileLevel, LogFileName);
195 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
197 }
198 else
199 {
200 log_file_open(verbose);
201 }
202
203 if (LogFileLevel >= LL_DEBUG5)
204 {
205 fprintf(LogFileFP, "\n"
206 "WARNING:\n"
207 " Logging at this level can reveal personal information.\n"
208 " Review the log carefully before posting in bug reports.\n"
209 "\n");
210 }
211
212 return 0;
213}
214
222void log_file_set_version(const char *version)
223{
225}
226
232{
233 return LogFileFP;
234}
235
247int log_disp_file(time_t stamp, const char *file, int line, const char *function,
248 enum LogLevel level, const char *format, ...)
249{
250 if (!LogFileFP || (level < LL_PERROR) || (level > LogFileLevel))
251 return 0;
252
253 int rc = 0;
254 int err = errno;
255
256 if (!function)
257 function = "UNKNOWN";
258
259 rc += fprintf(LogFileFP, "[%s]<%c> %s() ", timestamp(stamp),
260 LogLevelAbbr[level + 3], function);
261
262 va_list ap;
263 va_start(ap, format);
264 rc += vfprintf(LogFileFP, format, ap);
265 va_end(ap);
266
267 if (level == LL_PERROR)
268 {
269 fprintf(LogFileFP, ": %s\n", strerror(err));
270 }
271 else if (level <= LL_MESSAGE)
272 {
273 fputs("\n", LogFileFP);
274 rc++;
275 }
276
277 return rc;
278}
279
287int log_queue_add(struct LogLine *ll)
288{
289 if (!ll)
290 return -1;
291
292 STAILQ_INSERT_TAIL(&LogQueue, ll, entries);
293
294 if ((LogQueueMax > 0) && (LogQueueCount >= LogQueueMax))
295 {
296 ll = STAILQ_FIRST(&LogQueue);
297 STAILQ_REMOVE_HEAD(&LogQueue, entries);
298 FREE(&ll->message);
299 FREE(&ll);
300 }
301 else
302 {
304 }
305 return LogQueueCount;
306}
307
315{
316 if (size < 0)
317 size = 0;
318 LogQueueMax = size;
319}
320
327{
328 struct LogLine *ll = NULL;
329 struct LogLine *tmp = NULL;
330
331 STAILQ_FOREACH_SAFE(ll, &LogQueue, entries, tmp)
332 {
333 STAILQ_REMOVE(&LogQueue, ll, LogLine, entries);
334 FREE(&ll->message);
335 FREE(&ll);
336 }
337
338 LogQueueCount = 0;
339}
340
349{
350 struct LogLine *ll = NULL;
351 STAILQ_FOREACH(ll, &LogQueue, entries)
352 {
353 disp(ll->time, ll->file, ll->line, ll->function, ll->level, "%s", ll->message);
354 }
355
357}
358
363struct LogLineList log_queue_get(void)
364{
365 return LogQueue;
366}
367
379int log_disp_queue(time_t stamp, const char *file, int line, const char *function,
380 enum LogLevel level, const char *format, ...)
381{
382 char buf[LOG_LINE_MAX_LEN] = { 0 };
383 int err = errno;
384
385 va_list ap;
386 va_start(ap, format);
387 int rc = vsnprintf(buf, sizeof(buf), format, ap);
388 va_end(ap);
389
390 if (level == LL_PERROR)
391 {
392 if ((rc >= 0) && (rc < sizeof(buf)))
393 rc += snprintf(buf + rc, sizeof(buf) - rc, ": %s", strerror(err));
394 level = LL_ERROR;
395 }
396
397 struct LogLine *ll = MUTT_MEM_CALLOC(1, struct LogLine);
398 ll->time = (stamp != 0) ? stamp : mutt_date_now();
399 ll->file = file;
400 ll->line = line;
401 ll->function = function;
402 ll->level = level;
403 ll->message = mutt_str_dup(buf);
404
405 log_queue_add(ll);
406
407 return rc;
408}
409
422int log_disp_terminal(time_t stamp, const char *file, int line, const char *function,
423 enum LogLevel level, const char *format, ...)
424{
425 char buf[LOG_LINE_MAX_LEN] = { 0 };
426
427 va_list ap;
428 va_start(ap, format);
429 int rc = vsnprintf(buf, sizeof(buf), format, ap);
430 va_end(ap);
431
432 log_disp_file(stamp, file, line, function, level, "%s", buf);
433
434 if ((level < LL_PERROR) || (level > LL_MESSAGE))
435 return 0;
436
437 FILE *fp = (level < LL_MESSAGE) ? stderr : stdout;
438 int err = errno;
439 int color = 0;
440 bool tty = (isatty(fileno(fp)) == 1);
441
442 if (tty)
443 {
444 switch (level)
445 {
446 case LL_PERROR:
447 case LL_ERROR:
448 color = 31;
449 break;
450 case LL_WARNING:
451 color = 33;
452 break;
453 case LL_MESSAGE:
454 default:
455 break;
456 }
457 }
458
459 if (color > 0)
460 rc += fprintf(fp, "\033[1;%dm", color); // Escape
461
462 fputs(buf, fp);
463
464 if (level == LL_PERROR)
465 rc += fprintf(fp, ": %s", strerror(err));
466
467 if (color > 0)
468 rc += fprintf(fp, "\033[0m"); // Escape
469
470 rc += fprintf(fp, "\n");
471
472 return rc;
473}
474
483void log_multiline_full(enum LogLevel level, const char *str, const char *file,
484 int line, const char *func)
485{
486 while (str && (str[0] != '\0'))
487 {
488 const char *end = strchr(str, '\n');
489 if (end)
490 {
491 int len = end - str;
492 MuttLogger(0, file, line, func, level, "%.*s\n", len, str);
493 str = end + 1;
494 }
495 else
496 {
497 MuttLogger(0, file, line, func, level, "%s\n", str);
498 break;
499 }
500 }
501}
Time and date handling routines.
File management functions.
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
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:379
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:247
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:422
log_dispatcher_t MuttLogger
The log dispatcher -.
Definition logging.c:54
#define mutt_message(...)
Definition logging2.h:93
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:71
int log_dispatcher_t MuttLogger
LogLevel
Names for the Logging Levels.
Definition logging2.h:40
@ LL_ERROR
Log error.
Definition logging2.h:42
@ LL_PERROR
Log perror (using errno)
Definition logging2.h:41
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
@ LL_WARNING
Log warning.
Definition logging2.h:43
@ LL_MESSAGE
Log informational message.
Definition logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
@ LL_MAX
Definition logging2.h:52
#define LOG_LINE_MAX_LEN
Log lines longer than this will be truncated.
Definition logging2.h:32
Memory management wrappers.
#define FREE(x)
Definition memory.h:63
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:48
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition date.c:952
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:457
int log_file_open(bool verbose)
Start logging to a file.
Definition logging.c:121
static FILE * LogFileFP
Log file handle.
Definition logging.c:56
void log_queue_empty(void)
Free the contents of the queue.
Definition logging.c:326
void log_queue_set_max_size(int size)
Set a upper limit for the queue length.
Definition logging.c:314
int log_file_set_level(enum LogLevel level, bool verbose)
Set the logging level.
Definition logging.c:177
static struct LogLineList LogQueue
In-memory list of log lines.
Definition logging.c:64
bool log_file_running(void)
Is the log file running?
Definition logging.c:231
static char * LogFileVersion
Program version.
Definition logging.c:59
static int LogQueueMax
Maximum number of entries in the log queue.
Definition logging.c:67
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition logging.c:79
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:483
void log_queue_flush(log_dispatcher_t disp)
Replay the log queue.
Definition logging.c:348
static int LogQueueCount
Number of entries currently in the log queue.
Definition logging.c:66
static int LogFileLevel
Log file level.
Definition logging.c:58
static char * LogFileName
Log file name.
Definition logging.c:57
void log_file_close(bool verbose)
Close the log file.
Definition logging.c:100
int log_file_set_filename(const char *file, bool verbose)
Set the filename for the log.
Definition logging.c:151
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
Definition logging.c:287
struct LogLineList log_queue_get(void)
Get the Log Queue.
Definition logging.c:363
const char * LogLevelAbbr
Abbreviations of logging level names.
Definition logging.c:47
void log_file_set_version(const char *version)
Set the program's version number.
Definition logging.c:222
Message logging.
#define _(a)
Definition message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
#define STAILQ_REMOVE_HEAD(head, field)
Definition queue.h:461
#define STAILQ_REMOVE(head, elm, type, field)
Definition queue.h:441
#define STAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
#define STAILQ_FIRST(head)
Definition queue.h:388
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:400
String manipulation functions.
#define NONULL(x)
Definition string2.h:44
A Log line.
Definition logging2.h:80
const char * file
Source file.
Definition logging2.h:82
char * message
Message to be logged.
Definition logging2.h:86
const char * function
C function.
Definition logging2.h:84
int line
Line number in source file.
Definition logging2.h:83
enum LogLevel level
Log level, e.g. LL_DEBUG1.
Definition logging2.h:85
time_t time
Timestamp of the message.
Definition logging2.h:81