NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
logging2.h File Reference

Logging Dispatcher. More...

#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "queue.h"
+ Include dependency graph for logging2.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 LOG_LINE_MAX_LEN   10240
 Log lines longer than this will be truncated.
 
#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__)
 
#define log_multiline(LEVEL, STRING)   log_multiline_full(LEVEL, STRING, __FILE__, __LINE__, __func__)
 

Typedefs

typedef int(* log_dispatcher_t) (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...) __attribute__((__format__(__printf__
 
typedef int(*) log_dispatcher_ MuttLogger)
 

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)
 
void log_multiline_full (enum LogLevel level, const char *str, const char *file, int line, const char *func)
 Helper to dump multiline text to the log.
 
int log_disp_file (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...) __attribute__((__format__(__printf__
 
int int log_disp_queue (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...) __attribute__((__format__(__printf__
 
int int int log_disp_terminal (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...) __attribute__((__format__(__printf__
 
int int int int log_queue_add (struct LogLine *ll)
 Add a LogLine to the queue.
 
void log_queue_empty (void)
 Free the contents of the queue.
 
void log_queue_flush (log_dispatcher_t disp)
 Replay the log queue.
 
int log_queue_save (FILE *fp)
 Save the contents of the queue to a temporary file.
 
void log_queue_set_max_size (int size)
 Set a upper limit for the queue length.
 
void log_file_close (bool verbose)
 Close the log file.
 
int log_file_open (bool verbose)
 Start logging to a file.
 
bool log_file_running (void)
 Is the log file running?
 
int log_file_set_filename (const char *file, bool verbose)
 Set the filename for the log.
 
int log_file_set_level (enum LogLevel level, bool verbose)
 Set the logging level.
 
void log_file_set_version (const char *version)
 Set the program's version number.
 

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

Macro Definition Documentation

◆ LOG_LINE_MAX_LEN

#define LOG_LINE_MAX_LEN   10240

Log lines longer than this will be truncated.

Definition at line 32 of file logging2.h.

◆ log_multiline

#define log_multiline (   LEVEL,
  STRING 
)    log_multiline_full(LEVEL, STRING, __FILE__, __LINE__, __func__)

Definition at line 96 of file logging2.h.

Typedef Documentation

◆ log_dispatcher_t

typedef int(* log_dispatcher_t) (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...) __attribute__((__format__(__printf__

Definition at line 69 of file logging2.h.

◆ MuttLogger

typedef int(*) log_dispatcher_ MuttLogger)
extern

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 37 of file logging2.h.

38{
39 LL_PERROR = -3,
40 LL_ERROR = -2,
41 LL_WARNING = -1,
42 LL_MESSAGE = 0,
43 LL_DEBUG1 = 1,
44 LL_DEBUG2 = 2,
45 LL_DEBUG3 = 3,
46 LL_DEBUG4 = 4,
47 LL_DEBUG5 = 5,
48 LL_NOTIFY = 6,
49
50 LL_MAX,
51};
@ LL_DEBUG4
Log at debug level 4.
Definition: logging2.h:46
@ LL_ERROR
Log error.
Definition: logging2.h:40
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
@ LL_PERROR
Log perror (using errno)
Definition: logging2.h:39
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_WARNING
Log warning.
Definition: logging2.h:41
@ LL_MESSAGE
Log informational message.
Definition: logging2.h:42
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
@ LL_MAX
Definition: logging2.h:50

Function Documentation

◆ STAILQ_HEAD()

STAILQ_HEAD ( LogLineList  ,
LogLine   
)

◆ log_multiline_full()

void log_multiline_full ( enum LogLevel  level,
const char *  str,
const char *  file,
int  line,
const char *  func 
)

Helper to dump multiline text to the log.

Parameters
levelLogging level, e.g. LL_DEBUG1
strText to save
fileSource file
lineSource line number
funcSource function

Definition at line 502 of file logging.c.

504{
505 while (str && (str[0] != '\0'))
506 {
507 const char *end = strchr(str, '\n');
508 if (end)
509 {
510 int len = end - str;
511 MuttLogger(0, file, line, func, level, "%.*s\n", len, str);
512 str = end + 1;
513 }
514 else
515 {
516 MuttLogger(0, file, line, func, level, "%s\n", str);
517 break;
518 }
519 }
520}
log_dispatcher_t MuttLogger
The log dispatcher -.
Definition: logging.c:53

◆ log_disp_file()

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

◆ log_disp_queue()

int int log_disp_queue ( time_t  stamp,
const char *  file,
int  line,
const char *  function,
enum LogLevel  level,
const char *  format,
  ... 
)

◆ log_disp_terminal()

int int int log_disp_terminal ( time_t  stamp,
const char *  file,
int  line,
const char *  function,
enum LogLevel  level,
const char *  format,
  ... 
)

◆ log_queue_add()

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

286{
287 if (!ll)
288 return -1;
289
290 STAILQ_INSERT_TAIL(&LogQueue, ll, entries);
291
292 if ((LogQueueMax > 0) && (LogQueueCount >= LogQueueMax))
293 {
294 ll = STAILQ_FIRST(&LogQueue);
295 STAILQ_REMOVE_HEAD(&LogQueue, entries);
296 FREE(&ll->message);
297 FREE(&ll);
298 }
299 else
300 {
302 }
303 return LogQueueCount;
304}
#define FREE(x)
Definition: memory.h:45
static struct LogLineList LogQueue
In-memory list of log lines.
Definition: logging.c:63
static int LogQueueMax
Maximum number of entries in the log queue.
Definition: logging.c:66
static int LogQueueCount
Number of entries currently in the log queue.
Definition: logging.c:65
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
char * message
Message to be logged.
Definition: logging2.h:84
+ 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 324 of file logging.c.

325{
326 struct LogLine *ll = NULL;
327 struct LogLine *tmp = NULL;
328
329 STAILQ_FOREACH_SAFE(ll, &LogQueue, entries, tmp)
330 {
331 STAILQ_REMOVE(&LogQueue, ll, LogLine, entries);
332 FREE(&ll->message);
333 FREE(&ll);
334 }
335
336 LogQueueCount = 0;
337}
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
A Log line.
Definition: logging2.h:78
+ 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 346 of file logging.c.

347{
348 struct LogLine *ll = NULL;
349 STAILQ_FOREACH(ll, &LogQueue, entries)
350 {
351 disp(ll->time, ll->file, ll->line, ll->function, ll->level, "%s", ll->message);
352 }
353
355}
void log_queue_empty(void)
Free the contents of the queue.
Definition: logging.c:324
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
const char * file
Source file.
Definition: logging2.h:80
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_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 delete the file

Definition at line 367 of file logging.c.

368{
369 if (!fp)
370 return 0;
371
372 char buf[32] = { 0 };
373 int count = 0;
374 struct LogLine *ll = NULL;
375 STAILQ_FOREACH(ll, &LogQueue, entries)
376 {
377 mutt_date_localtime_format(buf, sizeof(buf), "%H:%M:%S", ll->time);
378 fprintf(fp, "[%s]<%c> %s", buf, LevelAbbr[ll->level + 3], ll->message);
379 if (ll->level <= LL_MESSAGE)
380 fputs("\n", fp);
381 count++;
382 }
383
384 return count;
385}
const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:46
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:951
+ 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 312 of file logging.c.

313{
314 if (size < 0)
315 size = 0;
316 LogQueueMax = size;
317}
+ 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 99 of file logging.c.

100{
101 if (!LogFileFP)
102 return;
103
104 fprintf(LogFileFP, "[%s] Closing log.\n", timestamp(0));
105 fprintf(LogFileFP, "# vim: syntax=neomuttlog\n");
107 if (verbose)
108 mutt_message(_("Closed log file: %s"), LogFileName);
109}
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_message(...)
Definition: logging2.h:91
static FILE * LogFileFP
Log file handle.
Definition: logging.c:55
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:78
static char * LogFileName
Log file name.
Definition: logging.c:56
#define _(a)
Definition: message.h:28
+ 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 120 of file logging.c.

121{
122 if (!LogFileName)
123 return -1;
124
125 if (LogFileFP)
126 log_file_close(false);
127
129 return -1;
130
132 if (!LogFileFP)
133 return -1;
134 setvbuf(LogFileFP, NULL, _IOLBF, 0);
135
136 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
138 if (verbose)
139 mutt_message(_("Debugging at level %d to file '%s'"), LogFileLevel, LogFileName);
140 return 0;
141}
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
static char * LogFileVersion
Program version.
Definition: logging.c:58
static int LogFileLevel
Log file level.
Definition: logging.c:57
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:99
#define NONULL(x)
Definition: string2.h:37
+ 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
trueThe log file is running

Definition at line 230 of file logging.c.

231{
232 return LogFileFP;
233}
+ 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 150 of file logging.c.

151{
152 if (!file)
153 return -1;
154
155 /* also handles both being NULL */
156 if (mutt_str_equal(LogFileName, file))
157 return 0;
158
160
161 if (!LogFileName)
162 log_file_close(verbose);
163
164 return log_file_open(verbose);
165}
int log_file_open(bool verbose)
Start logging to a file.
Definition: logging.c:120
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
+ 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 ( enum LogLevel  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 176 of file logging.c.

177{
178 if ((level < LL_MESSAGE) || (level >= LL_MAX))
179 return -1;
180
181 if (level == LogFileLevel)
182 return 0;
183
184 LogFileLevel = level;
185
186 if (level == LL_MESSAGE)
187 {
188 log_file_close(verbose);
189 }
190 else if (LogFileFP)
191 {
192 if (verbose)
193 mutt_message(_("Logging at level %d to file '%s'"), LogFileLevel, LogFileName);
194 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
196 }
197 else
198 {
199 log_file_open(verbose);
200 }
201
202 if (LogFileLevel >= LL_DEBUG5)
203 {
204 fprintf(LogFileFP, "\n"
205 "WARNING:\n"
206 " Logging at this level can reveal personal information.\n"
207 " Review the log carefully before posting in bug reports.\n"
208 "\n");
209 }
210
211 return 0;
212}
+ 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 221 of file logging.c.

222{
224}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: