1 : |
agomez |
1 |
#ifndef errorhandler_h
|
2 : |
|
|
#define errorhandler_h
|
3 : |
|
|
|
4 : |
|
|
#include "strstack.h" |
5 : |
|
|
#include "doublevector.h" |
6 : |
|
|
#include "gadget.h" |
7 : |
|
|
|
8 : |
|
|
enum LogLevel { LOGNONE = 0, LOGFAIL, LOGINFO, LOGWARN, LOGDEBUG, LOGMESSAGE, LOGDETAIL };
|
9 : |
|
|
|
10 : |
|
|
/**
|
11 : |
|
|
* \class ErrorHandler
|
12 : |
|
|
* \brief This is the class used to handle errors in the model, by displaying error messages to the user and logging information to a log file
|
13 : |
|
|
*/
|
14 : |
|
|
class ErrorHandler {
|
15 : |
|
|
public:
|
16 : |
|
|
/**
|
17 : |
|
|
* \brief This is the ErrorHandler constructor
|
18 : |
|
|
*/
|
19 : |
|
|
ErrorHandler();
|
20 : |
|
|
/**
|
21 : |
|
|
* \brief This is the default ErrorHandler destructor
|
22 : |
|
|
*/
|
23 : |
|
|
~ErrorHandler();
|
24 : |
|
|
/**
|
25 : |
|
|
* \brief This function will open a file for reading data from, and store the name of the file
|
26 : |
|
|
* \param filename is the name of the file
|
27 : |
|
|
*/
|
28 : |
|
|
void Open(const char* filename);
|
29 : |
|
|
/**
|
30 : |
|
|
* \brief This function will close the last file opened for reading data from
|
31 : |
|
|
*/
|
32 : |
|
|
void Close();
|
33 : |
|
|
/**
|
34 : |
|
|
* \brief This function will check to see if an iostream can be opened, and exit Gadget with exit(EXIT_FAILURE) if this check fails
|
35 : |
|
|
* \param infile is the iostream to be checked
|
36 : |
|
|
* \param text is the name of the iostream to be checked
|
37 : |
|
|
*/
|
38 : |
|
|
void checkIfFailure(ios& infile, const char* text);
|
39 : |
|
|
/**
|
40 : |
|
|
* \brief This function will open the file that the logging information will be written to
|
41 : |
|
|
* \param filename is the name of the file
|
42 : |
|
|
*/
|
43 : |
|
|
void setLogFile(const char* filename);
|
44 : |
|
|
/**
|
45 : |
|
|
* \brief This function will return the flag used to determine whether ta logfile exists
|
46 : |
|
|
* \return uselog
|
47 : |
|
|
*/
|
48 : |
|
|
int checkLogFile() { return uselog; };
|
49 : |
|
|
/**
|
50 : |
|
|
* \brief This function will log information about the finish of the current model run to std::cout and a log file if one exists
|
51 : |
|
|
*/
|
52 : |
|
|
void logFinish();
|
53 : |
|
|
/**
|
54 : |
|
|
* \brief This function will log a warning message
|
55 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
56 : |
|
|
* \param msg is the message to be logged
|
57 : |
|
|
*/
|
58 : |
|
|
void logMessage(LogLevel mlevel, const char* msg);
|
59 : |
|
|
/**
|
60 : |
|
|
* \brief This function will log 2 warning messages
|
61 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
62 : |
|
|
* \param msg1 is the first message to be logged
|
63 : |
|
|
* \param msg2 is the second message to be logged
|
64 : |
|
|
*/
|
65 : |
|
|
void logMessage(LogLevel mlevel, const char* msg1, const char* msg2);
|
66 : |
|
|
/**
|
67 : |
|
|
* \brief This function will log a warning message and a number
|
68 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
69 : |
|
|
* \param msg is the message to be logged
|
70 : |
|
|
* \param number is the number to be logged
|
71 : |
|
|
*/
|
72 : |
|
|
void logMessage(LogLevel mlevel, const char* msg, int number);
|
73 : |
|
|
/**
|
74 : |
|
|
* \brief This function will log a warning message and a number
|
75 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
76 : |
|
|
* \param msg is the message to be logged
|
77 : |
|
|
* \param number is the number to be logged
|
78 : |
|
|
*/
|
79 : |
|
|
void logMessage(LogLevel mlevel, const char* msg, double number);
|
80 : |
|
|
/**
|
81 : |
|
|
* \brief This function will log a warning message, a number and a second message
|
82 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
83 : |
|
|
* \param msg1 is the first message to be logged
|
84 : |
|
|
* \param number is the number to be logged
|
85 : |
|
|
* \param msg2 is the second message to be logged
|
86 : |
|
|
*/
|
87 : |
|
|
void logMessage(LogLevel mlevel, const char* msg1, int number, const char* msg2);
|
88 : |
|
|
/**
|
89 : |
|
|
* \brief This function will log a warning message, a number and a second message
|
90 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
91 : |
|
|
* \param msg1 is the first message to be logged
|
92 : |
|
|
* \param number is the number to be logged
|
93 : |
|
|
* \param msg2 is the second message to be logged
|
94 : |
|
|
*/
|
95 : |
|
|
void logMessage(LogLevel mlevel, const char* msg1, double number, const char* msg2);
|
96 : |
|
|
/**
|
97 : |
|
|
* \brief This function will log a vector of values
|
98 : |
|
|
* \param mlevel is the logging level of the values to be logged
|
99 : |
|
|
* \param vec is the DoubleVector of values to be logged
|
100 : |
|
|
*/
|
101 : |
|
|
void logMessage(LogLevel mlevel, DoubleVector vec);
|
102 : |
|
|
/**
|
103 : |
|
|
* \brief This function will log a warning message about a NaN found in the model
|
104 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
105 : |
|
|
* \param msg is the message to be logged
|
106 : |
|
|
*/
|
107 : |
|
|
void logMessageNaN(LogLevel mlevel, const char* msg);
|
108 : |
|
|
/**
|
109 : |
|
|
* \brief This function will log a warning message generated when reading information from file
|
110 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
111 : |
|
|
* \param msg is the message to be logged
|
112 : |
|
|
*/
|
113 : |
|
|
void logFileMessage(LogLevel mlevel, const char* msg);
|
114 : |
|
|
/**
|
115 : |
|
|
* \brief This function will log a warning message and a number, generated when reading information from file
|
116 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
117 : |
|
|
* \param msg is the message to be logged
|
118 : |
|
|
* \param number is the number to be logged
|
119 : |
|
|
*/
|
120 : |
|
|
void logFileMessage(LogLevel mlevel, const char* msg, int number);
|
121 : |
|
|
/**
|
122 : |
|
|
* \brief This function will log a warning message and a number, generated when reading information from file
|
123 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
124 : |
|
|
* \param msg is the message to be logged
|
125 : |
|
|
* \param number is the number to be logged
|
126 : |
|
|
*/
|
127 : |
|
|
void logFileMessage(LogLevel mlevel, const char* msg, double number);
|
128 : |
|
|
/**
|
129 : |
|
|
* \brief This function will log 2 warning messages generated when reading information from file
|
130 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
131 : |
|
|
* \param msg1 is the first message to be logged
|
132 : |
|
|
* \param msg2 is the second message to be logged
|
133 : |
|
|
*/
|
134 : |
|
|
void logFileMessage(LogLevel mlevel, const char* msg1, const char* msg2);
|
135 : |
|
|
/**
|
136 : |
|
|
* \brief This function will log an EOF warning message generated when reading information from file
|
137 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
138 : |
|
|
*/
|
139 : |
|
|
void logFileEOFMessage(LogLevel mlevel);
|
140 : |
|
|
/**
|
141 : |
|
|
* \brief This function will log an 'unexpected' warning message generated when reading information from file
|
142 : |
|
|
* \param mlevel is the logging level of the message to be logged
|
143 : |
|
|
* \param msg1 is the first (expected) message to be logged
|
144 : |
|
|
* \param msg2 is the second (unexpected) message to be logged
|
145 : |
|
|
*/
|
146 : |
|
|
void logFileUnexpected(LogLevel mlevel, const char* msg1, const char* msg2);
|
147 : |
|
|
/**
|
148 : |
|
|
* \brief This function will set the optimise flag for the current model run
|
149 : |
|
|
* \param opt is the optimise flag to be set
|
150 : |
|
|
*/
|
151 : |
|
|
void setRunOptimise(int opt) { runopt = opt; };
|
152 : |
|
|
/**
|
153 : |
|
|
* \brief This function will return the optimise flag for the current model run
|
154 : |
|
|
* \return runopt
|
155 : |
|
|
*/
|
156 : |
|
|
int getRunOptimise() const { return runopt; };
|
157 : |
|
|
/**
|
158 : |
|
|
* \brief This function will set the level of logging information used for the current model run
|
159 : |
|
|
* \param level is the logging level to be set
|
160 : |
|
|
*/
|
161 : |
|
|
void setLogLevel(int level);
|
162 : |
|
|
/**
|
163 : |
|
|
* \brief This function will return the level of logging information used for the current model run
|
164 : |
|
|
* \return loglevel
|
165 : |
|
|
*/
|
166 : |
|
|
LogLevel getLogLevel() const { return loglevel; };
|
167 : |
|
|
/**
|
168 : |
|
|
* \brief This function will return the flag denoting whether a NaN error has been rasied or not
|
169 : |
|
|
* \return nanflag
|
170 : |
|
|
*/
|
171 : |
|
|
int getNaNFlag() const { return nanflag; };
|
172 : |
|
|
/**
|
173 : |
|
|
* \brief This function will return the flag denoting whether a NaN error has been rasied or not
|
174 : |
|
|
*/
|
175 : |
|
|
void setNaNFlag(int flag) { nanflag = flag; };
|
176 : |
|
|
protected:
|
177 : |
|
|
/**
|
178 : |
|
|
* \brief This ofstream is the file that all the logging information will get sent to
|
179 : |
|
|
*/
|
180 : |
|
|
ofstream logfile;
|
181 : |
|
|
/**
|
182 : |
|
|
* \brief This is the StrStack of the names of files that are currently open to read from
|
183 : |
|
|
*/
|
184 : |
|
|
StrStack* files;
|
185 : |
|
|
private:
|
186 : |
|
|
/**
|
187 : |
|
|
* \brief This is the flag used to denote whether a NaN error has been raised or nont
|
188 : |
|
|
*/
|
189 : |
|
|
int nanflag;
|
190 : |
|
|
/**
|
191 : |
|
|
* \brief This is the flag used to denote whether the current run will optimise the model or not
|
192 : |
|
|
*/
|
193 : |
|
|
int runopt;
|
194 : |
|
|
/**
|
195 : |
|
|
* \brief This is the flag used to denote whether a logfile exists or not
|
196 : |
|
|
*/
|
197 : |
|
|
int uselog;
|
198 : |
|
|
/**
|
199 : |
|
|
* \brief This is the number of warning messages that gadget has displayed
|
200 : |
|
|
*/
|
201 : |
|
|
int numwarn;
|
202 : |
|
|
/**
|
203 : |
|
|
* \brief This denotes what level of logging information is used for the current model run
|
204 : |
|
|
*/
|
205 : |
|
|
LogLevel loglevel;
|
206 : |
|
|
};
|
207 : |
|
|
|
208 : |
|
|
#endif
|