-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.cpp
More file actions
79 lines (70 loc) · 2.18 KB
/
ConfigManager.cpp
File metadata and controls
79 lines (70 loc) · 2.18 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
#include "ConfigManager.h"
#include <Windows.h>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
static std::string get_base_path_without_extension() {
char path[MAX_PATH] = { 0 };
HMODULE hm = NULL;
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCSTR>(&GetConfigFilename), &hm) == 0)
{
return "";
}
if (GetModuleFileNameA(hm, path, sizeof(path)) == 0)
{
return "";
}
std::string base_path(path);
size_t dot_pos = base_path.find_last_of(".");
if (dot_pos != std::string::npos) {
return base_path.substr(0, dot_pos);
}
return base_path;
}
std::string GetConfigFilename() {
return get_base_path_without_extension() + ".json";
}
std::string GetLogFilename() {
std::string config_path = GetConfigFilename();
std::string default_name;
size_t dot_pos = config_path.find_last_of(".");
if (dot_pos != std::string::npos) {
default_name = config_path.substr(0, dot_pos) + "_log.txt";
}
else {
default_name = config_path + "_log.txt";
}
std::ifstream f(config_path);
if (f.is_open()) {
try {
json data = json::parse(f);
if (data.contains("script_name")) {
std::string script_name = data["script_name"];
return script_name + "_log.txt";
}
}
catch (json::parse_error&) { /* Fall through to default */ }
}
return default_name;
}
int GetLogLevel() {
std::string config_path = GetConfigFilename();
std::ifstream f(config_path);
// Default to ERROR level for production (minimal logging)
int default_level = 0; // LOG_ERROR
if (f.is_open()) {
try {
json data = json::parse(f);
if (data.contains("log_level")) {
int level = data["log_level"];
// Validate the level is in valid range
if (level >= 0 && level <= 3) {
return level;
}
}
}
catch (json::parse_error&) { /* Fall through to default */ }
}
return default_level;
}