NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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_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
...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 89 of file logging2.h.

◆ mutt_warning

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

Definition at line 90 of file logging2.h.

◆ mutt_message

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

Definition at line 91 of file logging2.h.

◆ mutt_error

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

Definition at line 92 of file logging2.h.

◆ mutt_perror

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

Definition at line 93 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 bytes += snprintf(buf + bytes, buflen - bytes, "\n");
129
130 fputs(buf, stdout);
131 return bytes;
132}
bool DebugLogLevel
Prefix log level, e.g. [E].
Definition: logging.c:40
static int log_level(char *buf, size_t buflen, enum LogLevel level)
Write a log level to a buffer.
Definition: logging.c:66
bool DebugLogColor
Output ANSI colours.
Definition: logging.c:39
static int log_timestamp(char *buf, size_t buflen, time_t time)
Write a timestamp to a buffer.
Definition: logging.c:52
bool DebugLogTimestamp
Show the timestamp.
Definition: logging.c:41
@ LL_ERROR
Log error.
Definition: logging2.h:40
@ LL_PERROR
Log perror (using errno)
Definition: logging2.h:39
@ LL_WARNING
Log warning.
Definition: logging2.h:41
@ LL_MESSAGE
Log informational message.
Definition: logging2.h:42
#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 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, format);
261 rc += vfprintf(LogFileFP, format, ap);
262 va_end(ap);
263
264 if (level == LL_PERROR)
265 {
266 fprintf(LogFileFP, ": %s\n", strerror(err));
267 }
268 else if (level <= LL_MESSAGE)
269 {
270 fputs("\n", LogFileFP);
271 rc++;
272 }
273
274 return rc;
275}
const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:45
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 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,
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 397 of file logging.c.

399{
400 char buf[LOG_LINE_MAX_LEN] = { 0 };
401 int err = errno;
402
403 va_list ap;
404 va_start(ap, format);
405 int rc = vsnprintf(buf, sizeof(buf), format, ap);
406 va_end(ap);
407
408 if (level == LL_PERROR)
409 {
410 if ((rc >= 0) && (rc < sizeof(buf)))
411 rc += snprintf(buf + rc, sizeof(buf) - rc, ": %s", strerror(err));
412 level = LL_ERROR;
413 }
414
415 struct LogLine *ll = mutt_mem_calloc(1, sizeof(*ll));
416 ll->time = (stamp != 0) ? stamp : mutt_date_now();
417 ll->file = file;
418 ll->line = line;
419 ll->function = function;
420 ll->level = level;
421 ll->message = mutt_str_dup(buf);
422
423 log_queue_add(ll);
424
425 return rc;
426}
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
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
Definition: logging.c:284
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
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
+ 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 440 of file logging.c.

442{
443 char buf[LOG_LINE_MAX_LEN] = { 0 };
444
445 va_list ap;
446 va_start(ap, format);
447 int rc = vsnprintf(buf, sizeof(buf), format, ap);
448 va_end(ap);
449
450 log_disp_file(stamp, file, line, function, level, "%s", buf);
451
452 if ((level < LL_PERROR) || (level > LL_MESSAGE))
453 return 0;
454
455 FILE *fp = (level < LL_MESSAGE) ? stderr : stdout;
456 int err = errno;
457 int color = 0;
458 bool tty = (isatty(fileno(fp)) == 1);
459
460 if (tty)
461 {
462 switch (level)
463 {
464 case LL_PERROR:
465 case LL_ERROR:
466 color = 31;
467 break;
468 case LL_WARNING:
469 color = 33;
470 break;
471 case LL_MESSAGE:
472 default:
473 break;
474 }
475 }
476
477 if (color > 0)
478 rc += fprintf(fp, "\033[1;%dm", color); // Escape
479
480 fputs(buf, fp);
481
482 if (level == LL_PERROR)
483 rc += fprintf(fp, ": %s", strerror(err));
484
485 if (color > 0)
486 rc += fprintf(fp, "\033[0m"); // Escape
487
488 rc += fprintf(fp, "\n");
489
490 return rc;
491}
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:245
+ 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 -.

Definition at line 496 of file logging.c.

498{
499 return 0;
500}
+ 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 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, format);
98 int rc = vsnprintf(buf, sizeof(buf), format, ap);
99 va_end(ap);
100
101 if ((level == LL_PERROR) && (rc >= 0) && (rc < sizeof(buf)))
102 {
103 char *buf2 = buf + rc;
104 int len = sizeof(buf) - rc;
105 const char *p = strerror(errno);
106 if (!p)
107 p = _("unknown error");
108
109 rc += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
110 }
111
112 const bool dupe = (mutt_str_equal(buf, ErrorBuf));
113 if (!dupe)
114 {
115 /* Only log unique messages */
116 log_disp_file(stamp, file, line, function, level, "%s", buf);
117 if (stamp == 0)
118 log_disp_queue(stamp, file, line, function, level, "%s", buf);
119 }
120
121 /* Don't display debugging message on screen */
122 if (level > LL_MESSAGE)
123 return 0;
124
125 /* Only pause if this is a message following an error */
126 if ((level > LL_ERROR) && OptMsgErr && !dupe)
127 error_pause();
128
129 mutt_str_copy(ErrorBuf, buf, sizeof(ErrorBuf));
130 ErrorBufMessage = true;
131
132 if (!OptKeepQuiet)
133 {
134 enum ColorId cid = MT_COLOR_NORMAL;
135 switch (level)
136 {
137 case LL_ERROR:
138 mutt_beep(false);
139 cid = MT_COLOR_ERROR;
140 break;
141 case LL_WARNING:
142 cid = MT_COLOR_WARNING;
143 break;
144 default:
145 cid = MT_COLOR_MESSAGE;
146 break;
147 }
148
149 msgwin_set_text(NULL, ErrorBuf, cid);
150 }
151
152 if ((level <= LL_ERROR) && !dupe)
153 {
154 OptMsgErr = true;
156 }
157 else
158 {
159 OptMsgErr = false;
160 LastError = 0;
161 }
162
164 return rc;
165}
ColorId
List of all colored objects.
Definition: color.h:39
@ MT_COLOR_MESSAGE
Informational message.
Definition: color.h:56
@ MT_COLOR_ERROR
Error message.
Definition: color.h:50
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:58
@ MT_COLOR_WARNING
Warning messages.
Definition: color.h:80
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:144
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:65
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: globals.c:71
char ErrorBuf[1024]
Copy of the last error message.
Definition: globals.c:37
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: globals.c:72
bool ErrorBufMessage
true if the last message was an error
Definition: globals.c:36
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:397
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition: msgwin.c:527
void msgwin_set_text(struct MuttWindow *win, const char *text, enum ColorId color)
Set the text for the Message Window.
Definition: msgwin.c:484
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
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:653
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:634
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ 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.