Core/Commands: Enable SQL query logging by config and command )patch by leak)

Closes issue 4853.

--HG--
branch : trunk
This commit is contained in:
click
2010-11-19 13:13:07 +01:00
parent 67cd8d18e1
commit 67fade84d3
13 changed files with 90 additions and 56 deletions
+12 -11
View File
@@ -301,17 +301,18 @@ ChatCommand * ChatHandler::getCommandTable()
static ChatCommand serverCommandTable[] =
{
{ "corpses", SEC_GAMEMASTER, true, OldHandler<&ChatHandler::HandleServerCorpsesCommand>, "", NULL },
{ "exit", SEC_CONSOLE, true, OldHandler<&ChatHandler::HandleServerExitCommand>, "", NULL },
{ "idlerestart", SEC_ADMINISTRATOR, true, NULL, "", serverIdleRestartCommandTable },
{ "idleshutdown", SEC_ADMINISTRATOR, true, NULL, "", serverShutdownCommandTable },
{ "info", SEC_PLAYER, true, OldHandler<&ChatHandler::HandleServerInfoCommand>, "", NULL },
{ "motd", SEC_PLAYER, true, OldHandler<&ChatHandler::HandleServerMotdCommand>, "", NULL },
{ "plimit", SEC_ADMINISTRATOR, true, OldHandler<&ChatHandler::HandleServerPLimitCommand>, "", NULL },
{ "restart", SEC_ADMINISTRATOR, true, NULL, "", serverRestartCommandTable },
{ "shutdown", SEC_ADMINISTRATOR, true, NULL, "", serverShutdownCommandTable },
{ "set", SEC_ADMINISTRATOR, true, NULL, "", serverSetCommandTable },
{ NULL, 0, false, NULL, "", NULL }
{ "corpses", SEC_GAMEMASTER, true, OldHandler<&ChatHandler::HandleServerCorpsesCommand>, "", NULL },
{ "exit", SEC_CONSOLE, true, OldHandler<&ChatHandler::HandleServerExitCommand>, "", NULL },
{ "idlerestart", SEC_ADMINISTRATOR, true, NULL, "", serverIdleRestartCommandTable },
{ "idleshutdown", SEC_ADMINISTRATOR, true, NULL, "", serverShutdownCommandTable },
{ "info", SEC_PLAYER, true, OldHandler<&ChatHandler::HandleServerInfoCommand>, "", NULL },
{ "motd", SEC_PLAYER, true, OldHandler<&ChatHandler::HandleServerMotdCommand>, "", NULL },
{ "plimit", SEC_ADMINISTRATOR, true, OldHandler<&ChatHandler::HandleServerPLimitCommand>, "", NULL },
{ "restart", SEC_ADMINISTRATOR, true, NULL, "", serverRestartCommandTable },
{ "shutdown", SEC_ADMINISTRATOR, true, NULL, "", serverShutdownCommandTable },
{ "set", SEC_ADMINISTRATOR, true, NULL, "", serverSetCommandTable },
{ "togglequerylog", SEC_CONSOLE, true, OldHandler<&ChatHandler::HandleServerToggleQueryLogging>, "", NULL },
{ NULL, 0, false, NULL, "", NULL }
};
static ChatCommand unbanCommandTable[] =
+1
View File
@@ -236,6 +236,7 @@ class ChatHandler
bool HandleServerShutDownCommand(const char* args);
bool HandleServerShutDownCancelCommand(const char* args);
bool HandleServerSetClosedCommand(const char* args);
bool HandleServerToggleQueryLogging(const char* args);
bool HandleServerSetLogFileLevelCommand(const char* args);
bool HandleServerSetDiffTimeCommand(const char* args);
+3 -1
View File
@@ -760,7 +760,9 @@ enum TrinityStrings
LANG_CHARACTER_DELETED_SKIP_FULL = 1024,
LANG_CHARACTER_DELETED_SKIP_NAME = 1025,
LANG_CHARACTER_DELETED_LIST_LINE_CHAT = 1026,
// Room for more level 4 1027-1099 not used
LANG_SQLDRIVER_QUERY_LOGGING_ENABLED = 1027,
LANG_SQLDRIVER_QUERY_LOGGING_DISABLED = 1028,
// Room for more level 4 1029-1099 not used
// Level 3 (continue)
LANG_ACCOUNT_SETADDON = 1100,
@@ -254,17 +254,18 @@ class DatabaseWorkerPool
void CommitTransaction(SQLTransaction transaction)
{
#ifdef SQLQUERY_LOG
if (transaction->GetSize() == 0)
if (sLog.GetSQLDriverQueryLogging())
{
sLog.outSQLDriver("Transaction contains 0 queries. Not executing.");
return;
if (transaction->GetSize() == 0)
{
sLog.outSQLDriver("Transaction contains 0 queries. Not executing.");
return;
}
if (transaction->GetSize() == 1)
{
sLog.outSQLDriver("Warning: Transaction only holds 1 query, consider removing Transaction context in code.");
}
}
if (transaction->GetSize() == 1)
{
sLog.outSQLDriver("Warning: Transaction only holds 1 query, consider removing Transaction context in code.");
}
#endif
Enqueue(new TransactionTask(transaction));
}
+28 -27
View File
@@ -30,6 +30,7 @@
#include "PreparedStatement.h"
#include "DatabaseWorker.h"
#include "Timer.h"
#include "Log.h"
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
m_queue(NULL),
@@ -154,20 +155,19 @@ bool MySQLConnection::Execute(const char* sql)
return false;
{
#ifdef SQLQUERY_LOG
uint32 _s = getMSTime();
#endif
uint32 _s;
if (sLog.GetSQLDriverQueryLogging())
_s = getMSTime();
if (mysql_query(m_Mysql, sql))
{
sLog.outSQLDriver("SQL: %s", sql);
sLog.outSQLDriver("SQL ERROR: %s", mysql_error(m_Mysql));
return false;
}
else
else if (sLog.GetSQLDriverQueryLogging())
{
#ifdef SQLQUERY_LOG
sLog.outSQLDriver("[%u ms] SQL: %s", getMSTimeDiff(_s, getMSTime()), sql);
#endif
}
}
@@ -191,9 +191,10 @@ bool MySQLConnection::Execute(PreparedStatement* stmt)
MYSQL_STMT* msql_STMT = m_mStmt->GetSTMT();
MYSQL_BIND* msql_BIND = m_mStmt->GetBind();
#ifdef SQLQUERY_LOG
uint32 _s = getMSTime();
#endif
uint32 _s;
if (sLog.GetSQLDriverQueryLogging())
_s = getMSTime();
if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
{
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u, database: `%s`) error binding params: %s",
@@ -209,11 +210,11 @@ bool MySQLConnection::Execute(PreparedStatement* stmt)
m_mStmt->ClearParameters();
return false;
}
#ifdef SQLQUERY_LOG
sLog.outSQLDriver("[%u ms] Prepared SQL: %u on database `%s`",
getMSTimeDiff(_s, getMSTime()), index, m_connectionInfo.database.c_str());
#endif
if (sLog.GetSQLDriverQueryLogging())
sLog.outSQLDriver("[%u ms] Prepared SQL: %u on database `%s`",
getMSTimeDiff(_s, getMSTime()), index, m_connectionInfo.database.c_str());
m_mStmt->ClearParameters();
return true;
}
@@ -236,9 +237,10 @@ bool MySQLConnection::_Query(PreparedStatement* stmt, MYSQL_RES **pResult, uint6
MYSQL_STMT* msql_STMT = m_mStmt->GetSTMT();
MYSQL_BIND* msql_BIND = m_mStmt->GetBind();
#ifdef SQLQUERY_LOG
uint32 _s = getMSTime();
#endif
uint32 _s;
if (sLog.GetSQLDriverQueryLogging())
_s = getMSTime();
if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
{
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u, database: `%s`) error binding params: %s",
@@ -255,10 +257,10 @@ bool MySQLConnection::_Query(PreparedStatement* stmt, MYSQL_RES **pResult, uint6
return false;
}
#ifdef SQLQUERY_LOG
sLog.outSQLDriver("[%u ms] Prepared SQL: %u on database `%s`",
getMSTimeDiff(_s, getMSTime()), index, m_connectionInfo.database.c_str());
#endif
if (sLog.GetSQLDriverQueryLogging())
sLog.outSQLDriver("[%u ms] Prepared SQL: %u on database `%s`",
getMSTimeDiff(_s, getMSTime()), index, m_connectionInfo.database.c_str());
m_mStmt->ClearParameters();
*pResult = mysql_stmt_result_metadata(msql_STMT);
@@ -292,20 +294,19 @@ bool MySQLConnection::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD *
return false;
{
#ifdef SQLQUERY_LOG
uint32 _s = getMSTime();
#endif
uint32 _s;
if (sLog.GetSQLDriverQueryLogging())
_s = getMSTime();
if (mysql_query(m_Mysql, sql))
{
sLog.outSQLDriver("SQL: %s", sql);
sLog.outSQLDriver("query ERROR: %s", mysql_error(m_Mysql));
return false;
}
else
else if (sLog.GetSQLDriverQueryLogging())
{
#ifdef SQLQUERY_LOG
sLog.outSQLDriver("[%u ms] SQL: %s", getMSTimeDiff(_s,getMSTime()), sql);
#endif
}
*pResult = mysql_store_result(m_Mysql);
+1
View File
@@ -166,6 +166,7 @@ void Log::Initialize()
m_logLevel = sConfig.GetIntDefault("LogLevel", LOGL_NORMAL);
m_logFileLevel = sConfig.GetIntDefault("LogFileLevel", LOGL_NORMAL);
m_dbLogLevel = sConfig.GetIntDefault("DBLogLevel", LOGL_NORMAL);
m_sqlDriverQueryLogging = sConfig.GetBoolDefault("SQLDriverQueryLogging", false);
m_logFilter = 0;
+6
View File
@@ -118,6 +118,7 @@ class Log
void SetLogLevel(char * Level);
void SetLogFileLevel(char * Level);
void SetDBLogLevel(char * Level);
void SetSQLDriverQueryLogging(bool newStatus) { m_sqlDriverQueryLogging = newStatus; }
void SetRealmID(uint32 id) { realm = id; }
uint32 getLogFilter() const { return m_logFilter; }
@@ -128,6 +129,7 @@ class Log
bool GetLogDBLater() { return m_enableLogDBLater; }
void SetLogDB(bool enable) { m_enableLogDB = enable; }
void SetLogDBLater(bool value) { m_enableLogDBLater = value; }
bool GetSQLDriverQueryLogging() { return m_sqlDriverQueryLogging; }
private:
FILE* openLogFile(char const* configFileName,char const* configTimeStampFlag, char const* mode);
FILE* openGmlogPerAccount(uint32 account);
@@ -157,6 +159,10 @@ class Log
bool m_colored;
ColorTypes m_colors[4];
// log levels:
// false: errors only, true: full query logging
bool m_sqlDriverQueryLogging;
// log levels:
// 0 minimum/string, 1 basic/error, 2 detail, 3 full/debug
uint8 m_dbLogLevel;
@@ -530,6 +530,18 @@ bool ChatHandler::HandleServerSetDiffTimeCommand(const char *args)
return true;
}
/// toggle sql driver query logging
bool ChatHandler::HandleServerToggleQueryLogging(const char* /* args */)
{
sLog.SetSQLDriverQueryLogging(!sLog.GetSQLDriverQueryLogging());
if(sLog.GetSQLDriverQueryLogging())
PSendSysMessage(LANG_SQLDRIVER_QUERY_LOGGING_ENABLED);
else
PSendSysMessage(LANG_SQLDRIVER_QUERY_LOGGING_DISABLED);
return true;
}
/// @}
#ifdef linux
+7 -1
View File
@@ -405,9 +405,14 @@ CleanCharacterDB = 0
#
# SQLDriverLogFile
# Log file of SQL driver events.
# For effective query logging you need to build in debug configuration.
# For effective query logging you need to configure SQLDriverQueryLogging.
# Default: "" - Empty name for disable
#
# SQLDriverQueryLogging
# Log SQL queries to the SQLDriverLogFile and console
# Default: 0 - disabled (Query errors only)
# 1 - enabled (Full query logging - may have performance impact)
#
# LogColors
# Color for messages (format "normal basic detail debug")
# Default: "" - no colors
@@ -538,6 +543,7 @@ RaLogFile = "ra_commands.log"
ArenaLogFile = ""
ArenaLog.ExtendedInfo = 0
SQLDriverLogFile = ""
SQLDriverQueryLogging = 0
LogColors = ""
EnableLogDB = 0
DBLogLevel = 2