-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.h
48 lines (36 loc) · 1.19 KB
/
Logger.h
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
#pragma warning(disable:4996)
#ifndef LOGGER_H
#define LOGGER_H
#include<ctime>
#include<fstream>
// Function to retrieve and return the current date and/or time from the system module.
inline std::string getCurrentDateTime(std::string Input)
{
time_t CurrentTime=time(0);
struct tm Time;
char Buffer[80];
Time=*localtime(&CurrentTime);
if(Input=="Now")
{
strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %X", &Time);
}
else if(Input=="Date")
{
strftime(Buffer, sizeof(Buffer), "%Y%m%d%H%M", &Time);
}
else if(Input=="Time")
{
strftime(Buffer, sizeof(Buffer), "%H%M", &Time);
}
return std::string(Buffer);
};
// Function to log all the events in the programme, and store in a log file.
inline void logger(std::string LogMessage)
{
std::string FilePath="log_"+getCurrentDateTime("Date")+".txt";
std::string CurrentTime=getCurrentDateTime("Now");
std::ofstream Output(FilePath.c_str(), std::ios_base::out | std::ios_base::app);
Output << CurrentTime << "\t" << LogMessage << std::endl;
Output.close();
}
#endif