1
0
Fork 0

Allow setting custom log file via -f FILENAME

`profanity -f TEST` will use `~/.local/share/profanity/logs/TEST.log` as
the log file.
This commit is contained in:
Michael Vetter 2020-02-21 21:10:00 +01:00
parent 99dc1c9494
commit 75cfe38808
7 changed files with 55 additions and 25 deletions

View File

@ -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);

View File

@ -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

View File

@ -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;
}
log_close();
rename(log_file, log_file_new);
log_init(log_get_filter());
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(), start);
free(lf);
free(log_file_new);
free(log_file);
log_info("Log has been rotated");

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -40,7 +40,7 @@
#include <pthread.h>
#include <glib.h>
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;