From 75cfe38808e76a5308dac7023876a9826ed56347 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Fri, 21 Feb 2020 21:10:00 +0100 Subject: Allow setting custom log file via -f FILENAME `profanity -f TEST` will use `~/.local/share/profanity/logs/TEST.log` as the log file. --- src/config/files.c | 14 ++++++++++++-- src/config/files.h | 2 +- src/log.c | 38 ++++++++++++++++++++++++++++---------- src/log.h | 2 +- src/main.c | 8 +++++--- src/profanity.c | 10 +++++----- src/profanity.h | 2 +- 7 files changed, 53 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/config/files.c b/src/config/files.c index 25b25c54..133a270e 100644 --- a/src/config/files.c +++ b/src/config/files.c @@ -112,16 +112,26 @@ files_get_inputrc_file(void) } char* -files_get_log_file(void) +files_get_log_file(char *log_file) { gchar *xdg_data = _files_get_xdg_data_home(); GString *logfile = g_string_new(xdg_data); - g_string_append(logfile, "/profanity/logs/profanity"); + + if (log_file) { + g_string_append(logfile, "/profanity/logs/"); + g_string_append(logfile, log_file); + } else { + g_string_append(logfile, "/profanity/logs/profanity"); + } + if (!prefs_get_boolean(PREF_LOG_SHARED)) { g_string_append_printf(logfile, "%d", getpid()); } + g_string_append(logfile, ".log"); + char *result = strdup(logfile->str); + free(xdg_data); g_string_free(logfile, TRUE); diff --git a/src/config/files.h b/src/config/files.h index b5cc5e1b..12c3f03a 100644 --- a/src/config/files.h +++ b/src/config/files.h @@ -61,7 +61,7 @@ void files_create_directories(void); char* files_get_config_path(char *config_base); char* files_get_data_path(char *data_base); -char* files_get_log_file(void); +char* files_get_log_file(char *log_file); char* files_get_inputrc_file(void); #endif diff --git a/src/log.c b/src/log.c index aa5f37b2..c2d32553 100644 --- a/src/log.c +++ b/src/log.c @@ -55,7 +55,7 @@ #define PROF "prof" static FILE *logp; -GString *mainlogfile; +GString *mainlogfile = NULL; static GTimeZone *tz; static GDateTime *dt; @@ -144,22 +144,32 @@ log_error(const char *const msg, ...) } void -log_init(log_level_t filter) +log_init(log_level_t filter, char *log_file) { level_filter = filter; tz = g_time_zone_new_local(); - char *log_file = files_get_log_file(); - logp = fopen(log_file, "a"); - g_chmod(log_file, S_IRUSR | S_IWUSR); - mainlogfile = g_string_new(log_file); - free(log_file); + + char *lf; + lf = files_get_log_file(log_file); + + logp = fopen(lf, "a"); + g_chmod(lf, S_IRUSR | S_IWUSR); + mainlogfile = g_string_new(lf); + free(lf); } void log_reinit(void) { + char *lf = strdup(mainlogfile->str); + char *start = strrchr(lf, '/') +1; + char *end = strstr(start, ".log"); + *end = '\0'; + log_close(); - log_init(level_filter); + log_init(level_filter, start); + + free(lf); } char* @@ -229,7 +239,7 @@ log_level_from_string(char *log_level) static void _rotate_log_file(void) { - gchar *log_file = files_get_log_file(); + gchar *log_file = mainlogfile->str; size_t len = strlen(log_file); gchar *log_file_new = malloc(len + 4); int i = 1; @@ -241,10 +251,18 @@ _rotate_log_file(void) break; } + char *lf = strdup(mainlogfile->str); + char *start = strrchr(lf, '/') +1; + char *end = strstr(start, ".log"); + *end = '\0'; + log_close(); + rename(log_file, log_file_new); - log_init(log_get_filter()); + log_init(log_get_filter(), start); + + free(lf); free(log_file_new); free(log_file); log_info("Log has been rotated"); diff --git a/src/log.h b/src/log.h index 6b2f9764..32c616fa 100644 --- a/src/log.h +++ b/src/log.h @@ -53,7 +53,7 @@ typedef enum { PROF_OUT_LOG } chat_log_direction_t; -void log_init(log_level_t filter); +void log_init(log_level_t filter, char *log_file); log_level_t log_get_filter(void); void log_close(void); void log_reinit(void); diff --git a/src/main.c b/src/main.c index fff74785..33367743 100644 --- a/src/main.c +++ b/src/main.c @@ -61,6 +61,7 @@ static gboolean version = FALSE; static char *log = "INFO"; +static char *log_file = NULL; static char *account_name = NULL; static char *config_file = NULL; @@ -76,8 +77,9 @@ main(int argc, char **argv) { { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Show version information", NULL }, { "account", 'a', 0, G_OPTION_ARG_STRING, &account_name, "Auto connect to an account on startup" }, - { "log",'l', 0, G_OPTION_ARG_STRING, &log, "Set logging levels, DEBUG, INFO (default), WARN, ERROR", "LEVEL" }, - { "config",'c', 0, G_OPTION_ARG_STRING, &config_file, "Use an alternative configuration file", NULL }, + { "log", 'l', 0, G_OPTION_ARG_STRING, &log, "Set logging levels, DEBUG, INFO (default), WARN, ERROR", "LEVEL" }, + { "config", 'c', 0, G_OPTION_ARG_STRING, &config_file, "Use an alternative configuration file", NULL }, + { "logfile", 'f', 0, G_OPTION_ARG_STRING, &log_file, "Specify log filename", NULL }, { NULL } }; @@ -173,7 +175,7 @@ main(int argc, char **argv) return 0; } - prof_run(log, account_name, config_file); + prof_run(log, account_name, config_file, log_file); return 0; } diff --git a/src/profanity.c b/src/profanity.c index ab27bd46..68b20aff 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -86,7 +86,7 @@ #include "omemo/omemo.h" #endif -static void _init(char *log_level, char *config_file); +static void _init(char *log_level, char *config_file, char *log_file); static void _shutdown(void); static void _connect_default(const char * const account); @@ -94,9 +94,9 @@ static gboolean cont = TRUE; static gboolean force_quit = FALSE; void -prof_run(char *log_level, char *account_name, char *config_file) +prof_run(char *log_level, char *account_name, char *config_file, char *log_file) { - _init(log_level, config_file); + _init(log_level, config_file, log_file); plugins_on_start(); _connect_default(account_name); @@ -157,7 +157,7 @@ _connect_default(const char *const account) } static void -_init(char *log_level, char *config_file) +_init(char *log_level, char *config_file, char *log_file) { setlocale(LC_ALL, ""); // ignore SIGPIPE @@ -173,7 +173,7 @@ _init(char *log_level, char *config_file) files_create_directories(); log_level_t prof_log_level = log_level_from_string(log_level); prefs_load(config_file); - log_init(prof_log_level); + log_init(prof_log_level, log_file); log_stderr_init(PROF_LEVEL_ERROR); if (strcmp(PACKAGE_STATUS, "development") == 0) { #ifdef HAVE_GIT_VERSION diff --git a/src/profanity.h b/src/profanity.h index 988facfe..44a116e8 100644 --- a/src/profanity.h +++ b/src/profanity.h @@ -40,7 +40,7 @@ #include #include -void prof_run(char *log_level, char *account_name, char * config_file); +void prof_run(char *log_level, char *account_name, char *config_file, char *log_file); void prof_set_quit(void); pthread_mutex_t lock; -- cgit 1.4.1-2-gfad0