Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Simple-logger: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
h1. 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 | ||
Line 5: | Line 5: | ||
Example of using | Example of using | ||
Logger. | <code><br />QPlainTextEdit *editor = new QPlainTextEdit(this);<br />QString fileName = "c:.txt&quot;;<br />Logger *logger = new Logger(this, fileName, editor);<br />logger.write("Hello Qt&quot;);<br /></code> | ||
Logger. | Logger.h<br /><code><br />#ifndef LOGGER_H<br />#define LOGGER_H | ||
#include <QObject&gt;<br />#include <QPlainTextEdit&gt;<br />#include <QFile&gt;<br />#include <QTextStream&gt;<br />#include <QDateTime&gt; | |||
* | class Logger : public QObject<br />{<br /> Q_OBJECT<br />public:<br /> explicit Logger(QObject *parent, QString fileName, QPlainTextEdit *editor = 0);<br /> ~Logger();<br /> void setShowDateTime(bool value); | ||
** | |||
private:<br /> QFile *file;<br /> QPlainTextEdit *m_editor;<br /> bool m_showDate; | |||
signals: | |||
public slots:<br /> void write(const QString &value); | |||
}; | |||
#endif // LOGGER_H<br /></code> | |||
Logger.cpp<br /><code><br />#include "Logger.h&quot; | |||
Logger::Logger(QObject *parent, QString fileName,<br /> QPlainTextEdit *editor) : QObject(parent) { | |||
m_editor = editor;<br /> m_showDate = true;<br /> if (!fileName.isEmpty()) {<br /> file = new QFile;<br /> file->setFileName(fileName);<br /> file->open(QIODevice::Append | QIODevice::Text);<br /> }<br />} | |||
void Logger::write(const QString &value) {<br /> QString text = value;// + "";<br /> if (m_showDate)<br /> text = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss ") + text;<br /> QTextStream out(file);<br /> out.setCodec("UTF-8&quot;);<br /> if (file != 0) {<br /> out << text;<br /> }<br /> if (m_editor != 0)<br /> m_editor->appendPlainText(text);<br />} | |||
void Logger::setShowDateTime(bool value) {<br /> m_showDate = value;<br />} | |||
Logger::~Logger() {<br /> if (file != 0)<br /> file->close();<br />}<br /></code> |
Revision as of 09:44, 24 February 2015
h1. 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
Example of using
<br />QPlainTextEdit *editor = new QPlainTextEdit(this);<br />QString fileName = "c:.txt&quot;;<br />Logger *logger = new Logger(this, fileName, editor);<br />logger.write("Hello Qt&quot;);<br />
Logger.h
<br />#ifndef LOGGER_H<br />#define LOGGER_H
#include <QObject&gt;<br />#include <QPlainTextEdit&gt;<br />#include <QFile&gt;<br />#include <QTextStream&gt;<br />#include <QDateTime&gt;
class Logger : public QObject<br />{<br /> Q_OBJECT<br />public:<br /> explicit Logger(QObject *parent, QString fileName, QPlainTextEdit *editor = 0);<br /> ~Logger();<br /> void setShowDateTime(bool value);
private:<br /> QFile *file;<br /> QPlainTextEdit *m_editor;<br /> bool m_showDate;
signals:
public slots:<br /> void write(const QString &value);
};
#endif // LOGGER_H<br />
Logger.cpp
<br />#include "Logger.h&quot;
Logger::Logger(QObject *parent, QString fileName,<br /> QPlainTextEdit *editor) : QObject(parent) {
m_editor = editor;<br /> m_showDate = true;<br /> if (!fileName.isEmpty()) {<br /> file = new QFile;<br /> file->setFileName(fileName);<br /> file->open(QIODevice::Append | QIODevice::Text);<br /> }<br />}
void Logger::write(const QString &value) {<br /> QString text = value;// + "";<br /> if (m_showDate)<br /> text = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss ") + text;<br /> QTextStream out(file);<br /> out.setCodec("UTF-8&quot;);<br /> if (file != 0) {<br /> out << text;<br /> }<br /> if (m_editor != 0)<br /> m_editor->appendPlainText(text);<br />}
void Logger::setShowDateTime(bool value) {<br /> m_showDate = value;<br />}
Logger::~Logger() {<br /> if (file != 0)<br /> file->close();<br />}<br />