-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgramMonitor.h
More file actions
127 lines (108 loc) · 2.67 KB
/
ProgramMonitor.h
File metadata and controls
127 lines (108 loc) · 2.67 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
123
124
125
126
127
#ifndef PROGRAMMONITOR_H
#define PROGRAMMONITOR_H
#include "BoxInfo.h"
enum ERROR_CODES
{
EVERYTHING_OK = 0,
ERROR_OPEN,
ERRORFILE_READ,
ERRORFILE_WRITE,
READ_END_ERR,
EMPTY_FILE,
LARGE_FILE_ERR,
FILE_OPER_ERR,
UKNOWN_OPER_ERR,
DIV_NULL_ERR,
ERR_ALLOC_CS,
POOL_RESERVE_ERR,
ERRORFILE_READ_BOX,
MEM_ALOC_ERROR
};
const char *errorCodes[256]
{
"", // EVERYTHING_OK
"Failed to open file. File doesn't exist or you don't have permissions", // ERROR_OPEN
"Cannot read from file opened for writing", // ERRORFILE_READ
"Cannot write into file opened for reading.", // ERRORFILE_WRITE
"Reached end of file, can not read further", // READ_END_ERR
"Execute box file is empty", // EMPTY_FILE
"File to large to read", // LARGE_FILE_ERR
"Not found reference to file", // FILE_OPER_ERR
"Trying to execute an unknown bytecode operation.", // UKNOWN_OPER_ERR
"Trying to divide with with zero", // DIV_NULL_ERR
"Code segment allocation error", // ERR_ALLOC_CS
"Not enough resource to dynamicaly allocate memory pool", // POOL_RESERVE_ERR
"Not enough resource to allocate data" // MEM_ALOC_ERROR
};
class ProgramMonitor {
public:
ProgramMonitor();
~ProgramMonitor();
void setError(Status);
Status getStatus();
void setLogPath(char *);
void errorReport(FILE *);
void writeErrorReport();
bool OK();
bool notOK();
protected:
Status status;
char *path;
};
ProgramMonitor::ProgramMonitor()
{
status = EVERYTHING_OK;
path = null;
}
ProgramMonitor::~ProgramMonitor()
{
if (status)
this->writeErrorReport();
}
void ProgramMonitor::setLogPath(char *execPath)
{
path = execPath;
}
void ProgramMonitor::setError(Status stat)
{
status = stat;
}
Status ProgramMonitor::getStatus()
{
return status;
}
bool ProgramMonitor::OK()
{
return status == 0;
}
bool ProgramMonitor::notOK()
{
return status > 0;
}
void ProgramMonitor::errorReport(FILE *stream)
{
fprintf(stream, "\n BOXVM RUNTIME ERROR REPORT:");
fprintf(stream, "\n [ERROR CODE = 0x%X] (%d) (%s)\n\n\t*** ", status, status, errorCodes[status]);
}
void ProgramMonitor::writeErrorReport()
{
char *ppath = null;
FILE *fp = null;
ppath = strchr(path, '.');
if (ppath)
{
strcpy(ppath, ".logbox");
fp = fopen(path, "w+");
if (fp)
{
errorReport(fp);
fclose(fp);
}
}
errorReport(stdout);
}
ProgramMonitor programMonitor;
#define SETERR(__ERROR) programMonitor.setError(__ERROR)
#define EOK programMonitor.OK()
#define NOK programMonitor.notOK()
#endif // PROGRAMMONITOR_H