-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.cpp
45 lines (39 loc) · 974 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
43
44
45
// Copyright 2018 AtomicSwap Solutions Ltd. All rights reserved.
// Use of this source code is governed by Microsoft Reference Source
// License (MS-RSL) that can be found in the LICENSE file.
#include "logger.h"
#include <QDebug>
#include <QTextStream>
#include <QDateTime>
#include <QDir>
namespace {
const QString loggerFileName = "info.log";
}
Logger::Logger() :
file(nullptr)
{
file = new QFile;
file->setFileName(loggerFileName);
file->open(QIODevice::WriteOnly | QIODevice::Text);
}
Logger::~Logger()
{
if (file) {
file->close();
delete file;
}
}
Logger& Logger::info()
{
static Logger logger;
return logger;
}
void Logger::operator << (const QString& str)
{
int pos = str.lastIndexOf("\n");
qDebug().noquote() << str.left(pos);
if (file) {
QTextStream out(file);
out << "[" << QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss") << "] " << str.left(pos) << endl;
}
}