-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterfaceClass.cpp
82 lines (64 loc) · 1.78 KB
/
InterfaceClass.cpp
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
#include <iostream>
#include <fstream>
class IErrorLog
{
public:
virtual void open_log(std::string file_name) = 0;
virtual void close_log(std::string file_name) = 0;
virtual void write_error(std::string error_message) = 0;
virtual void write_warning(std::string error_message) = 0;
virtual void write_info(std::string error_message) = 0;
};
class FileErrorLog : public IErrorLog
{
private:
std::ofstream m_log_file;
public:
virtual void open_log(std::string file_name);
virtual void close_log(std::string file_name);
virtual void write_error(std::string error_message);
virtual void write_warning(std::string error_message);
virtual void write_info(std::string error_message);
};
void FileErrorLog::open_log(std::string file_name)
{
m_log_file.open(file_name);
}
void FileErrorLog::close_log(std::string file_name)
{
m_log_file.close();
}
void FileErrorLog::write_error(std::string error_message)
{
m_log_file << "[E]: " << error_message << std::endl;
}
void FileErrorLog::write_warning(std::string warning_message)
{
m_log_file << "[W]: " << warning_message << std::endl;
}
void FileErrorLog::write_info(std::string info_message)
{
m_log_file << "[I]: " << info_message << std::endl;
}
double divide(double x, double y, IErrorLog& log)
{
double result = 0;
if (!y)
{
log.write_error("Division by zero in function divide. 0 is returned");
return 0;
}
result = x / y;
log.write_info("The result of the function divide is: " + std::to_string(result));
return result;
}
int main()
{
FileErrorLog log_file;
log_file.open_log("Log_file.txt");
divide(25.0, 5.0, log_file);
divide(5.0, 0.0, log_file);
divide(1.0, 3.0, log_file);
log_file.close_log(" Log_file.txt");
return 0;
}