kpmcore/src/util/globallog.h

82 lines
1.5 KiB
C
Raw Normal View History

/*
SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
SPDX-FileCopyrightText: 2014-2018 Andrius Štikonas <andrius@stikonas.eu>
SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#ifndef KPMCORE_GLOBALLOG_H
#define KPMCORE_GLOBALLOG_H
#include "util/libpartitionmanagerexport.h"
#include <QString>
#include <QObject>
2016-04-18 17:14:31 +01:00
#include <QtGlobal>
class LIBKPMCORE_EXPORT Log
{
2015-07-13 15:16:36 +01:00
public:
2018-04-09 15:14:34 +01:00
enum class Level {
debug,
information,
warning,
error,
2015-07-13 15:16:36 +01:00
};
public:
2018-04-09 15:14:34 +01:00
Log(Level lev = Level::information) : ref(1), level(lev) {}
2015-07-13 15:16:36 +01:00
~Log();
Log(const Log& other) : ref(other.ref + 1), level(other.level) {}
private:
quint32 ref;
Level level;
};
/** Global logging.
2015-07-13 15:16:36 +01:00
@author Volker Lanz <vl@fidra.de>
*/
class LIBKPMCORE_EXPORT GlobalLog : public QObject
{
2015-07-13 15:16:36 +01:00
Q_OBJECT
Q_DISABLE_COPY(GlobalLog)
2015-07-13 15:16:36 +01:00
friend class Log;
friend Log operator<<(Log l, const QString& s);
friend Log operator<<(Log l, qint64 i);
2015-07-13 15:16:36 +01:00
private:
GlobalLog() : msg() {}
2015-07-13 15:16:36 +01:00
Q_SIGNALS:
void newMessage(Log::Level, const QString&);
2015-07-13 15:16:36 +01:00
public:
static GlobalLog* instance();
2015-07-13 15:16:36 +01:00
private:
void append(const QString& s) {
msg += s;
}
void flush(Log::Level level);
2015-07-13 15:16:36 +01:00
private:
QString msg;
};
inline Log operator<<(Log l, const QString& s)
{
2015-07-13 15:16:36 +01:00
GlobalLog::instance()->append(s);
return l;
}
inline Log operator<<(Log l, qint64 i)
{
2015-07-13 15:16:36 +01:00
GlobalLog::instance()->append(QString::number(i));
return l;
}
#endif