36 #include <QTextStream>
56 retval.mChannel = channel;
62 return QString(
"%1/org.custusx.log.%2.txt").arg(mPath).arg(mChannel);
68 retval.mPath = QFileInfo(filename).path();
69 retval.mChannel = QFileInfo(filename).completeBaseName().split(
".").back();
76 QString formatInfo =
"[timestamp][source info][severity][thread] <text> ";
77 QString text = QString(
"-------> Logging initialized [%1], format: %2\n").arg(timestamp).arg(formatInfo);
78 bool success = this->appendToLogfile(this->
getFilename(), text);
84 QString text = this->formatMessage(message) +
"\n";
91 if (filename.isEmpty())
95 return file.open(QFile::WriteOnly | QFile::Append);
99 QString LogFile::timestampFormat()
const
101 return "hh:mm:ss.zzz";
104 QString LogFile::formatMessage(
Message msg)
109 retval += QString(
"[%1]").arg(msg.
getTimeStamp().toString(this->timestampFormat()));
113 retval += QString(
"[%1]").arg(msg.
mThread);
133 bool LogFile::appendToLogfile(QString filename, QString text)
135 if (filename.isEmpty())
138 QFile file(filename);
141 if (!file.open(QFile::WriteOnly | QFile::Append))
146 stream.setDevice(&file);
153 QRegExp LogFile::getRX_Timestamp()
const
155 return QRegExp(
"\\[(\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d)\\]");
160 QString text = this->readFileTail();
163 if (text.endsWith(
"\n"))
166 QStringList lines = text.split(
"\n");
168 QRegExp rx_ts = this->getRX_Timestamp();
170 std::vector<Message> retval;
172 for (
int i=0; i<lines.size(); ++i)
174 QString line = lines[i];
176 QDateTime timestamp = this->readTimestampFromSessionStartLine(lines[i]);
177 if (timestamp.isValid())
179 mInitTimestamp = timestamp;
184 retval.push_back(msg);
188 if (line.count(rx_ts))
190 Message msg = this->readMessageFirstLine(lines[i]);
192 retval.push_back(msg);
197 retval.back().mText +=
"\n"+line;
204 Message LogFile::readMessageFirstLine(QString line)
206 MESSAGE_LEVEL level = this->readMessageLevel(line);
209 QString levelSymbol = QString(
"[%1]").arg(enum2string<MESSAGE_LEVEL>(level));
210 QStringList parts = line.split(levelSymbol);
215 QString description = parts[1];
216 QStringList fields = parts[0].split(
"\t");
218 Message retval(description, level);
220 this->parseTimestamp(this->getIndex(fields, 0), &retval);
221 this->parseThread(this->getIndex(fields, 1), &retval);
222 this->parseSourceFileLine(this->getIndex(fields, 2), &retval);
223 this->parseSourceFunction(this->getIndex(fields, 3), &retval);
228 QString LogFile::getIndex(
const QStringList& list,
int index)
230 if (0>index || index >= list.size())
232 QString field = list[index];
233 if (field.startsWith(
"["))
235 if (field.endsWith(
"]"))
240 void LogFile::parseTimestamp(QString text,
Message* retval)
246 QTime time = QTime::fromString(text, this->timestampFormat());
250 void LogFile::parseThread(QString text,
Message* retval)
255 void LogFile::parseSourceFileLine(QString text,
Message* retval)
257 if (!text.count(
":"))
260 QStringList parts = text.split(
":");
269 void LogFile::parseSourceFunction(QString text,
Message* retval)
271 if (!text.count(
"("))
276 MESSAGE_LEVEL LogFile::readMessageLevel(QString line)
279 for (
int i=0; i<mlCOUNT; ++i)
280 levels << enum2string<MESSAGE_LEVEL>((MESSAGE_LEVEL)(i));
281 QRegExp rx_level(QString(
"\\[(%1)\\]").arg(levels.join(
"|")));
283 int pos = rx_level.indexIn(line);
284 QStringList hits = rx_level.capturedTexts();
289 return string2enum<MESSAGE_LEVEL>(hits[1]);
292 QDateTime LogFile::readTimestampFromSessionStartLine(QString text)
294 QString sessionStartSymbol(
"------->");
295 if (!text.startsWith(sessionStartSymbol))
298 QRegExp rx_ts_start(
"\\[([^\\]]*)");
299 if (text.indexOf(rx_ts_start) < 0)
302 QString rawTime = rx_ts_start.cap(1);
304 QDateTime ts = QDateTime::fromString(rawTime, format);
309 QString LogFile::readFileTail()
312 file.open(QIODevice::ReadOnly);
314 file.seek(mFilePosition);
315 QString text = file.readAll();
316 mFilePosition = file.pos();
QString qstring_cast(const T &val)
std::vector< Message > readMessages()
QString getText() const
The raw message.
void write(Message message)
QDateTime getTimeStamp() const
The time at which the message was created.
static LogFile fromFilename(QString filename)
static LogFile fromChannel(QString path, QString channel)
QString getFilename() const
QString timestampMilliSecondsFormatNice()
MESSAGE_LEVEL getMessageLevel() const
The category of the message.