-
-
Notifications
You must be signed in to change notification settings - Fork 321
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
first implementation of sql_auditing #1367
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#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> | ||
|
||
#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; | ||
friend class sql_auditor_settings; | ||
sql_auditor(); | ||
void open(); | ||
inline static sql_auditor& auditor() { | ||
static sql_auditor auditor{}; | ||
return auditor; | ||
} | ||
|
||
public: | ||
static void log(const std::string& message); | ||
}; | ||
|
||
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; | ||
} | ||
}; | ||
|
||
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(); | ||
} | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
logs are enabled always even though devs don't need them?