Files
HIS-GUI/include/utils/logger.h
2026-04-07 17:18:33 +08:00

178 lines
4.5 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.
#ifndef LOGGER_H
#define LOGGER_H
#include <string>
#include <vector>
#include <ctime>
#include <fstream>
/**
* 日志条目类型
*/
enum class LogEntryType {
SHELL_COMMAND, // shell命令
PATIENT_OPERATION, // 患者操作
DOCTOR_OPERATION, // 医生操作
MEDICINE_OPERATION, // 药物操作
WARD_OPERATION, // 病房操作
DIAGNOSIS_RECORD, // 诊断记录
MEDICINE_RECORD, // 药房记录
ADMISSION_RECORD, // 住院记录
DISCHARGE_RECORD, // 出院记录
CHECK_OPERATION, // 检查操作
CHECK_RECORD, // 检查记录
SURGERY_RECORD, // 手术记录
SYSTEM_EVENT // 系统事件
};
/**
* 单条日志条目
*/
struct LogEntry {
time_t Timestamp; // 时间戳
LogEntryType Type; // 日志类型
std::string Command; // 命令或操作标识
std::string Details; // 详细内容
std::string UserID; // 用户ID如有
std::string OperationID; // 操作涉及的对象ID
LogEntry();
LogEntry(LogEntryType type,
const std::string& command,
const std::string& details,
const std::string& operationID = "");
/**
* 格式化日志条目为字符串
* @param format 格式化字符串,支持的占位符:
* {time}: 时间戳转换为日期时间
* {type}: 日志类型
* {command}: 命令
* {details}: 详细内容
* {objectId}: 操作对象ID
* {timestamp}: 原始时间戳
*/
std::string format(const std::string& fmt = "[{time}] [{type}] {command}: {details}") const;
// 获取日志类型的字符串表示
std::string getTypeString() const;
// 获取格式化的时间字符串
std::string getFormattedTime() const;
};
/**
* 日志系统
*/
class Logger {
public:
/**
* 构造函数
* @param logFile 日志文件路径,为空则不保存到文件
* @param enableConsole 是否输出到控制台
*/
Logger(const std::string& logFile = "", bool enableConsole = true);
~Logger();
/**
* 记录日志
*/
void log(LogEntryType type,
const std::string& command,
const std::string& details,
const std::string& operationID = "");
/**
* 记录Shell命令
*/
void logShellCommand(const std::string& command);
/**
* 记录患者操作
*/
void logPatientOperation(const std::string& operation,
const std::string& patientId,
const std::string& details = "");
/**
* 记录诊断记录
*/
void logDiagnosisRecord(const std::string& patientId,
const std::string& doctorId,
const std::string& diagnosis);
/**
* 记录药房记录
*/
void logMedicineRecord(const std::string& patientId,
const std::string& medicineId,
const std::string& medicineName,
int quantity);
/**
* 记录住院记录
*/
void logAdmissionRecord(const std::string& patientId,
const std::string& wardId,
const std::string& bedId,
const std::string& reason = "");
/**
* 记录出院记录
*/
void logDischargeRecord(const std::string& patientId,
const std::string& summary = "");
/**
* 刷新日志(写入文件)
*/
void flush();
/**
* 清空日志缓冲区
*/
void clear();
/**
* 获取所有日志条目
*/
const std::vector<LogEntry>& getEntries() const { return entries_; }
/**
* 设置日志格式
*/
void setLogFormat(const std::string& format) { logFormat_ = format; }
/**
* 设置日志文件路径
*/
void setLogFilePath(const std::string& filePath);
/**
* 启用/禁用控制台输出
*/
void setConsoleOutput(bool enable) { enableConsole_ = enable; }
/**
* 获取日志条数
*/
size_t getLogCount() const { return entries_.size(); }
/**
* 导出日志到文件
*/
bool exportToFile(const std::string& filePath) const;
private:
std::vector<LogEntry> entries_;
std::string logFilePath_;
std::string logFormat_;
bool enableConsole_;
bool autoFlush_;
std::ofstream logFileStream_;
void writeEntry(const LogEntry& entry);
};
#endif