NeoMutt  2025-12-11-177-g48e272
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
logging.c File Reference

Logging Dispatcher. More...

#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "date.h"
#include "file.h"
#include "logging2.h"
#include "memory.h"
#include "message.h"
#include "queue.h"
#include "string2.h"
+ Include dependency graph for logging.c:

Go to the source code of this file.

Functions

static const char * timestamp (time_t stamp)
 Create a YYYY-MM-DD HH:MM:SS timestamp.
 
void log_file_close (bool verbose)
 Close the log file.
 
int log_file_open (bool verbose)
 Start logging to a file.
 
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.
 
bool log_file_running (void)
 Is the log file running?
 
int log_disp_file (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
 Save a log line to a file - Implements log_dispatcher_t -.
 
int log_queue_add (struct LogLine *ll)
 Add a LogLine to the queue.
 
void log_queue_set_max_size (int size)
 Set a upper limit for the queue length.
 
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.
 
int log_disp_queue (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
 Save a log line to an internal queue - Implements log_dispatcher_t -.
 
int log_disp_terminal (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
 Save a log line to the terminal - Implements log_dispatcher_t -.
 
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.
 

Variables

const char * LogLevelAbbr = "PEWM12345N"
 Abbreviations of logging level names.
 
log_dispatcher_t MuttLogger = log_disp_terminal
 The log dispatcher -.
 
static FILE * LogFileFP = NULL
 Log file handle.
 
static char * LogFileName = NULL
 Log file name.
 
static int LogFileLevel = 0
 Log file level.
 
static char * LogFileVersion = NULL
 Program version.
 
static struct LogLineList LogQueue = STAILQ_HEAD_INITIALIZER(LogQueue)
 In-memory list of log lines.
 
static int LogQueueCount = 0
 Number of entries currently in the log queue.
 
static int LogQueueMax = 0
 Maximum number of entries in the log queue.
 

Detailed Description

Logging Dispatcher.

Authors
  • Richard Russon
  • Pietro Cerutti
  • 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 logging.c.

Function Documentation

◆ timestamp()

static const char * timestamp ( time_t stamp)
static

Create a YYYY-MM-DD HH:MM:SS timestamp.

Parameters
stampUnix time
Return values
ptrTimestamp string

If stamp is 0, then the current time will be used.

Note
This function returns a pointer to a static buffer. Do not free it.

Definition at line 79 of file logging.c.

80{
81 static char buf[23] = { 0 };
82 static time_t last = 0;
83
84 if (stamp == 0)
85 stamp = mutt_date_now();
86
87 if (stamp != last)
88 {
89 mutt_date_localtime_format(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", stamp);
90 last = stamp;
91 }
92
93 return buf;
94}
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition date.c:952
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:457
+ Here is the call graph for this function:
+ 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
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
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_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 if ((level < LL_MESSAGE) || (level >= LL_MAX))
180 return -1;
181
182 if (level == LogFileLevel)
183 return 0;
184
185 LogFileLevel = level;
186
187 if (level == LL_MESSAGE)
188 {
189 log_file_close(verbose);
190 }
191 else if (LogFileFP)
192 {
193 if (verbose)
194 mutt_message(_("Logging at level %d to file '%s'"), LogFileLevel, LogFileName);
195 fprintf(LogFileFP, "[%s] NeoMutt%s debugging at level %d\n", timestamp(0),
197 }
198 else
199 {
200 log_file_open(verbose);
201 }
202
203 if (LogFileLevel >= LL_DEBUG5)
204 {
205 fprintf(LogFileFP, "\n"
206 "WARNING:\n"
207 " Logging at this level can reveal personal information.\n"
208 " Review the log carefully before posting in bug reports.\n"
209 "\n");
210 }
211
212 return 0;
213}
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
@ LL_MESSAGE
Log informational message.
Definition logging2.h:44
@ LL_MAX
Definition logging2.h:52
+ 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 222 of file logging.c.

223{
225}
+ 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 231 of file logging.c.

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

288{
289 if (!ll)
290 return -1;
291
292 STAILQ_INSERT_TAIL(&LogQueue, ll, entries);
293
294 if ((LogQueueMax > 0) && (LogQueueCount >= LogQueueMax))
295 {
296 ll = STAILQ_FIRST(&LogQueue);
297 STAILQ_REMOVE_HEAD(&LogQueue, entries);
298 FREE(&ll->message);
299 FREE(&ll);
300 }
301 else
302 {
304 }
305 return LogQueueCount;
306}
#define FREE(x)
Definition memory.h:63
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_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 314 of file logging.c.

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

327{
328 struct LogLine *ll = NULL;
329 struct LogLine *tmp = NULL;
330
331 STAILQ_FOREACH_SAFE(ll, &LogQueue, entries, tmp)
332 {
333 STAILQ_REMOVE(&LogQueue, ll, LogLine, entries);
334 FREE(&ll->message);
335 FREE(&ll);
336 }
337
338 LogQueueCount = 0;
339}
#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 348 of file logging.c.

349{
350 struct LogLine *ll = NULL;
351 STAILQ_FOREACH(ll, &LogQueue, entries)
352 {
353 disp(ll->time, ll->file, ll->line, ll->function, ll->level, "%s", ll->message);
354 }
355
357}
void log_queue_empty(void)
Free the contents of the queue.
Definition logging.c:326
#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 363 of file logging.c.

364{
365 return LogQueue;
366}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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

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

Variable Documentation

◆ LogLevelAbbr

const char* LogLevelAbbr = "PEWM12345N"

Abbreviations of logging level names.

Definition at line 47 of file logging.c.

◆ LogFileFP

FILE* LogFileFP = NULL
static

Log file handle.

Definition at line 56 of file logging.c.

◆ LogFileName

char* LogFileName = NULL
static

Log file name.

Definition at line 57 of file logging.c.

◆ LogFileLevel

int LogFileLevel = 0
static

Log file level.

Definition at line 58 of file logging.c.

◆ LogFileVersion

char* LogFileVersion = NULL
static

Program version.

Definition at line 59 of file logging.c.

◆ LogQueue

struct LogLineList LogQueue = STAILQ_HEAD_INITIALIZER(LogQueue)
static

In-memory list of log lines.

Definition at line 64 of file logging.c.

◆ LogQueueCount

int LogQueueCount = 0
static

Number of entries currently in the log queue.

Definition at line 66 of file logging.c.

◆ LogQueueMax

int LogQueueMax = 0
static

Maximum number of entries in the log queue.

Definition at line 67 of file logging.c.