-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger_settings.cpp
More file actions
81 lines (71 loc) · 1.83 KB
/
logger_settings.cpp
File metadata and controls
81 lines (71 loc) · 1.83 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
#include "pch.h"
#include "logger_settings.h"
#include <fstream>
#include <Windows.h>
#include <winrt/Windows.Data.Json.h>
#include <iostream>
using namespace winrt::Windows::Data::Json;
LogSettings::LogSettings()
{
this->logLevel = LogSettings::defaultLogLevel;
}
std::optional<JsonObject> from_file(std::wstring_view file_name)
{
try
{
std::ifstream file(file_name.data(), std::ios::binary);
if (file.is_open())
{
using isbi = std::istreambuf_iterator<char>;
std::string obj_str{ isbi{ file }, isbi{} };
return JsonValue::Parse(winrt::to_hstring(obj_str)).GetObjectW();
}
return std::nullopt;
}
catch (...)
{
return std::nullopt;
}
}
void to_file(std::wstring_view file_name, const JsonObject& obj)
{
std::wstring obj_str{ obj.Stringify().c_str() };
try
{
std::ofstream{ file_name.data(), std::ios::binary } << winrt::to_string(obj_str);
}
catch (...)
{
}
}
JsonObject to_json(LogSettings settings)
{
JsonObject result;
result.SetNamedValue(LogSettings::logLevelOption, JsonValue::CreateStringValue(settings.logLevel));
return result;
}
LogSettings to_settings(JsonObject jobject)
{
LogSettings result;
try
{
result.logLevel = jobject.GetNamedString(LogSettings::logLevelOption);
}
catch (...)
{
result.logLevel = LogSettings::defaultLogLevel;
}
return result;
}
// Get log settings from file. File with default options is created if it does not exist
LogSettings get_log_settings(std::wstring_view file_name)
{
auto jobject = from_file(file_name);
if (!jobject.has_value())
{
auto json = to_json(LogSettings());
to_file(file_name, json);
return to_settings(json);
}
return to_settings(jobject.value());
}