NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
logging.c
Go to the documentation of this file.
1
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 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
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}
bool DebugLogLevel
Prefix log level, e.g. [E].
Definition: logging.c:40
static int log_level(char *buf, size_t buflen, enum LogLevel level)
Write a log level to a buffer.
Definition: logging.c:66
bool DebugLogColor
Output ANSI colours.
Definition: logging.c:39
static int log_timestamp(char *buf, size_t buflen, time_t time)
Write a timestamp to a buffer.
Definition: logging.c:52
bool DebugLogTimestamp
Show the timestamp.
Definition: logging.c:41
const char * LevelAbbr
Abbreviations of logging level names.
Definition: logging.c:46
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:38
@ LL_ERROR
Log error.
Definition: logging2.h:40
@ LL_PERROR
Log perror (using errno)
Definition: logging2.h:39
@ LL_WARNING
Log warning.
Definition: logging2.h:41
@ LL_MESSAGE
Log informational message.
Definition: logging2.h:42
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define LOG_LINE_MAX_LEN
Log lines longer than this will be truncated.
Definition: logging2.h:32
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:951
Convenience wrapper for the library headers.
Key value store.