about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-11-11 14:56:04 -0800
committerJames Booth <boothj5@gmail.com>2012-11-11 14:56:04 -0800
commit2f1fa0d97a947ebeeb9d195a9e9bcd9f4d867e1b (patch)
tree11178bcc122e053420d0f9c9a712306e176494c1 /src
parente64889b6fc8c20d21328eebb1abbb82f9a3bf597 (diff)
parent291c6bc3d9f29372dcc01acdab387fafaff0a232 (diff)
downloadprofani-tty-2f1fa0d97a947ebeeb9d195a9e9bcd9f4d867e1b.tar.gz
Merge pull request #73 from pasis/rotate
introduce initial log rotate support
Diffstat (limited to 'src')
-rw-r--r--src/log.c57
-rw-r--r--src/preferences.c7
-rw-r--r--src/preferences.h3
-rw-r--r--src/profanity.c2
4 files changed, 64 insertions, 5 deletions
diff --git a/src/log.c b/src/log.c
index 5265e247..e1cbddb7 100644
--- a/src/log.c
+++ b/src/log.c
@@ -22,20 +22,32 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "glib.h"
 
 #include "common.h"
 #include "log.h"
+#include "preferences.h"
 
 #define PROF "prof"
 
+#ifdef _WIN32
+// replace 'struct stat' and 'stat()' for windows
+#define stat _stat
+#endif
+
 static FILE *logp;
 
 static GTimeZone *tz;
 static GDateTime *dt;
 static log_level_t level_filter;
 
+static GString *_get_log_file(void);
+static void _rotate_log_file(void);
+
 void
 log_debug(const char * const msg, ...)
 {
@@ -89,10 +101,7 @@ log_init(log_level_t filter)
 {
     level_filter = filter;
     tz = g_time_zone_new_local();
-    GString *log_file = g_string_new(getenv("HOME"));
-    g_string_append(log_file, "/.profanity/log");
-    create_dir(log_file->str);
-    g_string_append(log_file, "/profanity.log");
+    GString *log_file = _get_log_file();
     logp = fopen(log_file->str, "a");
     g_string_free(log_file, TRUE);
 }
@@ -114,6 +123,9 @@ void
 log_msg(log_level_t level, const char * const area, const char * const msg)
 {
     if (level >= level_filter) {
+        struct stat st;
+        int result;
+        GString *log_file = _get_log_file();
         dt = g_date_time_new_now(tz);
 
         gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S");
@@ -122,6 +134,43 @@ log_msg(log_level_t level, const char * const area, const char * const msg)
 
         fflush(logp);
         g_free(date_fmt);
+
+        result = stat(log_file->str, &st);
+        if (result == 0 && st.st_size >= prefs_get_max_log_size()) {
+            _rotate_log_file();
+        }
+
+        g_string_free(log_file, TRUE);
     }
 }
 
+static GString *
+_get_log_file(void)
+{
+    GString *log_file = g_string_new(getenv("HOME"));
+    g_string_append(log_file, "/.profanity/log");
+    create_dir(log_file->str);
+    g_string_append(log_file, "/profanity.log");
+
+    return log_file;
+}
+static void
+_rotate_log_file(void)
+{
+    GString *log_file = _get_log_file();
+    size_t len = strlen(log_file->str);
+    char *log_file_new = malloc(len + 3);
+
+    strncpy(log_file_new, log_file->str, len);
+    log_file_new[len] = '.';
+    log_file_new[len+1] = '1';
+    log_file_new[len+2] = 0;
+
+    log_close();
+    rename(log_file->str, log_file_new);
+    log_init(log_get_filter());
+
+    free(log_file_new);
+    g_string_free(log_file, TRUE);
+    log_info("Log has been rotated");
+}
diff --git a/src/preferences.c b/src/preferences.c
index e6275bbc..44e8c94f 100644
--- a/src/preferences.c
+++ b/src/preferences.c
@@ -300,6 +300,13 @@ prefs_set_notify_remind(gint value)
     _save_prefs();
 }
 
+gint
+prefs_get_max_log_size(void)
+{
+    /* TODO: make command and field in config file */
+    return PREFS_MAX_LOG_SIZE;
+}
+
 gboolean
 prefs_get_vercheck(void)
 {
diff --git a/src/preferences.h b/src/preferences.h
index e2975c51..be29e8bc 100644
--- a/src/preferences.h
+++ b/src/preferences.h
@@ -34,6 +34,8 @@
 #include <ncurses/ncurses.h>
 #endif
 
+#define PREFS_MAX_LOG_SIZE 1048580
+
 void prefs_load(void);
 void prefs_close(void);
 
@@ -67,6 +69,7 @@ void prefs_set_notify_typing(gboolean value);
 gboolean prefs_get_notify_typing(void);
 void prefs_set_notify_remind(gint period);
 gint prefs_get_notify_remind(void);
+gint prefs_get_max_log_size(void);
 
 void prefs_add_login(const char *jid);
 
diff --git a/src/profanity.c b/src/profanity.c
index 434a6c3a..6b59f18c 100644
--- a/src/profanity.c
+++ b/src/profanity.c
@@ -169,7 +169,7 @@ prof_handle_subscription(const char *from, jabber_subscr_t type)
         break;
     default:
         /* unknown type */
-	break;
+        break;
     }
 }