| 
									
										
										
										
											2017-11-11 10:29:32 +01:00
										 |  |  | /**
 | 
					
						
							|  |  |  |   @file | 
					
						
							|  |  |  |   @author Stefan Frings | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "logmessage.h"
 | 
					
						
							|  |  |  | #include <QThread>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace qtwebapp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LogMessage::LogMessage(const QtMsgType type, const QString& message, QHash<QString, QString>* logVars, const QString &file, const QString &function, const int line) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     this->type=type; | 
					
						
							|  |  |  |     this->message=message; | 
					
						
							|  |  |  |     this->file=file; | 
					
						
							|  |  |  |     this->function=function; | 
					
						
							|  |  |  |     this->line=line; | 
					
						
							|  |  |  |     timestamp=QDateTime::currentDateTime(); | 
					
						
							|  |  |  |     threadId=QThread::currentThreadId(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Copy the logVars if not null,
 | 
					
						
							|  |  |  |     // so that later changes in the original do not affect the copy
 | 
					
						
							|  |  |  |     if (logVars) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         this->logVars=*logVars; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString LogMessage::toString(const QString& msgFormat, const QString& timestampFormat) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QString decorated=msgFormat+"\n"; | 
					
						
							|  |  |  |     decorated.replace("{msg}",message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (decorated.contains("{timestamp}")) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         decorated.replace("{timestamp}",timestamp.toString(timestampFormat)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString typeNr; | 
					
						
							|  |  |  |     typeNr.setNum(type); | 
					
						
							|  |  |  |     decorated.replace("{typeNr}",typeNr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         case QtDebugMsg: | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |             decorated.replace("{type}","(D)"); | 
					
						
							| 
									
										
										
										
											2017-11-11 10:29:32 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case QtWarningMsg: | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |             decorated.replace("{type}","(W)"); | 
					
						
							| 
									
										
										
										
											2017-11-11 10:29:32 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case QtCriticalMsg: | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |             decorated.replace("{type}","(C)"); | 
					
						
							| 
									
										
										
										
											2017-11-11 10:29:32 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case QtFatalMsg: | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |             decorated.replace("{type}","(F)"); | 
					
						
							| 
									
										
										
										
											2017-11-11 10:29:32 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |     #if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
 | 
					
						
							|  |  |  |         case QtInfoMsg: | 
					
						
							| 
									
										
										
										
											2017-11-11 11:11:44 +01:00
										 |  |  |             decorated.replace("{type}","(I)"); | 
					
						
							| 
									
										
										
										
											2017-11-11 10:29:32 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |     #endif
 | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             decorated.replace("{type}",typeNr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     decorated.replace("{file}",file); | 
					
						
							|  |  |  |     decorated.replace("{function}",function); | 
					
						
							|  |  |  |     decorated.replace("{line}",QString::number(line)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString threadId; | 
					
						
							| 
									
										
										
										
											2017-11-12 03:00:54 +01:00
										 |  |  |     threadId.setNum((std::size_t)QThread::currentThreadId()); | 
					
						
							| 
									
										
										
										
											2017-11-11 10:29:32 +01:00
										 |  |  |     decorated.replace("{thread}",threadId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Fill in variables
 | 
					
						
							|  |  |  |     if (decorated.contains("{") && !logVars.isEmpty()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QList<QString> keys=logVars.keys(); | 
					
						
							|  |  |  |         foreach (QString key, keys) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             decorated.replace("{"+key+"}",logVars.value(key)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return decorated; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QtMsgType LogMessage::getType() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return type; | 
					
						
							|  |  |  | } |