-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.cpp
More file actions
65 lines (54 loc) · 1.32 KB
/
Copy pathlogging.cpp
File metadata and controls
65 lines (54 loc) · 1.32 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
// logging.cpp
#include "logging.h"
#include <fstream>
#include <iostream>
// ANSI color codes for terminal output
const std::string BLUE = "\033[34m";
const std::string ORANGE = "\033[33m";
const std::string RED = "\033[31m";
const std::string RESET = "\033[0m";
// Global log file
static std::ofstream logFile;
bool initLog(const std::string& filename) {
// Open file in truncation mode to overwrite previous content
logFile.open(filename, std::ios::out | std::ios::trunc);
if (!logFile.is_open()) {
std::cerr << "Failed to open log file: " << filename << std::endl;
return false;
}
return true;
}
void dbg(LogType type, const std::string& message) {
if (!logFile.is_open()) {
return; // Log not initialized
}
std::string typeStr;
std::string colorCode;
// Set type string and color based on log type
switch (type) {
case LOG_NOTE:
typeStr = "NOTE";
colorCode = BLUE;
break;
case LOG_WARNING:
typeStr = "WARNING";
colorCode = ORANGE;
break;
case LOG_ERROR:
typeStr = "ERROR";
colorCode = RED;
break;
default:
typeStr = "UNKNOWN";
colorCode = RESET;
}
// Write to log file (without colors)
logFile << "[" << typeStr << "] " << message << std::endl;
// Make sure it's written immediately
logFile.flush();
}
void closeLog() {
if (logFile.is_open()) {
logFile.close();
}
}