]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Log: aggiunta la scrittura del file.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 21 Aug 2014 09:50:24 +0000 (11:50 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 21 Aug 2014 09:50:24 +0000 (11:50 +0200)
src/log.c
tests/log.c

index 578d3f770b695a41e639a88cf9b0fbf646a250a5..3e57e62fde34d0a1b013c1d27d4d113564c8cb63 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -21,6 +21,7 @@
 #endif
 
 #include <stdio.h>
+#include <string.h>
 
 #include "log.h"
 
@@ -50,6 +51,9 @@ struct _SolipaLogPrivate
 
                gchar *app_name;
                gchar *dir;
+
+               GFile *file;
+               GFileOutputStream *stream;
        };
 
 G_DEFINE_TYPE (SolipaLog, solipa_log, G_TYPE_OBJECT)
@@ -86,6 +90,8 @@ SolipaLog
 
        gchar *_app_name;
 
+       GError *error;
+
        g_return_val_if_fail (IS_SOLIPA (solipa), NULL);
        g_return_val_if_fail (app_name != NULL, NULL);
 
@@ -110,6 +116,22 @@ 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)
+               {
+                       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;
+               }
+
        return solipa_log;
 }
 
@@ -120,7 +142,8 @@ solipa_log_add_logdomain (SolipaLog *solipalog, const gchar *log_domain)
 
        g_log_set_handler (g_strcmp0 (log_domain, "") == 0 ? NULL : log_domain,
                           G_LOG_LEVEL_MASK,
-                          solipa_log_handler, NULL);
+                          solipa_log_handler,
+                          (gpointer)solipalog);
 }
 
 void
@@ -182,5 +205,19 @@ solipa_log_handler (const gchar *log_domain,
                 const gchar *message,
                 gpointer user_data)
 {
-       g_printf ("solipa_log: (%d) %s - %s\n", log_level, log_domain, message);
+       GDateTime *gdt;
+       gchar *buf;
+
+       SolipaLog *solipa_log = (SolipaLog *)user_data;
+       SolipaLogPrivate *priv = SOLIPA_LOG_GET_PRIVATE (solipa_log);
+
+       gdt = g_date_time_new_now_local ();
+
+       buf = g_strdup_printf ("%s [%d] %s - %s\n",
+                              g_date_time_format (gdt, "%F %T"),
+                              log_level, log_domain, message);
+
+       g_output_stream_write (G_OUTPUT_STREAM (priv->stream), buf, strlen (buf), NULL, NULL);
+
+       g_free (buf);
 }
index e1f86dcffa793dd3eafbef95df1bf7a896847335..ee972157ea22b76a9b973225f8b05cd2f7bfed2e 100644 (file)
@@ -22,6 +22,8 @@
 #include <solipa.h>
 #include <log.h>
 
+#define G_LOG_DOMAIN "solipa_test_log"
+
 int
 main (int argc, char **argv)
 {
@@ -42,7 +44,7 @@ main (int argc, char **argv)
 
        solipalog = solipa_log_new (solipa, "test_log");
 
-       solipa_log_add_logdomains (solipalog, "", "GLib-GObject", "Solipa", NULL);
+       solipa_log_add_logdomains (solipalog, "", "solipa_test_log", "GLib-GObject", "Solipa", NULL);
 
        error = NULL;
        connection = gda_connection_open_from_string ("PostgreSQL",
@@ -67,5 +69,8 @@ main (int argc, char **argv)
        g_object_unref (parser);
        g_object_unref (connection);
 
+       g_object_unref (solipalog);
+       g_object_unref (solipa);
+
        return 0;
 }