1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Merge pull request #1455 from profanity-im/fix/1442-logging

Use whole path as logfile when defined via -f
This commit is contained in:
Michael Vetter 2020-12-11 11:52:36 +01:00 committed by GitHub
commit 880641528a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 31 deletions

View File

@ -1857,9 +1857,9 @@ static struct cmd_t command_defs[] = {
"Manage profanity log settings.") "Manage profanity log settings.")
CMD_ARGS( CMD_ARGS(
{ "where", "Show the current log file location." }, { "where", "Show the current log file location." },
{ "rotate on|off", "Rotate log, default on." }, { "rotate on|off", "Rotate log, default on. Does not take effect if you specified a filename yourself when starting Profanity." },
{ "maxsize <bytes>", "With rotate enabled, specifies the max log size, defaults to 1048580 (1MB)." }, { "maxsize <bytes>", "With rotate enabled, specifies the max log size, defaults to 1048580 (1MB)." },
{ "shared on|off", "Share logs between all instances, default: on. When off, the process id will be included in the log filename." }) { "shared on|off", "Share logs between all instances, default: on. When off, the process id will be included in the log filename. Does not take effect if you specified a filename yourself when starting Profanity." })
CMD_NOEXAMPLES CMD_NOEXAMPLES
}, },

View File

@ -6375,7 +6375,7 @@ cmd_log(ProfWin* window, const char* const command, gchar** args)
return TRUE; return TRUE;
} }
_cmd_set_boolean_preference(value, command, "Shared log", PREF_LOG_SHARED); _cmd_set_boolean_preference(value, command, "Shared log", PREF_LOG_SHARED);
log_reinit(); cons_show("Setting only takes effect after saving and restarting Profanity.");
return TRUE; return TRUE;
} }

View File

@ -116,20 +116,26 @@ char*
files_get_log_file(const char* const log_file) files_get_log_file(const char* const log_file)
{ {
gchar* xdg_data = _files_get_xdg_data_home(); gchar* xdg_data = _files_get_xdg_data_home();
GString* logfile = g_string_new(xdg_data); GString* logfile;
if (log_file) { if (log_file) {
g_string_append(logfile, "/profanity/logs/"); gchar *log_path = g_path_get_dirname(log_file);
g_string_append(logfile, log_file); if (!mkdir_recursive(log_path)) {
} else { log_error("Error while creating directory %s", log_path);
g_string_append(logfile, "/profanity/logs/profanity");
} }
g_free(log_path);
logfile = g_string_new(log_file);
} else {
logfile = g_string_new(xdg_data);
g_string_append(logfile, "/profanity/logs/profanity");
if (!prefs_get_boolean(PREF_LOG_SHARED)) { if (!prefs_get_boolean(PREF_LOG_SHARED)) {
g_string_append_printf(logfile, "%d", getpid()); g_string_append_printf(logfile, "%d", getpid());
} }
g_string_append(logfile, ".log"); g_string_append(logfile, ".log");
}
char* result = g_strdup(logfile->str); char* result = g_strdup(logfile->str);

View File

@ -56,6 +56,7 @@
static FILE* logp; static FILE* logp;
static gchar* mainlogfile = NULL; static gchar* mainlogfile = NULL;
static gboolean user_provided_log = FALSE;
static GTimeZone* tz; static GTimeZone* tz;
static GDateTime* dt; static GDateTime* dt;
@ -150,6 +151,11 @@ log_init(log_level_t filter, char* log_file)
{ {
level_filter = filter; level_filter = filter;
tz = g_time_zone_new_local(); tz = g_time_zone_new_local();
if (log_file) {
user_provided_log = TRUE;
}
gchar* lf = files_get_log_file(log_file); gchar* lf = files_get_log_file(log_file);
logp = fopen(lf, "a"); logp = fopen(lf, "a");
@ -159,20 +165,6 @@ log_init(log_level_t filter, char* log_file)
g_free(lf); g_free(lf);
} }
void
log_reinit(void)
{
char* lf = strdup(mainlogfile);
char* start = strrchr(lf, '/') + 1;
char* end = strstr(start, ".log");
*end = '\0';
log_close();
log_init(level_filter, start);
free(lf);
}
const char* const char*
get_log_file_location(void) get_log_file_location(void)
{ {
@ -212,7 +204,7 @@ log_msg(log_level_t level, const char* const area, const char* const msg)
fflush(logp); fflush(logp);
g_free(date_fmt); g_free(date_fmt);
if (prefs_get_boolean(PREF_LOG_ROTATE)) { if (prefs_get_boolean(PREF_LOG_ROTATE) && !user_provided_log) {
long result = ftell(logp); long result = ftell(logp);
if (result != -1 && result >= prefs_get_max_log_size()) { if (result != -1 && result >= prefs_get_max_log_size()) {
_rotate_log_file(); _rotate_log_file();

View File

@ -56,7 +56,6 @@ typedef enum {
void log_init(log_level_t filter, char* log_file); void log_init(log_level_t filter, char* log_file);
log_level_t log_get_filter(void); log_level_t log_get_filter(void);
void log_close(void); void log_close(void);
void log_reinit(void);
const char* get_log_file_location(void); const char* get_log_file_location(void);
void log_debug(const char* const msg, ...); void log_debug(const char* const msg, ...);
void log_info(const char* const msg, ...); void log_info(const char* const msg, ...);

View File

@ -36,10 +36,6 @@ log_get_filter(void)
return mock_type(log_level_t); return mock_type(log_level_t);
} }
void
log_reinit(void)
{
}
void void
log_close(void) log_close(void)
{ {