NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
mutt_logging.c File Reference

NeoMutt Logging. More...

#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt_logging.h"
#include "color/lib.h"
#include "globals.h"
#include "muttlib.h"
+ Include dependency graph for mutt_logging.c:

Go to the source code of this file.


#define S_TO_MS   1000L


static void error_pause (void)
 Wait for an error message to be read. More...
void mutt_clear_error (void)
 Clear the message line (bottom line of screen) More...
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 -. More...
void mutt_log_prep (void)
 Prepare to log. More...
void mutt_log_stop (void)
 Close the log file. More...
int mutt_log_set_file (const char *file)
 Change the logging file. More...
int mutt_log_set_level (enum LogLevel level, bool verbose)
 Change the logging level. More...
int mutt_log_start (void)
 Enable file logging. More...
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() -. More...
int main_log_observer (struct NotifyCallback *nc)
 Notification that a Config Variable has changed - Implements observer_t -. More...


static uint64_t LastError = 0
 Time of the last error message (in milliseconds since the Unix epoch) More...
static char * CurrentFile = NULL
 The previous log file name. More...
static const int NumOfLogs = 5
 How many log files to rotate. More...

Detailed Description

NeoMutt Logging.

  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see

Definition in file mutt_logging.c.

Macro Definition Documentation


#define S_TO_MS   1000L

Definition at line 51 of file mutt_logging.c.

Function Documentation

◆ error_pause()

static void error_pause ( void  )

Wait for an error message to be read.

If '$sleep_time' seconds hasn't elapsed since LastError, then wait

Definition at line 58 of file mutt_logging.c.

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;
67 mutt_date_sleep_ms(sleep - elapsed);
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:141
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:937
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
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_clear_error()

void mutt_clear_error ( void  )

Clear the message line (bottom line of screen)

Definition at line 73 of file mutt_logging.c.

75 /* Make sure the error message has had time to be read */
76 if (OptMsgErr)
79 ErrorBufMessage = false;
80 if (!OptNoCurses)
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:82
bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: globals.c:75
bool ErrorBufMessage
true if the last message was an error
Definition: globals.c:36
void msgwin_clear_text(void)
Clear the text in the Message Window.
Definition: msgwin.c:250
static void error_pause(void)
Wait for an error message to be read.
Definition: mutt_logging.c:58
+ Here is the call graph for this function:

◆ mutt_log_prep()

void mutt_log_prep ( void  )

Prepare to log.

Definition at line 173 of file mutt_logging.c.

175 char ver[64] = { 0 };
176 snprintf(ver, sizeof(ver), "-%s%s", PACKAGE_VERSION, GitVer);
const char * GitVer
void log_file_set_version(const char *version)
Set the program's version number.
Definition: logging.c:220
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_log_stop()

void mutt_log_stop ( void  )

Close the log file.

Definition at line 183 of file mutt_logging.c.

185 log_file_close(false);
void log_file_close(bool verbose)
Close the log file.
Definition: logging.c:98
#define FREE(x)
Definition: memory.h:43
static char * CurrentFile
The previous log file name.
Definition: mutt_logging.c:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_log_set_file()

int mutt_log_set_file ( const char *  file)

Change the logging file.

fileName to use
Return values
0Success, file opened
-1Error, see errno

Close the old log, rotate the new logs and open the new log.

Definition at line 197 of file mutt_logging.c.

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 = buf_pool_get();
203 buf_addstr(expanded, c_debug_file);
204 buf_expand_path(expanded);
206 const char *name = mutt_file_rotate(buf_string(expanded), NumOfLogs);
207 buf_pool_release(&expanded);
208 if (!name)
209 return -1;
211 log_file_set_filename(name, false);
212 FREE(&name);
213 mutt_str_replace(&CurrentFile, c_debug_file);
214 }
216 cs_subset_str_string_set(NeoMutt->sub, "debug_file", file, NULL);
218 return 0;
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:90
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
const char * mutt_file_rotate(const char *path, int count)
Rotate a set of numbered files.
Definition: file.c:519
int log_file_set_filename(const char *file, bool verbose)
Set the filename for the log.
Definition: logging.c:149
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
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
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:333
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
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:413
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_log_set_level()

int mutt_log_set_level ( enum LogLevel  level,
bool  verbose 

Change the logging level.

levelLogging level
verboseIf true, then log the event
Return values
-1Error, level is out of range

Definition at line 228 of file mutt_logging.c.

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 }
236 if (log_file_set_level(level, verbose) != 0)
237 return -1;
239 cs_subset_str_native_set(NeoMutt->sub, "debug_level", level, NULL);
240 return 0;
int log_file_set_level(enum LogLevel level, bool verbose)
Set the logging level.
Definition: logging.c:175
int mutt_log_set_file(const char *file)
Change the logging file.
Definition: mutt_logging.c:197
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:310
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_log_start()

int mutt_log_start ( void  )

Enable file logging.

Return values
0Success, or already running
-1Failed to start

This also handles file rotation.

Definition at line 250 of file mutt_logging.c.

252 const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
253 if (c_debug_level < 1)
254 return 0;
256 if (log_file_running())
257 return 0;
259 const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
260 mutt_log_set_file(c_debug_file);
262 /* This will trigger the file creation */
263 if (log_file_set_level(c_debug_level, true) < 0)
264 return -1;
266 return 0;
bool log_file_running(void)
Is the log file running?
Definition: logging.c:229
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ LastError

uint64_t LastError = 0

Time of the last error message (in milliseconds since the Unix epoch)

Definition at line 46 of file mutt_logging.c.

◆ CurrentFile

char* CurrentFile = NULL

The previous log file name.

Definition at line 48 of file mutt_logging.c.

◆ NumOfLogs

const int NumOfLogs = 5

How many log files to rotate.

Definition at line 49 of file mutt_logging.c.