15 #include <QTextStream> 35 retval.mChannel = channel;
41 return QString(
"%1/org.custusx.log.%2.txt").arg(mPath).arg(mChannel);
47 retval.mPath = QFileInfo(filename).path();
48 retval.mChannel = QFileInfo(filename).completeBaseName().split(
".").back();
55 QString formatInfo =
"[timestamp][source info][severity][thread] <text> ";
56 QString text = QString(
"-------> Logging initialized [%1], format: %2\n").arg(timestamp).arg(formatInfo);
57 bool success = this->appendToLogfile(this->
getFilename(), text);
63 QString text = this->formatMessage(message) +
"\n";
70 if (filename.isEmpty())
74 return file.open(QFile::WriteOnly | QFile::Append);
78 QString LogFile::timestampFormat()
const 80 return "hh:mm:ss.zzz";
83 QString LogFile::formatMessage(
Message msg)
88 retval += QString(
"[%1]").arg(msg.
getTimeStamp().toString(this->timestampFormat()));
92 retval += QString(
"[%1]").arg(msg.
mThread);
112 bool LogFile::appendToLogfile(QString filename, QString text)
114 if (filename.isEmpty())
117 QFile file(filename);
120 if (!file.open(QFile::WriteOnly | QFile::Append))
125 stream.setDevice(&file);
132 QRegExp LogFile::getRX_Timestamp()
const 134 return QRegExp(
"\\[(\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d)\\]");
139 QString text = this->readFileTail();
142 if (text.endsWith(
"\n"))
145 QStringList lines = text.split(
"\n");
147 QRegExp rx_ts = this->getRX_Timestamp();
149 std::vector<Message> retval;
151 for (
int i=0; i<lines.size(); ++i)
153 QString line = lines[i];
155 QDateTime timestamp = this->readTimestampFromSessionStartLine(lines[i]);
156 if (timestamp.isValid())
158 mInitTimestamp = timestamp;
163 retval.push_back(msg);
167 if (line.count(rx_ts))
169 Message msg = this->readMessageFirstLine(lines[i]);
171 retval.push_back(msg);
176 retval.back().mText +=
"\n"+line;
183 Message LogFile::readMessageFirstLine(QString line)
185 MESSAGE_LEVEL level = this->readMessageLevel(line);
188 QString levelSymbol = QString(
"[%1]").arg(enum2string<MESSAGE_LEVEL>(level));
189 QStringList parts = line.split(levelSymbol);
194 QString description = parts[1];
195 QStringList fields = parts[0].split(
"\t");
197 Message retval(description, level);
199 this->parseTimestamp(this->getIndex(fields, 0), &retval);
200 this->parseThread(this->getIndex(fields, 1), &retval);
201 this->parseSourceFileLine(this->getIndex(fields, 2), &retval);
202 this->parseSourceFunction(this->getIndex(fields, 3), &retval);
207 QString LogFile::getIndex(
const QStringList& list,
int index)
209 if (0>index || index >= list.size())
211 QString field = list[index];
212 if (field.startsWith(
"["))
214 if (field.endsWith(
"]"))
219 void LogFile::parseTimestamp(QString text,
Message* retval)
225 QTime time = QTime::fromString(text, this->timestampFormat());
229 void LogFile::parseThread(QString text,
Message* retval)
234 void LogFile::parseSourceFileLine(QString text,
Message* retval)
236 if (!text.count(
":"))
239 QStringList parts = text.split(
":");
248 void LogFile::parseSourceFunction(QString text,
Message* retval)
250 if (!text.count(
"("))
255 MESSAGE_LEVEL LogFile::readMessageLevel(QString line)
258 for (
int i=0; i<mlCOUNT; ++i)
259 levels << enum2string<MESSAGE_LEVEL>((MESSAGE_LEVEL)(i));
260 QRegExp rx_level(QString(
"\\[(%1)\\]").arg(levels.join(
"|")));
262 int pos = rx_level.indexIn(line);
263 QStringList hits = rx_level.capturedTexts();
268 return string2enum<MESSAGE_LEVEL>(hits[1]);
271 QDateTime LogFile::readTimestampFromSessionStartLine(QString text)
273 QString sessionStartSymbol(
"------->");
274 if (!text.startsWith(sessionStartSymbol))
277 QRegExp rx_ts_start(
"\\[([^\\]]*)");
278 if (text.indexOf(rx_ts_start) < 0)
281 QString rawTime = rx_ts_start.cap(1);
283 QDateTime ts = QDateTime::fromString(rawTime, format);
288 QString LogFile::readFileTail()
291 file.open(QIODevice::ReadOnly);
293 file.seek(mFilePosition);
294 QString text = file.readAll();
295 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.
Namespace for all CustusX production code.