-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorLogger.cpp
More file actions
92 lines (74 loc) · 2.29 KB
/
ErrorLogger.cpp
File metadata and controls
92 lines (74 loc) · 2.29 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
//
// Created by papraczy on 05/11/2019.
//
#include "ErrorLogger.h"
#include <fstream>
#include <iostream>
using namespace std;
ErrorLogger::ErrorLogger() {
//clear the file
ofstream ofile;
ofile.open("errors.txt");
this->errorCounter = 0;
this->printTime("Started at: ");
}
ErrorLogger::~ErrorLogger()
{
this->printTime("Ended at: ");
}
void ErrorLogger::logError(string message, long packets_received)
{
string msg = "";
msg += "\n" + to_string(errorCounter + 1) + ". " + message + "\n";
msg += "time :" + this->getTimeStr(this->getTime()) + " error percentage: " + to_string(this->getErrorPercentage(packets_received)) + "*10^4 %\n";
float errorPercent = this->getErrorPercentage(packets_received);
msg += "Error frequency: " + to_string(getErrorFrequency()) + "mHz\n";
this->logToFile(msg);
errorCounter++;
}
time_t* ErrorLogger::getTime()
{
auto time = chrono::system_clock::now();
time_t normal_format = chrono::system_clock::to_time_t(time);
return &normal_format;
}
float ErrorLogger::getErrorPercentage(long packets_received) {
const int MULTIPLY_FACTOR = 1000000;
float percent = (MULTIPLY_FACTOR * this->errorCounter)/packets_received;
return percent;
}
void ErrorLogger::printTime(string message) {
time_t* currTime = this->getTime();
this->logToFile(message + this->getTimeStr(currTime));
errorCounter++;
}
void ErrorLogger::logToFile(string message) {
ofstream ofile;
ofile.open("errors.txt", std::ios_base::app);
if (ofile.is_open())
{
ofile << message;
ofile.close();
}
else
{
cout << "Couldn't open the file" << endl;
}
}
string ErrorLogger::getTimeStr(time_t *currTime) {
time (currTime);
struct tm * timeinfo;
char buffer[80];
timeinfo = localtime(currTime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
string str(buffer);
return buffer;
}
void ErrorLogger::printEndMessage() {
this->logToFile("Ended at: " + this->getTimeStr(this->getTime()) + "\n");
}
float ErrorLogger::getErrorFrequency() {
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
double elapsed_time_ms = std::chrono::duration<double, std::milli>(end-start).count();
return errorCounter/elapsed_time_ms;
}