about summary refs log tree commit diff stats
path: root/src/log.c
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2022-04-29 15:11:25 +0200
committerMichael Vetter <jubalh@iodoru.org>2022-04-29 19:19:01 +0200
commit82a2b17c1febf544a81cdb4958519489960bcc81 (patch)
tree0d6c67fc1c40b9a9c34fcae002400a776902f9c8 /src/log.c
parent188afc58da17f77692e44839eccd260321c7955c (diff)
downloadprofani-tty-82a2b17c1febf544a81cdb4958519489960bcc81.tar.gz
Use profanity.001.log instead of profanity.log.001 for rotated logs
Rotated log files will now be: `.local/share/profanity/logs/profanity.001.log`
We only use logrotation if we use the default log file ending with .log.
So Replacing the `.` should be fine.

User supplied log files are not rotated.
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/log.c b/src/log.c
index 191f8a84..8108151f 100644
--- a/src/log.c
+++ b/src/log.c
@@ -59,9 +59,6 @@
 static FILE* logp;
 static gchar* mainlogfile = NULL;
 static gboolean user_provided_log = FALSE;
-
-static GTimeZone* tz;
-static GDateTime* dt;
 static log_level_t level_filter;
 
 static GHashTable* logs;
@@ -150,7 +147,6 @@ void
 log_init(log_level_t filter, char* log_file)
 {
     level_filter = filter;
-    tz = g_time_zone_new_local();
 
     if (log_file) {
         user_provided_log = TRUE;
@@ -182,7 +178,6 @@ log_close(void)
 {
     g_free(mainlogfile);
     mainlogfile = NULL;
-    g_time_zone_unref(tz);
     if (logp) {
         fclose(logp);
     }
@@ -192,7 +187,7 @@ void
 log_msg(log_level_t level, const char* const area, const char* const msg)
 {
     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);
 
@@ -235,17 +230,27 @@ _rotate_log_file(void)
 {
     gchar* log_file = g_strdup(mainlogfile);
     size_t len = strlen(log_file);
-    gchar* log_file_new = malloc(len + 4);
+    gchar* log_file_new = malloc(len + 5);
+
+    // the mainlog file should always end in '.log', lets remove this last part
+    // so that we can have profanity.001.log later
+    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_file, i);
+        g_sprintf(log_file_new, "%s.%03d.log", log_file, i);
         if (!g_file_test(log_file_new, G_FILE_TEST_EXISTS))
             break;
     }
 
     log_close();
 
+    if (len > 4) {
+        log_file[len - 4] = '.';
+    }
+
     rename(log_file, log_file_new);
 
     log_init(log_get_filter(), NULL);