NeoMutt  2023-03-22
Teaching an old dog new tricks
DOXYGEN
mutt_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 <stdint.h>
34#include <stdio.h>
35#include <string.h>
36#include <time.h>
37#include "mutt/lib.h"
38#include "config/lib.h"
39#include "core/lib.h"
40#include "gui/lib.h"
41#include "mutt_logging.h"
42#include "color/lib.h"
43#include "globals.h"
44#include "muttlib.h"
45
46uint64_t LastError = 0;
47
48char *CurrentFile = NULL;
49const int NumOfLogs = 5;
50
51#define S_TO_MS 1000L
52
58static void error_pause(void)
59{
60 const short c_sleep_time = cs_subset_number(NeoMutt->sub, "sleep_time");
61 const uint64_t elapsed = mutt_date_now_ms() - LastError;
62 const uint64_t sleep = c_sleep_time * S_TO_MS;
63 if ((LastError == 0) || (elapsed >= sleep))
64 return;
65
67 mutt_date_sleep_ms(sleep - elapsed);
68}
69
74{
75 /* Make sure the error message has had time to be read */
76 if (OptMsgErr)
78
79 ErrorBufMessage = false;
80 if (!OptNoCurses)
82}
83
87int log_disp_curses(time_t stamp, const char *file, int line,
88 const char *function, enum LogLevel level, ...)
89{
90 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
91 if (level > c_debug_level)
92 return 0;
93
94 char buf[1024] = { 0 };
95
96 va_list ap;
97 va_start(ap, level);
98 const char *fmt = va_arg(ap, const char *);
99 int rc = vsnprintf(buf, sizeof(buf), fmt, ap);
100 va_end(ap);
101
102 if ((level == LL_PERROR) && (rc >= 0) && (rc < sizeof(buf)))
103 {
104 char *buf2 = buf + rc;
105 int len = sizeof(buf) - rc;
106 const char *p = strerror(errno);
107 if (!p)
108 p = _("unknown error");
109
110 rc += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
111 }
112
113 const bool dupe = (strcmp(buf, ErrorBuf) == 0);
114 if (!dupe)
115 {
116 /* Only log unique messages */
117 log_disp_file(stamp, file, line, function, level, "%s", buf);
118 if (stamp == 0)
119 log_disp_queue(stamp, file, line, function, level, "%s", buf);
120 }
121
122 /* Don't display debugging message on screen */
123 if (level > LL_MESSAGE)
124 return 0;
125
126 /* Only pause if this is a message following an error */
127 if ((level > LL_ERROR) && OptMsgErr && !dupe)
128 error_pause();
129
130 size_t width = msgwin_get_width();
131 mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, width ? width : sizeof(ErrorBuf),
132 JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
133 ErrorBufMessage = true;
134
135 if (!OptKeepQuiet)
136 {
137 enum ColorId cid = MT_COLOR_NORMAL;
138 switch (level)
139 {
140 case LL_ERROR:
141 mutt_beep(false);
142 cid = MT_COLOR_ERROR;
143 break;
144 case LL_WARNING:
145 cid = MT_COLOR_WARNING;
146 break;
147 default:
148 cid = MT_COLOR_MESSAGE;
149 break;
150 }
151
153 }
154
155 if ((level <= LL_ERROR) && !dupe)
156 {
157 OptMsgErr = true;
159 }
160 else
161 {
162 OptMsgErr = false;
163 LastError = 0;
164 }
165
167 return rc;
168}
169
174{
175 char ver[64] = { 0 };
176 snprintf(ver, sizeof(ver), "-%s%s", PACKAGE_VERSION, GitVer);
178}
179
184{
185 log_file_close(false);
187}
188
197int mutt_log_set_file(const char *file)
198{
199 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
200 if (!mutt_str_equal(CurrentFile, c_debug_file))
201 {
202 struct Buffer *expanded = mutt_buffer_pool_get();
203 mutt_buffer_addstr(expanded, c_debug_file);
204 mutt_buffer_expand_path(expanded);
205
206 const char *name = mutt_file_rotate(mutt_buffer_string(expanded), NumOfLogs);
207 mutt_buffer_pool_release(&expanded);
208 if (!name)
209 return -1;
210
211 log_file_set_filename(name, false);
212 FREE(&name);
213 mutt_str_replace(&CurrentFile, c_debug_file);
214 }
215
216 cs_subset_str_string_set(NeoMutt->sub, "debug_file", file, NULL);
217
218 return 0;
219}
220
228int mutt_log_set_level(enum LogLevel level, bool verbose)
229{
230 if (!CurrentFile)
231 {
232 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
233 mutt_log_set_file(c_debug_file);
234 }
235
236 if (log_file_set_level(level, verbose) != 0)
237 return -1;
238
239 cs_subset_str_native_set(NeoMutt->sub, "debug_level", level, NULL);
240 return 0;
241}
242
251{
252 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
253 if (c_debug_level < 1)
254 return 0;
255
256 if (log_file_running())
257 return 0;
258
259 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
260 mutt_log_set_file(c_debug_file);
261
262 /* This will trigger the file creation */
263 if (log_file_set_level(c_debug_level, true) < 0)
264 return -1;
265
266 return 0;
267}
268
272int level_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
273 intptr_t value, struct Buffer *err)
274{
275 if ((value < 0) || (value >= LL_MAX))
276 {
277 mutt_buffer_printf(err, _("Invalid value for option %s: %ld"), cdef->name, value);
278 return CSR_ERR_INVALID;
279 }
280
281 return CSR_SUCCESS;
282}
283
288{
289 if (nc->event_type != NT_CONFIG)
290 return 0;
291 if (!nc->event_data)
292 return -1;
293
294 struct EventConfig *ev_c = nc->event_data;
295
296 if (mutt_str_equal(ev_c->name, "debug_file"))
297 {
298 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
299 mutt_log_set_file(c_debug_file);
300 }
301 else if (mutt_str_equal(ev_c->name, "debug_level"))
302 {
303 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
304 mutt_log_set_level(c_debug_level, true);
305 }
306 else
307 {
308 return 0;
309 }
310
311 mutt_debug(LL_DEBUG5, "log done\n");
312 return 0;
313}
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:233
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:168
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:78
Color and attribute parsing.
ColorId
List of all colored objects.
Definition: color.h:38
@ MT_COLOR_MESSAGE
Informational message.
Definition: color.h:55
@ MT_COLOR_ERROR
Error message.
Definition: color.h:49
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:57
@ MT_COLOR_WARNING
Warning messages.
Definition: color.h:79
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
Convenience wrapper for the config headers.
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
Convenience wrapper for the core headers.
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:139
void mutt_simple_format(char *buf, size_t buflen, int min_width, int max_width, enum FormatJustify justify, char pad_char, const char *s, size_t n, bool arboreal)
Format a string, like snprintf()
Definition: curs_lib.c:638
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:129
@ JUSTIFY_LEFT
Left justify the text.
Definition: curs_lib.h:42
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:437
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:711
const char * mutt_file_rotate(const char *path, int count)
Rotate a set of numbered files.
Definition: file.c:518
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: globals.c:72
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:81
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: globals.c:74
bool ErrorBufMessage
true if the last message was an error
Definition: globals.c:35
char ErrorBuf[256]
Copy of the last error message.
Definition: globals.c:36
Global variables.
const char * GitVer
int level_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate the "debug_level" config variable - Implements ConfigDef::validator() -.
Definition: mutt_logging.c:272
int log_disp_file(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Save a log line to a file - Implements log_dispatcher_t -.
Definition: logging.c:245
int log_disp_curses(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Display a log line in the message line - Implements log_dispatcher_t -.
Definition: mutt_logging.c:87
int log_disp_queue(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Save a log line to an internal queue - Implements log_dispatcher_t -.
Definition: logging.c:398
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
int main_log_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: mutt_logging.c:287
Convenience wrapper for the gui headers.
int log_file_set_level(enum LogLevel level, bool verbose)
Set the logging level.
Definition: logging.c:175
bool log_file_running(void)
Is the log file running?
Definition: logging.c:229
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
int log_file_set_filename(const char *file, bool verbose)
Set the filename for the log.
Definition: logging.c:149
void log_file_set_version(const char *version)
Set the program's version number.
Definition: logging.c:220
LogLevel
Names for the Logging Levels.
Definition: logging.h:35
@ LL_ERROR
Log error.
Definition: logging.h:37
@ LL_PERROR
Log perror (using errno)
Definition: logging.h:36
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_WARNING
Log warning.
Definition: logging.h:38
@ LL_MESSAGE
Log informational message.
Definition: logging.h:39
@ LL_MAX
Definition: logging.h:47
#define FREE(x)
Definition: memory.h:43
void msgwin_set_text(enum ColorId cid, const char *text)
Set the text for the Message Window.
Definition: msgwin.c:233
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:269
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition: msgwin.c:260
void msgwin_clear_text(void)
Clear the text in the Message Window.
Definition: msgwin.c:249
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
const int NumOfLogs
How many log files to rotate.
Definition: mutt_logging.c:49
int mutt_log_set_level(enum LogLevel level, bool verbose)
Change the logging level.
Definition: mutt_logging.c:228
int mutt_log_set_file(const char *file)
Change the logging file.
Definition: mutt_logging.c:197
uint64_t LastError
Time of the last error message (in milliseconds since the Unix epoch)
Definition: mutt_logging.c:46
#define S_TO_MS
Definition: mutt_logging.c:51
void mutt_log_stop(void)
Close the log file.
Definition: mutt_logging.c:183
int mutt_log_start(void)
Enable file logging.
Definition: mutt_logging.c:250
static void error_pause(void)
Wait for an error message to be read.
Definition: mutt_logging.c:58
void mutt_log_prep(void)
Prepare to log.
Definition: mutt_logging.c:173
char * CurrentFile
The previous log file name.
Definition: mutt_logging.c:48
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:603
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:322
Some miscellaneous functions.
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
String manipulation buffer.
Definition: buffer.h:34
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
Container for lots of config items.
Definition: set.h:252
A config-change event.
Definition: subset.h:70
const char * name
Name of config item that changed.
Definition: subset.h:72
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Data passed to a notification function.
Definition: observer.h:34
void * event_data
Data from notify_send()
Definition: observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:36
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:305
int cs_subset_str_string_set(const struct ConfigSubset *sub, const char *name, const char *value, struct Buffer *err)
Set a config item by string.
Definition: subset.c:408