From: Andrea Zagli Date: Thu, 21 Aug 2014 09:50:24 +0000 (+0200) Subject: Log: aggiunta la scrittura del file. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=26fd02df03bb3073f4db67f1e4ddf9e44f51d65d;p=solipa%2Flibsolipa Log: aggiunta la scrittura del file. --- diff --git a/src/log.c b/src/log.c index 578d3f7..3e57e62 100644 --- a/src/log.c +++ b/src/log.c @@ -21,6 +21,7 @@ #endif #include +#include #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); } diff --git a/tests/log.c b/tests/log.c index e1f86dc..ee97215 100644 --- a/tests/log.c +++ b/tests/log.c @@ -22,6 +22,8 @@ #include #include +#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; }