-
-
Notifications
You must be signed in to change notification settings - Fork 326
first implementation of sql_auditing #1367
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
#include <fstream> | ||
#include "error_code.h" | ||
#include <chrono> | ||
#include <ctime> // For std::localtime and std::tm | ||
#include <iomanip> // For std::put_time | ||
#include <string> | ||
|
||
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}; | ||
} | ||
} | ||
inline static sql_auditor& auditor() { | ||
static sql_auditor auditor{"sql_auditor.txt"}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the process which runs this code doesn't have file permissions to |
||
return auditor; | ||
} | ||
|
||
public: | ||
static void log(const std::string& message) { | ||
// 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, "%Y-%m-%d %H:%M:%S") // Custom format | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if somebody wants to use different date format? how devs who already has working log system would integrate this logic into their systems? |
||
<< " = "; | ||
|
||
auditor().log_file << message << std::endl; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if somebody wants to store logs not in
sql_auditor.txt
but somewhere else?