Simple-logger: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(clean-up)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Simple Logger=
This code shows as possibly of logging with recording to the file. Also if necessary it is possible show logging information in QPlainTextEdit
This code shows as possibly of logging with recording to the file. Also if necessary it is possible show logging information in QPlainTextEdit


Example of using
Example of using


Logger.h<br />
<code>
QPlainTextEdit *editor = new QPlainTextEdit(this);
QString fileName = "c:.txt";
Logger *logger = new Logger(this, fileName, editor);
logger.write("Hello Qt");
</code>
 
Logger.h
<code>
#ifndef LOGGER_H
#define LOGGER_H
 
#include <QObject>
#include <QPlainTextEdit>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
 
class Logger : public QObject
{
Q_OBJECT
public:
explicit Logger(QObject *parent, QString fileName, QPlainTextEdit *editor = 0);
~Logger();
void setShowDateTime(bool value);
 
private:
QFile *file;
QPlainTextEdit *m_editor;
bool m_showDate;
 
signals:
 
public slots:
void write(const QString &value);
 
};
 
#endif // LOGGER_H
</code>
 
Logger.cpp
 
<code>
#include "Logger.h"
 
Logger::Logger(QObject *parent, QString fileName, QPlainTextEdit *editor) : QObject(parent) {
m_editor = editor;
m_showDate = true;
if (!fileName.isEmpty()) {
  file = new QFile;
  file->setFileName(fileName);
  file->open(QIODevice::Append | QIODevice::Text);
}
}


Logger.cpp<br />
void Logger::write(const QString &value) {
QString text = value;// + "";
if (m_showDate)
  text = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss ") + text;
QTextStream out(file);
out.setCodec("UTF-8");
if (file != 0) {
  out << text;
}
if (m_editor != 0)
  m_editor->appendPlainText(text);
}


===Categories:===
void Logger::setShowDateTime(bool value) {
m_showDate = value;
}


* [[:Category:Learning|Learning]]
Logger::~Logger() {
** [[:Category:Learning::Demos and Examples|Demos_and_Examples]]
if (file != 0)
file->close();
}
</code>

Latest revision as of 14:52, 24 March 2016

This code shows as possibly of logging with recording to the file. Also if necessary it is possible show logging information in QPlainTextEdit

Example of using

QPlainTextEdit *editor = new QPlainTextEdit(this);
QString fileName = "c:.txt";
Logger *logger = new Logger(this, fileName, editor);
logger.write("Hello Qt");

Logger.h

#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QPlainTextEdit>
#include <QFile>
#include <QTextStream>
#include <QDateTime>

class Logger : public QObject
{
 Q_OBJECT
public:
 explicit Logger(QObject *parent, QString fileName, QPlainTextEdit *editor = 0);
 ~Logger();
 void setShowDateTime(bool value);

private:
 QFile *file;
 QPlainTextEdit *m_editor;
 bool m_showDate;

signals:

public slots:
 void write(const QString &value);

};

#endif // LOGGER_H

Logger.cpp

#include "Logger.h"

Logger::Logger(QObject *parent, QString fileName, QPlainTextEdit *editor) : QObject(parent) {
 m_editor = editor;
 m_showDate = true;
 if (!fileName.isEmpty()) {
  file = new QFile;
  file->setFileName(fileName);
  file->open(QIODevice::Append | QIODevice::Text);
 }
}

void Logger::write(const QString &value) {
 QString text = value;// + "";
 if (m_showDate)
  text = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss ") + text;
 QTextStream out(file);
 out.setCodec("UTF-8");
 if (file != 0) {
  out << text;
 }
 if (m_editor != 0)
  m_editor->appendPlainText(text);
}

void Logger::setShowDateTime(bool value) {
 m_showDate = value;
}

Logger::~Logger() {
 if (file != 0)
 file->close();
}