Skip to content

File abstractlogger.cpp#

File List > src > utils > abstractlogger.cpp

Go to the documentation of this file.


#include "abstractlogger.h"
#include "systemnotification.h"
#include <cassert>

#include <QFileInfo>

AbstractLogger::AbstractLogger(Channel channel, int targets)
  : m_defaultChannel(channel)
  , m_targets(targets)
{
    if (targets & LogFile) {
        // TODO
    }
}

AbstractLogger::AbstractLogger(QString& str,
                               Channel channel,
                               int additionalChannels)
  : AbstractLogger(channel, additionalChannels)
{
    m_textStreams << new QTextStream(&str);
}

AbstractLogger::~AbstractLogger()
{
    qDeleteAll(m_textStreams);
}

AbstractLogger AbstractLogger::info(int targets)
{
    return { Info, targets };
}

AbstractLogger AbstractLogger::warning(int targets)
{
    return { Warning, targets };
}

AbstractLogger AbstractLogger::error(int targets)
{
    return { Error, targets };
}

AbstractLogger& AbstractLogger::sendMessage(const QString& msg, Channel channel)
{
    if (m_targets & Notification) {
        SystemNotification().sendMessage(
          msg, messageHeader(channel, Notification), m_notificationPath);
    }
    if (!m_textStreams.isEmpty()) {
        foreach (auto* stream, m_textStreams) {
            *stream << messageHeader(channel, String) << msg << "\n";
        }
    }
    if (m_targets & LogFile) {
        // TODO
    }
    if (m_targets & Stderr) {
        QTextStream stream(stderr);
        stream << messageHeader(channel, Stderr) << msg << "\n";
    }

    if (m_targets & Stdout) {
        QTextStream stream(stdout);
        stream << messageHeader(channel, Stdout) << msg << "\n";
    }

    return *this;
}

AbstractLogger& AbstractLogger::operator<<(const QString& msg)
{
    sendMessage(msg, m_defaultChannel);
    return *this;
}

AbstractLogger& AbstractLogger::addOutputString(QString& str)
{
    m_textStreams << new QTextStream(&str);
    return *this;
}

AbstractLogger& AbstractLogger::attachNotificationPath(const QString& path)
{
    if (m_targets & Notification) {
        m_notificationPath = path;
    } else {
        assert("Cannot attach notification path to a logger without a "
               "notification channel.");
    }
    return *this;
}

AbstractLogger& AbstractLogger::enableMessageHeader(bool enable)
{
    m_enableMessageHeader = enable;
    return *this;
}

QString AbstractLogger::messageHeader(Channel channel, Target target)
{
    if (!m_enableMessageHeader) {
        return "";
    }
    QString messageChannel;
    if (channel == Info) {
        messageChannel = "info";
    } else if (channel == Warning) {
        messageChannel = "warning";
    } else if (channel == Error) {
        messageChannel = "error";
    }

    if (target == Notification) {
        messageChannel[0] = messageChannel[0].toUpper();
        return "Flameshot " + messageChannel;
    } else {
        return "flameshot: " + messageChannel + ": ";
    }
}