USB_Config_Vendor/CC_SDK/Include/Module/File/CCLogger.h

210 lines
6.4 KiB
C
Raw Permalink Normal View History

2026-02-03 14:36:30 +08:00
#ifndef CC_LOGGER_H
#define CC_LOGGER_H
#include "CC.h"
#include "CCTimer.h"
#include "CCJSONObject.h"
#include "TL/Map.h"
namespace CTL {
/**
*
*/
enum Logger_Level {
Debug = 0,
Info,
Warning,
Error,
Fatal
};
struct LogParentFile {
JSON_TYPE_INTRUSIVE(LogParentFile,Name,Date);
String Name = "";
String Date = "";
String File_Path = "";
};
struct LogFile_t {
JSON_TYPE_INTRUSIVE(LogFile_t,Name,Date,Size);
String Name;
size_t Size = 0;
String Date = "";
};
class Logger {
String Root_Folder_Path = "./logs";
String m_log_file_name;
String m_log_file_dir;
Logger_Level m_level = Logger_Level::Debug;
int log_m_count = 0,log_s_count = 5,log_size_count = 1;
CCQueue<String> m_log_s;
const char * Level_Count[5] = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
bool log_flag = false;
int is_save_cond_ = 7;
Timer m_log_thread;
CCMutex _mutex_;
Map<String,LogParentFile> m_log_map;
String CurrentDate = "";
public:
Logger();
~Logger();
/**
*
* @param Folder
*/
void SetLogFolder(const String& Folder);
/**
*
* @param count
*/
void SetLogCount(int count);
/**
*
* @param count KB
*/
void SetLogSize(int count);
/**
*
* @param cond
*/
void SetLogSave(int cond);
/**
*
* @param level
*/
void SetLevel(Logger_Level level);
/**
* Debug等级
* @param fmt
*/
void Debug(const String& fmt);
/**
* Info等级
* @param fmt
*/
void Info(const String& fmt);
/**
* Warning等级
* @param fmt
*/
void Warning(const String& fmt);
/**
* Error等级
* @param fmt
*/
void Error(const String& fmt);
/**
* Fatal等级
* @param fmt
*/
void Fatal(const String& fmt);
/**
*
* @param level
* @param message
* @param file __FILE__
* @param line __LINE__
*/
void LogOutput(Logger_Level level, const String& message, const String& file= __FILE__, int line = __LINE__);
/**
* 线 12
*/
void Start();
/**
* 线
*/
void Stop();
/**
*
* @return
*/
static bool isLeapYear(int year);
/**
*
* @return
*/
int dateToDays(int year, int month, int day);
/**
*
* @param dateStr 2025-03-01
* @param year
* @param month
* @param day
*/
void parseDateString(const std::string& dateStr, int& year, int& month, int& day);
/**
*
* @param date1 1
* @param date2 2
* @return
*/
int calculateDaysDifference(const std::string& date1, const std::string& date2);
/**
*
* @return
*/
CCVector<LogParentFile> GetLogs();
/**
*
* @param Date
* @return
*/
CCVector<LogFile_t> GetLogFiles(const String& Date);
/**
*
* @return
*/
String GetCurrentFolder() const;
/**
*
* @return
*/
String GetCurrentDate() const;
/**
*
* @param Date
* @param fileName
* @return
*/
String GetLogFile(const String& Date, const String& fileName);
/**
*
* @param Date
* @param fileName
* @return
*/
String GetLog(const String& Date, const String& fileName);
/**
*
* @param Date
* @param fileName
* @return
*/
bool DeleteLog(const String& Date, const String& fileName);
/**
*
*/
void Flush();
private:
void Log2(Logger_Level level, const String& message);
bool LogWriting(const String& message);
void WriteData(const String& Path);
void LogThread();
void InitializeFolder();
void DeleteOldFolders(const String& rootPath, int days);
String GetLogFilePath(const String& fileName);
std::chrono::system_clock::time_point StringToDate(const String& dateString);
bool CompareDateDifference(const String& dateStr1, const String& dateStr2, int days);
String GetsTheCurrentLogFile();
void CloseWrite();
int daysBetweenDates(const std::string& date1, const std::string& date2);
};
}
#define CC_Logger_Debug(LoggerObj,m,...) LoggerObj.LogOutput(CTL::Logger_Level::Debug,m, __FILE__, __LINE__)
#define CC_Logger_Info(LoggerObj,m,...) LoggerObj.LogOutput(CTL::Logger_Level::Info,m, __FILE__, __LINE__)
#define CC_Logger_Warning(LoggerObj,m,...) LoggerObj.LogOutput(CTL::Logger_Level::Warning,m, __FILE__, __LINE__)
#define CC_Logger_Error(LoggerObj,m,...) LoggerObj.LogOutput(CTL::Logger_Level::Error,m, __FILE__, __LINE__)
#define CC_Logger_Fatal(LoggerObj,m,...) LoggerObj.LogOutput(CTL::Logger_Level::Fatal,m, __FILE__, __LINE__)
#endif