55 lines
1.4 KiB
C++
55 lines
1.4 KiB
C++
//---------------------------------------------------------------------------
|
|
#ifndef logRotateH
|
|
#define logRotateH
|
|
//---------------------------------------------------------------------------
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <queue>
|
|
#include <thread>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <chrono>
|
|
#include <ctime>
|
|
#include <sys/time.h>
|
|
#include <atomic>
|
|
#include <cstdio>
|
|
//---------------------------------------------------------------------------
|
|
struct LogRec{
|
|
std::string thread;
|
|
std::string level;
|
|
std::string data;
|
|
bool cout;
|
|
};
|
|
//---------------------------------------------------------------------------
|
|
class Logger {
|
|
private:
|
|
|
|
std::ofstream file;
|
|
std::string fileName;
|
|
std::string date;
|
|
|
|
std::atomic<bool> stop;
|
|
|
|
std::mutex queueMutex;
|
|
|
|
std::queue<LogRec> logQueue;
|
|
std::condition_variable queueCondition;
|
|
std::thread logThread;
|
|
|
|
void processQueue();
|
|
void writeLog(std::string thread, std::string level, std::string data, bool cout);
|
|
void rotateLogFile();
|
|
|
|
void openLogFile();
|
|
std::string getCurrentDate();
|
|
std::string getCurrentDateTime();
|
|
public:
|
|
Logger(std::string fileName);
|
|
~Logger();
|
|
void log(std::string thread, std::string level, std::string data, bool cout=false);
|
|
};
|
|
//---------------------------------------------------------------------------
|
|
#endif
|