-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogger.cpp
More file actions
98 lines (83 loc) · 2.99 KB
/
Logger.cpp
File metadata and controls
98 lines (83 loc) · 2.99 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
/*
* Copyright @2026 | ViCast
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Logger.h"
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <filesystem>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
std::mutex Logger::mtx;
std::ofstream Logger::logFile;
std::string Logger::logPath;
void Logger::Init(const std::string& path)
{
std::lock_guard<std::mutex> lock(mtx);
logPath = path;
// --- NEW: Automatic Log Rotation ---
namespace fs = std::filesystem;
try {
if (fs::exists(logPath)) {
// Check if file is larger than 5 MB (5 * 1024 * 1024 = 5242880 bytes)
if (fs::file_size(logPath) > 5242880) {
std::string backupPath = logPath + ".bak";
// Delete the old backup if it exists
if (fs::exists(backupPath)) {
fs::remove(backupPath);
}
// Rename the current bloated log to act as the new backup
fs::rename(logPath, backupPath);
}
}
}
catch (const fs::filesystem_error& e) {
std::cerr << "Logger: Failed to rotate log file: " << e.what() << std::endl;
}
// -----------------------------------
logFile.open(logPath, std::ios::out | std::ios::app);
if (!logFile.is_open())
std::cerr << "Logger: Failed to open log file at " << path << std::endl;
}
void Logger::Log(const std::string& msg)
{
std::lock_guard<std::mutex> lock(mtx);
// Auto-reconnect to file if it somehow dropped
if (!logFile.is_open())
logFile.open(logPath, std::ios::out | std::ios::app);
if (logFile.is_open())
{
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
// MSVC safe local time (Prevents C4996 Warning/Error)
struct tm timeinfo;
localtime_s(&timeinfo, &t);
logFile << "[" << std::put_time(&timeinfo, "%Y-%m-%d %H:%M:%S") << "] " << msg << "\n";
logFile.flush();
}
}
void Logger::Log(const std::wstring& msg)
{
if (msg.empty()) return;
// Safely convert UTF-16 wide string back to UTF-8 standard string
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &msg[0], (int)msg.size(), NULL, 0, NULL, NULL);
std::string s(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &msg[0], (int)msg.size(), &s[0], size_needed, NULL, NULL);
// Route it through your incredibly robust main Log method
Log(s);
}