File indexing completed on 2024-04-28 05:45:58

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014-2018 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
0005 
0006     SPDX-License-Identifier: GPL-3.0-or-later
0007 */
0008 
0009 #ifndef KPMCORE_GLOBALLOG_H
0010 #define KPMCORE_GLOBALLOG_H
0011 
0012 #include "util/libpartitionmanagerexport.h"
0013 
0014 #include <QString>
0015 #include <QObject>
0016 #include <QtGlobal>
0017 
0018 class LIBKPMCORE_EXPORT Log
0019 {
0020 public:
0021     enum class Level {
0022         debug,
0023         information,
0024         warning,
0025         error,
0026     };
0027 
0028 public:
0029     Log(Level lev = Level::information) : ref(1), level(lev) {}
0030     ~Log();
0031     Log(const Log& other) : ref(other.ref + 1), level(other.level) {}
0032 
0033 private:
0034     quint32 ref;
0035     Level level;
0036 };
0037 
0038 /** Global logging.
0039     @author Volker Lanz <vl@fidra.de>
0040 */
0041 class LIBKPMCORE_EXPORT GlobalLog : public QObject
0042 {
0043     Q_OBJECT
0044     Q_DISABLE_COPY(GlobalLog)
0045 
0046     friend class Log;
0047     friend Log operator<<(Log l, const QString& s);
0048     friend Log operator<<(Log l, qint64 i);
0049 
0050 private:
0051     GlobalLog() : msg() {}
0052 
0053 Q_SIGNALS:
0054     void newMessage(Log::Level, const QString&);
0055 
0056 public:
0057     static GlobalLog* instance();
0058 
0059 private:
0060     void append(const QString& s) {
0061         msg += s;
0062     }
0063     void flush(Log::Level level);
0064 
0065 private:
0066     QString msg;
0067 };
0068 
0069 inline Log operator<<(Log l, const QString& s)
0070 {
0071     GlobalLog::instance()->append(s);
0072     return l;
0073 }
0074 
0075 inline Log operator<<(Log l, qint64 i)
0076 {
0077     GlobalLog::instance()->append(QString::number(i));
0078     return l;
0079 }
0080 
0081 #endif