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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 5: Line 5:
Example of using
Example of using


<code><br />QPlainTextEdit *editor = new QPlainTextEdit(this);<br />QString fileName = &quot;c:.txt&amp;quot;;<br />Logger *logger = new Logger(this, fileName, editor);<br />logger.write(&quot;Hello Qt&amp;quot;);<br /></code>
<code>
QPlainTextEdit *editor = new QPlainTextEdit(this);
QString fileName = "c:.txt";
Logger *logger = new Logger(this, fileName, editor);
logger.write("Hello Qt");
</code>


Logger.h<br /><code><br />#ifndef LOGGER_H<br />#define LOGGER_H
Logger.h
<code>
#ifndef LOGGER_H
#define LOGGER_H


#include &lt;QObject&amp;gt;<br />#include &lt;QPlainTextEdit&amp;gt;<br />#include &lt;QFile&amp;gt;<br />#include &lt;QTextStream&amp;gt;<br />#include &lt;QDateTime&amp;gt;
#include <QObject>
#include <QPlainTextEdit>
#include <QFile>
#include <QTextStream>
#include <QDateTime>


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);
class Logger : public QObject
{
Q_OBJECT
public:
explicit Logger(QObject *parent, QString fileName, QPlainTextEdit *editor = 0);
~Logger();
void setShowDateTime(bool value);


private:<br /> QFile *file;<br /> QPlainTextEdit *m_editor;<br /> bool m_showDate;
private:
QFile *file;
QPlainTextEdit *m_editor;
bool m_showDate;


signals:
signals:


public slots:<br /> void write(const QString &amp;value);
public slots:
void write(const QString &amp;value);


};
};


#endif // LOGGER_H<br /></code>
#endif // LOGGER_H
</code>


Logger.cpp<br /><code><br />#include &quot;Logger.h&amp;quot;
Logger.cpp
<code>
#include "Logger.h"


Logger::Logger(QObject *parent, QString fileName,<br /> QPlainTextEdit *editor) : QObject(parent) {
Logger::Logger(QObject *parent, QString fileName,
QPlainTextEdit *editor) : QObject(parent) {


m_editor = editor;<br /> m_showDate = true;<br /> if (!fileName.isEmpty()) {<br /> file = new QFile;<br /> file-&gt;setFileName(fileName);<br /> file-&gt;open(QIODevice::Append | QIODevice::Text);<br /> }<br />}
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 &amp;value) {<br /> QString text = value;// + &quot;&quot;;<br /> if (m_showDate)<br /> text = QDateTime::currentDateTime().toString(&quot;dd.MM.yyyy hh:mm:ss &quot;) + text;<br /> QTextStream out(file);<br /> out.setCodec(&quot;UTF-8&amp;quot;);<br /> if (file != 0) {<br /> out &lt;&lt; text;<br /> }<br /> if (m_editor != 0)<br /> m_editor-&gt;appendPlainText(text);<br />}
void Logger::write(const QString &amp;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) {<br /> m_showDate = value;<br />}
void Logger::setShowDateTime(bool value) {
m_showDate = value;
}


Logger::~Logger() {<br /> if (file != 0)<br /> file-&gt;close();<br />}<br /></code>
Logger::~Logger() {
if (file != 0)
file->close();
}
</code>

Revision as of 10:04, 25 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

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 &amp;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 &amp;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();
}