NeoMutt  2021-02-05-666-ge300cd
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 "mutt_globals.h"
43 #include "muttlib.h"
44 #include "options.h"
45 
46 uint64_t LastError = 0;
47 
48 char *CurrentFile = NULL;
49 const int NumOfLogs = 5;
50 
51 #define S_TO_MS 1000L
52 
58 static 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_epoch_ms() - LastError;
62  const uint64_t sleep = c_sleep_time * S_TO_MS;
63  if ((LastError == 0) || (elapsed >= sleep))
64  return;
65 
66  mutt_refresh();
67  mutt_date_sleep_ms(sleep - elapsed);
68 }
69 
83 static const char *rotate_logs(const char *file, int count)
84 {
85  if (!file)
86  return NULL;
87 
88  struct Buffer *old_file = mutt_buffer_pool_get();
89  struct Buffer *new_file = mutt_buffer_pool_get();
90 
91  /* rotate the old debug logs */
92  for (count -= 2; count >= 0; count--)
93  {
94  mutt_buffer_printf(old_file, "%s%d", file, count);
95  mutt_buffer_printf(new_file, "%s%d", file, count + 1);
96 
97  mutt_buffer_expand_path(old_file);
98  mutt_buffer_expand_path(new_file);
99  rename(mutt_buffer_string(old_file), mutt_buffer_string(new_file));
100  }
101 
102  file = mutt_buffer_strdup(old_file);
103  mutt_buffer_pool_release(&old_file);
104  mutt_buffer_pool_release(&new_file);
105 
106  return file;
107 }
108 
113 {
114  /* Make sure the error message has had time to be read */
115  if (OptMsgErr)
116  error_pause();
117 
118  ErrorBufMessage = false;
119  if (!OptNoCurses)
121 }
122 
126 int log_disp_curses(time_t stamp, const char *file, int line,
127  const char *function, enum LogLevel level, ...)
128 {
129  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
130  if (level > c_debug_level)
131  return 0;
132 
133  char buf[1024];
134 
135  va_list ap;
136  va_start(ap, level);
137  const char *fmt = va_arg(ap, const char *);
138  int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
139  va_end(ap);
140 
141  if (level == LL_PERROR)
142  {
143  char *buf2 = buf + ret;
144  int len = sizeof(buf) - ret;
145  const char *p = strerror(errno);
146  if (!p)
147  p = _("unknown error");
148 
149  ret += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
150  }
151 
152  const bool dupe = (strcmp(buf, ErrorBuf) == 0);
153  if (!dupe)
154  {
155  /* Only log unique messages */
156  log_disp_file(stamp, file, line, function, level, "%s", buf);
157  if (stamp == 0)
158  log_disp_queue(stamp, file, line, function, level, "%s", buf);
159  }
160 
161  /* Don't display debugging message on screen */
162  if (level > LL_MESSAGE)
163  return 0;
164 
165  /* Only pause if this is a message following an error */
166  if ((level > LL_ERROR) && OptMsgErr && !dupe)
167  error_pause();
168 
169  size_t width = msgwin_get_width();
170  mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, width ? width : sizeof(ErrorBuf),
171  JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
172  ErrorBufMessage = true;
173 
174  if (!OptKeepQuiet)
175  {
176  enum ColorId color = MT_COLOR_NORMAL;
177  switch (level)
178  {
179  case LL_ERROR:
180  mutt_beep(false);
181  color = MT_COLOR_ERROR;
182  break;
183  case LL_WARNING:
184  color = MT_COLOR_WARNING;
185  break;
186  default:
187  color = MT_COLOR_MESSAGE;
188  break;
189  }
190 
191  msgwin_set_text(color, ErrorBuf);
192  }
193 
194  if ((level <= LL_ERROR) && !dupe)
195  {
196  OptMsgErr = true;
198  }
199  else
200  {
201  OptMsgErr = false;
202  LastError = 0;
203  }
204 
205  return ret;
206 }
207 
211 void mutt_log_prep(void)
212 {
213  char ver[64];
214  snprintf(ver, sizeof(ver), "-%s%s", PACKAGE_VERSION, GitVer);
216 }
217 
221 void mutt_log_stop(void)
222 {
223  log_file_close(false);
224  FREE(&CurrentFile);
225 }
226 
236 int mutt_log_set_file(const char *file, bool verbose)
237 {
238  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
239  if (!mutt_str_equal(CurrentFile, c_debug_file))
240  {
241  const char *name = rotate_logs(c_debug_file, NumOfLogs);
242  if (!name)
243  return -1;
244 
245  log_file_set_filename(name, false);
246  FREE(&name);
247  mutt_str_replace(&CurrentFile, c_debug_file);
248  }
249 
250  cs_subset_str_string_set(NeoMutt->sub, "debug_file", file, NULL);
251 
252  return 0;
253 }
254 
262 int mutt_log_set_level(enum LogLevel level, bool verbose)
263 {
264  if (!CurrentFile)
265  {
266  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
267  mutt_log_set_file(c_debug_file, false);
268  }
269 
270  if (log_file_set_level(level, verbose) != 0)
271  return -1;
272 
273  cs_subset_str_native_set(NeoMutt->sub, "debug_level", level, NULL);
274  return 0;
275 }
276 
284 int mutt_log_start(void)
285 {
286  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
287  if (c_debug_level < 1)
288  return 0;
289 
290  if (log_file_running())
291  return 0;
292 
293  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
294  mutt_log_set_file(c_debug_file, false);
295 
296  /* This will trigger the file creation */
297  if (log_file_set_level(c_debug_level, true) < 0)
298  return -1;
299 
300  return 0;
301 }
302 
306 int level_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
307  intptr_t value, struct Buffer *err)
308 {
309  if ((value < 0) || (value >= LL_MAX))
310  {
311  mutt_buffer_printf(err, _("Invalid value for option %s: %ld"), cdef->name, value);
312  return CSR_ERR_INVALID;
313  }
314 
315  return CSR_SUCCESS;
316 }
317 
322 {
323  if ((nc->event_type != NT_CONFIG) || !nc->event_data)
324  return -1;
325 
326  struct EventConfig *ev_c = nc->event_data;
327 
328  if (mutt_str_equal(ev_c->name, "debug_file"))
329  {
330  const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
331  mutt_log_set_file(c_debug_file, true);
332  }
333  else if (mutt_str_equal(ev_c->name, "debug_level"))
334  {
335  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
336  mutt_log_set_level(c_debug_level, true);
337  }
338  else
339  {
340  return 0;
341  }
342 
343  mutt_debug(LL_DEBUG5, "log done\n");
344  return 0;
345 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Convenience wrapper for the gui headers.
Log error.
Definition: logging.h:37
Container for lots of config items.
Definition: set.h:259
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
uint64_t mutt_date_epoch_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:436
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
Left justify the text.
Definition: curs_lib.h:43
int mutt_log_start(void)
Enable file logging.
Definition: mutt_logging.c:284
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
Data passed to a notification function.
Definition: observer.h:39
Informational message.
Definition: color.h:56
const int NumOfLogs
How many log files to rotate.
Definition: mutt_logging.c:49
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer&#39;s string.
Definition: buffer.c:432
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 -This log dispatcher saves a line ...
Definition: logging.c:400
int mutt_log_set_file(const char *file, bool verbose)
Change the logging file.
Definition: mutt_logging.c:236
NeoMutt Logging.
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
size_t msgwin_get_width(void)
Get the width of the Message Window.
Definition: msgwin.c:262
Log perror (using errno)
Definition: logging.h:36
A config-change event.
Definition: subset.h:69
String manipulation buffer.
Definition: buffer.h:33
char * CurrentFile
The previous log file name.
Definition: mutt_logging.c:48
#define _(a)
Definition: message.h:28
uint64_t LastError
Time of the last error message (in milliseconds since the Unix epoch)
Definition: mutt_logging.c:46
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:47
Definition: logging.h:47
Log informational message.
Definition: logging.h:39
WHERE char ErrorBuf[256]
Copy of the last error message.
Definition: mutt_globals.h:43
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
Definition: set.h:63
void log_file_set_version(const char *version)
Set the program&#39;s version number.
Definition: logging.c:221
int log_file_set_filename(const char *file, bool verbose)
Set the filename for the log.
Definition: logging.c:149
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
void msgwin_clear_text(void)
Clear the text in the Message Window.
Definition: msgwin.c:242
Container for Accounts, Notifications.
Definition: neomutt.h:36
Convenience wrapper for the config headers.
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:105
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
Some miscellaneous functions.
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 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 -This log dispatcher saves a line of text to ...
Definition: logging.c:246
LogLevel
Names for the Logging Levels.
Definition: logging.h:34
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
int mutt_log_set_level(enum LogLevel level, bool verbose)
Change the logging level.
Definition: mutt_logging.c:262
Convenience wrapper for the core headers.
ColorId
List of all colored objects.
Definition: color.h:39
const char * name
User-visible name.
Definition: set.h:65
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:112
int main_log_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t.
Definition: mutt_logging.c:321
Plain text.
Definition: color.h:58
int log_file_set_level(enum LogLevel level, bool verbose)
Set the logging level.
Definition: logging.c:175
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:115
bool log_file_running(void)
Is the log file running?
Definition: logging.c:230
#define S_TO_MS
Definition: mutt_logging.c:51
void mutt_log_stop(void)
Close the log file.
Definition: mutt_logging.c:221
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void * event_data
Data from notify_send()
Definition: observer.h:44
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:322
Error message.
Definition: color.h:51
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:446
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:306
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:126
#define FREE(x)
Definition: memory.h:40
WHERE bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: options.h:40
void mutt_log_prep(void)
Prepare to log.
Definition: mutt_logging.c:211
Hundreds of global variables to back the user variables.
Handling of global boolean variables.
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
WHERE bool ErrorBufMessage
true if the last message was an error
Definition: mutt_globals.h:42
Log at debug level 5.
Definition: logging.h:44
const char * GitVer
Convenience wrapper for the library headers.
static void error_pause(void)
Wait for an error message to be read.
Definition: mutt_logging.c:58
WHERE bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program ...
Definition: options.h:38
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:704
const char * name
Name of config item that changed.
Definition: subset.h:72
static const char * rotate_logs(const char *file, int count)
Rotate a set of numbered files.
Definition: mutt_logging.c:83
Warning messages.
Definition: color.h:79
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:716
Log warning.
Definition: logging.h:38
void msgwin_set_text(enum ColorId color, const char *text)
Set the text for the Message Window.
Definition: msgwin.c:223