Qt 如何编写自己的日志类
伙计们,我想按类型划分,例如警告和信息日志应该保存在其他文件中。。。下面是QXmpp库的类QXmppLogger,它将所有日志记录类型保存到一个文件中,但我必须按照前面提到的方式进行。有人能给我指路吗 PS 我无法更改QXmppLogger类 QXmppLogger.hQt 如何编写自己的日志类,qt,xmpp,Qt,Xmpp,伙计们,我想按类型划分,例如警告和信息日志应该保存在其他文件中。。。下面是QXmpp库的类QXmppLogger,它将所有日志记录类型保存到一个文件中,但我必须按照前面提到的方式进行。有人能给我指路吗 PS 我无法更改QXmppLogger类 QXmppLogger.h #ifndef QXMPPLOGGER_H #define QXMPPLOGGER_H #include <QObject> #include "QXmppGlobal.h" #ifdef QXMPP_LOGG
#ifndef QXMPPLOGGER_H
#define QXMPPLOGGER_H
#include <QObject>
#include "QXmppGlobal.h"
#ifdef QXMPP_LOGGABLE_TRACE
#define qxmpp_loggable_trace(x) QString("%1(0x%2) %3").arg(metaObject()->className(), QString::number(reinterpret_cast<qint64>(this), 16), x)
#else
#define qxmpp_loggable_trace(x) (x)
#endif
class QXmppLoggerPrivate;
/// \brief The QXmppLogger class represents a sink for logging messages.
///
/// \ingroup Core
class QXMPP_EXPORT QXmppLogger : public QObject
{
Q_OBJECT
Q_ENUMS(LoggingType)
Q_FLAGS(MessageType MessageTypes)
Q_PROPERTY(QString logFilePath READ logFilePath WRITE setLogFilePath)
Q_PROPERTY(LoggingType loggingType READ loggingType WRITE setLoggingType)
Q_PROPERTY(MessageTypes messageTypes READ messageTypes WRITE setMessageTypes)
public:
/// This enum describes how log message are handled.
enum LoggingType
{
NoLogging = 0, ///< Log messages are discarded
FileLogging = 1, ///< Log messages are written to a file
StdoutLogging = 2, ///< Log messages are written to the standard output
SignalLogging = 4, ///< Log messages are emitted as a signal
};
/// This enum describes a type of log message.
enum MessageType
{
NoMessage = 0, ///< No message type
DebugMessage = 1, ///< Debugging message
InformationMessage = 2, ///< Informational message
WarningMessage = 4, ///< Warning message
ReceivedMessage = 8, ///< Message received from server
SentMessage = 16, ///< Message sent to server
AnyMessage = 31, ///< Any message type
};
Q_DECLARE_FLAGS(MessageTypes, MessageType)
QXmppLogger(QObject *parent = 0);
~QXmppLogger();
static QXmppLogger* getLogger();
QXmppLogger::LoggingType loggingType();
void setLoggingType(QXmppLogger::LoggingType type);
QString logFilePath();
void setLogFilePath(const QString &path);
QXmppLogger::MessageTypes messageTypes();
void setMessageTypes(QXmppLogger::MessageTypes types);
public slots:
virtual void setGauge(const QString &gauge, double value);
virtual void updateCounter(const QString &counter, qint64 amount);
void log(QXmppLogger::MessageType type, const QString& text);
void reopen();
signals:
/// This signal is emitted whenever a log message is received.
void message(QXmppLogger::MessageType type, const QString &text);
private:
static QXmppLogger* m_logger;
QXmppLoggerPrivate *d;
};
/// \brief The QXmppLoggable class represents a source of logging messages.
///
/// \ingroup Core
class QXMPP_EXPORT QXmppLoggable : public QObject
{
Q_OBJECT
public:
QXmppLoggable(QObject *parent = 0);
protected:
/// \cond
virtual void childEvent(QChildEvent *event);
/// \endcond
/// Logs a debugging message.
///
/// \param message
void debug(const QString &message)
{
emit logMessage(QXmppLogger::DebugMessage, qxmpp_loggable_trace(message));
}
/// Logs an informational message.
///
/// \param message
void info(const QString &message)
{
emit logMessage(QXmppLogger::InformationMessage, qxmpp_loggable_trace(message));
}
/// Logs a warning message.
///
/// \param message
void warning(const QString &message)
{
emit logMessage(QXmppLogger::WarningMessage, qxmpp_loggable_trace(message));
}
/// Logs a received packet.
///
/// \param message
void logReceived(const QString &message)
{
emit logMessage(QXmppLogger::ReceivedMessage, qxmpp_loggable_trace(message));
}
/// Logs a sent packet.
///
/// \param message
void logSent(const QString &message)
{
emit logMessage(QXmppLogger::SentMessage, qxmpp_loggable_trace(message));
}
signals:
/// Sets the given \a gauge to \a value.
void setGauge(const QString &gauge, double value);
/// This signal is emitted to send logging messages.
void logMessage(QXmppLogger::MessageType type, const QString &msg);
/// Updates the given \a counter by \a amount.
void updateCounter(const QString &counter, qint64 amount = 1);
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QXmppLogger::MessageTypes)
#endif // QXMPPLOGGER_H
\ifndef QXMPPLOGGER\u H
#定义QXMPPLOGGER_H
#包括
#包括“QXmppGlobal.h”
#ifdef QXMPP_可记录_跟踪
#定义qxmpp_loggable_跟踪(x)QString(“%1(0x%2)%3”).arg(metaObject()->className(),QString::number(reinterpret_cast(this),16),x)
#否则
#定义qxmpp_可记录_跟踪(x)(x)
#恩迪夫
类QXmppLoggerPrivate;
///\brief QXmppLogger类表示用于记录消息的接收器。
///
///\n组核心
类QXMPP_导出QXmppLogger:公共QObject
{
Q_对象
Q_枚举(日志类型)
Q_标志(消息类型消息类型)
Q_属性(QString logFilePath读取logFilePath写入setLogFilePath)
Q_属性(LoggingType LoggingType读取LoggingType写入设置LoggingType)
Q_属性(MessageTypes MessageTypes读取MessageTypes写入设置MessageTypes)
公众:
///此枚举描述如何处理日志消息。
枚举日志类型
{
NoLogging=0,//<日志消息被丢弃
FileLogging=1,//<将日志消息写入文件
StdoutLogging=2,//<将日志消息写入标准输出
SignalLogging=4,//<日志消息作为信号发出
};
///此枚举描述一种日志消息类型。
枚举消息类型
{
NoMessage=0,//<无消息类型
DebugMessage=1,//<调试消息
InformationMessage=2,//<信息性消息
警告消息=4,//<警告消息
ReceivedMessage=8,//<从服务器接收到消息
SentMessage=16,//<发送到服务器的消息
AnyMessage=31,//<任何消息类型
};
Q_声明_标志(MessageType,MessageType)
QxMPLogger(QObject*parent=0);
~QXmppLogger();
静态QXmppLogger*getLogger();
QXmppLogger::LoggingType LoggingType();
void setLoggingType(QXmppLogger::LoggingType类型);
QString日志文件路径();
void setLogFilePath(常量QString&path);
QXmppLogger::MessageTypes MessageTypes();
void setMessageTypes(QXmppLogger::MessageTypes);
公众时段:
虚拟空隙设置规(常数管柱和规,双值);
虚拟作废更新计数器(常量QString&计数器,qint64金额);
无效日志(QXmppLogger::MessageType类型、const QString和text);
无效重新打开();
信号:
///每当接收到日志消息时,就会发出此信号。
无效消息(QXmppLogger::MessageType类型、const QString和text);
私人:
静态QXmppLogger*m_记录器;
QXmppLoggerPrivate*d;
};
///\brief QXmppLoggable类表示日志消息的源。
///
///\n组核心
类QXMPP_导出QXmppLoggable:公共QObject
{
Q_对象
公众:
QxMPplogable(QObject*parent=0);
受保护的:
///\cond
虚拟无效子事件(QChildEvent*事件);
///\endond
///记录调试消息。
///
///\param消息
无效调试(常量QString和消息)
{
发出日志消息(QXmppLogger::DebugMessage,qxmpp_loggable_跟踪(消息));
}
///记录一条信息性消息。
///
///\param消息
无效信息(常量字符串和消息)
{
发出日志消息(QXmppLogger::InformationMessage,qxmpp_loggable_跟踪(消息));
}
///记录一条警告消息。
///
///\param消息
无效警告(常量字符串和消息)
{
发出日志消息(QXmppLogger::WarningMessage,qxmpp_loggable_跟踪(消息));
}
///记录接收到的数据包。
///
///\param消息
收到无效日志(常量字符串和消息)
{
发出日志消息(QXmppLogger::ReceivedMessage,qxmpp_loggable_跟踪(消息));
}
///记录发送的数据包。
///
///\param消息
已发送无效日志(常量字符串和消息)
{
发出日志消息(QXmppLogger::SentMessage,qxmpp_loggable_跟踪(消息));
}
信号:
///将给定的\a仪表设置为\a值。
空隙设置规(常数管柱和规,双值);
///发出此信号以发送日志消息。
void logMessage(QXmppLogger::MessageType,const QString&msg);
///将给定的\a计数器更新\a金额。
无效更新计数器(常量QString和计数器,qint64金额=1);
};
Q_为_标志声明_运算符(QXmppLogger::MessageTypes)
#endif//QXMPPLOGGER_H
这里是QXmppLogger.cpp
#include <iostream>
#include <QChildEvent>
#include <QDateTime>
#include <QFile>
#include <QMetaType>
#include <QTextStream>
#include "QXmppLogger.h"
QXmppLogger* QXmppLogger::m_logger = 0;
static const char *typeName(QXmppLogger::MessageType type)
{
switch (type)
{
case QXmppLogger::DebugMessage:
return "DEBUG";
case QXmppLogger::InformationMessage:
return "INFO";
case QXmppLogger::WarningMessage:
return "WARNING";
case QXmppLogger::ReceivedMessage:
return "RECEIVED";
case QXmppLogger::SentMessage:
return "SENT";
default:
return "";
}
}
static QString formatted(QXmppLogger::MessageType type, const QString& text)
{
return QDateTime::currentDateTime().toString() + " " +
QString::fromLatin1(typeName(type)) + " " +
text;
}
static void relaySignals(QXmppLoggable *from, QXmppLoggable *to)
{
QObject::connect(from, SIGNAL(logMessage(QXmppLogger::MessageType,QString)),
to, SIGNAL(logMessage(QXmppLogger::MessageType,QString)));
QObject::connect(from, SIGNAL(setGauge(QString,double)),
to, SIGNAL(setGauge(QString,double)));
QObject::connect(from, SIGNAL(updateCounter(QString,qint64)),
to, SIGNAL(updateCounter(QString,qint64)));
}
/// Constructs a new QXmppLoggable.
///
/// \param parent
QXmppLoggable::QXmppLoggable(QObject *parent)
: QObject(parent)
{
QXmppLoggable *logParent = qobject_cast<QXmppLoggable*>(parent);
if (logParent) {
relaySignals(this, logParent);
}
}
/// \cond
void QXmppLoggable::childEvent(QChildEvent *event)
{
QXmppLoggable *child = qobject_cast<QXmppLoggable*>(event->child());
if (!child)
return;
if (event->added()) {
relaySignals(child, this);
} else if (event->removed()) {
disconnect(child, SIGNAL(logMessage(QXmppLogger::MessageType,QString)),
this, SIGNAL(logMessage(QXmppLogger::MessageType,QString)));
disconnect(child, SIGNAL(setGauge(QString,double)),
this, SIGNAL(setGauge(QString,double)));
disconnect(child, SIGNAL(updateCounter(QString,qint64)),
this, SIGNAL(updateCounter(QString,qint64)));
}
}
/// \endcond
class QXmppLoggerPrivate
{
public:
QXmppLoggerPrivate(QXmppLogger *qq);
QXmppLogger::LoggingType loggingType;
QFile *logFile;
QString logFilePath;
QXmppLogger::MessageTypes messageTypes;
private:
QXmppLogger *q;
};
QXmppLoggerPrivate::QXmppLoggerPrivate(QXmppLogger *qq)
: loggingType(QXmppLogger::NoLogging),
logFile(0),
logFilePath("QXmppClientLog.log"),
messageTypes(QXmppLogger::AnyMessage),
q(qq)
{
}
/// Constructs a new QXmppLogger.
///
/// \param parent
QXmppLogger::QXmppLogger(QObject *parent)
: QObject(parent)
{
d = new QXmppLoggerPrivate(this);
// make it possible to pass QXmppLogger::MessageType between threads
qRegisterMetaType< QXmppLogger::MessageType >("QXmppLogger::MessageType");
}
QXmppLogger::~QXmppLogger()
{
delete d;
}
/// Returns the default logger.
///
QXmppLogger* QXmppLogger::getLogger()
{
if(!m_logger)
m_logger = new QXmppLogger();
return m_logger;
}
/// Returns the handler for logging messages.
///
QXmppLogger::LoggingType QXmppLogger::loggingType()
{
return d->loggingType;
}
/// Sets the handler for logging messages.
///
/// \param type
void QXmppLogger::setLoggingType(QXmppLogger::LoggingType type)
{
if (d->loggingType != type) {
d->loggingType = type;
reopen();
}
}
/// Returns the types of messages to log.
///
QXmppLogger::MessageTypes QXmppLogger::messageTypes()
{
return d->messageTypes;
}
/// Sets the types of messages to log.
///
/// \param types
void QXmppLogger::setMessageTypes(QXmppLogger::MessageTypes types)
{
d->messageTypes = types;
}
/// Add a logging message.
///
/// \param type
/// \param text
void QXmppLogger::log(QXmppLogger::MessageType type, const QString& text)
{
// filter messages
if (!d->messageTypes.testFlag(type))
return;
switch(d->loggingType)
{
case QXmppLogger::FileLogging:
if (!d->logFile) {
d->logFile = new QFile(d->logFilePath);
d->logFile->open(QIODevice::WriteOnly | QIODevice::Append);
}
QTextStream(d->logFile) << formatted(type, text) << "\n";
break;
case QXmppLogger::StdoutLogging:
std::cout << qPrintable(formatted(type, text)) << std::endl;
break;
case QXmppLogger::SignalLogging:
emit message(type, text);
break;
default:
break;
}
}
/// Sets the given \a gauge to \a value.
///
/// NOTE: the base implementation does nothing.
void QXmppLogger::setGauge(const QString &gauge, double value)
{
Q_UNUSED(gauge);
Q_UNUSED(value);
}
/// Updates the given \a counter by \a amount.
///
/// NOTE: the base implementation does nothing.
void QXmppLogger::updateCounter(const QString &counter, qint64 amount)
{
Q_UNUSED(counter);
Q_UNUSED(amount);
}
/// Returns the path to which logging messages should be written.
///
/// \sa loggingType()
QString QXmppLogger::logFilePath()
{
return d->logFilePath;
}
/// Sets the path to which logging messages should be written.
///
/// \param path
///
/// \sa setLoggingType()
void QXmppLogger::setLogFilePath(const QString &path)
{
if (d->logFilePath != path) {
d->logFilePath = path;
reopen();
}
}
/// If logging to a file, causes the file to be re-opened.
///
void QXmppLogger::reopen()
{
if (d->logFile) {
delete d->logFile;
d->logFile = 0;
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“QXmppLogger.h”
QXmppLogger*QXmppLogger::m_logger=0;
静态常量char*typeName(QXmppLogger::MessageType类型)
{
开关(类型)
{
案例QXmppLogger::调试消息:
返回“调试”;
案例QXmppLogger::信息消息:
返回“信息”;
案例QXmppLogger::警告消息:
返回“警告”;
案例QXmppLogger::ReceivedMessage:
返回“已收到”;
案例QXmppLogger::SentMessage:
返回“已发送”;
违约:
返回“”;
}
}
静态QString格式化(QXmppLogger::MessageType,const QString&text)
{
返回QDateTime::currentDateTime().toString()+“”+
QString::fromLatin1(typeName(type))+“”+
文本;
}
静态空隙松弛信号(QXmppLoggable*from,QXmppLoggable*to)
{
QObject::connect(从,信号(logMessage(QXmppLogger::MessageType,QString)),
到,信号(logMessage(QXmppLogger::MessageType,QString));
QObject::connect(从,信号(设置规(QString,双)),
至,信号(设置规(QString,double));
QObject::connect(从,信号(更新计数器(QString,qint64)),
到