Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LOG_H
- #define LOG_H
- #include "singleton.h"
- #include <QTextStream>
- /*!
- * \brief Provides log
- * \author Etrnls <[email protected]>
- */
- class Log : public QObject, public Singleton<Log>
- {
- Q_OBJECT
- public:
- template <class T>
- Log& operator<<(const T &x)
- {
- stream << ' ' << x;
- return *this;
- }
- private:
- QTextStream stream;
- Log();
- virtual ~Log();
- friend class Singleton<Log>;
- friend Log& log(const QObject *object);
- };
- /*!
- * \brief The global function to retrieve the instance of the Log
- */
- Log& log(const QObject *object);
- #endif
- //-------------------------------------
- #include "log.h"
- #include <QFile>
- #include <QDir>
- #include <QFileInfo>
- #include <QMetaClassInfo>
- #include <QDateTime>
- #include <QCoreApplication>
- Log::Log()
- {
- QFile *file = new QFile(QFileInfo(QDir(QCoreApplication::applicationDirPath()),
- QLatin1String("log")).absoluteFilePath());
- file->open(QFile::WriteOnly | QFile::Truncate | QFile::Text | QFile::Unbuffered);
- stream.setDevice(file);
- }
- Log::~Log()
- {
- delete stream.device();
- }
- Log& log(const QObject *object)
- {
- const QMetaObject *metaObject = object->metaObject();
- Log::getInstance()->stream << '[' << QDateTime::currentDateTime().toString(Qt::ISODate) << ']'
- << '[' << metaObject->classInfo(metaObject->indexOfClassInfo("log")).value() << ']';
- return *Log::getInstance();
- }
Advertisement
Add Comment
Please, Sign In to add comment