NeoMutt  2025-12-11-276-g10b23b
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
Logging API

Prototype for a Logging Function. More...

Macros

#define mutt_debug(LEVEL, ...)
 
#define mutt_warning(...)
 
#define mutt_message(...)
 
#define mutt_error(...)
 
#define mutt_perror(...)
 

Functions

int log_disp_debug (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
 Display a log line on screen - Implements log_dispatcher_t -.
 
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 -.
 
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 -.
 
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 -.
 
int log_disp_null (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
 Discard log lines - Implements log_dispatcher_t -.
 
int log_disp_curses (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
 Display a log line in the message line - Implements log_dispatcher_t -.
 

Variables

log_dispatcher_t MuttLogger = log_disp_terminal
 The log dispatcher -.
 

Detailed Description

Prototype for a Logging Function.

Parameters
stampUnix time (optional)
fileSource file
lineSource line
functionSource function
levelLogging level, e.g. LL_WARNING
formatprintf()-style formatting string
...Parameters, like printf()
Return values
-1Error
0Success, filtered
>0Success, number of characters written

Macro Definition Documentation

◆ mutt_debug

#define mutt_debug ( LEVEL,
... )
Value:
MuttLogger(0, __FILE__, __LINE__, __func__, LEVEL, __VA_ARGS__)
int log_dispatcher_t MuttLogger

Definition at line 91 of file logging2.h.

◆ mutt_warning

#define mutt_warning ( ...)
Value:
MuttLogger(0, __FILE__, __LINE__, __func__, LL_WARNING, __VA_ARGS__)
@ LL_WARNING
Log warning.
Definition logging2.h:43

Definition at line 92 of file logging2.h.

◆ mutt_message

#define mutt_message ( ...)
Value:
MuttLogger(0, __FILE__, __LINE__, __func__, LL_MESSAGE, __VA_ARGS__)
@ LL_MESSAGE
Log informational message.
Definition logging2.h:44

Definition at line 93 of file logging2.h.

◆ mutt_error

#define mutt_error ( ...)
Value:
MuttLogger(0, __FILE__, __LINE__, __func__, LL_ERROR, __VA_ARGS__)
@ LL_ERROR
Log error.
Definition logging2.h:42

Definition at line 94 of file logging2.h.

◆ mutt_perror

#define mutt_perror ( ...)
Value:
MuttLogger(0, __FILE__, __LINE__, __func__, LL_PERROR, __VA_ARGS__)
@ LL_PERROR
Log perror (using errno)
Definition logging2.h:41

Definition at line 95 of file logging2.h.

Function Documentation

◆ log_disp_debug()

int log_disp_debug ( time_t stamp,
const char * file,
int line,
const char * function,
enum LogLevel level,
const char * format,
... )

Display a log line on screen - Implements log_dispatcher_t -.

Definition at line 74 of file logging.c.

76{
77 char buf[LOG_LINE_MAX_LEN] = { 0 };
78 size_t buflen = sizeof(buf);
79 int err = errno;
80 int colour = 0;
81 int bytes = 0;
82
83 if (DebugLogColor)
84 {
85 switch (level)
86 {
87 case LL_PERROR:
88 case LL_ERROR:
89 colour = 31;
90 break;
91 case LL_WARNING:
92 colour = 33;
93 break;
94 case LL_MESSAGE:
95 default:
96 break;
97 }
98
99 if (colour > 0)
100 {
101 bytes += snprintf(buf + bytes, buflen - bytes, "\033[1;%dm", colour); // Escape
102 }
103 }
104
106 {
107 bytes += log_timestamp(buf + bytes, buflen - bytes, stamp);
108 }
109
110 if (DebugLogLevel)
111 {
112 bytes += log_level(buf + bytes, buflen - bytes, level);
113 }
114
115 va_list ap;
116 va_start(ap, format);
117 bytes += vsnprintf(buf + bytes, buflen - bytes, format, ap);
118 va_end(ap);
119
120 if (level == LL_PERROR)
121 bytes += snprintf(buf + bytes, buflen - bytes, ": %s", strerror(err));
122
123 if (colour > 0)
124 {
125 bytes += snprintf(buf + bytes, buflen - bytes, "\033[0m"); // Escape
126 }
127
128 if (level < LL_DEBUG1)
129 bytes += snprintf(buf + bytes, buflen - bytes, "\n");
130
131 fputs(buf, stdout);
132 return bytes;
133}
bool DebugLogLevel
Prefix log level, e.g. [E].
Definition logging.c:40
bool DebugLogColor
Output ANSI colours.
Definition logging.c:39
bool DebugLogTimestamp
Show the timestamp.
Definition logging.c:41
static int log_level(char *buf, size_t buflen, enum LogLevel level)
Write a log level to a buffer.
Definition logging.c:66
static int log_timestamp(char *buf, size_t buflen, time_t time)
Write a timestamp to a buffer.
Definition logging.c:52
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define LOG_LINE_MAX_LEN
Log lines longer than this will be truncated.
Definition logging2.h:32
+ Here is the call graph for this function:

◆ log_disp_file()

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 -.

This log dispatcher saves a line of text to a file. The format is:

  • [TIMESTAMP]<LEVEL> FUNCTION() FORMATTED-MESSAGE

The caller must first set LogFileName and LogFileLevel, then call log_file_open(). Any logging above LogFileLevel will be ignored.

If stamp is 0, then the current time will be used.

Definition at line 252 of file logging.c.

254{
255 if (!LogFileFP || (level < LL_PERROR) || (level > LogFileLevel))
256 return 0;
257
258 int rc = 0;
259 int err = errno;
260
261 if (!function)
262 function = "UNKNOWN";
263
264 rc += fprintf(LogFileFP, "[%s]<%c> %s() ", timestamp(stamp),
265 LogLevelAbbr[level + 3], function);
266
267 va_list ap;
268 va_start(ap, format);
269 rc += vfprintf(LogFileFP, format, ap);
270 va_end(ap);
271
272 if (level == LL_PERROR)
273 {
274 fprintf(LogFileFP, ": %s\n", strerror(err));
275 }
276 else if (level <= LL_MESSAGE)
277 {
278 fputs("\n", LogFileFP);
279 rc++;
280 }
281
282 return rc;
283}
static FILE * LogFileFP
Log file handle.
Definition logging.c:56
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition logging.c:79
static int LogFileLevel
Log file level.
Definition logging.c:58
const char * LogLevelAbbr
Abbreviations of logging level names.
Definition logging.c:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_disp_queue()

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 -.

This log dispatcher saves a line of text to a queue. The format string and parameters are expanded and the other parameters are stored as they are.

See also
log_queue_set_max_size(), log_queue_flush(), log_queue_empty()
Warning
Log lines are limited to LOG_LINE_MAX_LEN bytes

Definition at line 384 of file logging.c.

386{
387 char buf[LOG_LINE_MAX_LEN] = { 0 };
388 int err = errno;
389
390 va_list ap;
391 va_start(ap, format);
392 int rc = vsnprintf(buf, sizeof(buf), format, ap);
393 va_end(ap);
394
395 if (level == LL_PERROR)
396 {
397 if ((rc >= 0) && (rc < sizeof(buf)))
398 rc += snprintf(buf + rc, sizeof(buf) - rc, ": %s", strerror(err));
399 level = LL_ERROR;
400 }
401
402 struct LogLine *ll = MUTT_MEM_CALLOC(1, struct LogLine);
403 ll->time = (stamp != 0) ? stamp : mutt_date_now();
404 ll->file = file;
405 ll->line = line;
406 ll->function = function;
407 ll->level = level;
408 ll->message = mutt_str_dup(buf);
409
410 log_queue_add(ll);
411
412 return rc;
413}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:457
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
Definition logging.c:292
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_disp_terminal()

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 -.

This log dispatcher saves a line of text to the terminal. The format is:

  • [TIMESTAMP]<LEVEL> FUNCTION() FORMATTED-MESSAGE
Warning
Log lines are limited to LOG_LINE_MAX_LEN bytes
Note
The output will be coloured using ANSI escape sequences, unless the output is redirected.

Definition at line 427 of file logging.c.

429{
430 char buf[LOG_LINE_MAX_LEN] = { 0 };
431
432 va_list ap;
433 va_start(ap, format);
434 int rc = vsnprintf(buf, sizeof(buf), format, ap);
435 va_end(ap);
436
437 log_disp_file(stamp, file, line, function, level, "%s", buf);
438
439 if ((level < LL_PERROR) || (level > LL_MESSAGE))
440 return 0;
441
442 FILE *fp = (level < LL_MESSAGE) ? stderr : stdout;
443 int err = errno;
444 int color = 0;
445 bool tty = (isatty(fileno(fp)) == 1);
446
447 if (tty)
448 {
449 switch (level)
450 {
451 case LL_PERROR:
452 case LL_ERROR:
453 color = 31;
454 break;
455 case LL_WARNING:
456 color = 33;
457 break;
458 case LL_MESSAGE:
459 default:
460 break;
461 }
462 }
463
464 if (color > 0)
465 rc += fprintf(fp, "\033[1;%dm", color); // Escape
466
467 fputs(buf, fp);
468
469 if (level == LL_PERROR)
470 rc += fprintf(fp, ": %s", strerror(err));
471
472 if (color > 0)
473 rc += fprintf(fp, "\033[0m"); // Escape
474
475 rc += fprintf(fp, "\n");
476
477 return rc;
478}
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:252
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_disp_null()

int log_disp_null ( time_t stamp,
const char * file,
int line,
const char * function,
enum LogLevel level,
const char * format,
... )

Discard log lines - Implements log_dispatcher_t -.

This log dispatcher simply discards all log messages. It can be used to disable logging when it isn't needed, such as in tests and fuzzers.

Definition at line 515 of file logging.c.

517{
518 return 0;
519}
+ Here is the caller graph for this function:

◆ log_disp_curses()

int log_disp_curses ( time_t stamp,
const char * file,
int line,
const char * function,
enum LogLevel level,
const char * format,
... )

Display a log line in the message line - Implements log_dispatcher_t -.

Definition at line 88 of file mutt_logging.c.

90{
91 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
92 if (level > c_debug_level)
93 return 0;
94
95 char buf[LOG_LINE_MAX_LEN] = { 0 };
96
97 va_list ap;
98 va_start(ap, format);
99 int rc = vsnprintf(buf, sizeof(buf), format, ap);
100 va_end(ap);
101
102 if ((level == LL_PERROR) && (rc >= 0) && (rc < sizeof(buf)))
103 {
104 char *buf2 = buf + rc;
105 int len = sizeof(buf) - rc;
106 const char *p = strerror(errno);
107 if (!p)
108 p = _("unknown error");
109
110 rc += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
111 }
112
113 const bool dupe = (mutt_str_equal(buf, ErrorBuf));
114 if (!dupe)
115 {
116 /* Only log unique messages */
117 log_disp_file(stamp, file, line, function, level, "%s", buf);
118 if (stamp == 0)
119 log_disp_queue(stamp, file, line, function, level, "%s", buf);
120 }
121
122 /* Don't display debugging message on screen */
123 if (level > LL_MESSAGE)
124 return 0;
125
126 /* Only pause if this is a message following an error */
127 if ((level > LL_ERROR) && OptMsgErr && !dupe)
128 error_pause();
129
130 mutt_str_copy(ErrorBuf, buf, sizeof(ErrorBuf));
131 ErrorBufMessage = true;
132
133 if (!OptKeepQuiet)
134 {
135 enum ColorId cid = MT_COLOR_NORMAL;
136 switch (level)
137 {
138 case LL_ERROR:
139 mutt_beep(false);
140 cid = MT_COLOR_ERROR;
141 break;
142 case LL_WARNING:
143 cid = MT_COLOR_WARNING;
144 break;
145 default:
146 cid = MT_COLOR_MESSAGE;
147 break;
148 }
149
150 msgwin_set_text(NULL, ErrorBuf, cid);
151 }
152
153 if ((level <= LL_ERROR) && !dupe)
154 {
155 OptMsgErr = true;
157 }
158 else
159 {
160 OptMsgErr = false;
161 LastError = 0;
162 }
163
165 return rc;
166}
ColorId
List of all coloured objects.
Definition color.h:35
@ MT_COLOR_MESSAGE
Informational message.
Definition color.h:52
@ MT_COLOR_ERROR
Error message.
Definition color.h:46
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:53
@ MT_COLOR_WARNING
Warning messages.
Definition color.h:84
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
void mutt_beep(bool force)
Irritate the user.
Definition curs_lib.c:68
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition globals.c:49
char ErrorBuf[1024]
Copy of the last error message.
Definition globals.c:34
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition globals.c:50
bool ErrorBufMessage
true if the last message was an error
Definition globals.c:33
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:384
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition msgwin.c:529
void msgwin_set_text(struct MuttWindow *win, const char *text, enum ColorId color)
Set the text for the Message Window.
Definition msgwin.c:483
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition date.c:466
#define _(a)
Definition message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition string.c:583
static uint64_t LastError
Time of the last error message (in milliseconds since the Unix epoch)
static void error_pause(void)
Wait for an error message to be read.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ MuttLogger

The log dispatcher -.

This function pointer controls where log messages are redirected.

Definition at line 54 of file logging.c.