#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);
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,
GFile *file;
GFileOutputStream *stream;
+
+ guint rotation_size;
+ guint rotation_rotate;
+ gboolean compress;
};
G_DEFINE_TYPE (SolipaLog, solipa_log, G_TYPE_OBJECT)
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));
}
solipa_log_init (SolipaLog *solipa_log)
{
SolipaLogPrivate *priv = SOLIPA_LOG_GET_PRIVATE (solipa_log);
+
+ priv->file = NULL;
+ priv->stream = NULL;
}
/**
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;
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;
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,
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",
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);
+ }
}