NeoMutt  2022-04-29-145-g9b6a0e
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 "mutt_globals.h"
44 #include "muttlib.h"
45 #include "options.h"
46 
47 uint64_t LastError = 0;
48 
49 char *CurrentFile = NULL;
50 const int NumOfLogs = 5;
51 
52 #define S_TO_MS 1000L
53 
59 static void error_pause(void)
60 {
61  const short c_sleep_time = cs_subset_number(NeoMutt->sub, "sleep_time");
62  const uint64_t elapsed = mutt_date_epoch_ms() - LastError;
63  const uint64_t sleep = c_sleep_time * S_TO_MS;
64  if ((LastError == 0) || (elapsed >= sleep))
65  return;
66 
67  mutt_refresh();
68  mutt_date_sleep_ms(sleep - elapsed);
69 }
70 
74 void mutt_clear_error(void)
75 {
76  /* Make sure the error message has had time to be read */
77  if (OptMsgErr)
78  error_pause();
79 
80  ErrorBufMessage = false;
81  if (!OptNoCurses)
83 }
84 
88 int log_disp_curses(time_t stamp, const char *file, int line,
89  const char *function, enum LogLevel level, ...)
90 {
91  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
92  if (level > c_debug_level)
93  return 0;
94 
95  char buf[1024];
96 
97  va_list ap;
98  va_start(ap, level);
99  const char *fmt = va_arg(ap, const char *);
100  int rc = vsnprintf(buf, sizeof(buf), fmt, ap);
101  va_end(ap);
102 
103  if ((level == LL_PERROR) && (rc >= 0) && (rc < sizeof(buf)))
104  {
105  char *buf2 = buf + rc;
106  int len = sizeof(buf) - rc;
107  const char *p = strerror(errno);
108  if (!p)
109  p = _("unknown error");
110 
111  rc += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
112  }
113 
114  const bool dupe = (strcmp(buf, ErrorBuf) == 0);
115  if (!dupe)
116  {
117  /* Only log unique messages */
118  log_disp_file(stamp, file, line, function, level, "%s", buf);
119  if (stamp == 0)
120  log_disp_queue(stamp, file, line, function, level, "%s", buf);
121  }
122 
123  /* Don't display debugging message on screen */
124  if (level > LL_MESSAGE)
125  return 0;
126 
127  /* Only pause if this is a message following an error */
128  if ((level > LL_ERROR) && OptMsgErr && !dupe)
129  error_pause();
130 
131  size_t width = msgwin_get_width();
132  mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, width ? width : sizeof(ErrorBuf),
133  JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
134  ErrorBufMessage = true;
135 
136  if (!OptKeepQuiet)
137  {
138  enum ColorId cid = MT_COLOR_NORMAL;
139  switch (level)
140  {
141  case LL_ERROR:
142  mutt_beep(false);
143  cid = MT_COLOR_ERROR;
144  break;
145  case LL_WARNING:
146  cid = MT_COLOR_WARNING;
147  break;
148  default:
149  cid = MT_COLOR_MESSAGE;
150  break;
151  }
152 
154  }
155 
156  if ((level <= LL_ERROR) && !dupe)
157  {
158  OptMsgErr = true;
160  }
161  else
162  {
163  OptMsgErr = false;
164  LastError = 0;
165  }
166 
168  return rc;
169 }
170 
174 void mutt_log_prep(void)
175 {
176  char ver[64];
177  snprintf(ver, sizeof(ver), "-%s%s", PACKAGE_VERSION, GitVer);
179 }
180 
184 void mutt_log_stop(void)
185 {
186  log_file_close(false);
187  FREE(&CurrentFile);
188 }
189 
198 int mutt_log_set_file(const char *file)
199 {
200  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
201  if (!mutt_str_equal(CurrentFile, c_debug_file))
202  {
203  struct Buffer *expanded = mutt_buffer_pool_get();
204  mutt_buffer_addstr(expanded, c_debug_file);
205  mutt_buffer_expand_path(expanded);
206 
207  const char *name = mutt_file_rotate(mutt_buffer_string(expanded), NumOfLogs);
208  mutt_buffer_pool_release(&expanded);
209  if (!name)
210  return -1;
211 
212  log_file_set_filename(name, false);
213  FREE(&name);
214  mutt_str_replace(&CurrentFile, c_debug_file);
215  }
216 
217  cs_subset_str_string_set(NeoMutt->sub, "debug_file", file, NULL);
218 
219  return 0;
220 }
221 
229 int mutt_log_set_level(enum LogLevel level, bool verbose)
230 {
231  if (!CurrentFile)
232  {
233  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
234  mutt_log_set_file(c_debug_file);
235  }
236 
237  if (log_file_set_level(level, verbose) != 0)
238  return -1;
239 
240  cs_subset_str_native_set(NeoMutt->sub, "debug_level", level, NULL);
241  return 0;
242 }
243 
251 int mutt_log_start(void)
252 {
253  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
254  if (c_debug_level < 1)
255  return 0;
256 
257  if (log_file_running())
258  return 0;
259 
260  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
261  mutt_log_set_file(c_debug_file);
262 
263  /* This will trigger the file creation */
264  if (log_file_set_level(c_debug_level, true) < 0)
265  return -1;
266 
267  return 0;
268 }
269 
273 int level_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
274  intptr_t value, struct Buffer *err)
275 {
276  if ((value < 0) || (value >= LL_MAX))
277  {
278  mutt_buffer_printf(err, _("Invalid value for option %s: %ld"), cdef->name, value);
279  return CSR_ERR_INVALID;
280  }
281 
282  return CSR_SUCCESS;
283 }
284 
289 {
290  if ((nc->event_type != NT_CONFIG) || !nc->event_data)
291  return -1;
292 
293  struct EventConfig *ev_c = nc->event_data;
294 
295  if (mutt_str_equal(ev_c->name, "debug_file"))
296  {
297  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
298  mutt_log_set_file(c_debug_file);
299  }
300  else if (mutt_str_equal(ev_c->name, "debug_level"))
301  {
302  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
303  mutt_log_set_level(c_debug_level, true);
304  }
305  else
306  {
307  return 0;
308  }
309 
310  mutt_debug(LL_DEBUG5, "log done\n");
311  return 0;
312 }
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:223
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:158
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
Color and attribute parsing.
ColorId
List of all colored objects.
Definition: color.h:35
@ MT_COLOR_MESSAGE
Informational message.
Definition: color.h:51
@ MT_COLOR_ERROR
Error message.
Definition: color.h:46
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:53
@ MT_COLOR_WARNING
Warning messages.
Definition: color.h:74
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.
Convenience wrapper for the core headers.
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:140
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:640
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:130
@ JUSTIFY_LEFT
Left justify the text.
Definition: curs_lib.h:42
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:704
uint64_t mutt_date_epoch_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:436
const char * mutt_file_rotate(const char *path, int count)
Rotate a set of numbered files.
Definition: file.c:519
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:273
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:88
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:288
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:231
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:267
struct MuttWindow * msgwin_get_window(void)
Get the Message Window pointer.
Definition: msgwin.c:258
void msgwin_clear_text(void)
Clear the text in the Message Window.
Definition: msgwin.c:247
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:784
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
Hundreds of global variables to back the user variables.
bool ErrorBufMessage
true if the last message was an error
Definition: mutt_globals.h:46
char ErrorBuf[256]
Copy of the last error message.
Definition: mutt_globals.h:47
const char * GitVer
const int NumOfLogs
How many log files to rotate.
Definition: mutt_logging.c:50
int mutt_log_set_level(enum LogLevel level, bool verbose)
Change the logging level.
Definition: mutt_logging.c:229
int mutt_log_set_file(const char *file)
Change the logging file.
Definition: mutt_logging.c:198
uint64_t LastError
Time of the last error message (in milliseconds since the Unix epoch)
Definition: mutt_logging.c:47
#define S_TO_MS
Definition: mutt_logging.c:52
void mutt_log_stop(void)
Close the log file.
Definition: mutt_logging.c:184
int mutt_log_start(void)
Enable file logging.
Definition: mutt_logging.c:251
static void error_pause(void)
Wait for an error message to be read.
Definition: mutt_logging.c:59
void mutt_log_prep(void)
Prepare to log.
Definition: mutt_logging.c:174
char * CurrentFile
The previous log file name.
Definition: mutt_logging.c:49
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:604
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
Handling of global boolean variables.
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: options.h:44
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:53
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: options.h:46
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
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
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:260
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