Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log SQLite queries. #561

Merged
merged 4 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Server/Components/Databases/database_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ IDatabaseResultSet* DatabaseConnection::executeQuery(StringView query)
if (ret)
{
// TODO: Properly handle errors
parentDatabasesComponent->logQuery("[log_sqlite_queries]: %.*s", PRINT_VIEW(query));
if (sqlite3_exec(databaseConnectionHandle, query.data(), queryStepExecuted, ret, nullptr) != SQLITE_OK)
{
parentDatabasesComponent->log(LogLevel::Error, "[log_sqlite]: Error executing query.");
parentDatabasesComponent->freeResultSet(*ret);
ret = nullptr;
}
}
else
{
parentDatabasesComponent->log(LogLevel::Error, "[log_sqlite]: Could not create SQLite result set.");
}
return ret;
}

Expand Down
31 changes: 30 additions & 1 deletion Server/Components/Databases/databases_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,36 @@ IDatabaseResultSet* DatabasesComponent::createResultSet()
/// Called for every component after components have been loaded
/// Should be used for storing the core interface, registering player/core event handlers
/// Should NOT be used for interacting with other components as they might not have been initialised yet
void DatabasesComponent::onLoad(ICore* c) { }
void DatabasesComponent::onLoad(ICore* c)
{
core_ = c;
logSQLite_ = core_->getConfig().getBool("logging.log_sqlite");
logSQLiteQueries_ = core_->getConfig().getBool("logging.log_sqlite_queries");
}

/// To optionally log things from connections.
void DatabasesComponent::log(LogLevel level, const char* fmt, ...) const
{
if (core_ && logSQLite_ && *logSQLite_)
{
va_list args;
va_start(args, fmt);
core_->vlogLn(level, fmt, args);
va_end(args);
}
}

/// To optionally log queries from connections.
void DatabasesComponent::logQuery(const char* fmt, ...) const
{
if (core_ && logSQLiteQueries_ && *logSQLiteQueries_)
{
va_list args;
va_start(args, fmt);
core_->vlogLn(LogLevel::Message, fmt, args);
va_end(args);
}
}

/// Opens a new database connection
/// @param path Path to the database
Expand Down
11 changes: 11 additions & 0 deletions Server/Components/Databases/databases_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class DatabasesComponent final : public IDatabasesComponent, public NoCopy
/// TODO: Replace with a pool type that grows dynamically
DynamicPoolStorage<DatabaseResultSet, IDatabaseResultSet, 1, 2049> databaseResultSets;

bool* logSQLite_;
bool* logSQLiteQueries_;

ICore* core_;

public:
/// Creates a result set
/// @returns Result set if successful, otherwise "nullptr"
Expand Down Expand Up @@ -100,6 +105,12 @@ class DatabasesComponent final : public IDatabasesComponent, public NoCopy
/// @returns Database result set
IDatabaseResultSet& getDatabaseResultSetByID(int databaseResultSetID) override;

/// To optionally log things from connections.
void log(LogLevel level, const char* fmt, ...) const;

/// To optionally log queries from connections.
void logQuery(const char* fmt, ...) const;

void free() override
{
delete this;
Expand Down