效果演示
使用方法:直接将logsystem.cpp文件和logsystem.h文件添加到工程目录中,然后包含即可使用。
直接调用打印接口
然后在程序exe文件夹下会生成一个文件名叫log_debug.txt文件。
源文件.C
#include "logsystem.h" LogSystem::LogSystem() { } void LogSystem::error(QString msg) { if(!Write_debug_information("error",msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } void LogSystem::debug(QString msg) { if(!Write_debug_information("debug",msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } void LogSystem::warning(QString msg) { if(!Write_debug_information("warning",msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } void LogSystem::info(QString msg) { if(!Write_debug_information("info",msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } bool LogSystem::Write_debug_information(QString level, QString message) { QDateTime current_date_time =QDateTime::currentDateTime(); QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm"); QString fileName = QCoreApplication::applicationDirPath() + "/debug_log.txt";// 获取程序当前运行目录 QTextStream txtOutput(&file); QString write_str; file.setFileName(fileName); if (!file.open(QIODevice::ReadWrite | QIODevice::Append)) /* 以append的方式打开 */ { return false; } if (level == "DEBUG" || level == "debug") { write_str = QString("[%1][%2]:%3").arg("DEBUG").arg(current_date).arg(message); txtOutput << write_str <<endl; // file.write(write_str.toLocal8Bit()); } else if(level == "WARNING" || level == "warning") { write_str = QString("[%1][%2]:%3").arg("WARNING").arg(current_date).arg(message); txtOutput << write_str <<endl; // file.write(write_str.toLocal8Bit()); } else if(level == "ERROR" || level == "error") { write_str = QString("[%1][%2]:%3").arg("ERROR").arg(current_date).arg(message); txtOutput << write_str <<endl; // file.write(write_str.toLocal8Bit()); } else if(level == "INFO" || level == "info") { write_str = QString("[%1][%2]:%3").arg("INFO").arg(current_date).arg(message); txtOutput << write_str <<endl; // file.write(write_str.toLocal8Bit()); }else { file.close();/* 关闭文件 */ return false; } file.close();/* 关闭文件 */ return true; }
源文件.h文件
#ifndef LOGSYSTEM_H #define LOGSYSTEM_H #include <QFile> #include <QString> #include <QTime> #include <QDebug> #include <QTextStream> #include <QCoreApplication> #include <QMessageBox> class LogSystem { public: LogSystem(); QFile file; QString fileName; private: bool Write_debug_information(QString level, QString info); public slots: void info(QString msg); void error(QString msg); void warning(QString msg); void debug(QString msg); }; #endif // LOGSYSTEM_H
2021-11-21更新
.cpp文件
#include "logsystem.h" /*** * 构造函数: * 初始化日志系统目录 ***/ LogSystem::LogSystem() { QDateTime current_date_time =QDateTime::currentDateTime(); QString current_date =current_date_time.toString("yyyyMMdd"); QDir dir; if (!dir.exists(fileAddress + current_date)) { dir.mkdir(fileAddress + current_date); qDebug() << "[INFO][:" +current_date_time.toString("yyyy.MM.dd hh:ss") + "]:新建LOG文件夹" << endl; } fileName = fileAddress + current_date + fileName; } /*** * 打印错误信息并附带系统时间 ***/ void LogSystem::error(QString msg) { if(openFlag == false) return; if(!Write_debug_information("error",true,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 打印调试信息并附带系统时间 ***/ void LogSystem::debug(QString msg) { if(openFlag == false) return; if(!Write_debug_information("debug",true,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 打印警告信息并附带系统时间 ***/ void LogSystem::warning(QString msg) { if(openFlag == false) return; if(!Write_debug_information("warning",true,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 打印提示信息并附带系统时间 ***/ void LogSystem::info(QString msg) { if(openFlag == false) return; if(!Write_debug_information("info",true,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 打印错误信息 ***/ void LogSystem::error_notime(QString msg) { if(openFlag == false) return; if(!Write_debug_information("error",false,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 打印调试信息 ***/ void LogSystem::debug_notime(QString msg) { if(openFlag == false) return; if(!Write_debug_information("debug",false,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 打印警告信息 ***/ void LogSystem::warning_notime(QString msg) { if(openFlag == false) return; if(!Write_debug_information("warning",false,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 打印提示信息 ***/ void LogSystem::info_notime(QString msg) { if(openFlag == false) return; if(!Write_debug_information("info",false,msg)) { QMessageBox::information(NULL, "information", "log system error!"); } } /*** * 设置日志信息文件名字 ***/ void LogSystem::setFileName(QString name) { this->fileName = name; } /*** * 设置是否开启日子系统的qdebug信息输出 ***/ void LogSystem::setQdebugFlag(bool flag) { this->qDebugFlag = flag; } /*** * 打开日志系统 ***/ void LogSystem::start() { openFlag = true; } /*** * 关闭日志系统 ***/ void LogSystem::stop() { openFlag = false; } /*** * 写入信息到日志信息文本文件中 ***/ bool LogSystem::Write_debug_information(QString level, bool time, QString message) { QDateTime current_date_time =QDateTime::currentDateTime(); QString current_date =current_date_time.toString("yyyy-MM-dd hh:mm"); QTextStream txtOutput(&file); QString write_str; file.setFileName(fileName); if (!file.open(QIODevice::ReadWrite | QIODevice::Append)) /* 以append的方式打开 */ { return false; } if (time == true){ if (level == "DEBUG" || level == "debug") { write_str = QString("[%1][%2]:%3").arg("DEBUG").arg(current_date).arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; } else if(level == "WARNING" || level == "warning") { write_str = QString("[%1][%2]:%3").arg("WARNING").arg(current_date).arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; } else if(level == "ERROR" || level == "error") { write_str = QString("[%1][%2]:%3").arg("ERROR").arg(current_date).arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; } else if(level == "INFO" || level == "info") { write_str = QString("[%1][%2]:%3").arg("INFO").arg(current_date).arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; }else { file.close();/* 关闭文件 */ return false; } }else{ if (level == "DEBUG" || level == "debug") { write_str = QString("[%1]:%2").arg("DEBUG_NOTIME").arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; } else if(level == "WARNING" || level == "warning") { write_str = QString("[%1]:%2").arg("WARNING_NOTIME").arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; } else if(level == "ERROR" || level == "error") { write_str = QString("[%1]:%2").arg("ERROR_NOTIME").arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; } else if(level == "INFO" || level == "info") { write_str = QString("[%1]:%2").arg("INFO_NOTIME").arg(message); txtOutput << write_str <<endl; if(qDebugFlag) qDebug() << write_str <<endl; }else { file.close();/* 关闭文件 */ return false; } } file.close();/* 关闭文件 */ return true; }
.h文件
调用
LogSystem *log = new LogSystem(); log.setQdebugFlag(true); log.start();