NeoMutt
2025-12-11-1009-ga75d9e
Teaching an old dog new tricks
DOXYGEN
Toggle main menu visibility
Loading...
Searching...
No Matches
logging.c
Go to the documentation of this file.
1
22
28
29
#include "config.h"
30
#include <errno.h>
31
#include <stdarg.h>
32
#include <stdbool.h>
33
#include <stdio.h>
34
#include <string.h>
35
#include <time.h>
36
#include "
mutt/lib.h
"
37
#include "
lib.h
"
38
39
bool
DebugLogColor
=
false
;
40
bool
DebugLogLevel
=
false
;
41
bool
DebugLogTimestamp
=
false
;
42
43
extern
const
char
*
LevelAbbr
;
44
52
static
int
log_timestamp
(
char
*buf,
size_t
buflen, time_t time)
53
{
54
return
mutt_date_localtime_format
(buf, buflen,
"[%H:%M:%S]"
, time);
55
}
56
66
static
int
log_level
(
char
*buf,
size_t
buflen,
enum
LogLevel
level)
67
{
68
return
snprintf(buf, buflen,
"<%c>"
,
LevelAbbr
[level + 3]);
69
}
70
74
int
log_disp_debug
(time_t stamp,
const
char
*file,
int
line,
const
char
*function,
75
enum
LogLevel
level,
const
char
*format, ...)
76
{
77
char
buf[
LOG_LINE_MAX_LEN
] = { 0 };
78
size_t
buflen =
sizeof
(buf);
79
int
err = errno;
80
int
colour = 0;
81
int
bytes = 0;
82
83
if
(
DebugLogColor
)
84
{
85
switch
(level)
86
{
87
case
LL_PERROR
:
88
case
LL_ERROR
:
89
colour = 31;
90
break
;
91
case
LL_WARNING
:
92
colour = 33;
93
break
;
94
case
LL_MESSAGE
:
95
default
:
96
break
;
97
}
98
99
if
(colour > 0)
100
{
101
bytes += snprintf(buf + bytes, buflen - bytes,
"\033[1;%dm"
, colour);
// Escape
102
}
103
}
104
105
if
(
DebugLogTimestamp
)
106
{
107
bytes +=
log_timestamp
(buf + bytes, buflen - bytes, stamp);
108
}
109
110
if
(
DebugLogLevel
)
111
{
112
bytes +=
log_level
(buf + bytes, buflen - bytes, level);
113
}
114
115
va_list ap;
116
va_start(ap, format);
117
bytes += vsnprintf(buf + bytes, buflen - bytes, format, ap);
118
va_end(ap);
119
120
if
(level ==
LL_PERROR
)
121
bytes += snprintf(buf + bytes, buflen - bytes,
": %s"
, strerror(err));
122
123
if
(colour > 0)
124
{
125
bytes += snprintf(buf + bytes, buflen - bytes,
"\033[0m"
);
// Escape
126
}
127
128
if
(level <
LL_DEBUG1
)
129
bytes += snprintf(buf + bytes, buflen - bytes,
"\n"
);
130
131
fputs(buf, stdout);
132
return
bytes;
133
}
lib.h
Convenience wrapper for the debug headers.
DebugLogLevel
bool DebugLogLevel
Prefix log level, e.g. [E].
Definition
logging.c:40
DebugLogColor
bool DebugLogColor
Output ANSI colours.
Definition
logging.c:39
DebugLogTimestamp
bool DebugLogTimestamp
Show the timestamp.
Definition
logging.c:41
log_level
static int log_level(char *buf, size_t buflen, enum LogLevel level)
Write a log level to a buffer.
Definition
logging.c:66
log_timestamp
static int log_timestamp(char *buf, size_t buflen, time_t time)
Write a timestamp to a buffer.
Definition
logging.c:52
LevelAbbr
const char * LevelAbbr
log_disp_debug
int log_disp_debug(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
Display a log line on screen - Implements log_dispatcher_t -.
Definition
logging.c:74
LogLevel
LogLevel
Names for the Logging Levels.
Definition
logging2.h:40
LL_ERROR
@ LL_ERROR
Log error.
Definition
logging2.h:42
LL_PERROR
@ LL_PERROR
Log perror (using errno).
Definition
logging2.h:41
LL_WARNING
@ LL_WARNING
Log warning.
Definition
logging2.h:43
LL_MESSAGE
@ LL_MESSAGE
Log informational message.
Definition
logging2.h:44
LL_DEBUG1
@ LL_DEBUG1
Log at debug level 1.
Definition
logging2.h:45
LOG_LINE_MAX_LEN
#define LOG_LINE_MAX_LEN
Log lines longer than this will be truncated.
Definition
logging2.h:32
mutt_date_localtime_format
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition
date.c:957
lib.h
Convenience wrapper for the library headers.