NeoMutt  2025-12-11-276-g10b23b
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
logging2.h File Reference

Logging Dispatcher. More...

#include <stdbool.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, ...)
 
#define mutt_warning(...)
 
#define mutt_message(...)
 
#define mutt_error(...)
 
#define mutt_perror(...)
 
#define log_multiline(LEVEL, STRING)
 

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_t 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_null (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_queue (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_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 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.
 
struct LogLineList log_queue_get (void)
 Get the Log Queue.
 
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.
 

Variables

const char * LogLevelAbbr
 Abbreviations of logging level names.
 

Detailed Description

Logging Dispatcher.

Authors
  • Richard Russon
  • Dennis Schön

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 )
Value:
log_multiline_full(LEVEL, STRING, __FILE__, __LINE__, __func__)
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.
Definition logging.c:488

Definition at line 98 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 71 of file logging2.h.

◆ MuttLogger

typedef int log_dispatcher_t 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 39 of file logging2.h.

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

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

490{
491 while (str && (str[0] != '\0'))
492 {
493 const char *end = strchr(str, '\n');
494 if (end)
495 {
496 int len = end - str;
497 MuttLogger(0, file, line, func, level, "%.*s\n", len, str);
498 str = end + 1;
499 }
500 else
501 {
502 MuttLogger(0, file, line, func, level, "%s\n", str);
503 break;
504 }
505 }
506}
log_dispatcher_t MuttLogger
The log dispatcher -.
Definition logging.c:54

◆ 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_null()

int int log_disp_null ( time_t stamp,
const char * file,
int line,
const char * function,
enum LogLevel level,
const char * format,
... )
+ Here is the call graph for this function:

◆ log_disp_queue()

int int int log_disp_queue ( time_t stamp,
const char * file,
int line,
const char * function,
enum LogLevel level,
const char * format,
... )
+ Here is the call graph for this function:

◆ log_disp_terminal()

int int int int log_disp_terminal ( time_t stamp,
const char * file,
int line,
const char * function,
enum LogLevel level,
const char * format,
... )
+ Here is the call graph for this function:

◆ log_queue_add()

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

293{
294 if (!ll)
295 return -1;
296
297 STAILQ_INSERT_TAIL(&LogQueue, ll, entries);
298
299 if ((LogQueueMax > 0) && (LogQueueCount >= LogQueueMax))
300 {
301 ll = STAILQ_FIRST(&LogQueue);
302 STAILQ_REMOVE_HEAD(&LogQueue, entries);
303 FREE(&ll->message);
304 FREE(&ll);
305 }
306 else
307 {
309 }
310 return LogQueueCount;
311}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
static struct LogLineList LogQueue
In-memory list of log lines.
Definition logging.c:64
static int LogQueueMax
Maximum number of entries in the log queue.
Definition logging.c:67
static int LogQueueCount
Number of entries currently in the log queue.
Definition logging.c:66
#define STAILQ_REMOVE_HEAD(head, field)
Definition queue.h:461
#define STAILQ_FIRST(head)
Definition queue.h:388
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
char * message
Message to be logged.
Definition logging2.h:86
+ 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 331 of file logging.c.

332{
333 struct LogLine *ll = NULL;
334 struct LogLine *tmp = NULL;
335
336 STAILQ_FOREACH_SAFE(ll, &LogQueue, entries, tmp)
337 {
338 STAILQ_REMOVE(&LogQueue, ll, LogLine, entries);
339 FREE(&ll->message);
340 FREE(&ll);
341 }
342
343 LogQueueCount = 0;
344}
#define STAILQ_REMOVE(head, elm, type, field)
Definition queue.h:441
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:400
A Log line.
Definition logging2.h:80
+ 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 353 of file logging.c.

354{
355 struct LogLine *ll = NULL;
356 STAILQ_FOREACH(ll, &LogQueue, entries)
357 {
358 disp(ll->time, ll->file, ll->line, ll->function, ll->level, "%s", ll->message);
359 }
360
362}
void log_queue_empty(void)
Free the contents of the queue.
Definition logging.c:331
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
const char * file
Source file.
Definition logging2.h:82
const char * function
C function.
Definition logging2.h:84
int line
Line number in source file.
Definition logging2.h:83
enum LogLevel level
Log level, e.g. LL_DEBUG1.
Definition logging2.h:85
time_t time
Timestamp of the message.
Definition logging2.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log_queue_get()

struct LogLineList log_queue_get ( void )

Get the Log Queue.

Return values
objLog Queue

Definition at line 368 of file logging.c.

369{
370 return LogQueue;
371}
+ 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 319 of file logging.c.

320{
321 if (size < 0)
322 size = 0;
323 LogQueueMax = size;
324}
+ 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 100 of file logging.c.

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

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

237{
238 return LogFileFP;
239}
+ 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 151 of file logging.c.

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

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

228{
230}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ LogLevelAbbr

const char* LogLevelAbbr
extern

Abbreviations of logging level names.

Definition at line 47 of file logging.c.