From: Andrea Zagli Date: Wed, 27 Nov 2019 20:18:15 +0000 (+0100) Subject: Added command line arguments for debug. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=39d78e260ac7bf4d091ca9e9fb264e584a86e848;p=zakaudit%2Flibzakaudit Added command line arguments for debug. --- diff --git a/src/audit.c b/src/audit.c index 5d3cc04..7a7e294 100644 --- a/src/audit.c +++ b/src/audit.c @@ -1,7 +1,7 @@ /* * audit.c * - * Copyright (C) 2005-2018 Andrea Zagli + * Copyright (C) 2005-2019 Andrea Zagli * * This file is part of libzakaudit. * @@ -36,6 +36,16 @@ #include "libzakaudit.h" +static guint debug; +static gchar *log_file; + +static GOptionEntry entries[] = +{ + { "zakaudit-debug-level", 0, 0, G_OPTION_ARG_INT, &debug, "Sets the debug level", NULL }, + { "zakaudit-log-file", 0, 0, G_OPTION_ARG_FILENAME, &log_file, "Path to file where to write debug info (or stdout or stderr)", NULL }, + { NULL } +}; + enum { COL_CHANGES_ID, @@ -134,6 +144,9 @@ struct _ZakAuditPrivate gchar *guidir; gchar *guifile; GtkBuilder *gtkbuilder; + + guint debug; + GFileOutputStream *log_file; }; /* PRIVATE */ @@ -182,6 +195,8 @@ zak_audit_init (ZakAudit *zak_audit) priv->datasources = NULL; priv->records_updated = NULL; priv->fields_updated = NULL; + priv->debug = 0; + priv->log_file = 0; } static ZakAudit @@ -984,6 +999,11 @@ zak_audit_action_v (ZakAudit *zak_audit, switch (action) { case ZAK_AUDIT_ACTION_INSERT: + if (priv->debug > 0) + { + g_message (_("Action_v INSERT")); + } + sql = g_strdup_printf ("SELECT %s FROM %s", table->keys_sql, table->name); @@ -1004,15 +1024,32 @@ zak_audit_action_v (ZakAudit *zak_audit, } sql = g_strconcat (sql, sql_where, NULL); + + if (priv->debug > 0) + { + g_message (_("Sql: %s"), sql); + } + dm = gdaex_query (datasource->gdaex, sql); g_free (sql); g_free (sql_where); - if (dm == NULL) return FALSE; + if (dm == NULL) + { + if (priv->debug > 0) + { + g_message (_("No record found")); + } + return FALSE; + } if (gda_data_model_get_n_rows (dm) == 0 || gda_data_model_get_n_rows (dm) > 1) { + if (priv->debug > 0) + { + g_message (_("None or more than one record found.")); + } return FALSE; } @@ -1028,6 +1065,11 @@ zak_audit_action_v (ZakAudit *zak_audit, break; case ZAK_AUDIT_ACTION_BEFORE_UPDATE: + if (priv->debug > 0) + { + g_message (_("Action_v UPDATE")); + } + if (priv->fields_updated != NULL) { g_hash_table_destroy (priv->fields_updated); @@ -1053,15 +1095,32 @@ zak_audit_action_v (ZakAudit *zak_audit, sql_where = g_strconcat (sql_where, " AND ", field_name, " = ", value, "", NULL); } sql = g_strconcat (sql, sql_where, NULL); + + if (priv->debug > 0) + { + g_message (_("Sql: %s"), sql); + } + dm = gdaex_query (datasource->gdaex, sql); g_free (sql); g_free (sql_where); - if (dm == NULL) return FALSE; + if (dm == NULL) + { + if (priv->debug > 0) + { + g_message (_("No record found")); + } + return FALSE; + } if (gda_data_model_get_n_rows (dm) == 0 || gda_data_model_get_n_rows (dm) > 1) { + if (priv->debug > 0) + { + g_message (_("None or more than one record found.")); + } return FALSE; } @@ -1077,6 +1136,11 @@ zak_audit_action_v (ZakAudit *zak_audit, break; case ZAK_AUDIT_ACTION_AFTER_UPDATE: + if (priv->debug > 0) + { + g_message (_("Action_v AFTER_UPDATE")); + } + if (priv->fields_updated == NULL) { g_warning (_("You must call before an action of type ZAK_AUDIT_ACTION_AFTER_UPDATE.")); @@ -1103,15 +1167,32 @@ zak_audit_action_v (ZakAudit *zak_audit, sql_where = g_strconcat (sql_where, " AND ", field_name, " = ", value, "", NULL); } sql = g_strconcat (sql, sql_where, NULL); + + if (priv->debug > 0) + { + g_message (_("Sql: %s"), sql); + } + dm = gdaex_query (datasource->gdaex, sql); g_free (sql); g_free (sql_where); - if (dm == NULL) return FALSE; + if (dm == NULL) + { + if (priv->debug > 0) + { + g_message (_("No record found")); + } + return FALSE; + } if (gda_data_model_get_n_rows (dm) == 0 || gda_data_model_get_n_rows (dm) > 1) { + if (priv->debug > 0) + { + g_message (_("None or more than one record found.")); + } return FALSE; } @@ -1140,6 +1221,11 @@ zak_audit_action_v (ZakAudit *zak_audit, break; case ZAK_AUDIT_ACTION_DELETE: + if (priv->debug > 0) + { + g_message (_("Action_v DELETE")); + } + /* saving all fields */ sql = g_strdup_printf ("SELECT %s FROM %s", table->fields_sql, @@ -1160,15 +1246,32 @@ zak_audit_action_v (ZakAudit *zak_audit, sql_where = g_strconcat (sql_where, " AND ", field_name, " = ", value, "", NULL); } sql = g_strconcat (sql, sql_where, NULL); + + if (priv->debug > 0) + { + g_message (_("Sql: %s"), sql); + } + dm = gdaex_query (datasource->gdaex, sql); g_free (sql); g_free (sql_where); - if (dm == NULL) return FALSE; + if (dm == NULL) + { + if (priv->debug > 0) + { + g_message (_("No record found")); + } + return FALSE; + } if (gda_data_model_get_n_rows (dm) == 0 || gda_data_model_get_n_rows (dm) > 1) { + if (priv->debug > 0) + { + g_message (_("None or more than one record found.")); + } return FALSE; } @@ -1448,6 +1551,8 @@ zak_audit_action_from_gdastatement (ZakAudit *zak_audit, gchar **values; + ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit); + ret = FALSE; str = g_string_new (""); @@ -1500,6 +1605,11 @@ zak_audit_action_from_gdastatement (ZakAudit *zak_audit, values = g_slist_next (values); } + if (priv->debug > 0) + { + g_message (_("Action from GdaStatement: INSERT")); + } + break; } @@ -1517,6 +1627,11 @@ zak_audit_action_from_gdastatement (ZakAudit *zak_audit, g_string_printf (str, "%s", strtmp); g_free (strtmp); + if (priv->debug > 0) + { + g_message (_("Action from GdaStatemente: UPDATE")); + } + break; } @@ -1534,6 +1649,11 @@ zak_audit_action_from_gdastatement (ZakAudit *zak_audit, g_string_printf (str, "%s", strtmp); g_free (strtmp); + if (priv->debug > 0) + { + g_message (_("Action from GdaStatement: DELETE")); + } + break; } @@ -2094,3 +2214,90 @@ zak_audit_get_record_at (ZakAudit *zak_audit, return TRUE; } + +static void +zak_audit_log_handler (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + GError *error; + + gchar *msg; + + ZakAuditPrivate *priv = ZAK_AUDIT_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 +zak_audit_post_parse_options (GOptionContext *context, + GOptionGroup *group, + gpointer user_data, + GError **error) +{ + ZakAuditPrivate *priv = ZAK_AUDIT_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, zak_audit_log_handler, user_data); + } + } + } + + return TRUE; +} + +/** + * zak_audit_get_option_group: + * #zak_audit: a #ZakAudit object. + * + * Returns: the #GOptionGroup. + */ +GOptionGroup +*zak_audit_get_option_group (ZakAudit *zak_audit) +{ + GOptionGroup *ret; + + ret = g_option_group_new ("zakaudit", "ZakAudit", "ZakAudit", (gpointer)zak_audit, g_free); + if (ret != NULL) + { + g_option_group_add_entries (ret, entries); + g_option_group_set_parse_hooks (ret, NULL, zak_audit_post_parse_options); + } + + return ret; +} diff --git a/src/libzakaudit.h b/src/libzakaudit.h index 0615150..26ad18d 100644 --- a/src/libzakaudit.h +++ b/src/libzakaudit.h @@ -1,10 +1,10 @@ /* * libzakaudit.h * - * Copyright (C) 2005-2015 Andrea Zagli + * Copyright (C) 2005-2019 Andrea Zagli * * This file is part of libzak_audit. - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -107,6 +107,8 @@ const gchar *zak_audit_get_user_insertion (ZakAudit *zak_audit, void zak_audit_destroy (ZakAudit *zak_audit); +GOptionGroup *zak_audit_get_option_group (ZakAudit *zak_audit); + G_END_DECLS