-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
123 lines (93 loc) · 2.43 KB
/
Copy pathLogger.cpp
File metadata and controls
123 lines (93 loc) · 2.43 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
119
120
121
122
#include "Logger.h"
int DO = 22;
int CLK = 23;
int DI = 24;
int CS = 25;
Logger::Logger(){
startSession();
}
Logger::~Logger(){
endSession();
}
Logger& Logger::getInstance(){
static Logger instance;
return instance;
}
char* Logger::getErrorString(Error error){
switch(error){
case UNDEFINED: return "[UNDEFINED]";
case WARNING: return "[WARNING]";
case SEVERE: return "[SEVERE]";
case CRITICAL: return "[CRITICAL]";
default: return "";
};
}
char* Logger::getCurrentTimeStr(){
time_t currentTime;
currentTime = time(¤tTime);
tm timeStruct;
timeStruct = *localtime(¤tTime);
char* formattedString = (char*)malloc(11);
char* temp = (char*)malloc(11);
strcpy(formattedString, "[");
strftime(temp, 11, "%X", &timeStruct);
strcat(temp, "]");
strcat(formattedString, temp);
char formatted[12];
strncpy(formatted, formattedString, 11);
free(temp);
free(formattedString);
return &formatted[0];
}
char* Logger::getCurrentDateStr(){
time_t currentTime;
currentTime = time(¤tTime);
tm timeStruct;
timeStruct = *localtime(¤tTime);
char* formattedString = (char*)malloc(11);
char* temp = (char*)malloc(11);
strcpy(formattedString, "[");
strftime(temp, 11, "%x", &timeStruct);
strcat(temp, "]");
strcat(formattedString, temp);
char formatted[12];
strncpy(formatted, formattedString, 11);
free(temp);
free(formattedString);
return &formatted[0];
}
void Logger::error(Error error, const char* message){
char* temp = this->getCurrentDateStr();
char formatted[MAX_LOGGER_STRING_SIZE] = "\0";
strncpy(formatted, temp, strlen(temp));
temp = this->getCurrentTimeStr();
strncat(formatted, temp, strlen(temp));
strcat(formatted, " : ");
strcat(formatted, getErrorString(error));
strcat(formatted, " ");
strcat(formatted, message);
strcat(formatted, "\n");
free(temp);
std::cout << formatted;
fwrite(formatted, 1, strlen(formatted), sdCard);
}
void Logger::log(const char* message){
char* temp = this->getCurrentDateStr();
char formatted[MAX_LOGGER_STRING_SIZE] = "\0";
strncpy(formatted, temp, strlen(temp));
temp = this->getCurrentTimeStr();
strncat(formatted, temp, strlen(temp));
strcat(formatted, " : ");
strcat(formatted, message);
strcat(formatted, "\n");
free(temp);
std::cout << formatted;
fwrite(formatted, 1, strlen(formatted), sdCard);
}
void Logger::startSession(){
//sd_mount(DO, CLK, DI, CS);
sdCard = fopen("Log.txt", "w");
}
void Logger::endSession(){
fclose(sdCard);
}