Skip to content

Commit

Permalink
config: mutt_logging.c
Browse files Browse the repository at this point in the history
Eliminate Config global variables from mutt_logging.c
  • Loading branch information
flatcap committed Mar 24, 2021
1 parent e3f42a8 commit f4d6bee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
12 changes: 6 additions & 6 deletions mutt_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@ struct ConfigDef MainVars[] = {
{ "date_format", DT_STRING|DT_NOT_EMPTY|R_MENU, &C_DateFormat, IP "!%a, %b %d, %Y at %I:%M:%S%p %Z", 0, NULL,
"strftime format string for the `%d` expando"
},
{ "debug_file", DT_PATH|DT_PATH_FILE, &C_DebugFile, IP "~/.neomuttdebug", 0, NULL,
"File to save debug logs"
},
{ "debug_level", DT_NUMBER, &C_DebugLevel, 0, 0, level_validator,
"Logging level for debug logs"
},
{ "default_hook", DT_STRING, &C_DefaultHook, IP "~f %s !~P | (~P ~C %s)", 0, NULL,
"Pattern to use for hooks that only have a simple regex"
},
Expand Down Expand Up @@ -585,6 +579,12 @@ struct ConfigDef MainNoVars[] = {
{ "collapse_unread", DT_BOOL, NULL, true, 0, NULL,
"Prevent the collapse of threads with unread emails"
},
{ "debug_file", DT_PATH|DT_PATH_FILE, NULL, IP "~/.neomuttdebug", 0, NULL,
"File to save debug logs"
},
{ "debug_level", DT_NUMBER, NULL, 0, 0, level_validator,
"Logging level for debug logs"
},
{ "digest_collapse", DT_BOOL, NULL, true, 0, NULL,
"Hide the subparts of a multipart/digest"
},
Expand Down
34 changes: 21 additions & 13 deletions mutt_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@

uint64_t LastError = 0; ///< Time of the last error message (in milliseconds since the Unix epoch)

short C_DebugLevel = 0; ///< Config: Logging level for debug logs
char *C_DebugFile = NULL; ///< Config: File to save debug logs
char *CurrentFile = NULL; ///< The previous log file name
const int NumOfLogs = 5; ///< How many log files to rotate

Expand All @@ -59,8 +57,9 @@ const int NumOfLogs = 5; ///< How many log files to rotate
*/
static void error_pause(void)
{
const short c_sleep_time = cs_subset_number(NeoMutt->sub, "sleep_time");
const uint64_t elapsed = mutt_date_epoch_ms() - LastError;
const uint64_t sleep = C_SleepTime * S_TO_MS;
const uint64_t sleep = c_sleep_time * S_TO_MS;
if ((LastError == 0) || (elapsed >= sleep))
return;

Expand Down Expand Up @@ -127,7 +126,8 @@ void mutt_clear_error(void)
int log_disp_curses(time_t stamp, const char *file, int line,
const char *function, enum LogLevel level, ...)
{
if (level > C_DebugLevel)
const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
if (level > c_debug_level)
return 0;

char buf[1024];
Expand Down Expand Up @@ -237,15 +237,16 @@ void mutt_log_stop(void)
*/
int mutt_log_set_file(const char *file, bool verbose)
{
if (!mutt_str_equal(CurrentFile, C_DebugFile))
const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
if (!mutt_str_equal(CurrentFile, c_debug_file))
{
const char *name = rotate_logs(C_DebugFile, NumOfLogs);
const char *name = rotate_logs(c_debug_file, NumOfLogs);
if (!name)
return -1;

log_file_set_filename(name, false);
FREE(&name);
mutt_str_replace(&CurrentFile, C_DebugFile);
mutt_str_replace(&CurrentFile, c_debug_file);
}

cs_subset_str_string_set(NeoMutt->sub, "debug_file", file, NULL);
Expand All @@ -263,7 +264,10 @@ int mutt_log_set_file(const char *file, bool verbose)
int mutt_log_set_level(enum LogLevel level, bool verbose)
{
if (!CurrentFile)
mutt_log_set_file(C_DebugFile, false);
{
const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
mutt_log_set_file(c_debug_file, false);
}

if (log_file_set_level(level, verbose) != 0)
return -1;
Expand All @@ -281,16 +285,18 @@ int mutt_log_set_level(enum LogLevel level, bool verbose)
*/
int mutt_log_start(void)
{
if (C_DebugLevel < 1)
const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
if (c_debug_level < 1)
return 0;

if (log_file_running())
return 0;

mutt_log_set_file(C_DebugFile, false);
const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
mutt_log_set_file(c_debug_file, false);

/* This will trigger the file creation */
if (log_file_set_level(C_DebugLevel, true) < 0)
if (log_file_set_level(c_debug_level, true) < 0)
return -1;

return 0;
Expand Down Expand Up @@ -323,10 +329,12 @@ int mutt_log_observer(struct NotifyCallback *nc)

struct EventConfig *ec = nc->event_data;

const char *const c_debug_file = cs_subset_path(NeoMutt->sub, "debug_file");
const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
if (mutt_str_equal(ec->name, "debug_file"))
mutt_log_set_file(C_DebugFile, true);
mutt_log_set_file(c_debug_file, true);
else if (mutt_str_equal(ec->name, "debug_level"))
mutt_log_set_level(C_DebugLevel, true);
mutt_log_set_level(c_debug_level, true);

return 0;
}
3 changes: 0 additions & 3 deletions mutt_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
struct ConfigDef;
struct ConfigSet;

extern short C_DebugLevel;
extern char *C_DebugFile;

int log_disp_curses(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, ...);

void mutt_log_prep(void);
Expand Down

0 comments on commit f4d6bee

Please sign in to comment.