From 90fdbf88607171b3500625aa596c259784bf6545 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Tue, 6 Feb 2018 13:42:26 +0200 Subject: [PATCH] MXS-1652 Add possibility to log SQL statements With the flag --debug=enable-statement-logging it is now possible to instruct MaxScale to log all SQL statements it sends to the servers. The format of the logged string looks like: notice : SQL(127.0.0.1): 0, "SELECT ..." First the fixed string "SQL", followed by the server address in parenthesis followed by the actual return value of mysql_query(), followed by the statement itself. The "SQL" string makes the lines easy to grep for and having the return value before the statement makes it easier to spot since the length of the return value string does not wary much, but the length of the statements do wary a lot. --- include/maxscale/mysql_utils.h | 15 ++++++++++++++ server/core/gateway.cc | 21 +++++++++++++++++++ server/core/mysql_utils.cc | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/include/maxscale/mysql_utils.h b/include/maxscale/mysql_utils.h index 4f6dd2059..5ad0b1c0b 100644 --- a/include/maxscale/mysql_utils.h +++ b/include/maxscale/mysql_utils.h @@ -124,4 +124,19 @@ mxs_mysql_name_kind_t mxs_mysql_name_to_pcre(char *pcre, */ void mxs_mysql_set_server_version(MYSQL* mysql, SERVER* server); +/** + * Enable/disable the logging of all SQL statements MaxScale sends to + * the servers. + * + * @param enable If true, enable, if false, disable. + */ +void mxs_mysql_set_log_statements(bool enable); + +/** + * Returns whether SQL statements sent to the servers are logged or not. + * + * @return True, if statements are logged, false otherwise. + */ +bool mxs_mysql_get_log_statements(); + MXS_END_DECLS diff --git a/server/core/gateway.cc b/server/core/gateway.cc index d2caf5695..9f37fdb51 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -188,6 +189,8 @@ static bool modules_process_init(); static void modules_process_finish(); static void disable_module_unloading(const char* arg); static void enable_module_unloading(const char* arg); +static void enable_statement_logging(const char* arg); +static void disable_statement_logging(const char* arg); static void redirect_output_to_file(const char* arg); static bool user_is_acceptable(const char* specified_user); static bool init_sqlite3(); @@ -217,6 +220,14 @@ const DEBUG_ARGUMENT debug_arguments[] = "redirect-output-to-file", redirect_output_to_file, "redirect stdout and stderr to the file given as an argument" }, + { + "enable-statement-logging", enable_statement_logging, + "enable the logging of SQL statements sent by MaxScale to the servers" + }, + { + "disable-statement-logging", disable_statement_logging, + "disable the logging of SQL statements sent by MaxScale to the servers" + }, {NULL, NULL, NULL} }; @@ -3185,6 +3196,16 @@ static void disable_module_unloading(const char* arg) unload_modules_at_exit = false; } +static void enable_statement_logging(const char* arg) +{ + mxs_mysql_set_log_statements(true); +} + +static void disable_statement_logging(const char* arg) +{ + mxs_mysql_set_log_statements(false); +} + static void redirect_output_to_file(const char* arg) { if (arg) diff --git a/server/core/mysql_utils.cc b/server/core/mysql_utils.cc index e49ef39b5..47548951f 100644 --- a/server/core/mysql_utils.cc +++ b/server/core/mysql_utils.cc @@ -32,6 +32,21 @@ #include #include +namespace +{ + +struct THIS_UNIT +{ + bool log_statements; // Should all statements sent to server be logged? +}; + +static THIS_UNIT this_unit = +{ + false +}; + +} + /** * @brief Calculate the length of a length-encoded integer in bytes * @@ -221,6 +236,19 @@ int mxs_mysql_query(MYSQL* conn, const char* query) rc = mysql_query(conn, query); } + if (this_unit.log_statements) + { + const char* host; + if (mariadb_get_info(conn, MARIADB_CONNECTION_HOST, &host) != 0) + { + // No idea about the host, but let's use something that looks like + // an IP-address as a placeholder. + host = "0.0.0.0"; + } + + MXS_NOTICE("SQL(%s): %d, \"%s\"", host, rc, query); + } + return rc; } @@ -368,3 +396,13 @@ void mxs_mysql_set_server_version(MYSQL* mysql, SERVER* server) server_set_version(server, version_string, version); } } + +void mxs_mysql_set_log_statements(bool enable) +{ + this_unit.log_statements = enable; +} + +bool mxs_mysql_get_log_statements() +{ + return this_unit.log_statements; +}