NeoMutt  2018-07-16 +2481-68dcde
Teaching an old dog new tricks
DOXYGEN
logging.h File Reference

Logging Dispatcher. More...

#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "queue.h"
+ Include dependency graph for logging.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LogLine
 A Log line. 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__)
 

Typedefs

typedef int(* log_dispatcher_t) (time_t stamp, const char *file, int line, const char *function, int level,...)
 typedef log_dispatcher_t - Prototype for a logging function More...
 

Enumerations

enum  LogLevel {
  LL_PERROR = -3, LL_ERROR = -2, LL_WARNING = -1, LL_MESSAGE = 0,
  LL_DEBUG1 = 1, LL_DEBUG2 = 2, LL_DEBUG3 = 3, LL_DEBUG4 = 4,
  LL_DEBUG5 = 5, LL_NOTIFY = 6, LL_MAX
}
 Names for the Logging Levels. More...
 

Functions

 STAILQ_HEAD (LogLineList, LogLine)
 
int log_disp_file (time_t stamp, const char *file, int line, const char *function, int level,...)
 Save a log line to a file. More...
 
int log_disp_null (time_t stamp, const char *file, int line, const char *function, int level,...)
 Discard log lines - Implements log_dispatcher_t. More...
 
int log_disp_queue (time_t stamp, const char *file, int line, const char *function, int 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, int level,...)
 Save a log line to the terminal - Implements log_dispatcher_t. More...
 
int log_queue_add (struct LogLine *ll)
 Add a LogLine to the queue. More...
 
void log_queue_empty (void)
 Free the contents of the queue. More...
 
void log_queue_flush (log_dispatcher_t disp)
 Replay the log queue. More...
 
int log_queue_save (FILE *fp)
 Save the contents of the queue to a temporary file. More...
 
void log_queue_set_max_size (int size)
 Set a upper limit for the queue length. More...
 
void log_file_close (bool verbose)
 Close the log file. More...
 
int log_file_open (bool verbose)
 Start logging to a file. More...
 
bool log_file_running (void)
 Is the log file running? More...
 
int log_file_set_filename (const char *file, bool verbose)
 Set the filename for the log. More...
 
int log_file_set_level (int level, bool verbose)
 Set the logging level. More...
 
void log_file_set_version (const char *version)
 Set the program's version number. More...
 

Variables

log_dispatcher_t MuttLogger
 The log dispatcher. More...
 

Detailed Description

Logging Dispatcher.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file logging.h.

Macro Definition Documentation

◆ mutt_debug

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

Definition at line 81 of file logging.h.

◆ mutt_warning

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

Definition at line 82 of file logging.h.

◆ mutt_message

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

Definition at line 83 of file logging.h.

◆ mutt_error

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

Definition at line 84 of file logging.h.

◆ mutt_perror

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

Definition at line 85 of file logging.h.

Typedef Documentation

◆ log_dispatcher_t

typedef int(* log_dispatcher_t) (time_t stamp, const char *file, int line, const char *function, int level,...)

typedef log_dispatcher_t - 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

Definition at line 43 of file logging.h.

Enumeration Type Documentation

◆ LogLevel

enum LogLevel

Names for the Logging Levels.

Enumerator
LL_PERROR 

Log perror (using errno)

LL_ERROR 

Log error.

LL_WARNING 

Log warning.

LL_MESSAGE 

Log informational message.

LL_DEBUG1 

Log at debug level 1.

LL_DEBUG2 

Log at debug level 2.

LL_DEBUG3 

Log at debug level 3.

LL_DEBUG4 

Log at debug level 4.

LL_DEBUG5 

Log at debug level 5.

LL_NOTIFY 

Log of notifications.

LL_MAX 

Definition at line 50 of file logging.h.

51 {
52  LL_PERROR = -3,
53  LL_ERROR = -2,
54  LL_WARNING = -1,
55  LL_MESSAGE = 0,
56  LL_DEBUG1 = 1,
57  LL_DEBUG2 = 2,
58  LL_DEBUG3 = 3,
59  LL_DEBUG4 = 4,
60  LL_DEBUG5 = 5,
61  LL_NOTIFY = 6,
62 
63  LL_MAX,
64 };
Log at debug level 4.
Definition: logging.h:59
Log error.
Definition: logging.h:53
Log of notifications.
Definition: logging.h:61
Log perror (using errno)
Definition: logging.h:52
Definition: logging.h:63
Log informational message.
Definition: logging.h:55
Log at debug level 2.
Definition: logging.h:57
Log at debug level 1.
Definition: logging.h:56
Log at debug level 5.
Definition: logging.h:60
Log at debug level 3.
Definition: logging.h:58
Log warning.
Definition: logging.h:54

Function Documentation

◆ STAILQ_HEAD()

STAILQ_HEAD ( LogLineList  ,
LogLine   
)

◆ log_disp_file()

int log_disp_file ( time_t  stamp,
const char *  file,
int  line,
const char *  function,
int  level,
  ... 
)

Save a log line to a file.

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

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

256 {
257  if (!LogFileFP || (level < LL_PERROR) || (level > LogFileLevel))
258  return 0;
259 
260  int ret = 0;
261  int err = errno;
262 
263  if (!function)
264  function = "UNKNOWN";
265 
266  ret += fprintf(LogFileFP, "[%s]<%c> %s() ", timestamp(stamp),
267  LevelAbbr[level + 3], function);
268 
269  va_list ap;
270  va_start(ap, level);
271  const char *fmt = va_arg(ap, const char *);
272  ret += vfprintf(LogFileFP, fmt, ap);
273  va_end(ap);
274 
275  if (level == LL_PERROR)
276  {
277  fprintf(LogFileFP, ": %s\n", strerror(err));
278  }
279  else if (level <= 0)
280  {
281  fputs("\n", LogFileFP);
282  ret++;
283  }
284 
285  return ret;
286 }
int level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:75
const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:45
FILE * LogFileFP
Log file handle.
Definition: logging.c:54
Log perror (using errno)
Definition: logging.h:52
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:77
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_null()

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

Discard log lines - Implements log_dispatcher_t.

Definition at line 513 of file logging.c.

515 {
516  return 0;
517 }
+ 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,
int  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 408 of file logging.c.

410 {
411  char buf[1024] = { 0 };
412  int err = errno;
413 
414  va_list ap;
415  va_start(ap, level);
416  const char *fmt = va_arg(ap, const char *);
417  int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
418  va_end(ap);
419 
420  if (level == LL_PERROR)
421  {
422  ret += snprintf(buf + ret, sizeof(buf) - ret, ": %s", strerror(err));
423  level = LL_ERROR;
424  }
425 
426  struct LogLine *ll = mutt_mem_calloc(1, sizeof(*ll));
427  ll->time = (stamp != 0) ? stamp : mutt_date_epoch();
428  ll->file = file;
429  ll->line = line;
430  ll->function = function;
431  ll->level = level;
432  ll->message = mutt_str_strdup(buf);
433 
434  log_queue_add(ll);
435 
436  return ret;
437 }
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:410
Log error.
Definition: logging.h:53
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
int level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:75
char * message
Message to be logged.
Definition: logging.h:76
Log perror (using errno)
Definition: logging.h:52
int line
Line number in source file.
Definition: logging.h:73
A Log line.
Definition: logging.h:69
const char * line
Definition: common.c:36
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
time_t time
Timestamp of the message.
Definition: logging.h:71
const char * file
Source file.
Definition: logging.h:72
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
Definition: logging.c:295
const char * function
C function.
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,
int  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 449 of file logging.c.

451 {
452  if ((level < LL_PERROR) || (level > LL_MESSAGE))
453  return 0;
454 
455  char buf[1024];
456 
457  va_list ap;
458  va_start(ap, level);
459  const char *fmt = va_arg(ap, const char *);
460  int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
461  va_end(ap);
462 
463  log_disp_file(stamp, file, line, function, level, "%s", buf);
464 
465  FILE *fp = (level < LL_MESSAGE) ? stderr : stdout;
466  int err = errno;
467  int colour = 0;
468  bool tty = (isatty(fileno(fp)) == 1);
469 
470  if (tty)
471  {
472  switch (level)
473  {
474  case LL_PERROR:
475  case LL_ERROR:
476  colour = 31;
477  break;
478  case LL_WARNING:
479  colour = 33;
480  break;
481  case LL_MESSAGE:
482  // colour = 36;
483  break;
484  case LL_DEBUG1:
485  case LL_DEBUG2:
486  case LL_DEBUG3:
487  case LL_DEBUG4:
488  case LL_DEBUG5:
489  case LL_NOTIFY:
490  break;
491  }
492  }
493 
494  if (colour > 0)
495  ret += fprintf(fp, "\033[1;%dm", colour); // Escape
496 
497  fputs(buf, fp);
498 
499  if (level == LL_PERROR)
500  ret += fprintf(fp, ": %s", strerror(err));
501 
502  if (colour > 0)
503  ret += fprintf(fp, "\033[0m"); // Escape
504 
505  ret += fprintf(fp, "\n");
506 
507  return ret;
508 }
Log at debug level 4.
Definition: logging.h:59
Log error.
Definition: logging.h:53
int level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:75
Log of notifications.
Definition: logging.h:61
Log perror (using errno)
Definition: logging.h:52
Log informational message.
Definition: logging.h:55
Log at debug level 2.
Definition: logging.h:57
const char * line
Definition: common.c:36
Log at debug level 1.
Definition: logging.h:56
int log_disp_file(time_t stamp, const char *file, int line, const char *function, int level,...)
Save a log line to a file.
Definition: logging.c:254
Log at debug level 5.
Definition: logging.h:60
const char * file
Source file.
Definition: logging.h:72
Log at debug level 3.
Definition: logging.h:58
Log warning.
Definition: logging.h:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_queue_add()

int log_queue_add ( struct LogLine ll)

Add a LogLine to the queue.

Parameters
llLogLine to add
Return values
numEntries in the queue

If LogQueueMax is non-zero, the queue will be limited to this many items.

Definition at line 295 of file logging.c.

296 {
297  if (!ll)
298  return -1;
299 
300  STAILQ_INSERT_TAIL(&LogQueue, ll, entries);
301 
302  if ((LogQueueMax > 0) && (LogQueueCount >= LogQueueMax))
303  {
304  ll = STAILQ_FIRST(&LogQueue);
305  STAILQ_REMOVE_HEAD(&LogQueue, entries);
306  FREE(&ll->message);
307  FREE(&ll);
308  }
309  else
310  {
311  LogQueueCount++;
312  }
313  return LogQueueCount;
314 }
char * message
Message to be logged.
Definition: logging.h:76
static struct LogLineList LogQueue
In-memory list of log lines.
Definition: logging.c:62
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:387
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:420
int LogQueueMax
Maximum number of entries in the log queue.
Definition: logging.c:65
#define FREE(x)
Definition: memory.h:40
int LogQueueCount
Number of entries currently in the log queue.
Definition: logging.c:64
#define STAILQ_FIRST(head)
Definition: queue.h:348
+ Here is the caller graph for this function:

◆ log_queue_empty()

void log_queue_empty ( void  )

Free the contents of the queue.

Free any log lines in the queue.

Definition at line 334 of file logging.c.

335 {
336  struct LogLine *ll = NULL;
337  struct LogLine *tmp = NULL;
338 
339  STAILQ_FOREACH_SAFE(ll, &LogQueue, entries, tmp)
340  {
341  STAILQ_REMOVE(&LogQueue, ll, LogLine, entries);
342  FREE(&ll->message);
343  FREE(&ll);
344  }
345 
346  LogQueueCount = 0;
347 }
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:400
char * message
Message to be logged.
Definition: logging.h:76
static struct LogLineList LogQueue
In-memory list of log lines.
Definition: logging.c:62
A Log line.
Definition: logging.h:69
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:360
#define FREE(x)
Definition: memory.h:40
int LogQueueCount
Number of entries currently in the log queue.
Definition: logging.c:64
+ Here is the caller graph for this function:

◆ log_queue_flush()

void log_queue_flush ( log_dispatcher_t  disp)

Replay the log queue.

Parameters
dispLog dispatcher - Implements log_dispatcher_t

Pass all of the log entries in the queue to the log dispatcher provided. The queue will be emptied afterwards.

Definition at line 356 of file logging.c.

357 {
358  struct LogLine *ll = NULL;
359  STAILQ_FOREACH(ll, &LogQueue, entries)
360  {
361  disp(ll->time, ll->file, ll->line, ll->function, ll->level, "%s", ll->message);
362  }
363 
364  log_queue_empty();
365 }
int level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:75
char * message
Message to be logged.
Definition: logging.h:76
static struct LogLineList LogQueue
In-memory list of log lines.
Definition: logging.c:62
int line
Line number in source file.
Definition: logging.h:73
A Log line.
Definition: logging.h:69
void log_queue_empty(void)
Free the contents of the queue.
Definition: logging.c:334
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
time_t time
Timestamp of the message.
Definition: logging.h:71
const char * file
Source file.
Definition: logging.h:72
const char * function
C function.
Definition: logging.h:74
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_queue_save()

int log_queue_save ( FILE *  fp)

Save the contents of the queue to a temporary file.

Parameters
fpOpen file handle
Return values
numLines written to the file

The queue is written to a temporary file. The format is:

  • [HH:MM:SS]<LEVEL> FORMATTED-MESSAGE
Note
The caller should free the returned string and delete the file.

Definition at line 377 of file logging.c.

378 {
379  if (!fp)
380  return 0;
381 
382  char buf[32];
383  int count = 0;
384  struct LogLine *ll = NULL;
385  STAILQ_FOREACH(ll, &LogQueue, entries)
386  {
387  mutt_date_localtime_format(buf, sizeof(buf), "%H:%M:%S", ll->time);
388  fprintf(fp, "[%s]<%c> %s", buf, LevelAbbr[ll->level + 3], ll->message);
389  if (ll->level <= 0)
390  fputs("\n", fp);
391  count++;
392  }
393 
394  return count;
395 }
int level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:75
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:773
const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:45
char * message
Message to be logged.
Definition: logging.h:76
static struct LogLineList LogQueue
In-memory list of log lines.
Definition: logging.c:62
A Log line.
Definition: logging.h:69
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
time_t time
Timestamp of the message.
Definition: logging.h:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_queue_set_max_size()

void log_queue_set_max_size ( int  size)

Set a upper limit for the queue length.

Parameters
sizeNew maximum queue length
Note
size of 0 means unlimited

Definition at line 322 of file logging.c.

323 {
324  if (size < 0)
325  size = 0;
326  LogQueueMax = size;
327 }
int LogQueueMax
Maximum number of entries in the log queue.
Definition: logging.c:65
+ Here is the caller graph for this function:

◆ log_file_close()

void log_file_close ( bool  verbose)

Close the log file.

Parameters
verboseIf true, then log the event

Definition at line 98 of file logging.c.

99 {
100  if (!LogFileFP)
101  return;
102 
103  fprintf(LogFileFP, "[%s] Closing log.\n", timestamp(0));
105  if (verbose)
106  mutt_message(_("Closed log file: %s"), LogFileName);
107 }
#define mutt_message(...)
Definition: logging.h:83
FILE * LogFileFP
Log file handle.
Definition: logging.c:54
#define _(a)
Definition: message.h:28
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:77
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:150
char * LogFileName
Log file name.
Definition: logging.c:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_file_open()

int log_file_open ( bool  verbose)

Start logging to a file.

Parameters
verboseIf true, then log the event
Return values
0Success
-1Error, see errno

Before opening a log file, call log_file_set_version(), log_file_set_level() and log_file_set_filename().

Definition at line 118 of file logging.c.

119 {
120  if (!LogFileName)
121  return -1;
122 
123  if (LogFileFP)
124  log_file_close(false);
125 
126  if (LogFileLevel < LL_DEBUG1)
127  return -1;
128 
130  if (!LogFileFP)
131  return -1;
132  setvbuf(LogFileFP, NULL, _IOLBF, 0);
133 
134  fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
136  if (verbose)
137  mutt_message(_("Debugging at level %d to file '%s'"), LogFileLevel, LogFileName);
138  return 0;
139 }
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
#define NONULL(x)
Definition: string2.h:37
#define mutt_message(...)
Definition: logging.h:83
FILE * LogFileFP
Log file handle.
Definition: logging.c:54
#define _(a)
Definition: message.h:28
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:77
Log at debug level 1.
Definition: logging.h:56
int LogFileLevel
Log file level.
Definition: logging.c:56
char * LogFileVersion
Program version.
Definition: logging.c:57
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:583
char * LogFileName
Log file name.
Definition: logging.c:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_file_running()

bool log_file_running ( void  )

Is the log file running?

Return values
trueIf the log file is running

Definition at line 229 of file logging.c.

230 {
231  return LogFileFP;
232 }
FILE * LogFileFP
Log file handle.
Definition: logging.c:54
+ Here is the caller graph for this function:

◆ log_file_set_filename()

int log_file_set_filename ( const char *  file,
bool  verbose 
)

Set the filename for the log.

Parameters
fileName to use
verboseIf true, then log the event
Return values
0Success, file opened
-1Error, see errno

Definition at line 148 of file logging.c.

149 {
150  if (!file)
151  return -1;
152 
153  /* also handles both being NULL */
154  if (mutt_str_strcmp(LogFileName, file) == 0)
155  return 0;
156 
158 
159  if (!LogFileName)
160  log_file_close(verbose);
161 
162  return log_file_open(verbose);
163 }
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
int log_file_open(bool verbose)
Start logging to a file.
Definition: logging.c:118
void mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:453
const char * file
Source file.
Definition: logging.h:72
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
char * LogFileName
Log file name.
Definition: logging.c:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_file_set_level()

int log_file_set_level ( int  level,
bool  verbose 
)

Set the logging level.

Parameters
levelLogging level
verboseIf true, then log the event
Return values
0Success
-1Error, level is out of range

The level should be: LL_MESSAGE <= level < LL_MAX.

Definition at line 174 of file logging.c.

175 {
176  if ((level < LL_MESSAGE) || (level >= LL_MAX))
177  return -1;
178 
179  if (level == LogFileLevel)
180  return 0;
181 
183 
184  if (level == LL_MESSAGE)
185  {
186  log_file_close(verbose);
187  }
188  else if (LogFileFP)
189  {
190  if (verbose)
191  mutt_message(_("Logging at level %d to file '%s'"), LogFileLevel, LogFileName);
192  fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
194  }
195  else
196  {
197  log_file_open(verbose);
198  }
199 
200  if (LogFileLevel >= LL_DEBUG5)
201  {
202  fprintf(LogFileFP,
203  "\n"
204  "WARNING:\n"
205  " Logging at this level can reveal personal information.\n"
206  " Review the log carefully before posting in bug reports.\n"
207  "\n");
208  }
209 
210  return 0;
211 }
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
#define NONULL(x)
Definition: string2.h:37
int level
Log level, e.g. LL_DEBUG1.
Definition: logging.h:75
#define mutt_message(...)
Definition: logging.h:83
FILE * LogFileFP
Log file handle.
Definition: logging.c:54
#define _(a)
Definition: message.h:28
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:77
Definition: logging.h:63
Log informational message.
Definition: logging.h:55
int log_file_open(bool verbose)
Start logging to a file.
Definition: logging.c:118
int LogFileLevel
Log file level.
Definition: logging.c:56
char * LogFileVersion
Program version.
Definition: logging.c:57
Log at debug level 5.
Definition: logging.h:60
char * LogFileName
Log file name.
Definition: logging.c:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_file_set_version()

void log_file_set_version ( const char *  version)

Set the program's version number.

Parameters
versionVersion number

The string will be appended directly to 'NeoMutt', so it should begin with a hyphen.

Definition at line 220 of file logging.c.

221 {
222  mutt_str_replace(&LogFileVersion, version);
223 }
void mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:453
char * LogFileVersion
Program version.
Definition: logging.c:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ MuttLogger

log_dispatcher_t MuttLogger

The log dispatcher.

This function pointer controls where log messages are redirected.

Definition at line 52 of file logging.c.