]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Log: aggiunto il rotate (solo 1).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 21 Aug 2014 14:01:25 +0000 (16:01 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 21 Aug 2014 14:01:25 +0000 (16:01 +0200)
src/log.c
tests/log.c

index 3e57e62fde34d0a1b013c1d27d4d113564c8cb63..e0da3ef2ca63f289055deaa69c3901ecafe349f1 100644 (file)
--- a/src/log.c
+++ b/src/log.c
 
 #include "log.h"
 
+enum
+{
+       PROP_0,
+       PROP_SIZE,
+       PROP_ROTATE,
+       PROP_COMPRESS
+};
+
 static void solipa_log_class_init (SolipaLogClass *class);
 static void solipa_log_init (SolipaLog *solipa_log);
 
@@ -37,6 +45,8 @@ static void solipa_log_get_property (GObject *object,
                                GValue *value,
                                GParamSpec *pspec);
 
+static gboolean solipa_log_create_file (SolipaLog *solipalog);
+
 static void solipa_log_handler (const gchar *log_domain,
                 GLogLevelFlags log_level,
                 const gchar *message,
@@ -54,6 +64,10 @@ struct _SolipaLogPrivate
 
                GFile *file;
                GFileOutputStream *stream;
+
+               guint rotation_size;
+               guint rotation_rotate;
+               gboolean compress;
        };
 
 G_DEFINE_TYPE (SolipaLog, solipa_log, G_TYPE_OBJECT)
@@ -66,6 +80,29 @@ solipa_log_class_init (SolipaLogClass *class)
        object_class->set_property = solipa_log_set_property;
        object_class->get_property = solipa_log_get_property;
 
+       g_object_class_install_property (object_class, PROP_SIZE,
+                                        g_param_spec_uint ("size",
+                                                           "Rotation file size",
+                                                           "Log files are rotated when they grow bigger then size bytes.",
+                                                           0,
+                                                           G_MAXUINT,
+                                                           2048000,
+                                                           G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+       g_object_class_install_property (object_class, PROP_ROTATE,
+                                        g_param_spec_uint ("rotate",
+                                                           "Number of file rotated",
+                                                           "Log files are rotated <count> times before being removed.",
+                                                           0,
+                                                           G_MAXUINT,
+                                                           4,
+                                                           G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+       g_object_class_install_property (object_class, PROP_COMPRESS,
+                                        g_param_spec_boolean ("compress",
+                                                              "If compress old files",
+                                                              "Old  versions  of log files are compressed with gzip.",
+                                                              TRUE,
+                                                              G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
        g_type_class_add_private (object_class, sizeof (SolipaLogPrivate));
 }
 
@@ -73,6 +110,9 @@ static void
 solipa_log_init (SolipaLog *solipa_log)
 {
        SolipaLogPrivate *priv = SOLIPA_LOG_GET_PRIVATE (solipa_log);
+
+       priv->file = NULL;
+       priv->stream = NULL;
 }
 
 /**
@@ -116,17 +156,8 @@ SolipaLog
                        return NULL;
                }
 
-       priv->file = g_file_new_for_path (g_build_filename (priv->dir, "log.log"));
-
-       error = NULL;
-       priv->stream = g_file_append_to (priv->file,
-                                        G_FILE_CREATE_PRIVATE,
-                                        NULL,
-                                        &error);
-       if (priv->stream == NULL || error != NULL)
+       if (!solipa_log_create_file (solipa_log))
                {
-                       g_warning ("SolipaLog: unable to create log file: %s.",
-                                  error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
                        g_free (priv->app_name);
                        g_free (priv->dir);
                        return NULL;
@@ -176,6 +207,18 @@ solipa_log_set_property (GObject *object,
 
        switch (property_id)
                {
+                       case PROP_SIZE:
+                               priv->rotation_size = g_value_get_uint (value);
+                               break;
+
+                       case PROP_ROTATE:
+                               priv->rotation_rotate = g_value_get_uint (value);
+                               break;
+
+                       case PROP_COMPRESS:
+                               priv->compress = g_value_get_boolean (value);
+                               break;
+
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;
@@ -193,12 +236,48 @@ solipa_log_get_property (GObject *object,
 
        switch (property_id)
                {
+                       case PROP_SIZE:
+                               g_value_set_uint (value, priv->rotation_size);
+                               break;
+
+                       case PROP_ROTATE:
+                               g_value_set_uint (value, priv->rotation_rotate);
+                               break;
+
+                       case PROP_COMPRESS:
+                               g_value_set_boolean (value, priv->compress);
+                               break;
+
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;
                }
 }
 
+static gboolean
+solipa_log_create_file (SolipaLog *solipalog)
+{
+       GError *error;
+
+       SolipaLogPrivate *priv = SOLIPA_LOG_GET_PRIVATE (solipalog);
+
+       priv->file = g_file_new_for_path (g_build_filename (priv->dir, "log.log", NULL));
+
+       error = NULL;
+       priv->stream = g_file_append_to (priv->file,
+                                        G_FILE_CREATE_PRIVATE,
+                                        NULL,
+                                        &error);
+       if (priv->stream == NULL || error != NULL)
+               {
+                       g_warning ("SolipaLog: unable to create log file: %s.",
+                                  error != NULL && error->message != NULL ? error->message : "no details");
+                       return FALSE;
+               }
+
+       return TRUE;
+}
+
 static void
 solipa_log_handler (const gchar *log_domain,
                 GLogLevelFlags log_level,
@@ -208,9 +287,19 @@ solipa_log_handler (const gchar *log_domain,
        GDateTime *gdt;
        gchar *buf;
 
+       GFileInfo *finfo;
+
        SolipaLog *solipa_log = (SolipaLog *)user_data;
+
+       g_return_if_fail (IS_SOLIPA_LOG (solipa_log));
+
        SolipaLogPrivate *priv = SOLIPA_LOG_GET_PRIVATE (solipa_log);
 
+       if (priv->stream == NULL)
+               {
+                       return;
+               }
+
        gdt = g_date_time_new_now_local ();
 
        buf = g_strdup_printf ("%s [%d] %s - %s\n",
@@ -219,5 +308,43 @@ solipa_log_handler (const gchar *log_domain,
 
        g_output_stream_write (G_OUTPUT_STREAM (priv->stream), buf, strlen (buf), NULL, NULL);
 
+       g_date_time_unref (gdt);
        g_free (buf);
+
+       /* controllo se deve essere ruotato */
+       finfo = g_file_query_info (priv->file,
+                                  G_FILE_ATTRIBUTE_STANDARD_SIZE,
+                                  G_FILE_QUERY_INFO_NONE,
+                                  NULL,
+                                  NULL);
+       if (finfo != NULL)
+               {
+                       if (g_file_info_get_size (finfo) > priv->rotation_size)
+                               {
+                                       GFile *fold;
+
+                                       fold = g_file_new_for_path (g_build_filename (g_file_get_path (g_file_get_parent (priv->file)), "log.log.0", NULL));
+                                       g_file_delete (fold, NULL, NULL);
+                                       g_object_unref (fold);
+
+                                       g_output_stream_close (G_OUTPUT_STREAM (priv->stream), NULL, NULL);
+                                       g_file_set_display_name (priv->file, "log.log.0", NULL, NULL);
+
+                                       if (priv->compress)
+                                               {
+                                                       GZlibCompressor *compress;
+
+                                                       compress = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP,
+                                                                                         -1);
+                                               }
+
+                                       g_object_unref (priv->file);
+
+                                       if (!solipa_log_create_file (solipa_log))
+                                               {
+                                                       g_object_unref (solipa_log);
+                                               }
+                               }
+                       g_object_unref (finfo);
+               }
 }
index ee972157ea22b76a9b973225f8b05cd2f7bfed2e..5843113e12e17657297f32c2f722fc75d4137042 100644 (file)
@@ -46,6 +46,8 @@ main (int argc, char **argv)
 
        solipa_log_add_logdomains (solipalog, "", "solipa_test_log", "GLib-GObject", "Solipa", NULL);
 
+       g_object_set (G_OBJECT (solipalog), "size", 800, NULL);
+
        error = NULL;
        connection = gda_connection_open_from_string ("PostgreSQL",
                                                   "DB_NAME=pippo",