NeoMutt  2019-12-07
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/mutt.h"
38 #include "config/lib.h"
39 #include "mutt_logging.h"
40 #include "color.h"
41 #include "curs_lib.h"
42 #include "globals.h"
43 #include "mutt_curses.h"
44 #include "mutt_window.h"
45 #include "muttlib.h"
46 #include "options.h"
47 
48 size_t LastError = 0;
49 
50 short C_DebugLevel = 0;
51 char *C_DebugFile = NULL;
52 char *CurrentFile = NULL;
53 const int NumOfLogs = 5;
54 
55 #define S_TO_MS 1000L
56 
62 static void error_pause(void)
63 {
64  const size_t elapsed = mutt_date_epoch_ms() - LastError;
65  const size_t sleep = C_SleepTime * S_TO_MS;
66  if ((LastError == 0) || (elapsed >= sleep))
67  return;
68 
69  mutt_refresh();
70  mutt_date_sleep_ms(sleep - elapsed);
71 }
72 
86 static const char *rotate_logs(const char *file, int count)
87 {
88  if (!file)
89  return NULL;
90 
91  struct Buffer *old_file = mutt_buffer_pool_get();
92  struct Buffer *new_file = mutt_buffer_pool_get();
93 
94  /* rotate the old debug logs */
95  for (count -= 2; count >= 0; count--)
96  {
97  mutt_buffer_printf(old_file, "%s%d", file, count);
98  mutt_buffer_printf(new_file, "%s%d", file, count + 1);
99 
100  mutt_buffer_expand_path(old_file);
101  mutt_buffer_expand_path(new_file);
102  rename(mutt_b2s(old_file), mutt_b2s(new_file));
103  }
104 
105  file = mutt_buffer_strdup(old_file);
106  mutt_buffer_pool_release(&old_file);
107  mutt_buffer_pool_release(&new_file);
108 
109  return file;
110 }
111 
116 {
117  /* Make sure the error message has had time to be read */
118  if (OptMsgErr)
119  error_pause();
120 
121  ErrorBufMessage = false;
122  if (!OptNoCurses)
124 }
125 
129 int log_disp_curses(time_t stamp, const char *file, int line,
130  const char *function, enum LogLevel level, ...)
131 {
132  if (level > C_DebugLevel)
133  return 0;
134 
135  char buf[1024];
136 
137  va_list ap;
138  va_start(ap, level);
139  const char *fmt = va_arg(ap, const char *);
140  int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
141  va_end(ap);
142 
143  if (level == LL_PERROR)
144  {
145  char *buf2 = buf + ret;
146  int len = sizeof(buf) - ret;
147  char *p = strerror(errno);
148  if (!p)
149  p = _("unknown error");
150 
151  ret += snprintf(buf2, len, ": %s (errno = %d)", p, errno);
152  }
153 
154  const bool dupe = (strcmp(buf, ErrorBuf) == 0);
155  if (!dupe)
156  {
157  /* Only log unique messages */
158  log_disp_file(stamp, file, line, function, level, "%s", buf);
159  if (stamp == 0)
160  log_disp_queue(stamp, file, line, function, level, "%s", buf);
161  }
162 
163  /* Don't display debugging message on screen */
164  if (level > LL_MESSAGE)
165  return 0;
166 
167  /* Only pause if this is a message following an error */
168  if ((level > LL_ERROR) && OptMsgErr && !dupe)
169  error_pause();
170 
173  JUSTIFY_LEFT, 0, buf, sizeof(buf), false);
174  ErrorBufMessage = true;
175 
176  if (!OptKeepQuiet)
177  {
178  switch (level)
179  {
180  case LL_ERROR:
181  mutt_beep(false);
183  break;
184  case LL_WARNING:
186  break;
187  default:
189  break;
190  }
191 
195  mutt_refresh();
196  }
197 
198  if ((level <= LL_ERROR) && !dupe)
199  {
200  OptMsgErr = true;
202  }
203  else
204  {
205  OptMsgErr = false;
206  LastError = 0;
207  }
208 
209  return ret;
210 }
211 
215 void mutt_log_prep(void)
216 {
217  char ver[64];
218  snprintf(ver, sizeof(ver), "-%s%s", PACKAGE_VERSION, GitVer);
220 }
221 
225 void mutt_log_stop(void)
226 {
227  log_file_close(false);
228  FREE(&CurrentFile);
229 }
230 
240 int mutt_log_set_file(const char *file, bool verbose)
241 {
243  {
244  const char *name = rotate_logs(C_DebugFile, NumOfLogs);
245  if (!name)
246  return -1;
247 
248  log_file_set_filename(name, false);
249  FREE(&name);
251  }
252 
253  cs_str_string_set(Config, "debug_file", file, NULL);
254 
255  return 0;
256 }
257 
265 int mutt_log_set_level(enum LogLevel level, bool verbose)
266 {
267  if (!CurrentFile)
269 
270  if (log_file_set_level(level, verbose) != 0)
271  return -1;
272 
273  cs_str_native_set(Config, "debug_level", level, NULL);
274  return 0;
275 }
276 
284 int mutt_log_start(void)
285 {
286  if (C_DebugLevel < 1)
287  return 0;
288 
289  if (log_file_running())
290  return 0;
291 
293 
294  /* This will trigger the file creation */
295  if (log_file_set_level(C_DebugLevel, true) < 0)
296  return -1;
297 
298  return 0;
299 }
300 
310 int level_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
311  intptr_t value, struct Buffer *err)
312 {
313  if ((value < 0) || (value >= LL_MAX))
314  {
315  mutt_buffer_printf(err, _("Invalid value for option %s: %ld"), cdef->name, value);
316  return CSR_ERR_INVALID;
317  }
318 
319  return CSR_SUCCESS;
320 }
321 
326 {
327  if (!nc)
328  return -1;
329 
330  struct EventConfig *ec = (struct EventConfig *) nc->event;
331 
332  if (mutt_str_strcmp(ec->name, "debug_file") == 0)
334  else if (mutt_str_strcmp(ec->name, "debug_level") == 0)
336 
337  return 0;
338 }
Log error.
Definition: logging.h:37
Container for lots of config items.
Definition: set.h:187
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
size_t LastError
Time of the last error message (in milliseconds since the Unix epoch)
Definition: mutt_logging.c:48
Define wrapper functions around Curses/Slang.
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:129
uint64_t mutt_date_epoch_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:420
Left justify the text.
Definition: curs_lib.h:47
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:40
int level_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate the "debug_level" config variable.
Definition: mutt_logging.c:310
Window management.
GUI miscellaneous curses (window drawing) routines.
Informational message.
Definition: color.h:76
const int NumOfLogs
How many log files to rotate.
Definition: mutt_logging.c:53
int cs_str_string_set(const struct ConfigSet *cs, const char *name, const char *value, struct Buffer *err)
Set a config item by string.
Definition: set.c:627
WHERE short C_SleepTime
Config: Time to pause after certain info messages.
Definition: globals.h:151
void mutt_curses_set_color(enum ColorId color)
Set the current colour for text.
Definition: mutt_curses.c:49
int mutt_log_set_file(const char *file, bool verbose)
Change the logging file.
Definition: mutt_logging.c:240
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:117
NeoMutt Logging.
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
Log perror (using errno)
Definition: logging.h:36
char * mutt_buffer_strdup(struct Buffer *buf)
Copy a Buffer&#39;s string.
Definition: buffer.c:432
A config-change event.
Definition: set.h:199
String manipulation buffer.
Definition: buffer.h:33
char * CurrentFile
The previous log file name.
Definition: mutt_logging.c:52
#define _(a)
Definition: message.h:28
WHERE struct ConfigSet * Config
Wrapper around the user&#39;s config settings.
Definition: globals.h:40
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:46
intptr_t event
Send: event data.
Definition: observer.h:46
Definition: logging.h:47
Log informational message.
Definition: logging.h:39
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
Config item definition.
Definition: set.h:153
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
Convenience wrapper for the config headers.
Hundreds of global variables to back the user variables.
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:96
Some miscellaneous functions.
LogLevel
Names for the Logging Levels.
Definition: logging.h:34
int mutt_log_set_level(enum LogLevel level, bool verbose)
Change the logging level.
Definition: mutt_logging.c:265
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
Definition: mutt_window.c:95
const char * name
Definition: pgpmicalg.c:46
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.
Definition: logging.c:255
const char * name
User-visible name.
Definition: set.h:155
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:115
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
Plain text.
Definition: color.h:78
int log_file_set_level(enum LogLevel level, bool verbose)
Set the logging level.
Definition: logging.c:175
#define mutt_b2s(buf)
Definition: buffer.h:41
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:105
const char * line
Definition: common.c:36
short C_DebugLevel
Config: Logging level for debug logs.
Definition: mutt_logging.c:50
bool log_file_running(void)
Is the log file running?
Definition: logging.c:230
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:49
const char * GitVer
WHERE char ErrorBuf[256]
Copy of the last error message.
Definition: globals.h:46
WHERE bool ErrorBufMessage
true if the last message was an error
Definition: globals.h:45
#define S_TO_MS
Definition: mutt_logging.c:55
void mutt_log_stop(void)
Close the log file.
Definition: mutt_logging.c:225
Color and attribute parsing.
void mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:453
int mutt_window_mvaddstr(struct MuttWindow *win, int row, int col, const char *str)
Move the cursor and write a fixed string to a Window.
Definition: mutt_window.c:212
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:343
Error message.
Definition: color.h:71
char * C_DebugFile
Config: File to save debug logs.
Definition: mutt_logging.c:51
#define FREE(x)
Definition: memory.h:40
WHERE bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: options.h:39
struct MuttWindow * MuttMessageWindow
Message Window.
Definition: mutt_window.c:42
void mutt_log_prep(void)
Prepare to log.
Definition: mutt_logging.c:215
Handling of global boolean variables.
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:409
static void error_pause(void)
Wait for an error message to be read.
Definition: mutt_logging.c:62
WHERE bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program ...
Definition: options.h:37
int mutt_log_observer(struct NotifyCallback *nc)
Listen for config changes affecting the log file - Implements observer_t()
Definition: mutt_logging.c:325
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:787
const char * name
Name of config item that changed.
Definition: set.h:203
static const char * rotate_logs(const char *file, int count)
Rotate a set of numbered files.
Definition: mutt_logging.c:86
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
Warning messages.
Definition: color.h:99
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:1021
Log warning.
Definition: logging.h:38
int cs_str_native_set(const struct ConfigSet *cs, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: set.c:772