首页 > 代码库 > Qt浅谈之二十一log调试日志

Qt浅谈之二十一log调试日志

一、简介

      最近因调试code时,想了解程序的流程,但苦于没有一个简易的日志记录,不停使用qDebug打印输出,而最终提交代码时得去多次删除打印信息,有时还会出现新修改的代码分不清是哪些部分。而使用#ifdef _DEBUG又比较烦这套,因此写了些简单的日志,方便排除问题,暂时不能用于多线程中,以后需要再补充。

二、详解

1、追踪函数

#ifdef _DEBUG_PRINT
#define DEBUGPRINT DEBUGInfo printinfo(__FILE__, __LINE__, __FUNCTION__);
#else
#define DEBUGPRINT
#endif

class DEBUGInfo
{
public:
    DEBUGInfo(QString file, int line, QString func);
    ~DEBUGInfo();
    
private:
    QString fileName;
    int fileLine;
    QString funcName;
};
DEBUGInfo::DEBUGInfo(QString file, int line, QString func)
    :fileName(file)
    ,fileLine(line)
    ,funcName(func)
{
  QString result = "";
  QString beginTime = QDateTime::currentDateTime().toString("[yyyy-dd-MM hh:mm:ss.zzz] ");
  result += beginTime + "Enter:{" + fileName + ":" + QString::number(fileLine) + "---" + funcName + "}";
  qDebug() << result.toStdString().c_str();
}
DEBUGInfo::~DEBUGInfo()
{
  QString result = "";
  QString beginTime = QDateTime::currentDateTime().toString("[yyyy-dd-MM hh:mm:ss.zzz] ");
  result += beginTime + "Leave:{" + fileName + ":" + QString::number(fileLine) + "---" + funcName + "}";
  qDebug() << result.toStdString().c_str();
}

使用:在需要查看的函数中加入DEBUGPRINT即可,输出文件名,所在的行和函数名字。

void HelloWorld()
{
    DEBUGPRINT
}

2、打印信息和写日志文件

enum LOGLEVEL{          
    LOG_DEBUG = 0,      /**< Debug >**/
    LOG_INFO,           /**< Info >**/
    LOG_WARN,           /**< Warn >**/
    LOG_ERROR           /**< Error >**/
};
class LogWriter{
    public:
        static LogWriter* getLogCenter();
        void PrintLog(LOGLEVEL level, const char* msg, ...);
        void SaveFileLog(LOGLEVEL level, const char* msg, ...);
        void setLogPath(QString logPath);         //defalut: current path
        void setLogLevel(LOGLEVEL logLevel);      //defalut: LOG_DEBUG
    private:
        static LogWriter * _logCenter;
        explicit LogWriter();
        ~LogWriter();
    private:
        QString _logPath;
        LOGLEVEL _logLevel;
    private:
        QString getLevelStr(LOGLEVEL level);
};
LogWriter *LogWriter::getLogCenter()
{
    if (_logCenter == NULL)
        _logCenter = new LogWriter;
    return _logCenter;
}

LogWriter::LogWriter()
{
    _logLevel = LOG_DEBUG;
    _logPath = QDir::currentPath();
}

LogWriter::~LogWriter()
{
    if (_logCenter) {
        delete _logCenter;
        _logCenter = NULL;
    }
}

void LogWriter::PrintLog(LOGLEVEL level, const char *msg,  ...)
{
    if (level < _logLevel)  return;     //low level
    char logBuffer[8192] = {0};
    va_list vl_fmt;                     //buffer
    va_start(vl_fmt, msg);
    vsprintf(logBuffer, msg, vl_fmt);
    va_end(vl_fmt);

    QString fileTime = "";
    QString logTime = "";
    QString logLevel = getLevelStr(level);
    fileTime = QDateTime::currentDateTime().toString("yyyyddMM");
    logTime = QDateTime::currentDateTime().toString("yyyy-dd-MM hh:mm:ss.zzz");
    qDebug("[%s] [%s] {%s}", logTime.toStdString().c_str(), logLevel.toStdString().c_str(), logBuffer);
}

void LogWriter::SaveFileLog(LOGLEVEL level, const char *msg,  ...)
{
    if (level < _logLevel)  return;  //low level
    char logBuffer[8192] = {0};
    va_list vl_fmt;                  //buff
    va_start(vl_fmt, msg);
    vsprintf(logBuffer, msg, vl_fmt);
    va_end(vl_fmt);

    QString logTime = "";
    QString fileTime = "";
    fileTime = QDateTime::currentDateTime().toString("yyyyddMM");
    logTime = QDateTime::currentDateTime().toString("[yyyy-dd-MM hh:mm:ss.zzz]");
    QString logLevel = getLevelStr(level);
    QString logFile = _logPath;
    if (logFile.right(1) != "/") {
        logFile += "/";
    }
    QDir mDir(logFile);
    if (!mDir.exists()) {
        mDir.mkpath(logFile);
    }
    logFile += "isoft_";
    logFile += fileTime;
    logFile += ".log";

    QFile file(logFile);
    file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
    QTextStream out(&file);
    out << logTime << " [" << logLevel << "] " << "{" << logBuffer << "}" << endl;
    file.close();
}

QString LogWriter::getLevelStr(LOGLEVEL level)
{
    switch(level) {
        case LOG_DEBUG: return "LOG_DEBUG"; break;
        case LOG_INFO: return "LOG_INFO"; break;
        case LOG_WARN: return "LOG_WARN"; break;
        case LOG_ERROR: return "LOG_ERROR"; break;
    }
}

void LogWriter::setLogPath(QString logPath)
{
    _logPath = logPath;
}

void LogWriter::setLogLevel(LOGLEVEL logLevel)
{
    _logLevel = logLevel;
}
可以设置日志的级别和日志文件的路径。

在函数中使用:

LogWriter::getLogCenter()->PrintLog(LOG_DEBUG, "hello world");
LogWriter::getLogCenter()->PrintLog(LOG_INFO, "%s:%s,%d", "hello", "world", 1234);
LogWriter::getLogCenter()->SaveFileLog(LOG_WARN, "hello world");
LogWriter::getLogCenter()->SaveFileLog(LOG_ERROR, "%s:%s,%d", "hello", "world", 1234);

也会在当前路径下生成文件。

三、总结

(1)本文只是一个简单的日志记录,还可以设计成异步的多线程式的,甚至可以加入到线程池,对性能要求较高的系统还得考虑文件的大小控制、存储空间的控制、文件的级别控制、文件的日期控制和自动清除等操作。

(2)本人思路有限,若有更好的设计建议,也可发邮件沟通,在此先感谢!邮箱地址yang.ao@i-soft.com.cn。

Qt浅谈之二十一log调试日志