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 TextEditUpdater* textEditUpdater;
137 QMutex outStreamMutex;
138 QMutex fileStreamMutex;
142 LoggerImplementation(LoggerImplementation&);
145 LoggerImplementation& operator=(LoggerImplementation&);
148 LoggerImplementation& LoggerImplementation::getInstance()
151 static LoggerImplementation loggerImplementation;
153 return loggerImplementation;
156 void LoggerImplementation::info(QString msg)
163 void LoggerImplementation::warning(QString msg)
166 logIt(
"WARNING", msg);
170 void LoggerImplementation::error(QString msg)
177 void LoggerImplementation::setQTextEdit(QTextEdit* textedit)
179 textEditUpdater->setTextEditToUpdate(textedit);
182 void LoggerImplementation::enableStdOut(
bool enabled)
192 void LoggerImplementation::setLogFilename(QString logfile)
195 file =
new QFile(logfile);
198 LoggerImplementation::LoggerImplementation() :
201 textEditUpdater(new TextEditUpdater()),
202 logLevel(Logger::LogAll),
208 LoggerImplementation::~LoggerImplementation()
210 delete textEditUpdater;
215 textEditUpdater = NULL;
220 void LoggerImplementation::logIt(QString level, QString msg)
222 QString logtmpl(
"[%1] %2: %3");
223 QString timestamp = QDateTime::currentDateTime().toString(
"dd-MM-yyyy hh:mm:ss.zzz" );
224 QString logmsg = logtmpl.arg( timestamp ).arg( level, -10 ).arg( msg );
226 QMutexLocker locker(&outStreamMutex);
228 QTextStream outStream(stdout, QIODevice::WriteOnly);
229 outStream << logmsg <<
"\n";
231 if ( textEditUpdater->hasTextEdit() ) {
233 QString color =
"#ffffff";
234 if ( level ==
"INFO" ) {
236 }
else if ( level ==
"WARNING" ) {
238 }
else if ( level ==
"ERROR" ) {
244 qApp->postEvent( textEditUpdater,
new TextToAppend( QString(
"<pre style=\"margin-top: 0px; margin-bottom: 0px; color: ")+color+
";\">"+logmsg+QString(
"</pre>"), level, msg ) );
249 if ( file != NULL ) {
250 QMutexLocker locker(&fileStreamMutex);
252 QTextStream fileStream;
253 fileStream.setDevice(file);
254 fileStream << logmsg <<
"\n";
261 LoggerImplementation::getInstance().info(msg);
266 LoggerImplementation::getInstance().warning(msg);
271 LoggerImplementation::getInstance().error(msg);
276 LoggerImplementation::getInstance().setLogFilename(logfile);
281 LoggerImplementation::getInstance().setQTextEdit(textedit);
286 LoggerImplementation::getInstance().enableStdOut(enabled);
290 LoggerImplementation::getInstance().setLogLevel(level);
295 QString str =
"unknown";
320 if (level.toUpper() ==
"LOGALL") {
322 }
else if (level.toUpper() ==
"WARNING") {
324 }
else if (level.toUpper() ==
"QUIET") {
326 }
else if (level.toUpper() ==
"SUPERQUIET") {
static void enableStdOut(bool enabled)
Enable/Disable the printing of log messages on the standard out.
static void setLogFilename(QString logfile)
Set the filename where to log messages.
static LogLevel stringToLogLevel(QString level)
Returns the log level given its string representation.
A macro to deprecate functions.
static void setQTextEdit(QTextEdit *textedit)
Set a QTextEdit dedicated for showing log messages.
static void setLogLevel(LogLevel level)
Sets the current log level (which influences what is logged and what isn't).
static void info(QString msg)
Log an informative message.
static void error(QString msg)
Log an error message.
static QString logLevelToString(LogLevel level)
Returns the string representation of the given log level.
Only warnings and errors are logged.
static void warning(QString msg)
Log a warning message.