NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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
46static uint64_t LastError = 0;
47
48static char *CurrentFile = NULL;
49static const 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, const char *function,
88 enum LogLevel level, const char *format, ...)
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[LOG_LINE_MAX_LEN] = { 0 };
95
96 va_list ap;
97 va_start(ap, format);
98 int rc = vsnprintf(buf, sizeof(buf), format, ap);
99 va_end(ap);
100
101 if ((level == LL_PERROR) && (rc >= 0) && (rc < sizeof(buf)))
102 {
103 char *buf2 = buf + rc;
104 int len = sizeof(buf) - rc;
105 const char *p = strerror(errno);
106 if (!p)
107 p = _("unknown error");
108
109 rc += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
110 }
111
112 const bool dupe = (mutt_str_equal(buf, ErrorBuf));
113 if (!dupe)
114 {
115 /* Only log unique messages */
116 log_disp_file(stamp, file, line, function, level, "%s", buf);
117 if (stamp == 0)
118 log_disp_queue(stamp, file, line, function, level, "%s", buf);
119 }
120
121 /* Don't display debugging message on screen */
122 if (level > LL_MESSAGE)
123 return 0;
124
125 /* Only pause if this is a message following an error */
126 if ((level > LL_ERROR) && OptMsgErr && !dupe)
127 error_pause();
128
129 mutt_str_copy(ErrorBuf, buf, sizeof(ErrorBuf));
130 ErrorBufMessage = true;
131
132 if (!OptKeepQuiet)
133 {
134 enum ColorId cid = MT_COLOR_NORMAL;
135 switch (level)
136 {
137 case LL_ERROR:
138 mutt_beep(false);
139 cid = MT_COLOR_ERROR;
140 break;
141 case LL_WARNING:
142 cid = MT_COLOR_WARNING;
143 break;
144 default:
145 cid = MT_COLOR_MESSAGE;
146 break;
147 }
148
149 msgwin_set_text(NULL, ErrorBuf, cid);
150 }
151
152 if ((level <= LL_ERROR) && !dupe)
153 {
154 OptMsgErr = true;
156 }
157 else
158 {
159 OptMsgErr = false;
160 LastError = 0;
161 }
162
164 return rc;
165}
166
171{
172 char ver[64] = { 0 };
173 snprintf(ver, sizeof(ver), "-%s%s", PACKAGE_VERSION, GitVer);
175}
176
181{
182 log_file_close(false);
184}
185
194int mutt_log_set_file(const char *file)
195{
196 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
197 if (!mutt_str_equal(CurrentFile, c_debug_file))
198 {
199 struct Buffer *expanded = buf_pool_get();
200 buf_addstr(expanded, c_debug_file);
201 buf_expand_path(expanded);
202
203 const char *name = mutt_file_rotate(buf_string(expanded), NumOfLogs);
204 buf_pool_release(&expanded);
205 if (!name)
206 return -1;
207
208 log_file_set_filename(name, false);
209 FREE(&name);
210 mutt_str_replace(&CurrentFile, c_debug_file);
211 }
212
213 cs_subset_str_string_set(NeoMutt->sub, "debug_file", file, NULL);
214
215 return 0;
216}
217
225int mutt_log_set_level(enum LogLevel level, bool verbose)
226{
227 if (!CurrentFile)
228 {
229 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
230 mutt_log_set_file(c_debug_file);
231 }
232
233 if (log_file_set_level(level, verbose) != 0)
234 return -1;
235
236 cs_subset_str_native_set(NeoMutt->sub, "debug_level", level, NULL);
237 return 0;
238}
239
248{
249 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
250 if (c_debug_level < 1)
251 return 0;
252
253 if (log_file_running())
254 return 0;
255
256 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
257 mutt_log_set_file(c_debug_file);
258
259 /* This will trigger the file creation */
260 if (log_file_set_level(c_debug_level, true) < 0)
261 return -1;
262
263 return 0;
264}
265
269int level_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
270 intptr_t value, struct Buffer *err)
271{
272 if ((value < 0) || (value >= LL_MAX))
273 {
274 buf_printf(err, _("Invalid value for option %s: %ld"), cdef->name, value);
275 return CSR_ERR_INVALID;
276 }
277
278 return CSR_SUCCESS;
279}
280
285{
286 if (nc->event_type != NT_CONFIG)
287 return 0;
288 if (!nc->event_data)
289 return -1;
290
291 struct EventConfig *ev_c = nc->event_data;
292
293 if (mutt_str_equal(ev_c->name, "debug_file"))
294 {
295 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
296 mutt_log_set_file(c_debug_file);
297 }
298 else if (mutt_str_equal(ev_c->name, "debug_level"))
299 {
300 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
301 mutt_log_set_level(c_debug_level, true);
302 }
303 else
304 {
305 return 0;
306 }
307
308 mutt_debug(LL_DEBUG5, "log done\n");
309 return 0;
310}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:238
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
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:81
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:144
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
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:79
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:69
const char * mutt_file_rotate(const char *path, int count)
Rotate a set of numbered files.
Definition: file.c:521
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: globals.c:71
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:79
char ErrorBuf[1024]
Copy of the last error message.
Definition: globals.c:37
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: globals.c:72
bool ErrorBufMessage
true if the last message was an error
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:269
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 -.
Definition: logging.c:397
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 -.
Definition: logging.c:245
int log_disp_curses(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, const char *format,...)
Display a log line in the message line - Implements log_dispatcher_t -.
Definition: mutt_logging.c:87
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
int main_log_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: mutt_logging.c:284
Convenience wrapper for the gui headers.
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_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_WARNING
Log warning.
Definition: logging2.h:41
@ LL_MESSAGE
Log informational message.
Definition: logging2.h:42
@ LL_MAX
Definition: logging2.h:50
#define LOG_LINE_MAX_LEN
Log lines longer than this will be truncated.
Definition: logging2.h:32
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
#define FREE(x)
Definition: memory.h:45
void msgwin_clear_text(struct MuttWindow *win)
Clear the text in the Message Window.
Definition: msgwin.c:515
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition: msgwin.c:526
void msgwin_set_text(struct MuttWindow *win, const char *text, enum ColorId color)
Set the text for the Message Window.
Definition: msgwin.c:486
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:455
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:956
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:798
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:653
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
static 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:225
int mutt_log_set_file(const char *file)
Change the logging file.
Definition: mutt_logging.c:194
static 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:180
int mutt_log_start(void)
Enable file logging.
Definition: mutt_logging.c:247
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:170
static 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:634
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:335
Some miscellaneous functions.
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
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:71
const char * name
Name of config item that changed.
Definition: subset.h:73
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
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:304
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:407