diff --git a/cli/logger.cpp b/cli/logger.cpp new file mode 100644 index 0000000..7c61bef --- /dev/null +++ b/cli/logger.cpp @@ -0,0 +1,36 @@ +#include +#include "logger.h" + +Logger::Logger(QFile *file) +{ + out = new QDebug(file); + this->file = file; +} + +Logger::Logger(Logger &&o) +{ + this->out = o.out; + o.out = nullptr; +} + +Logger &Logger::error() +{ + static Logger result = [] + { + QFile *file = new QFile(); + file->open(stderr, QIODevice::WriteOnly); + return Logger(file); + }(); + return result; +} + +Logger &Logger::info() +{ + static Logger result = [] + { + QFile *file = new QFile(); + file->open(stdout, QIODevice::WriteOnly); + return Logger(file); + }(); + return result; +} diff --git a/cli/logger.h b/cli/logger.h new file mode 100644 index 0000000..9d5281f --- /dev/null +++ b/cli/logger.h @@ -0,0 +1,43 @@ +#ifndef LOGGER_H +#define LOGGER_H +#include +#include +#include +class Logger +{ + private: + QDebug *out; + QMutex mutex; + QFile *file; + + public: + Logger(QFile *file); + Logger(Logger &&o); + + template Logger &operator<<(const T &val) + { + QMutexLocker locker(&this->mutex); + *out << val; + file->flush(); + return *this; + } + + template Logger &operator<<(const T *val) + { + QMutexLocker locker(&this->mutex); + *out << val; + file->flush(); + return *this; + } + + ~Logger() + { + delete out; + } + + static Logger &error(); + + static Logger &info(); +}; + +#endif // LOGGER_H