-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoLogRoll.cpp
More file actions
118 lines (92 loc) · 2.18 KB
/
Copy pathAutoLogRoll.cpp
File metadata and controls
118 lines (92 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
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
118
#include "AutoLogRoll.h"
#include <windows.h>
namespace
{
constexpr wchar_t SETTINGS_KEY[] = L"Software\\AudioCppTray";
constexpr wchar_t AUTO_ROLL_VALUE[] = L"AutomaticLogRoll";
constexpr wchar_t LAST_ROLL_DATE_VALUE[] = L"LastAutomaticLogRollDate";
constexpr wchar_t ROTATION_HOUR_VALUE[] = L"RotationHour";
DWORD GetTodayKey()
{
SYSTEMTIME now{};
GetLocalTime(&now);
return now.wYear * 10000 + now.wMonth * 100 + now.wDay;
}
DWORD ReadSetting(const wchar_t* name, DWORD defaultValue)
{
HKEY key = nullptr;
if (RegOpenKeyExW(
HKEY_CURRENT_USER,
SETTINGS_KEY,
0,
KEY_QUERY_VALUE,
&key
) != ERROR_SUCCESS)
{
return defaultValue;
}
DWORD value = defaultValue;
DWORD type = REG_DWORD;
DWORD size = sizeof(value);
if (RegQueryValueExW(
key,
name,
nullptr,
&type,
reinterpret_cast<BYTE*>(&value),
&size
) != ERROR_SUCCESS || type != REG_DWORD)
{
value = defaultValue;
}
RegCloseKey(key);
return value;
}
void WriteSetting(const wchar_t* name, DWORD value)
{
HKEY key = nullptr;
if (RegCreateKeyExW(
HKEY_CURRENT_USER,
SETTINGS_KEY,
0,
nullptr,
0,
KEY_SET_VALUE,
nullptr,
&key,
nullptr
) == ERROR_SUCCESS)
{
RegSetValueExW(
key,
name,
0,
REG_DWORD,
reinterpret_cast<const BYTE*>(&value),
sizeof(value)
);
RegCloseKey(key);
}
}
}
bool IsAutomaticLogRollEnabled()
{
return ReadSetting(AUTO_ROLL_VALUE, 0) != 0;
}
void SetAutomaticLogRollEnabled(bool enabled)
{
WriteSetting(AUTO_ROLL_VALUE, enabled ? 1 : 0);
}
bool HasAutomaticLogRollRunToday()
{
return ReadSetting(LAST_ROLL_DATE_VALUE, 0) == GetTodayKey();
}
void MarkAutomaticLogRollRunToday()
{
WriteSetting(LAST_ROLL_DATE_VALUE, GetTodayKey());
}
int GetAutomaticLogRollHour()
{
DWORD value = ReadSetting(ROTATION_HOUR_VALUE, 4);
return value > 23 ? 4 : static_cast<int>(value);
}