22 #include <QTextStream>
28 #include <QApplication>
30 #include <QMutexLocker>
31 #include <QMessageBox>
37 class TextToAppend :
public QEvent {
39 TextToAppend( QString text, QString level, QString pureMessage ) :
43 pureMessage(pureMessage) { };
50 QString getPureMessage() {
59 int TextToAppend::type = QEvent::registerEventType();
61 class TextEditUpdater :
public QObject {
69 void setTextEditToUpdate(QTextEdit* e)
74 bool hasTextEdit()
const
76 return (textEdit != NULL);
80 virtual void customEvent( QEvent* event ) {
82 TextToAppend* tevent =
dynamic_cast<TextToAppend*
>( event );
84 textEdit->append( tevent->getText() );
85 textEdit->moveCursor( QTextCursor::End );
86 textEdit->moveCursor( QTextCursor::StartOfLine );
87 if ( tevent->getLevel() ==
"ERROR" ) {
88 QMessageBox::critical( 0,
"Error from Component", tevent->getPureMessage() );
92 QObject::customEvent( event );
102 class LoggerImplementation
106 static LoggerImplementation& getInstance();
108 void info(QString msg);
110 void warning(QString msg);
112 void error(QString msg);
114 void setQTextEdit(QTextEdit* textedit);
116 void enableStdOut(
bool enabled);
120 void setLogFilename(QString logfile);
124 LoggerImplementation();
127 ~LoggerImplementation();
130 void logIt(QString level, QString msg);
134 QTextStream* fileStream;
135 QTextStream* outStream;
136 TextEditUpdater* textEditUpdater;
139 QMutex outStreamMutex;
140 QMutex fileStreamMutex;
144 LoggerImplementation(LoggerImplementation&);
147 LoggerImplementation& operator=(LoggerImplementation&);
150 LoggerImplementation& LoggerImplementation::getInstance()
153 static LoggerImplementation loggerImplementation;
155 return loggerImplementation;
158 void LoggerImplementation::info(QString msg)
165 void LoggerImplementation::warning(QString msg)
168 logIt(
"WARNING", msg);
172 void LoggerImplementation::error(QString msg)
179 void LoggerImplementation::setQTextEdit(QTextEdit* textedit)
181 textEditUpdater->setTextEditToUpdate(textedit);
184 void LoggerImplementation::enableStdOut(
bool enabled)
194 void LoggerImplementation::setLogFilename(QString logfile)
196 QFile* oldfile = file;
197 file =
new QFile(logfile);
198 fileStream->setDevice(file);
203 LoggerImplementation::LoggerImplementation() :
206 fileStream(new QTextStream()),
207 outStream(new QTextStream(stdout)),
208 textEditUpdater(new TextEditUpdater()),
209 logLevel(Logger::LogAll),
215 LoggerImplementation::~LoggerImplementation()
217 delete textEditUpdater;
224 textEditUpdater = NULL;
231 void LoggerImplementation::logIt(QString level, QString msg)
233 QString logtmpl(
"[%1] %2: %3");
234 QString timestamp = QDateTime::currentDateTime().toString(
"dd-MM-yyyy hh:mm:ss.zzz" );
235 QString logmsg = logtmpl.arg( timestamp ).arg( level, -10 ).arg( msg );
237 QMutexLocker locker(&outStreamMutex);
239 (*outStream) << logmsg <<
"\n";
242 if ( textEditUpdater->hasTextEdit() ) {
244 QString color =
"#ffffff";
245 if ( level ==
"INFO" ) {
247 }
else if ( level ==
"WARNING" ) {
249 }
else if ( level ==
"ERROR" ) {
255 qApp->postEvent( textEditUpdater,
new TextToAppend( QString(
"<pre style=\"margin-top: 0px; margin-bottom: 0px; color: ")+color+
";\">"+logmsg+QString(
"</pre>"), level, msg ) );
260 if ( fileStream && fileStream->device() ) {
261 QMutexLocker locker(&fileStreamMutex);
263 (*fileStream) << logmsg <<
"\n";
271 LoggerImplementation::getInstance().info(msg);
276 LoggerImplementation::getInstance().warning(msg);
281 LoggerImplementation::getInstance().error(msg);
286 LoggerImplementation::getInstance().setLogFilename(logfile);
291 LoggerImplementation::getInstance().setQTextEdit(textedit);
296 LoggerImplementation::getInstance().enableStdOut(enabled);
300 LoggerImplementation::getInstance().setLogLevel(level);
305 QString str =
"unknown";
330 if (level.toUpper() ==
"LOGALL") {
332 }
else if (level.toUpper() ==
"WARNING") {
334 }
else if (level.toUpper() ==
"QUIET") {
336 }
else if (level.toUpper() ==
"SUPERQUIET") {