Skip to content

Commit

Permalink
second attempt to sql_auditing
Browse files Browse the repository at this point in the history
  • Loading branch information
juandent committed Dec 19, 2024
1 parent dd9e4c6 commit 4a9d67e
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 40 deletions.
88 changes: 68 additions & 20 deletions dev/sql_auditing.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,83 @@
#include <iomanip> // For std::put_time
#include <string>

#include "sql_auditing.h"

enum class auditing_behavior : signed char { OFF = 0, ON = 1 };

class sql_auditor_settings;

class sql_auditor {

std::ofstream log_file;
sql_auditor(std::string path) {
using namespace std;
log_file.open(path, ios::trunc | ios::out);
if(!log_file.good()) {
throw std::system_error{sqlite_orm::orm_error_code::failure_to_init_logfile};
}
}
friend class sql_auditor_settings;
sql_auditor();
void open();
inline static sql_auditor& auditor() {
static sql_auditor auditor{"sql_auditor.txt"};
static sql_auditor auditor{};
return auditor;
}

public:
static void log(const std::string& message) {
// would use format if C++ 20
auto now = std::chrono::system_clock::now();
static void log(const std::string& message);
};

std::time_t now_time = std::chrono::system_clock::to_time_t(now);
class sql_auditor_settings {
std::string destination_file = "sql_auditor.txt";
friend class sql_auditor;
sql_auditor_settings() {}
inline static sql_auditor_settings& settings() {
static sql_auditor_settings sql_settings;
return sql_settings;
}
auditing_behavior behavior = auditing_behavior::ON;
std::string format_str = "%Y-%m-%d %H:%M:%S";

public:
static void set_destination_file(const std::string& filename);
static void set_behavior(auditing_behavior behavior) {
settings().behavior = behavior;
}
static void set_format(const std::string& format_str) {
settings().format_str = format_str;
}
};

// Convert to local time (std::tm structure)
// WARNING: localtime is not thread safe!
std::tm local_time = *std::localtime(&now_time);
inline sql_auditor::sql_auditor() {
open();
}

// Print the local time in a human-readable format
auditor().log_file << "@: " << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S") // Custom format
<< " = ";
inline void sql_auditor_settings::set_destination_file(const std::string& filename) {
settings().destination_file = filename;
sql_auditor::auditor().open();
}

auditor().log_file << message << std::endl;
inline void sql_auditor::open() {
using namespace std;
if(!log_file.is_open()) {
log_file.open(sql_auditor_settings::settings().destination_file, ios::trunc | ios::out);
if(!log_file.good()) {
throw std::system_error{sqlite_orm::orm_error_code::failure_to_init_logfile};
}
}
};
}

inline void sql_auditor::log(const std::string& message) {
// guard and exit if off
if(sql_auditor_settings::settings().behavior == auditing_behavior::OFF)
return;

// would use format if C++ 20
auto now = std::chrono::system_clock::now();

std::time_t now_time = std::chrono::system_clock::to_time_t(now);

// Convert to local time (std::tm structure)
// WARNING: localtime is not thread safe!
std::tm local_time = *std::localtime(&now_time);

// Print the local time in a human-readable format
auditor().log_file << "@: " << std::put_time(&local_time, sql_auditor_settings::settings().format_str.c_str())
<< " = ";
auditor().log_file << message << std::endl;
}
88 changes: 68 additions & 20 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13448,38 +13448,86 @@ namespace sqlite_orm {
#include <iomanip> // For std::put_time
#include <string>

// #include "sql_auditing.h"

enum class auditing_behavior : signed char { OFF = 0, ON = 1 };

class sql_auditor_settings;

class sql_auditor {

std::ofstream log_file;
sql_auditor(std::string path) {
using namespace std;
log_file.open(path, ios::trunc | ios::out);
if(!log_file.good()) {
throw std::system_error{sqlite_orm::orm_error_code::failure_to_init_logfile};
}
}
friend class sql_auditor_settings;
sql_auditor();
void open();
inline static sql_auditor& auditor() {
static sql_auditor auditor{"sql_auditor.txt"};
static sql_auditor auditor{};
return auditor;
}

public:
static void log(const std::string& message) {
// would use format if C++ 20
auto now = std::chrono::system_clock::now();
static void log(const std::string& message);
};

std::time_t now_time = std::chrono::system_clock::to_time_t(now);
class sql_auditor_settings {
std::string destination_file = "sql_auditor.txt";
friend class sql_auditor;
sql_auditor_settings() {}
inline static sql_auditor_settings& settings() {
static sql_auditor_settings sql_settings;
return sql_settings;
}
auditing_behavior behavior = auditing_behavior::ON;
std::string format_str = "%Y-%m-%d %H:%M:%S";

// Convert to local time (std::tm structure)
// WARNING: localtime is not thread safe!
std::tm local_time = *std::localtime(&now_time);
public:
static void set_destination_file(const std::string& filename);
static void set_behavior(auditing_behavior behavior) {
settings().behavior = behavior;
}
static void set_format(const std::string& format_str) {
settings().format_str = format_str;
}
};

// Print the local time in a human-readable format
auditor().log_file << "@: " << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S") // Custom format
<< " = ";
inline sql_auditor::sql_auditor() {
open();
}

inline void sql_auditor_settings::set_destination_file(const std::string& filename) {
settings().destination_file = filename;
sql_auditor::auditor().open();
}

auditor().log_file << message << std::endl;
inline void sql_auditor::open() {
using namespace std;
if(!log_file.is_open()) {
log_file.open(sql_auditor_settings::settings().destination_file, ios::trunc | ios::out);
if(!log_file.good()) {
throw std::system_error{sqlite_orm::orm_error_code::failure_to_init_logfile};
}
}
};
}

inline void sql_auditor::log(const std::string& message) {
// guard and exit if off
if(sql_auditor_settings::settings().behavior == auditing_behavior::OFF)
return;

// would use format if C++ 20
auto now = std::chrono::system_clock::now();

std::time_t now_time = std::chrono::system_clock::to_time_t(now);

// Convert to local time (std::tm structure)
// WARNING: localtime is not thread safe!
std::tm local_time = *std::localtime(&now_time);

// Print the local time in a human-readable format
auditor().log_file << "@: " << std::put_time(&local_time, sql_auditor_settings::settings().format_str.c_str())
<< " = ";
auditor().log_file << message << std::endl;
}

namespace sqlite_orm {

Expand Down
7 changes: 7 additions & 0 deletions tests/prepared_statement_tests/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ TEST_CASE("Prepared select") {

auto filename = "prepared.sqlite";
remove(filename);

#define JD_AUDITING_SETTINGS
#ifdef JD_AUDITING_SETTINGS
sql_auditor_settings::set_destination_file("log.txt");
sql_auditor_settings::set_behavior(auditing_behavior::OFF);

#endif
auto storage = make_storage(filename,
make_index("user_id_index", &User::id),
make_table("users",
Expand Down

0 comments on commit 4a9d67e

Please sign in to comment.