NeoMutt  2025-12-11-1059-g2f5469
Teaching an old dog new tricks
DOXYGEN
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
39bool DebugLogColor = false;
40bool DebugLogLevel = false;
41bool DebugLogTimestamp = false;
42
43extern const char *LevelAbbr;
44
52static 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
66static int log_level(char *buf, size_t buflen, enum LogLevel level)
67{
68 return snprintf(buf, buflen, "<%c>", LevelAbbr[level + 3]);
69}
70
74int log_disp_debug(time_t stamp, const char *file, int line, const char *function,
75 enum LogLevel level, const char *format, ...)
76{
77 struct Buffer *buf = buf_pool_get();
78 int err = errno;
79 int colour = 0;
80
81 if (DebugLogColor)
82 {
83 switch (level)
84 {
85 case LL_PERROR:
86 case LL_ERROR:
87 colour = 31;
88 break;
89 case LL_WARNING:
90 colour = 33;
91 break;
92 case LL_MESSAGE:
93 default:
94 break;
95 }
96
97 if (colour > 0)
98 {
99 buf_add_printf(buf, "\033[1;%dm", colour); // Escape
100 }
101 }
102
104 {
105 char ts[32] = { 0 };
106 log_timestamp(ts, sizeof(ts), stamp);
107 buf_addstr(buf, ts);
108 }
109
110 if (DebugLogLevel)
111 {
112 char lvl[8] = { 0 };
113 log_level(lvl, sizeof(lvl), level);
114 buf_addstr(buf, lvl);
115 }
116
117 va_list ap;
118 va_start(ap, format);
119 buf_vaprintf(buf, format, ap);
120 va_end(ap);
121
122 if (level == LL_PERROR)
123 buf_add_printf(buf, ": %s", strerror(err));
124
125 if (colour > 0)
126 {
127 buf_addstr(buf, "\033[0m"); // Escape
128 }
129
130 if (level < LL_DEBUG1)
131 buf_addch(buf, '\n');
132
133 fputs(buf_string(buf), stdout);
134
135 int bytes = buf_len(buf);
136 buf_pool_release(&buf);
137 return bytes;
138}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:211
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:497
int buf_vaprintf(struct Buffer *buf, const char *fmt, va_list ap)
Format a string into a Buffer.
Definition buffer.c:133
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:248
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:233
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:99
Convenience wrapper for the debug headers.
bool DebugLogLevel
Prefix log level, e.g. [E].
Definition logging.c:40
bool DebugLogColor
Output ANSI colours.
Definition logging.c:39
bool DebugLogTimestamp
Show the timestamp.
Definition logging.c:41
static int log_level(char *buf, size_t buflen, enum LogLevel level)
Write a log level to a buffer.
Definition logging.c:66
static int log_timestamp(char *buf, size_t buflen, time_t time)
Write a timestamp to a buffer.
Definition logging.c:52
const char * LevelAbbr
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
Names for the Logging Levels.
Definition logging2.h:40
@ LL_ERROR
Log error.
Definition logging2.h:42
@ LL_PERROR
Log perror (using errno).
Definition logging2.h:41
@ LL_WARNING
Log warning.
Definition logging2.h:43
@ LL_MESSAGE
Log informational message.
Definition logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition date.c:957
Convenience wrapper for the library headers.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:37