Files
newspark110/device/deviceinfo.cpp

140 lines
4.7 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "deviceinfo.h"
#include <QDateTime>
#include "myexception.h"
#include "exceptioncode.h"
#include <QDir>
DeviceInfo* DeviceInfo::uniqueInstance = nullptr;
DeviceInfo* DeviceInfo::instance() {
if(!uniqueInstance) {
uniqueInstance = new DeviceInfo;
QDateTime now = QDateTime::currentDateTime();
QString nowStr;
nowStr = now.toString("yyyyMMdd_hhmmss");
QString imagePath = QDir::currentPath()+"/log/";
QDir dstDir(imagePath);
if(!dstDir.exists())
{
if(!dstDir.mkdir(imagePath))
{
//qDebug()<<"创建Image文件夹失败"<<endl;
}
}
// 定义Info日志名称
QString fNameInfo = QString("data/log/info/info_%1.log").arg(nowStr);
QByteArray qbyInfo = fNameInfo.toLocal8Bit();
char* fNameInfo_c = qbyInfo.data();
// 获取日志文件句柄
uniqueInstance->streamInfo = fopen(fNameInfo_c, "wt");
if(uniqueInstance->streamInfo == nullptr)
{
fNameInfo = QString("./log/info_%1.log").arg(nowStr);
qbyInfo = fNameInfo.toLocal8Bit();
char* fNameInfo_c = qbyInfo.data();
// 获取日志文件句柄
uniqueInstance->streamInfo = fopen(fNameInfo_c, "wt");
}
// 定义Alarm日志名称
QString fNameAlarm = QString("data/log/alarm/alarm_%1.log").arg(nowStr);
QByteArray qbyAlarm = fNameAlarm.toLocal8Bit();
char* fNameAlarm_c = qbyAlarm.data();
uniqueInstance->streamAlarm = fopen(fNameAlarm_c, "wt");
if(uniqueInstance->streamAlarm == nullptr)
{
fNameAlarm = QString("./log/alarm_%1.log").arg(nowStr);
qbyAlarm = fNameAlarm.toLocal8Bit();
char* fNameAlarm_c = qbyAlarm.data();
uniqueInstance->streamAlarm = fopen(fNameAlarm_c, "wt");
}
}
return uniqueInstance;
}
void DeviceInfo::deleteInstance() {
if (uniqueInstance) {
delete uniqueInstance;
}
}
DeviceInfo::DeviceInfo()
{
}
/**
* 实现把文本字符串写入文件,并且文本字符可以带多个格式参数
* 写信息日志
*/
void DeviceInfo::printInfo(const char* format ...) {
if(uniqueInstance->streamInfo == nullptr)
{
throw MyException(QString("写入日志失败!异常代码为:%1").arg(WRITE_LOG_FAIL),WRITE_LOG_FAIL);
}
// 定义可变参数指针
va_list ap;
// 初始化可变参数指针,指向第一个可变参数
va_start(ap, format);
// 使用参数列表发送格式化输出到流 streamInfo 中
// 参数1这是指向 FILE 对象的指针,该 FILE 对象标识了流
// 参数2这是 C 字符串,包含了要被写入到流 streamInfo 中的文本。它可以包含嵌入的 format 标签format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。
// 参数3可变参数指针
vfprintf(streamInfo, format, ap);
fprintf(streamInfo, "\n");
va_end(ap);
fflush(streamInfo);
}
/**
* 实现把文本字符串写入文件,并且文本字符可以带多个格式参数
* 写报警日志
*/
void DeviceInfo::printAlarm(const char* format ...) {
// 定义可变参数指针
va_list ap;
// 初始化可变参数指针,指向第一个可变参数
va_start(ap, format);
// 使用参数列表发送格式化输出到流 streamInfo 中
// 参数1这是指向 FILE 对象的指针,该 FILE 对象标识了流
// 参数2这是 C 字符串,包含了要被写入到流 streamInfo 中的文本。它可以包含嵌入的 format 标签format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。
// 参数3可变参数指针
vfprintf(streamAlarm, format, ap);
fprintf(streamAlarm, "\n");
va_end(ap);
fflush(streamAlarm);
}
void DeviceInfo::printDeviceSystemInfo(const QString text,SYS_INFO_LEVEL level)
{
QDateTime now = QDateTime::currentDateTime();
QString nowStr;
nowStr = now.toString("yyyyMMdd_hh:mm:ss:zzz ");
nowStr+=text;
emit deviceSystemInfoSGL(nowStr,level);
//记录日志
QByteArray qbyInfo = nowStr.toLocal8Bit();
char* info_c = qbyInfo.data();
printInfo(info_c);
}
void DeviceInfo::printDeviceSalamInfo(const QString text,SYS_INFO_LEVEL level)
{
QDateTime now = QDateTime::currentDateTime();
QString nowStr;
nowStr = now.toString("yyyyMMdd_hh:mm:ss:zzz ");
nowStr += text;
emit deviceSalamInfoSGL(nowStr,level);
emit deviceSystemInfoSGL(nowStr,level);
//记录日志
QByteArray qbyInfo = nowStr.toLocal8Bit();
char* info_c = qbyInfo.data();
printInfo(info_c);
printAlarm(info_c);
}