1 |
mocchiut |
1.1 |
//============================================================================ |
2 |
|
|
// $Id: Logger.cpp,v 1.4 2008-03-05 11:36:27 messineo Exp $ |
3 |
|
|
// Description : |
4 |
|
|
//============================================================================ |
5 |
|
|
#include "Logger.h" |
6 |
|
|
#include <iostream> |
7 |
|
|
|
8 |
|
|
namespace PamOffLineSW{ |
9 |
|
|
|
10 |
|
|
LogUtil::LogUtil(std::string& filename){ |
11 |
|
|
mtempstring=""; |
12 |
|
|
mFilename=filename; |
13 |
|
|
mLevel=LogUtil::LOGERROR; |
14 |
|
|
mOutStream.open(mFilename.c_str(), std::ios::binary); |
15 |
|
|
} |
16 |
|
|
LogUtil::LogUtil(char* filename){ |
17 |
|
|
mtempstring=""; |
18 |
|
|
mFilename=std::string(filename); |
19 |
|
|
mLevel=LogUtil::LOGERROR; |
20 |
|
|
mOutStream.open(mFilename.c_str(), std::ios::binary); |
21 |
|
|
} |
22 |
|
|
LogUtil::LogUtil(std::string& filename, logLevel level){ |
23 |
|
|
mtempstring=""; |
24 |
|
|
mFilename=filename; |
25 |
|
|
mLevel=level; |
26 |
|
|
mOutStream.open(mFilename.c_str(), std::ios::binary); |
27 |
|
|
} |
28 |
|
|
LogUtil::LogUtil(char* filename, logLevel level){ |
29 |
|
|
mtempstring=""; |
30 |
|
|
mFilename=std::string(filename); |
31 |
|
|
mLevel=level; |
32 |
|
|
mOutStream.open(mFilename.c_str(), std::ios::binary); |
33 |
|
|
} |
34 |
|
|
std::string& LogUtil::getLogFileName(){ |
35 |
|
|
return mFilename; |
36 |
|
|
} |
37 |
|
|
void LogUtil::setLoglevel(logLevel level){ |
38 |
|
|
mLevel=level; |
39 |
|
|
} |
40 |
|
|
void LogUtil::logError(std::string& msg){ |
41 |
|
|
if(!mOutStream.fail()){ |
42 |
|
|
mOutStream << "[Error] " << msg << std::endl; |
43 |
|
|
}else{ |
44 |
|
|
std::cout << "[Error] " << msg << std::endl; |
45 |
|
|
} |
46 |
|
|
} |
47 |
|
|
void LogUtil::logWarning(std::string& msg){ |
48 |
|
|
if(mLevel >= LogUtil::LOGWARN){ |
49 |
|
|
if(!mOutStream.fail()){ |
50 |
|
|
mOutStream << "[Warning] " << msg << std::endl; |
51 |
|
|
}else{ |
52 |
|
|
std::cout << "[Warning] " << msg << std::endl; |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
void LogUtil::logInfo(std::string& msg){ |
58 |
|
|
if(mLevel >= LogUtil::LOGINFO){ |
59 |
|
|
if(!mOutStream.fail()){ |
60 |
|
|
mOutStream << "[Info] " << msg << std::endl; |
61 |
|
|
}else{ |
62 |
|
|
std::cout << "[Info] " << msg << std::endl; |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
void LogUtil::logAll(std::string& msg){ |
68 |
|
|
if(mLevel >= LogUtil::LOGALL){ |
69 |
|
|
if(!mOutStream.fail()){ |
70 |
|
|
mOutStream << "[Debug:] " << msg << std::endl; |
71 |
|
|
}else{ |
72 |
|
|
std::cout << "[Debug:] " << msg << std::endl; |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
void LogUtil::logAlways(std::string& msg){ |
78 |
|
|
if(!mOutStream.fail()){ |
79 |
|
|
mOutStream << "[LOG:] " << msg << std::endl; |
80 |
|
|
}else{ |
81 |
|
|
std::cout << "[LOG:] " << msg << std::endl; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
void LogUtil::logAlways(char* msg){ |
87 |
|
|
std::string tmp = msg; |
88 |
|
|
logAlways(tmp); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
void LogUtil::logError(char* msg){ |
92 |
|
|
std::string tmp = msg; |
93 |
|
|
logError(tmp); |
94 |
|
|
} |
95 |
|
|
void LogUtil::logWarning(char* msg){ |
96 |
|
|
std::string tmp = msg; |
97 |
|
|
logWarning(tmp); |
98 |
|
|
} |
99 |
|
|
void LogUtil::logInfo(char* msg){ |
100 |
|
|
std::string tmp = msg; |
101 |
|
|
logInfo(tmp); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
void LogUtil::logAll(char* msg){ |
105 |
|
|
std::string tmp = msg; |
106 |
|
|
logAll(tmp); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
LogUtil::~LogUtil(){ |
110 |
|
|
mOutStream.close(); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
|
114 |
|
|
const int Logger::OPEN_FILE_OPERATION = 1; |
115 |
|
|
const int Logger::LOG_MSG_OPERATION = 2; |
116 |
|
|
const int Logger::CLOSE_FILE_OPERATION = 3; |
117 |
|
|
|
118 |
|
|
Logger::Logger(){ |
119 |
|
|
mAutoFlush = true; |
120 |
|
|
mFileName = ""; |
121 |
|
|
//mOutStream = new std::ofstream(); |
122 |
|
|
initStringMsg(); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
Logger::Logger(bool autoflush){ |
126 |
|
|
mAutoFlush = autoflush; |
127 |
|
|
mFileName = ""; |
128 |
|
|
//mOutStream = new std::ofstream(); |
129 |
|
|
initStringMsg(); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
Logger::~Logger(){ |
133 |
|
|
mOutStream.flush(); |
134 |
|
|
/* |
135 |
|
|
if(mOutStream){ |
136 |
|
|
delete mOutStream; |
137 |
|
|
} |
138 |
|
|
*/ |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
void Logger::initStringMsg(){ |
142 |
|
|
mDummyOptionalMsg.assign(""); |
143 |
|
|
mBlankString.assign(""); |
144 |
|
|
mUnableToOpenFileMsg.assign("Unable to open file "); |
145 |
|
|
mUnableToLogMsg.assign("Unable to log msg \""); |
146 |
|
|
mToFileMsg.assign("\" to file "); |
147 |
|
|
mUnableToCloseMsg.assign("Unable to close \""); |
148 |
|
|
mFileMsg.assign("\" file "); |
149 |
|
|
} |
150 |
|
|
std::string& Logger::getLogFileName(){ |
151 |
|
|
return mFileName; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
bool Logger::setLogFile(std::string& fileName){ |
155 |
|
|
mFileName.assign(fileName.c_str()); |
156 |
|
|
mOutStream.open(fileName.c_str(), std::ios::binary); |
157 |
|
|
return checkOutFileStreamError(Logger::OPEN_FILE_OPERATION,mDummyOptionalMsg); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
bool Logger::setLogFile(char* fileName){ |
161 |
|
|
mFileName.assign(fileName); |
162 |
|
|
mOutStream.open(fileName, std::ios::binary); |
163 |
|
|
return checkOutFileStreamError(Logger::OPEN_FILE_OPERATION,mDummyOptionalMsg); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
std::ofstream& Logger::getOutStream(){ |
167 |
|
|
return mOutStream; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
bool Logger::close(){ |
171 |
|
|
//std::string dummyOptionalMsg(""); |
172 |
|
|
/* |
173 |
|
|
if(mOutStream){ |
174 |
|
|
mOutStream.close(); |
175 |
|
|
} |
176 |
|
|
*/ |
177 |
|
|
mOutStream.close(); |
178 |
|
|
return checkOutFileStreamError(Logger::CLOSE_FILE_OPERATION,mDummyOptionalMsg); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
bool Logger::checkOutFileStreamError(int operation, std::string& optionalMsg){ |
184 |
|
|
if(mOutStream.fail()){ |
185 |
|
|
switch(operation){ |
186 |
|
|
case Logger::OPEN_FILE_OPERATION : { |
187 |
|
|
mBlankString.assign(mUnableToOpenFileMsg.c_str()); |
188 |
|
|
mBlankString.append(mFileName); |
189 |
|
|
|
190 |
|
|
}break; |
191 |
|
|
case Logger::LOG_MSG_OPERATION : { |
192 |
|
|
mBlankString.assign(mUnableToLogMsg.c_str()); |
193 |
|
|
mBlankString.append(optionalMsg.c_str()); |
194 |
|
|
mBlankString.append(mToFileMsg.c_str()); |
195 |
|
|
mBlankString.append(mFileName.c_str()); |
196 |
|
|
//msg+="Unable to log msg \""; |
197 |
|
|
//msg+=optionalMsg; |
198 |
|
|
//msg+="\" to file "; |
199 |
|
|
//msg+=mFileName; |
200 |
|
|
}break; |
201 |
|
|
case Logger::CLOSE_FILE_OPERATION : { |
202 |
|
|
mBlankString.assign(mUnableToCloseMsg.c_str()); |
203 |
|
|
mBlankString.append(optionalMsg.c_str()); |
204 |
|
|
mBlankString.append(mFileMsg.c_str()); |
205 |
|
|
mBlankString.append(mFileName.c_str()); |
206 |
|
|
}; |
207 |
|
|
} |
208 |
|
|
////MessageBox(NULL,msg.c_str(),"Error",MB_OK); |
209 |
|
|
//mBlankString.assign("");//TOO optimisation needed |
210 |
|
|
std::cout << mBlankString << std::endl; |
211 |
|
|
return false; |
212 |
|
|
} |
213 |
|
|
else |
214 |
|
|
return true; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
bool Logger::checkOutFileStreamError(int operation) |
218 |
|
|
{ |
219 |
|
|
if(mOutStream.fail()){ |
220 |
|
|
switch(operation){ |
221 |
|
|
case Logger::OPEN_FILE_OPERATION : { |
222 |
|
|
mBlankString.assign(mUnableToOpenFileMsg.c_str()); |
223 |
|
|
mBlankString.append(mFileName); |
224 |
|
|
|
225 |
|
|
}break; |
226 |
|
|
case Logger::LOG_MSG_OPERATION : { |
227 |
|
|
mBlankString.assign(mUnableToLogMsg.c_str()); |
228 |
|
|
mBlankString.append(mToFileMsg.c_str()); |
229 |
|
|
mBlankString.append(mFileName.c_str()); |
230 |
|
|
|
231 |
|
|
}break; |
232 |
|
|
case Logger::CLOSE_FILE_OPERATION : { |
233 |
|
|
mBlankString.assign(mUnableToCloseMsg.c_str()); |
234 |
|
|
mBlankString.append(mFileMsg.c_str()); |
235 |
|
|
mBlankString.append(mFileName.c_str()); |
236 |
|
|
}; |
237 |
|
|
} |
238 |
|
|
std::cout << mBlankString << std::endl; |
239 |
|
|
return false; |
240 |
|
|
} |
241 |
|
|
else |
242 |
|
|
return true; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
bool Logger::logENDL(){ |
246 |
|
|
mOutStream << std::endl; |
247 |
|
|
return checkOutFileStreamError(Logger::LOG_MSG_OPERATION); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
bool Logger::logMsg(std::string& msg){ |
251 |
|
|
mOutStream << msg;// << std::endl; |
252 |
|
|
return checkOutFileStreamError(Logger::LOG_MSG_OPERATION, msg); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
bool Logger::logMsg(char* charstar_nullterminated){ |
256 |
|
|
mOutStream << charstar_nullterminated; |
257 |
|
|
return checkOutFileStreamError(Logger::LOG_MSG_OPERATION); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
bool Logger::logMsg(char* charstar, int lenght){ |
261 |
|
|
mOutStream.write(charstar,lenght); |
262 |
|
|
return checkOutFileStreamError(Logger::LOG_MSG_OPERATION); |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
bool Logger::logMsg(char* charstar, long int lenght){ |
266 |
|
|
mOutStream.write(charstar,lenght); |
267 |
|
|
return checkOutFileStreamError(Logger::LOG_MSG_OPERATION); |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
} |