-
Notifications
You must be signed in to change notification settings - Fork 60
/
logger.cpp
42 lines (34 loc) · 975 Bytes
/
logger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "logger.h"
Logger::Logger(QObject *parent, QString fileName, QPlainTextEdit *editor) : QObject(parent) {
m_editor = editor;
m_showDate = false;
if (!fileName.isEmpty()) {
file = new QFile;
file->setFileName(fileName);
file->open(QIODevice::Append | QIODevice::Text);
}
}
void Logger::write(const QString &value) {
QString text = value + " ";
if (m_showDate)
text = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss ") + text;
QTextStream out(file);
out.setCodec("UTF-8");
if (file != 0) {
out << text;
}
if (m_editor != 0)
{
QTextCursor prev_cursor = m_editor->textCursor();
m_editor->moveCursor (QTextCursor::End);
m_editor->insertPlainText (text);
m_editor->setTextCursor(prev_cursor);
}
}
void Logger::setShowDateTime(bool value) {
m_showDate = value;
}
Logger::~Logger() {
if (file != 0)
file->close();
}