]> saetta.ns0.it Git - libgdaex/commitdiff
Added command line options for debug. debug
authorAndrea Zagli <azagli@libero.it>
Mon, 30 Aug 2010 15:54:47 +0000 (17:54 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 30 Aug 2010 15:54:47 +0000 (17:54 +0200)
configure.ac
src/gdaex.c
src/libgdaex.h
tests/test_prefix.c

index 572e5f18fadc925d22ed22ba52a545307c0d43af..1c1b691cdfe45b69bd85384147622d8d8b8ce838 100644 (file)
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ(2.59)
-AC_INIT([libgdaex], [0.2.1], [azagli@libero.it])
+AC_INIT([libgdaex], [0.2.2], [azagli@libero.it])
 AC_CONFIG_SRCDIR([src/gdaex.c])
 AM_CONFIG_HEADER([config.h])
 
@@ -27,7 +27,8 @@ AC_PROG_LIBTOOL
 GTK_DOC_CHECK(1.0)
 
 # Checks for libraries.
-PKG_CHECK_MODULES(GDAEX, [libgda-4.0 >= 4])
+PKG_CHECK_MODULES(GDAEX, [libgda-4.0 >= 4
+                          gio-2.0 >= 2.24])
 
 AC_SUBST(GDAEX_CFLAGS)
 AC_SUBST(GDAEX_LIBS)
index ae751a19fb6d9f933ae882e48b3e7d06f0fd99e1..a06f79693cc6cac994ff9dfa3704f887a2c1e9c3 100644 (file)
 #endif
 
 #include <string.h>
+
+#include <gio/gio.h>
 #include <libgda/sql-parser/gda-sql-parser.h>
 
 #include "libgdaex.h"
 
+static guint debug;
+static gchar *log_file;
+
+static GOptionEntry entries[] = 
+{
+       { "gdaex-debug-level", 0, 0, G_OPTION_ARG_INT, &debug, "Sets the debug level", NULL },
+       { "gdaex-log-file", 0, 0, G_OPTION_ARG_FILENAME, &log_file, "Path to file where to write debug info (or stdout or stderr)", NULL },
+       { NULL }
+};
+
 static void gdaex_class_init (GdaExClass *klass);
 static void gdaex_init (GdaEx *gdaex);
 
@@ -53,6 +65,9 @@ struct _GdaExPrivate
                GdaSqlParser *gda_parser;
 
                gchar *tables_name_prefix;
+
+               guint debug;
+               GFileOutputStream *log_file;
        };
 
 G_DEFINE_TYPE (GdaEx, gdaex, G_TYPE_OBJECT)
@@ -104,6 +119,10 @@ static void
 gdaex_init (GdaEx *gdaex)
 {
        GdaExPrivate *priv = GDAEX_GET_PRIVATE (gdaex);
+
+       priv->tables_name_prefix = NULL;
+       priv->debug = 0;
+       priv->log_file = 0;
 }
 
 static GdaEx
@@ -255,8 +274,8 @@ GdaEx
                                                       &error);
        if (error != NULL)
                {
-                       g_warning ("Error creating database connection: %s\n",
-                                  error->message);
+                       g_warning ("Error creating database connection: %s",
+                                  error->message != NULL ? error->message : "no details.");
                        return NULL;
                }
 
@@ -292,6 +311,93 @@ GdaEx
        return gdaex;
 }
 
+static void
+gdaex_log_handler (const gchar *log_domain,
+                   GLogLevelFlags log_level,
+                   const gchar *message,
+                   gpointer user_data)
+{
+       GError *error;
+
+       gchar *msg;
+
+       GdaExPrivate *priv = GDAEX_GET_PRIVATE ((GdaEx *)user_data);
+
+       msg = g_strdup_printf ("%s **: %s\n\n", log_domain, message);
+
+       if (g_output_stream_write (G_OUTPUT_STREAM (priv->log_file),
+           msg, strlen (msg), NULL, &error) < 0)
+               {
+                       g_warning ("Error on writing on log file: %s",
+                                  error != NULL && error->message != NULL ? error->message : "no details.");
+               }
+}
+
+static gboolean
+gdaex_post_parse_options (GOptionContext *context,
+                          GOptionGroup *group,
+                          gpointer user_data,
+                          GError **error)
+{
+       GdaExPrivate *priv = GDAEX_GET_PRIVATE ((GdaEx *)user_data);
+
+       GError *my_error;
+
+       priv->debug = debug;
+       if (log_file == NULL)
+               {
+                       priv->log_file = 0;
+               }
+       else if (priv->debug > 0)
+               {
+                       gchar *filename = g_strstrip (g_strdup (log_file));
+                       if (g_ascii_strncasecmp (filename, "stdout", 6) == 0
+                           || g_ascii_strncasecmp (filename, "stderr", 6) == 0)
+                               {
+                               }
+                       else
+                               {
+                                       my_error = NULL;
+                                       priv->log_file = g_file_replace (g_file_new_for_path (filename),
+                                                                        NULL, FALSE, G_FILE_CREATE_NONE, NULL, &my_error);
+                                       if (priv->log_file == NULL)
+                                               {
+                                                       g_warning ("Error on opening log file: %s.",
+                                                                  my_error != NULL && my_error->message != NULL ? my_error->message : "no details.");
+                                               }
+                                       else
+                                               {
+                                                       /* set handler */
+                                                       g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
+                                                                          | G_LOG_FLAG_RECURSION, gdaex_log_handler, user_data);
+                                               }
+                               }       
+               }
+
+       return TRUE;
+}
+
+/**
+ * gdaex_get_option_group:
+ * #gdaex: a #GdaEx object.
+ *
+ * Returns: the #GOptionGroup.
+ */
+GOptionGroup
+*gdaex_get_option_group (GdaEx *gdaex)
+{
+       GOptionGroup *ret;
+
+       ret = g_option_group_new ("gdaex", "GdaEx", "GdaEx", (gpointer)gdaex, g_free);
+       if (ret != NULL)
+               {
+                       g_option_group_add_entries (ret, entries);
+                       g_option_group_set_parse_hooks (ret, NULL, gdaex_post_parse_options);
+               }
+
+       return ret;
+}
+
 /**
  * gdaex_get_gdaconnection:
  * @gdaex: a #GdaEx object.
@@ -520,7 +626,7 @@ GdaDataModel
        stmt = gda_sql_parser_parse_string (priv->gda_parser, sql, NULL, &error);
        if (!GDA_IS_STATEMENT (stmt))
                {
-                       g_warning ("Error parsing query string: %s\n%s\n",
+                       g_warning ("Error parsing query string: %s\n%s",
                                   error != NULL && error->message != NULL ? error->message : "no details", sql);
                        return NULL;
                }
@@ -535,10 +641,17 @@ GdaDataModel
        GdaDataModel *dm = gda_connection_statement_execute_select (priv->gda_conn, stmt, NULL, &error);
        if (!GDA_IS_DATA_MODEL (dm))
                {
-                       g_warning ("Error executing selection query: %s\n%s\n",
+                       g_warning ("Error executing selection query: %s\n%s",
                                   error != NULL && error->message != NULL ? error->message : "no details", sql);
                        return NULL;
                }
+       else
+               {
+                       if (priv->debug > 0)
+                               {
+                                       g_message ("Selection query executed: %s", sql);
+                               }
+               }
 
        return dm;
 }
@@ -1201,6 +1314,13 @@ gdaex_begin (GdaEx *gdaex)
                        g_warning ("Error opening transaction: %s\n",
                                   error->message);
                }
+       else
+               {
+                       if (priv->debug > 0)
+                               {
+                                       g_message ("Transaction opened.");
+                               }
+               }
 
        return ret;
 }
@@ -1255,10 +1375,17 @@ gdaex_execute (GdaEx *gdaex, const gchar *sql)
 
        if (error != NULL)
                {
-                       g_warning ("Error executing command query: %s\n%s\n",
+                       g_warning ("Error executing command query: %s\n%s",
                                   error->message, sql);
                        return -1;
                }
+       else
+               {
+                       if (priv->debug > 0)
+                               {
+                                       g_message ("Query executed: %s", sql);
+                               }
+               }
 
        g_signal_emit (gdaex, klass->after_execute_signal_id, 0, stmt);
 
@@ -1290,6 +1417,10 @@ gdaex_commit (GdaEx *gdaex)
        if (tstatus == NULL)
                {
                        ret = TRUE;
+                       if (priv->debug > 0)
+                               {
+                                       g_message ("No transaction opened.");
+                               }
                }
        else
                {
@@ -1302,6 +1433,13 @@ gdaex_commit (GdaEx *gdaex)
                                                           error->message);
                                        ret = FALSE;
                                }
+                       else
+                               {
+                                       if (priv->debug > 0)
+                                               {
+                                                       g_message ("Transaction committed.");
+                                               }
+                               }
                }
 
        return ret;
@@ -1332,6 +1470,10 @@ gdaex_rollback (GdaEx *gdaex)
        if (tstatus == NULL)
                {
                        ret = TRUE;
+                       if (priv->debug > 0)
+                               {
+                                       g_message ("No transaction opened.");
+                               }
                }
        else
                {
@@ -1344,6 +1486,13 @@ gdaex_rollback (GdaEx *gdaex)
                                                           error->message);
                                        ret = FALSE;
                                }
+                       else
+                               {
+                                       if (priv->debug > 0)
+                                               {
+                                                       g_message ("Transaction rolled back.");
+                                               }
+                               }
                }
 
        return ret;
@@ -1362,6 +1511,12 @@ gdaex_free (GdaEx *gdaex)
                {
                        gda_connection_close (priv->gda_conn);
                }
+
+       if (priv->log_file != NULL)
+               {
+                       g_output_stream_close (G_OUTPUT_STREAM (priv->log_file), NULL, NULL);
+                       g_object_unref (priv->log_file);
+               }
 }
 
 /* UTILITY'S FUNCTIONS */
index 7658341344203ae643025a4c06e5520820473a8e..0a903b6ae45164290cbdb2934090188fcd25b967 100644 (file)
@@ -65,6 +65,8 @@ GdaEx *gdaex_new_from_dsn (const gchar *dsn,
 GdaEx *gdaex_new_from_string (const gchar *cnc_string);
 GdaEx *gdaex_new_from_connection (GdaConnection *conn);
 
+GOptionGroup *gdaex_get_option_group (GdaEx *gdaex);
+
 const GdaConnection *gdaex_get_gdaconnection (GdaEx *gdaex);
 const gchar *gdaex_get_provider (GdaEx *gdaex);
 
index 0772088ce4fff17e0936211b9b0f49804945562f..6bc18528f556c17b0a960c3b874a234794488423 100644 (file)
@@ -22,11 +22,22 @@ int
 main (int argc, char **argv)
 {
        GdaEx *gdaex;
+       GError *error;
+       GOptionContext *context;
 
        g_type_init ();
 
        gdaex = gdaex_new_from_string (g_strdup_printf ("SQLite://DB_DIR=%s;DB_NAME=test_prefix.db", TESTSDIR));
 
+       error = NULL;
+       context = g_option_context_new ("tests");
+       g_option_context_add_group (context, gdaex_get_option_group (gdaex));
+       g_option_context_parse (context, &argc, &argv, &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error on command line parsing: %s", error->message);
+               }
+
        gdaex_set_tables_name_prefix (gdaex, "paperinik_");
 
        gdaex_execute (gdaex, "INSERT INTO table1 (id) VALUES (1)");
@@ -43,5 +54,9 @@ main (int argc, char **argv)
 
        gdaex_query (gdaex, "SELECT * FROM table_sel1 AS t1 INNER JOIN table_sel2 ON t1.id = table_sel2.id2");
 
+       gdaex_query (gdaex, "SELECT * FROM table_sel1 AS t1");
+
+       gdaex_free (gdaex);
+
        return 0;
 }