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

Merge pull request #1701 from profanity-im/log-changes

Log file improvements
This commit is contained in:
Michael Vetter 2022-04-29 19:38:51 +02:00 committed by GitHub
commit 20e6577114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View File

@ -42,7 +42,7 @@
#include <glib.h> #include <glib.h>
#define PREFS_MIN_LOG_SIZE 64 #define PREFS_MIN_LOG_SIZE 64
#define PREFS_MAX_LOG_SIZE 1048580 #define PREFS_MAX_LOG_SIZE (10 * 1024 * 1024)
// represents all settings in .profrc // represents all settings in .profrc
// each enum value is mapped to a group and key in .profrc (see preferences.c) // each enum value is mapped to a group and key in .profrc (see preferences.c)

View File

@ -59,9 +59,6 @@
static FILE* logp; static FILE* logp;
static gchar* mainlogfile = NULL; static gchar* mainlogfile = NULL;
static gboolean user_provided_log = FALSE; static gboolean user_provided_log = FALSE;
static GTimeZone* tz;
static GDateTime* dt;
static log_level_t level_filter; static log_level_t level_filter;
static GHashTable* logs; static GHashTable* logs;
@ -150,7 +147,6 @@ void
log_init(log_level_t filter, char* log_file) log_init(log_level_t filter, char* log_file)
{ {
level_filter = filter; level_filter = filter;
tz = g_time_zone_new_local();
if (log_file) { if (log_file) {
user_provided_log = TRUE; user_provided_log = TRUE;
@ -182,7 +178,6 @@ log_close(void)
{ {
g_free(mainlogfile); g_free(mainlogfile);
mainlogfile = NULL; mainlogfile = NULL;
g_time_zone_unref(tz);
if (logp) { if (logp) {
fclose(logp); fclose(logp);
} }
@ -192,7 +187,7 @@ void
log_msg(log_level_t level, const char* const area, const char* const msg) log_msg(log_level_t level, const char* const area, const char* const msg)
{ {
if (level >= level_filter && logp) { if (level >= level_filter && logp) {
dt = g_date_time_new_now(tz); GDateTime* dt = g_date_time_new_now_local();
char* level_str = _log_string_from_level(level); char* level_str = _log_string_from_level(level);
@ -235,17 +230,27 @@ _rotate_log_file(void)
{ {
gchar* log_file = g_strdup(mainlogfile); gchar* log_file = g_strdup(mainlogfile);
size_t len = strlen(log_file); size_t len = strlen(log_file);
gchar* log_file_new = malloc(len + 4); gchar* log_file_new = malloc(len + 5);
// find an empty name. from .log -> log.01 -> log.99 // the mainlog file should always end in '.log', lets remove this last part
for (int i = 1; i < 100; i++) { // so that we can have profanity.001.log later
g_sprintf(log_file_new, "%s.%02d", log_file, i); if (len > 4) {
log_file[len - 4] = '\0';
}
// find an empty name. from .log -> log.001 -> log.999
for (int i = 1; i < 1000; i++) {
g_sprintf(log_file_new, "%s.%03d.log", log_file, i);
if (!g_file_test(log_file_new, G_FILE_TEST_EXISTS)) if (!g_file_test(log_file_new, G_FILE_TEST_EXISTS))
break; break;
} }
log_close(); log_close();
if (len > 4) {
log_file[len - 4] = '.';
}
rename(log_file, log_file_new); rename(log_file, log_file_new);
log_init(log_get_filter(), NULL); log_init(log_get_filter(), NULL);