NeoMutt  2021-10-29-33-g41675a
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 246 of file logging.c.

248 {
249  if (!LogFileFP || (level < LL_PERROR) || (level > LogFileLevel))
250  return 0;
251 
252  int ret = 0;
253  int err = errno;
254 
255  if (!function)
256  function = "UNKNOWN";
257 
258  ret += fprintf(LogFileFP, "[%s]<%c> %s() ", timestamp(stamp),
259  LevelAbbr[level + 3], function);
260 
261  va_list ap;
262  va_start(ap, level);
263  const char *fmt = va_arg(ap, const char *);
264  ret += vfprintf(LogFileFP, fmt, 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  ret++;
275  }
276 
277  return ret;
278 }
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 400 of file logging.c.

402 {
403  char buf[1024] = { 0 };
404  int err = errno;
405 
406  va_list ap;
407  va_start(ap, level);
408  const char *fmt = va_arg(ap, const char *);
409  int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
410  va_end(ap);
411 
412  if (level == LL_PERROR)
413  {
414  ret += snprintf(buf + ret, sizeof(buf) - ret, ": %s", strerror(err));
415  level = LL_ERROR;
416  }
417 
418  struct LogLine *ll = mutt_mem_calloc(1, sizeof(*ll));
419  ll->time = (stamp != 0) ? stamp : mutt_date_epoch();
420  ll->file = file;
421  ll->line = line;
422  ll->function = function;
423  ll->level = level;
424  ll->message = mutt_str_dup(buf);
425 
426  log_queue_add(ll);
427 
428  return ret;
429 }
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:287
@ 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:181
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 441 of file logging.c.

443 {
444  char buf[1024];
445 
446  va_list ap;
447  va_start(ap, level);
448  const char *fmt = va_arg(ap, const char *);
449  int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
450  va_end(ap);
451 
452  log_disp_file(stamp, file, line, function, level, "%s", buf);
453 
454  if ((level < LL_PERROR) || (level > LL_MESSAGE))
455  return 0;
456 
457  FILE *fp = (level < LL_MESSAGE) ? stderr : stdout;
458  int err = errno;
459  int colour = 0;
460  bool tty = (isatty(fileno(fp)) == 1);
461 
462  if (tty)
463  {
464  switch (level)
465  {
466  case LL_PERROR:
467  case LL_ERROR:
468  colour = 31;
469  break;
470  case LL_WARNING:
471  colour = 33;
472  break;
473  case LL_MESSAGE:
474  // colour = 36;
475  break;
476  case LL_DEBUG1:
477  case LL_DEBUG2:
478  case LL_DEBUG3:
479  case LL_DEBUG4:
480  case LL_DEBUG5:
481  case LL_NOTIFY:
482  default:
483  break;
484  }
485  }
486 
487  if (colour > 0)
488  ret += fprintf(fp, "\033[1;%dm", colour); // Escape
489 
490  fputs(buf, fp);
491 
492  if (level == LL_PERROR)
493  ret += fprintf(fp, ": %s", strerror(err));
494 
495  if (colour > 0)
496  ret += fprintf(fp, "\033[0m"); // Escape
497 
498  ret += fprintf(fp, "\n");
499 
500  return ret;
501 }
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:246
@ LL_DEBUG4
Log at debug level 4.
Definition: logging.h:43
@ LL_DEBUG3
Log at debug level 3.
Definition: logging.h:42
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_WARNING
Log warning.
Definition: logging.h:38
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
+ 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 506 of file logging.c.

508 {
509  return 0;
510 }

◆ 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 127 of file mutt_logging.c.

129 {
130  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
131  if (level > c_debug_level)
132  return 0;
133 
134  char buf[1024];
135 
136  va_list ap;
137  va_start(ap, level);
138  const char *fmt = va_arg(ap, const char *);
139  int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
140  va_end(ap);
141 
142  if (level == LL_PERROR)
143  {
144  char *buf2 = buf + ret;
145  int len = sizeof(buf) - ret;
146  const char *p = strerror(errno);
147  if (!p)
148  p = _("unknown error");
149 
150  ret += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
151  }
152 
153  const bool dupe = (strcmp(buf, ErrorBuf) == 0);
154  if (!dupe)
155  {
156  /* Only log unique messages */
157  log_disp_file(stamp, file, line, function, level, "%s", buf);
158  if (stamp == 0)
159  log_disp_queue(stamp, file, line, function, level, "%s", buf);
160  }
161 
162  /* Don't display debugging message on screen */
163  if (level > LL_MESSAGE)
164  return 0;
165 
166  /* Only pause if this is a message following an error */
167  if ((level > LL_ERROR) && OptMsgErr && !dupe)
168  error_pause();
169 
170  size_t width = msgwin_get_width();
171  mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, width ? width : sizeof(ErrorBuf),
172  JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
173  ErrorBufMessage = true;
174 
175  if (!OptKeepQuiet)
176  {
177  enum ColorId color = MT_COLOR_NORMAL;
178  switch (level)
179  {
180  case LL_ERROR:
181  mutt_beep(false);
182  color = MT_COLOR_ERROR;
183  break;
184  case LL_WARNING:
185  color = MT_COLOR_WARNING;
186  break;
187  default:
188  color = MT_COLOR_MESSAGE;
189  break;
190  }
191 
192  msgwin_set_text(color, ErrorBuf);
193  }
194 
195  if ((level <= LL_ERROR) && !dupe)
196  {
197  OptMsgErr = true;
199  }
200  else
201  {
202  OptMsgErr = false;
203  LastError = 0;
204  }
205 
206  return ret;
207 }
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:721
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:104
@ 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:400
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 color, const char *text)
Set the text for the Message Window.
Definition: msgwin.c:221
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:260
#define _(a)
Definition: message.h:28
bool ErrorBufMessage
true if the last message was an error
Definition: mutt_globals.h:48
char ErrorBuf[256]
Copy of the last error message.
Definition: mutt_globals.h:49
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
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.