NeoMutt  2022-04-29-70-g0c028c
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 84 of file logging.h.

◆ mutt_warning

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

Definition at line 85 of file logging.h.

◆ mutt_message

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

Definition at line 86 of file logging.h.

◆ mutt_error

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

Definition at line 87 of file logging.h.

◆ mutt_perror

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

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

Definition at line 398 of file logging.c.

400 {
401  char buf[1024] = { 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_epoch();
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 }
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
Definition: logging.c:285
@ LL_ERROR
Log error.
Definition: logging.h:37
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
A Log line.
Definition: logging.h:73
const char * file
Source file.
Definition: logging.h:75
char * message
Message to be logged.
Definition: logging.h:79
const char * function
C function.
Definition: logging.h:77
int line
Line number in source file.
Definition: logging.h:76
enum LogLevel level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:78
time_t time
Timestamp of the message.
Definition: logging.h:74
+ 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
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[1024];
444 
445  va_list ap;
446  va_start(ap, level);
447  const char *fmt = va_arg(ap, const char *);
448  int rc = vsnprintf(buf, sizeof(buf), fmt, 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 colour = 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  colour = 31;
468  break;
469  case LL_WARNING:
470  colour = 33;
471  break;
472  case LL_MESSAGE:
473  // colour = 36;
474  break;
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: logging.h:38
+ 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 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[1024];
96 
97  va_list ap;
98  va_start(ap, level);
99  const char *fmt = va_arg(ap, const char *);
100  int rc = vsnprintf(buf, sizeof(buf), fmt, ap);
101  va_end(ap);
102 
103  if ((level == LL_PERROR) && (rc >= 0) && (rc < sizeof(buf)))
104  {
105  char *buf2 = buf + rc;
106  int len = sizeof(buf) - rc;
107  const char *p = strerror(errno);
108  if (!p)
109  p = _("unknown error");
110 
111  rc += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
112  }
113 
114  const bool dupe = (strcmp(buf, ErrorBuf) == 0);
115  if (!dupe)
116  {
117  /* Only log unique messages */
118  log_disp_file(stamp, file, line, function, level, "%s", buf);
119  if (stamp == 0)
120  log_disp_queue(stamp, file, line, function, level, "%s", buf);
121  }
122 
123  /* Don't display debugging message on screen */
124  if (level > LL_MESSAGE)
125  return 0;
126 
127  /* Only pause if this is a message following an error */
128  if ((level > LL_ERROR) && OptMsgErr && !dupe)
129  error_pause();
130 
131  size_t width = msgwin_get_width();
132  mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, width ? width : sizeof(ErrorBuf),
133  JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
134  ErrorBufMessage = true;
135 
136  if (!OptKeepQuiet)
137  {
138  enum ColorId cid = MT_COLOR_NORMAL;
139  switch (level)
140  {
141  case LL_ERROR:
142  mutt_beep(false);
143  cid = MT_COLOR_ERROR;
144  break;
145  case LL_WARNING:
146  cid = MT_COLOR_WARNING;
147  break;
148  default:
149  cid = MT_COLOR_MESSAGE;
150  break;
151  }
152 
154  }
155 
156  if ((level <= LL_ERROR) && !dupe)
157  {
158  OptMsgErr = true;
160  }
161  else
162  {
163  OptMsgErr = false;
164  LastError = 0;
165  }
166 
168  return rc;
169 }
ColorId
List of all colored objects.
Definition: color.h:35
@ MT_COLOR_MESSAGE
Informational message.
Definition: color.h:51
@ 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:74
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:750
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:146
@ JUSTIFY_LEFT
Left justify the text.
Definition: curs_lib.h:43
uint64_t mutt_date_epoch_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:436
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
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
void msgwin_set_text(enum ColorId cid, const char *text)
Set the text for the Message Window.
Definition: msgwin.c:232
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:268
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition: msgwin.c:259
#define _(a)
Definition: message.h:28
bool ErrorBufMessage
true if the last message was an error
Definition: mutt_globals.h:46
char ErrorBuf[256]
Copy of the last error message.
Definition: mutt_globals.h:47
uint64_t LastError
Time of the last error message (in milliseconds since the Unix epoch)
Definition: mutt_logging.c:47
static void error_pause(void)
Wait for an error message to be read.
Definition: mutt_logging.c:59
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:604
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: options.h:44
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: options.h:46
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.