Add logger.h and locker.h
This commit is contained in:
141
common/locker.h
Normal file
141
common/locker.h
Normal file
@@ -0,0 +1,141 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(disable: 4996)
|
||||
#pragma warning(disable: 4819)
|
||||
|
||||
// 互斥锁、睡眠函数、自动锁、自动计时、自动日志等
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
// 自动日志
|
||||
class CAutoLog
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION *m_cs;
|
||||
const char* name;
|
||||
public:
|
||||
CAutoLog(const char* _name, CRITICAL_SECTION *cs=NULL) : name(_name), m_cs(cs)
|
||||
{
|
||||
Mprintf(">>> Enter thread %s: [%d]\n", name ? name : "", GetCurrentThreadId());
|
||||
if (m_cs)EnterCriticalSection(m_cs);
|
||||
}
|
||||
|
||||
~CAutoLog()
|
||||
{
|
||||
if (m_cs)LeaveCriticalSection(m_cs);
|
||||
Mprintf(">>> Leave thread %s: [%d]\n", name ? name : "", GetCurrentThreadId());
|
||||
}
|
||||
};
|
||||
|
||||
class CLock {
|
||||
public:
|
||||
CLock(CRITICAL_SECTION& cs) : m_cs(&cs)
|
||||
{
|
||||
Lock();
|
||||
}
|
||||
CLock() : m_cs(nullptr)
|
||||
{
|
||||
InitializeCriticalSection(&i_cs);
|
||||
}
|
||||
~CLock()
|
||||
{
|
||||
m_cs ? Unlock() : DeleteCriticalSection(&i_cs);
|
||||
}
|
||||
|
||||
void Unlock()
|
||||
{
|
||||
LeaveCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void Lock()
|
||||
{
|
||||
EnterCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
LeaveCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
EnterCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
protected:
|
||||
CRITICAL_SECTION* m_cs; // 外部锁
|
||||
CRITICAL_SECTION i_cs; // 内部锁
|
||||
};
|
||||
|
||||
typedef CLock CLocker;
|
||||
|
||||
class CAutoLock
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION &m_cs;
|
||||
|
||||
public:
|
||||
CAutoLock(CRITICAL_SECTION& cs) : m_cs(cs)
|
||||
{
|
||||
EnterCriticalSection(&m_cs);
|
||||
}
|
||||
|
||||
~CAutoLock()
|
||||
{
|
||||
LeaveCriticalSection(&m_cs);
|
||||
}
|
||||
};
|
||||
|
||||
// 智能计时器,计算函数的耗时
|
||||
class auto_tick {
|
||||
private:
|
||||
const char* func;
|
||||
int span;
|
||||
clock_t tick;
|
||||
__inline clock_t now() const {
|
||||
return clock();
|
||||
}
|
||||
__inline int time() const {
|
||||
return now() - tick;
|
||||
}
|
||||
|
||||
public:
|
||||
auto_tick(const char* func_name, int th = 5) : func(func_name), span(th), tick(now()) { }
|
||||
~auto_tick() {
|
||||
stop();
|
||||
}
|
||||
|
||||
__inline void stop() {
|
||||
if (span != 0) {
|
||||
int s(this->time());
|
||||
if (s > span)Mprintf("[%s] cost: [%d]ms.\n", func, s);
|
||||
span = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef _DEBUG
|
||||
// 智能计算当前函数的耗时,超时会打印
|
||||
#define AUTO_TICK(thresh) auto_tick TICK(__FUNCTION__, thresh)
|
||||
#define STOP_TICK TICK.stop()
|
||||
#else
|
||||
#define AUTO_TICK(thresh)
|
||||
#define STOP_TICK
|
||||
#endif
|
||||
|
||||
#define AUTO_TICK_C AUTO_TICK
|
||||
|
||||
#include <MMSystem.h>
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
|
||||
// 高精度的睡眠函数
|
||||
#define Sleep_m(ms) { Sleep(ms); }
|
||||
|
||||
// 以步长n毫秒在条件C下等待T秒(n是步长,必须能整除1000)
|
||||
#define WAIT_n(C, T, n) { int s=(1000*(T))/(n); s=max(s,1); do{Sleep(n);}while((C)&&(--s)); }
|
||||
|
||||
// 在条件C成立时等待T秒(步长10ms)
|
||||
#define WAIT(C, T) { WAIT_n(C, T, 10); }
|
||||
|
||||
// 在条件C成立时等待T秒(步长1ms)
|
||||
#define WAIT_1(C, T) { WAIT_n(C, T, 1); }
|
||||
234
common/logger.h
Normal file
234
common/logger.h
Normal file
@@ -0,0 +1,234 @@
|
||||
#pragma once
|
||||
#pragma warning(disable: 4996)
|
||||
#ifdef _WIN32
|
||||
#ifdef _WINDOWS
|
||||
#include <afxwin.h>
|
||||
#else
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <cstdarg>
|
||||
#include "skCrypter.h"
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
inline bool stringToBool(const std::string& str) {
|
||||
std::string lower = str;
|
||||
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
|
||||
return (lower == "true" || lower == "1");
|
||||
}
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
enum LogLevel {
|
||||
InfoLevel, WarningLevel, ErrorLevel
|
||||
};
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ģʽ
|
||||
static Logger& getInstance() {
|
||||
static Logger instance;
|
||||
if (instance.pid.empty()) {
|
||||
char buf[16] = {};
|
||||
sprintf_s(buf, "%d", GetCurrentProcessId());
|
||||
instance.pid = buf;
|
||||
instance.InitLogFile("C:\\Windows\\Temp", instance.pid);
|
||||
#ifdef _WINDOWS
|
||||
instance.enable = true; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־Ĭ<D6BE>ϴ<EFBFBD><CFB4><EFBFBD>
|
||||
#else
|
||||
char var[32] = {};
|
||||
const char* name = skCrypt("ENABLE_LOG");
|
||||
DWORD size = GetEnvironmentVariableA(name, var, sizeof(var));
|
||||
instance.enable = stringToBool(var);
|
||||
instance.log("logger.h", __LINE__, "GetEnvironmentVariable: %s=%s\n", name, var);
|
||||
#endif
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
Logger(const Logger&) = delete;
|
||||
Logger& operator=(const Logger&) = delete;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
void setLogFile(const std::string& filename) {
|
||||
std::lock_guard<std::mutex> lock(fileMutex);
|
||||
logFileName = filename;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
||||
void usingLog(bool b = true) {
|
||||
enable = b;
|
||||
}
|
||||
|
||||
// д<><D0B4>־<EFBFBD><D6BE>֧<EFBFBD><D6A7> printf <20><>ʽ<EFBFBD><CABD>
|
||||
void log(const char* file, int line, const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
std::string message = formatString(format, args);
|
||||
|
||||
va_end(args);
|
||||
|
||||
auto timestamp = getCurrentTimestamp();
|
||||
std::string id = pid.empty() ? "" : "[" + pid + "]";
|
||||
|
||||
std::string logEntry = id + "[" + timestamp + "] [" + file + ":" + std::to_string(line) + "] " + message;
|
||||
if (enable)
|
||||
{
|
||||
if (running) {
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
logQueue.push(logEntry);
|
||||
} else {
|
||||
writeToFile(logEntry);
|
||||
}
|
||||
}
|
||||
#ifndef _WINDOWS
|
||||
#ifdef _DEBUG
|
||||
printf(logEntry.c_str());
|
||||
#endif
|
||||
#endif
|
||||
cv.notify_one(); // ֪ͨд<D6AA>߳<EFBFBD>
|
||||
}
|
||||
|
||||
// ֹͣ<CDA3><D6B9>־ϵͳ
|
||||
void stop() {
|
||||
if (!running) return;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
running = false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
|
||||
}
|
||||
cv.notify_one();
|
||||
if (workerThread.joinable()) {
|
||||
workerThread.join();
|
||||
}
|
||||
for (int i = 0; threadRun && i++ < 1000; Sleep(1));
|
||||
}
|
||||
|
||||
private:
|
||||
// <20><>־<EFBFBD><D6BE><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
void InitLogFile(const std::string & dir, const std::string& pid) {
|
||||
time_t currentTime = time(nullptr);
|
||||
tm* localTime = localtime(¤tTime);
|
||||
char timeString[32];
|
||||
strftime(timeString, sizeof(timeString), "%Y-%m", localTime);
|
||||
char fileName[100];
|
||||
#ifdef _WINDOWS
|
||||
sprintf_s(fileName, "\\YAMA_%s_%s.txt", timeString, pid.c_str());
|
||||
#else
|
||||
sprintf_s(fileName, "\\log_%s_%s.txt", timeString, pid.c_str());
|
||||
#endif
|
||||
logFileName = dir + fileName;
|
||||
}
|
||||
std::string logFileName; // <20><>־<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
bool enable; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
bool threadRun; // <20><>־<EFBFBD>߳<EFBFBD>״̬
|
||||
std::queue<std::string> logQueue; // <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
||||
std::mutex queueMutex; // <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
|
||||
std::condition_variable cv; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
std::atomic<bool> running; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
std::thread workerThread; // <20><>̨<EFBFBD>߳<EFBFBD>
|
||||
std::mutex fileMutex; // <20>ļ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
||||
std::string pid; // <20><><EFBFBD><EFBFBD>ID
|
||||
|
||||
Logger() : enable(false), threadRun(false), running(true), workerThread(&Logger::processLogs, this) {}
|
||||
|
||||
~Logger() {
|
||||
stop();
|
||||
}
|
||||
|
||||
// <20><>̨<EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD>־
|
||||
void processLogs() {
|
||||
threadRun = true;
|
||||
while (running) {
|
||||
std::unique_lock<std::mutex> lock(queueMutex);
|
||||
cv.wait(lock, [this]() {
|
||||
return !running || !logQueue.empty();
|
||||
});
|
||||
|
||||
while (running && !logQueue.empty()) {
|
||||
std::string logEntry = logQueue.front();
|
||||
logQueue.pop();
|
||||
lock.unlock();
|
||||
|
||||
// д<><D0B4><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD>
|
||||
writeToFile(logEntry);
|
||||
|
||||
lock.lock();
|
||||
}
|
||||
lock.unlock();
|
||||
}
|
||||
threadRun = false;
|
||||
}
|
||||
|
||||
// д<><D0B4><EFBFBD>ļ<EFBFBD>
|
||||
void writeToFile(const std::string& logEntry) {
|
||||
std::lock_guard<std::mutex> lock(fileMutex);
|
||||
std::ofstream logFile(logFileName, std::ios::app);
|
||||
if (logFile.is_open()) {
|
||||
logFile << logEntry << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1><EFBFBD><EFBFBD>
|
||||
std::string getCurrentTimestamp() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto in_time_t = std::chrono::system_clock::to_time_t(now);
|
||||
|
||||
std::tm tm;
|
||||
#ifdef _WIN32
|
||||
localtime_s(&tm, &in_time_t); // Windows <20><>ȫ<EFBFBD>汾
|
||||
#else
|
||||
localtime_r(&in_time_t, &tm); // POSIX <20><>ȫ<EFBFBD>汾
|
||||
#endif
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
std::string logLevelToString(LogLevel level) {
|
||||
switch (level) {
|
||||
case InfoLevel: return "INFO";
|
||||
case WarningLevel: return "WARNING";
|
||||
case ErrorLevel: return "ERROR";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
// <20><>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
std::string formatString(const char* format, va_list args) {
|
||||
char buffer[1024];
|
||||
vsnprintf(buffer, sizeof(buffer), format, args);
|
||||
return std::string(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
inline const char* getFileName(const char* path) {
|
||||
const char* fileName = strrchr(path, '\\');
|
||||
if (!fileName) {
|
||||
fileName = strrchr(path, '/');
|
||||
}
|
||||
return fileName ? fileName + 1 : path;
|
||||
}
|
||||
|
||||
#ifdef _WINDOWS
|
||||
#ifdef _DEBUG
|
||||
#define Mprintf(format, ...) TRACE(format, __VA_ARGS__)
|
||||
#else
|
||||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(__FILE__), __LINE__, format, __VA_ARGS__)
|
||||
#endif
|
||||
#else
|
||||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(skCrypt(__FILE__)), __LINE__, skCrypt(format), __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#endif // _WIN32
|
||||
Reference in New Issue
Block a user