-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissionLog.cpp
More file actions
117 lines (96 loc) · 4.21 KB
/
Copy pathMissionLog.cpp
File metadata and controls
117 lines (96 loc) · 4.21 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "MissionLog.h"
#include "Colors.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <ctime>
#include <algorithm>
const char* MissionLog::LOG_FILE = "mission_log.txt";
// -----------------------------------------------------------------------------
// Timestamp helper - "YYYY-MM-DD HH:MM:SS"
// -----------------------------------------------------------------------------
static std::string timestamp() {
std::time_t t = std::time(nullptr);
std::tm tmBuf{};
#ifdef _WIN32
localtime_s(&tmBuf, &t);
#else
localtime_r(&t, &tmBuf);
#endif
std::ostringstream oss;
oss << std::put_time(&tmBuf, "%Y-%m-%d %H:%M:%S");
return oss.str();
}
// -----------------------------------------------------------------------------
// record - append one log line
// -----------------------------------------------------------------------------
void MissionLog::record(const std::string& operation, const std::string& input,
const std::string& result, long long timeTakenMs) {
std::ofstream f(LOG_FILE, std::ios::app);
if (!f.is_open()) return;
f << "[" << timestamp() << "] " << operation
<< " | " << input
<< " | " << result
<< " | " << timeTakenMs << "ms\n";
}
// -----------------------------------------------------------------------------
// viewLast - show the most recent N entries inside the CLI
// -----------------------------------------------------------------------------
void MissionLog::viewLast(int count) {
std::ifstream f(LOG_FILE);
std::cout << "\n " << CLR_RESULT << "[Mission Log - last " << count << " entries]" << CLR_RESET << "\n";
std::cout << " " << std::string(70, '-') << "\n";
if (!f.is_open()) {
std::cout << " " << CLR_WARN << "No log entries yet - run an algorithm first." << CLR_RESET << "\n";
return;
}
std::vector<std::string> lines;
std::string line;
while (std::getline(f, line))
if (!line.empty()) lines.push_back(line);
if (lines.empty()) {
std::cout << " " << CLR_WARN << "Log file is empty." << CLR_RESET << "\n";
return;
}
int start = std::max(0, static_cast<int>(lines.size()) - count);
for (int i = start; i < static_cast<int>(lines.size()); i++)
std::cout << " " << CLR_RESULT << lines[i] << CLR_RESET << "\n";
std::cout << " " << std::string(70, '-') << "\n";
std::cout << " Showing " << (lines.size() - start) << " of " << lines.size()
<< " total entries.\n";
}
// -----------------------------------------------------------------------------
// exportReport - write a formatted report file from the full log
// -----------------------------------------------------------------------------
bool MissionLog::exportReport(const std::string& filename) {
std::ifstream in(LOG_FILE);
if (!in.is_open()) {
std::cout << " " << CLR_WARN << "No log file found - nothing to export." << CLR_RESET << "\n";
return false;
}
std::vector<std::string> lines;
std::string line;
while (std::getline(in, line))
if (!line.empty()) lines.push_back(line);
std::ofstream out(filename);
if (!out.is_open()) {
std::cout << " " << CLR_WARN << "Cannot write report file: " << filename << CLR_RESET << "\n";
return false;
}
out << "==============================================================\n";
out << " MILITARY LOGISTICS & TACTICAL NETWORK OPTIMIZER\n";
out << " MISSION LOG - FORMATTED REPORT\n";
out << " Generated : " << timestamp() << "\n";
out << " Total runs: " << lines.size() << "\n";
out << "==============================================================\n\n";
for (size_t i = 0; i < lines.size(); i++)
out << " " << std::setw(4) << (i + 1) << ". " << lines[i] << "\n";
out << "\n==============================================================\n";
out << " End of report - " << lines.size() << " operation(s) logged.\n";
out << "==============================================================\n";
std::cout << " " << CLR_BORDER << "Report exported to '" << filename << "' ("
<< lines.size() << " entries)." << CLR_RESET << "\n";
return true;
}