-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLogs.cpp
More file actions
67 lines (55 loc) · 1.37 KB
/
Logs.cpp
File metadata and controls
67 lines (55 loc) · 1.37 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
#include <iostream>
#include <fstream>
#include <ctime>
#include <filesystem>
#define _CRT_SECURE_NO_WARNINGS
char* CurrentTime()
{
time_t rawTime;
time(&rawTime);
struct tm* localTime = localtime(&rawTime);
char* timeString = asctime(localTime);
// Remove newline from asctime result
timeString[strcspn(timeString, "\n")] = '\0';
return timeString;
}
bool WriteLog(std::string ErrorReason);
bool WriteToFile(std::string String, std::string FileName)
{
char* CT = CurrentTime();
std::fstream File(FileName, std::ios::app);
if (!File.is_open())
{
std::cout << "Failed to open file" << std::endl;
return false;
}
File << String << " : " << CT << std::endl;
File.close();
return true;
}
bool CheckLogFile()
{
return std::filesystem::exists("SnapKeyLogs.txt");
}
bool CreateLogFile()
{
std::ofstream CreateLog("SnapKeyLogs.txt");
return CreateLog.is_open();
}
bool WriteLog(std::string ErrorReason)
{
try
{
if (!CheckLogFile()) // FIXED — calling the function
{
CreateLogFile();
}
WriteToFile(ErrorReason, "SnapKeyLogs.txt");
return true;
}
catch (const std::exception&)
{
std::cout << "FAILED TO WRITE LOG" << std::endl;
return false;
}
}