NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
Logging API

Prototype for a Logging Function. More...

Macros

#define mutt_debug(LEVEL, ...)   MuttLogger(0, __FILE__, __LINE__, __func__, LEVEL, __VA_ARGS__)
 
#define mutt_warning(...)   MuttLogger(0, __FILE__, __LINE__, __func__, LL_WARNING, __VA_ARGS__)
 
#define mutt_message(...)   MuttLogger(0, __FILE__, __LINE__, __func__, LL_MESSAGE, __VA_ARGS__)
 
#define mutt_error(...)   MuttLogger(0, __FILE__, __LINE__, __func__, LL_ERROR, __VA_ARGS__)
 
#define mutt_perror(...)   MuttLogger(0, __FILE__, __LINE__, __func__, LL_PERROR, __VA_ARGS__)
 

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

Variables

log_dispatcher_t MuttLogger = log_disp_terminal
 The log dispatcher -. More...
 

Detailed Description

Prototype for a Logging Function.

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

Macro Definition Documentation

◆ mutt_debug

#define mutt_debug (   LEVEL,
  ... 
)    MuttLogger(0, __FILE__, __LINE__, __func__, LEVEL, __VA_ARGS__)

Definition at line 87 of file logging2.h.

◆ mutt_warning

#define mutt_warning (   ...)    MuttLogger(0, __FILE__, __LINE__, __func__, LL_WARNING, __VA_ARGS__)

Definition at line 88 of file logging2.h.

◆ mutt_message

#define mutt_message (   ...)    MuttLogger(0, __FILE__, __LINE__, __func__, LL_MESSAGE, __VA_ARGS__)

Definition at line 89 of file logging2.h.

◆ mutt_error

#define mutt_error (   ...)    MuttLogger(0, __FILE__, __LINE__, __func__, LL_ERROR, __VA_ARGS__)

Definition at line 90 of file logging2.h.

◆ mutt_perror

#define mutt_perror (   ...)    MuttLogger(0, __FILE__, __LINE__, __func__, LL_PERROR, __VA_ARGS__)

Definition at line 91 of file logging2.h.

Function Documentation

◆ log_disp_file()

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

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 245 of file logging.c.

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}
@ LL_PERROR
Log perror (using errno)
Definition: logging2.h:39
@ LL_MESSAGE
Log informational message.
Definition: logging2.h:42
static FILE * LogFileFP
Log file handle.
Definition: logging.c:54
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:77
static const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:45
static int LogFileLevel
Log file level.
Definition: logging.c:56
+ 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,
  ... 
)

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 398 of file logging.c.

400{
401 char buf[LOG_LINE_MAX_LEN] = { 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}
@ LL_ERROR
Log error.
Definition: logging2.h:40
#define LOG_LINE_MAX_LEN
Log lines longer than this will be truncated.
Definition: logging2.h:32
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
Definition: logging.c:285
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:446
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
A Log line.
Definition: logging2.h:76
const char * file
Source file.
Definition: logging2.h:78
char * message
Message to be logged.
Definition: logging2.h:82
const char * function
C function.
Definition: logging2.h:80
int line
Line number in source file.
Definition: logging2.h:79
enum LogLevel level
Log level, e.g. LL_DEBUG1.
Definition: logging2.h:81
time_t time
Timestamp of the message.
Definition: logging2.h:77
+ 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,
  ... 
)

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 442 of file logging.c.

444{
445 char buf[LOG_LINE_MAX_LEN] = { 0 };
446
447 va_list ap;
448 va_start(ap, level);
449 const char *fmt = va_arg(ap, const char *);
450 int rc = vsnprintf(buf, sizeof(buf), fmt, ap);
451 va_end(ap);
452
453 log_disp_file(stamp, file, line, function, level, "%s", buf);
454
455 if ((level < LL_PERROR) || (level > LL_MESSAGE))
456 return 0;
457
458 FILE *fp = (level < LL_MESSAGE) ? stderr : stdout;
459 int err = errno;
460 int colour = 0;
461 bool tty = (isatty(fileno(fp)) == 1);
462
463 if (tty)
464 {
465 switch (level)
466 {
467 case LL_PERROR:
468 case LL_ERROR:
469 colour = 31;
470 break;
471 case LL_WARNING:
472 colour = 33;
473 break;
474 case LL_MESSAGE:
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}
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
@ LL_WARNING
Log warning.
Definition: logging2.h:41
+ 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,
  ... 
)

Discard log lines - Implements log_dispatcher_t -.

Definition at line 499 of file logging.c.

501{
502 return 0;
503}
+ 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,
  ... 
)

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

Definition at line 87 of file mutt_logging.c.

89{
90 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
91 if (level > c_debug_level)
92 return 0;
93
94 char buf[LOG_LINE_MAX_LEN] = { 0 };
95
96 va_list ap;
97 va_start(ap, level);
98 const char *fmt = va_arg(ap, const char *);
99 int rc = vsnprintf(buf, sizeof(buf), fmt, 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 size_t width = msgwin_get_width();
131 mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, width ? width : sizeof(ErrorBuf),
132 JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
133 ErrorBufMessage = true;
134
135 if (!OptKeepQuiet)
136 {
137 enum ColorId cid = MT_COLOR_NORMAL;
138 switch (level)
139 {
140 case LL_ERROR:
141 mutt_beep(false);
142 cid = MT_COLOR_ERROR;
143 break;
144 case LL_WARNING:
145 cid = MT_COLOR_WARNING;
146 break;
147 default:
148 cid = MT_COLOR_MESSAGE;
149 break;
150 }
151
153 }
154
155 if ((level <= LL_ERROR) && !dupe)
156 {
157 OptMsgErr = true;
159 }
160 else
161 {
162 OptMsgErr = false;
163 LastError = 0;
164 }
165
167 return rc;
168}
ColorId
List of all colored objects.
Definition: color.h:38
@ MT_COLOR_MESSAGE
Informational message.
Definition: color.h:55
@ MT_COLOR_ERROR
Error message.
Definition: color.h:49
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:57
@ MT_COLOR_WARNING
Warning messages.
Definition: color.h:79
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
void mutt_simple_format(char *buf, size_t buflen, int min_width, int max_width, enum FormatJustify justify, char pad_char, const char *s, size_t n, bool arboreal)
Format a string, like snprintf()
Definition: curs_lib.c:640
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:131
@ JUSTIFY_LEFT
Left justify the text.
Definition: curs_lib.h:42
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: globals.c:73
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: globals.c:75
bool ErrorBufMessage
true if the last message was an error
Definition: globals.c:36
char ErrorBuf[256]
Copy of the last error message.
Definition: globals.c:37
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
void msgwin_set_text(enum ColorId cid, const char *text)
Set the text for the Message Window.
Definition: msgwin.c:234
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:270
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition: msgwin.c:261
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:455
#define _(a)
Definition: message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
static uint64_t LastError
Time of the last error message (in milliseconds since the Unix epoch)
Definition: mutt_logging.c:46
static void error_pause(void)
Wait for an error message to be read.
Definition: mutt_logging.c:58
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:605
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ 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 52 of file logging.c.