Skip to content
Open
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ add_subdirectory(yaml-cpp)

SET(CC_PLUGIN_DEFAULT "STATIC" CACHE STRING "want static" FORCE)


SET(INSTALL_PLUGINDIR "${CMAKE_CURRENT_BINARY_DIR}/mariadb-connector-c")
add_subdirectory(mariadb-connector-c)

CONFIGURE_FILE("${CMAKE_CURRENT_BINARY_DIR}/mariadb-connector-c/include/mariadb_version.h"
Expand Down
26 changes: 25 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <filesystem>
#include "yaml-cpp/yaml.h"
#include "logger.h"
#include <Windows.h>


namespace YAML {
Expand Down Expand Up @@ -41,8 +42,31 @@ ConfigDateType dateTypeFromString(std::string_view str) {
throw std::invalid_argument("Invalid value passed as parseDateTime entry");
}

std::filesystem::path Config::GetCurrentDLLPath() {
char path[MAX_PATH];
HMODULE hm = NULL;

if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)Config::initCommands, &hm) == 0)
{
int ret = GetLastError();
fprintf(stderr, "GetModuleHandle failed, error = %d\\n", ret);
// Return or however you want to handle an error.
}
if (GetModuleFileName(hm, path, sizeof(path)) == 0)
{
int ret = GetLastError();
fprintf(stderr, "GetModuleFileName failed, error = %d\\n", ret);
// Return or however you want to handle an error.
}

return std::filesystem::path(path);
}

void Config::reloadConfig() {
std::filesystem::path configFilePath("@InterceptDB/config.yaml");

std::filesystem::path configFilePath(Config::GetCurrentDLLPath().parent_path().parent_path() / "config.yaml");

if (!std::filesystem::exists(configFilePath))
throw std::filesystem::filesystem_error("File not found", configFilePath, std::error_code());
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ConfigStatement {
class Config : public intercept::singleton<Config> {

public:
std::filesystem::path GetCurrentDLLPath();
void reloadConfig();

ConfigStatement getStatement(r_string name) {
Expand Down
44 changes: 42 additions & 2 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

#include <fstream>
#include <ctime>
#include <iostream>
#include <chrono>
#include <filesystem>

void Logger::init(std::filesystem::path outputDir) {
std::filesystem::create_directories(outputDir);
outputDirectory = std::move(outputDir);
deleteOldLogs(10);
refreshLogfiles();
}

Expand Down Expand Up @@ -40,18 +44,54 @@ void Logger::pushTimestamp(std::ostream& str) const {
}

void Logger::refreshLogfiles() {
// Get the current timestamp
auto currentTime = std::chrono::system_clock::now();
auto timeT = std::chrono::system_clock::to_time_t(currentTime);

char timestampBuffer[20];
strftime(timestampBuffer, sizeof(timestampBuffer), "%Y%m%d%H%M%S", localtime(&timeT));

std::string currentTimeString = timestampBuffer;


// Add the timestamp to the log file names
std::string queryLogFileName = "query_" + currentTimeString + ".log";
std::string threadLogFileName = "thread_" + currentTimeString + ".log";

{
std::unique_lock lock(queryLog_lock);
if (queryLog) queryLog.reset();

if (queryLogEnabled)
queryLog = std::make_unique<std::ofstream>(outputDirectory/"query.log", std::ofstream::app);
queryLog = std::make_unique<std::ofstream>(outputDirectory / queryLogFileName, std::ofstream::app);
}
{
std::unique_lock lock(threadLog_lock);
if (threadLog) threadLog.reset();

if (threadLogEnabled)
threadLog = std::make_unique<std::ofstream>(outputDirectory/"thread.log", std::ofstream::app);
threadLog = std::make_unique<std::ofstream>(outputDirectory / threadLogFileName, std::ofstream::app);
}
}


void Logger::deleteOldLogs(int days) {
std::filesystem::path& path = outputDirectory;
try {
auto now = std::filesystem::file_time_type::clock::now();
auto duration = std::chrono::hours(24 * days);
for (auto& p : std::filesystem::directory_iterator(path)) {
if (p.is_regular_file() && p.path().extension() == ".log") {
auto last_modified = std::filesystem::last_write_time(p);
if (now - last_modified > duration) {
std::filesystem::remove(p);
std::cout << "Deleted: " << p.path() << '\n';
}
}
}
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
}
}

1 change: 1 addition & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Logger : public intercept::singleton<Logger> {
private:
void pushTimestamp(std::ostream& str) const;
void refreshLogfiles();
void deleteOldLogs(int days);

std::mutex queryLog_lock;
bool queryLogEnabled = false;
Expand Down