-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuditLogger.cpp
More file actions
45 lines (43 loc) · 1.55 KB
/
AuditLogger.cpp
File metadata and controls
45 lines (43 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "../include/AuditLogger.h"
#include "../include/Utils.h"
#include <fstream>
#include <ctime>
#include <iostream>
using namespace std;
void AuditLogger::log(const string& username, const string& action, const string& details, const string& status) {
ofstream out("data/audit.txt", ios::app);
if (!out.is_open()) return;
time_t now = time(nullptr);
out << now << "|" << username << "|" << action << "|" << details << "|" << status << "\n";
out.close();
}
ostream& operator<<(ostream& os, const AuditLogger& logger) {
ifstream in("data/audit.txt");
if (!in.is_open()) {
os << "[-] No audit logs found.\n";
return os;
}
string line;
os << "=== Audit Logs ===\n";
while (getline(in, line)) {
string* parts = split(line, '|');
if (!parts[0].empty() && !parts[1].empty() && !parts[2].empty() && !parts[3].empty() && !parts[4].empty()) {
time_t t = atol(parts[0].c_str());
string timestamp = prettyprint(t);
string username = parts[1];
string action = parts[2];
string details = parts[3];
string status = parts[4];
os << "-----------------------------\n";
os << "Time: " << timestamp << "\n";
os << "User: " << username << "\n";
os << "Action: " << action << "\n";
os << "Details: " << details << "\n";
os << "Status: " << status << "\n";
}
delete[] parts;
}
os << "-----------------------------\n";
in.close();
return os;
}