-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.hpp
133 lines (114 loc) · 4.39 KB
/
Logger.hpp
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
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <list>
#include <utility>
#include "Target.hpp"
#include <mutex>
#include "config.h"
#ifdef _WIN32
#include <windows.h>
#include <memoryapi.h>
#include <fileapi.h>
#endif
using namespace std;
class Injection{
public:
Injection(const chrono::duration<long, std::ratio<1, 1000>> &elapsed, string faultType, Target obj
) : elapsed(elapsed), faultType(std::move(faultType)), object(std::move(obj)) {}
Target object;
chrono::duration<long, std::ratio<1, 1000>> elapsed{};
string faultType;
};
class Logger{
private:
list<Injection> inj;
FILE* logFile;
#if PARALLELIZATION
mutex m;
#endif
public:
Logger() = default;
void addInjection(Target& t, chrono::duration<long, std::ratio<1, 1000>> elapsed, string faultType){
Injection *i;
i = new Injection(elapsed, std::move(faultType), t);
#if PARALLELIZATION
lock_guard<mutex> lock(m);
#endif
inj.push_back(*i);
}
#ifdef _WIN32
static void writeLog(const string& log, HANDLE& h){
bool bErrorFlag = WriteFile(
h, // open file handle
log.c_str(), // start of data to write
log.length(), // number of bytes to write
NULL, // number of bytes that were written
NULL); // no overlapped structure
if (FALSE == bErrorFlag)
{
printf("Terminal failure: Unable to write to file.\n");
}
}
#endif
void logOnfile() {
time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
string init_str =
"--------------------------\nWriting results of " + string(ctime(&time)) + " --- Injected Object : " +
inj.back().object.getName() + " ---\n--------------------------\n";
#ifdef linux
logFile = fopen("../logs/logFile.txt", "a");
string cmd = "flock " + to_string(fileno(logFile));
system(cmd.c_str());
fprintf(logFile, "%s", init_str.c_str());
for(const Injection& i : inj){
string s_inj = "Address : " + to_string(i.object.getAddress()) + " --- Time : " + to_string(i.elapsed.count()) + " --- Fault type : " + i.faultType +
((!i.object.getSubName().empty()) ? " --- Struct parameter : " + i.object.getSubName() + "\n" : "\n");
fprintf(logFile, "%s", s_inj.c_str());
}
fclose(logFile);
#endif
#ifdef _WIN32
HANDLE h;
long err;
do {
h = CreateFile("../logs/logFile.txt", // name of the write
FILE_APPEND_DATA, // append
0, // do not share
NULL, // default security
OPEN_ALWAYS, //
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
err = GetLastError();
cout << "Creating log file\n";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}while(err == 32);
if (h == INVALID_HANDLE_VALUE) {
printf("hFile is NULL\n");
printf("Could not open log\n");
// return error
exit(-1);
}
writeLog(init_str, h);
for (const Injection &i : inj) {
string s_inj =
"Address : " + to_string(i.object.getAddress()) + " --- Time : " + to_string(i.elapsed.count()) +
" --- Fault type : " + i.faultType + "\n";
writeLog(s_inj, h);
}
CloseHandle(h);
#endif
}
void printInj(){
cout << endl;
time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
string init_str = "--------------------------\nWriting results of " + string(ctime(&time)) + " --- Injected Object : " + inj.back().object.getName() + " ---\n--------------------------\n";
cout << init_str;
for(const Injection& i : inj){
string s_inj = "Address : " + to_string(i.object.getAddress()) + " --- Time : " + to_string(i.elapsed.count()) + " --- Fault type : " + i.faultType +
((!i.object.getSubName().empty()) ? " --- Struct parameter : " + i.object.getSubName() + "\n" : "\n");;
cout << s_inj;
}
}
};