-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextLog.cpp
108 lines (95 loc) · 2.41 KB
/
TextLog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "TextLog.h"
#ifdef WIN32
#include "XZip.h"
#endif
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdio.h>
TextLog::TextLog(int lev)
{
m_filterLev = lev;
m_RunFlag = false;
}
bool TextLog::PopOneLogItem(LogItem& item)
{
std::lock_guard<std::mutex> lock(m_logMutex);
if (!m_logQue.empty())
{
item = m_logQue.front();
m_logQue.pop_front();
return true;
}
return false;
}
void TextLog::ProcRun()
{
#ifdef WIN32
::CreateDirectory(L"d:/log/", 0);
#else
mkdir("log", S_IRWXU | S_IRWXG | S_IROTH);
#endif
for (unsigned long tick = 0; m_RunFlag; ++tick)
{
LogItem item;
if (PopOneLogItem(item))
{
string fname = "D://log/" + item.file;
wstring wfname = A2U(fname);
ofstream log(fname, ios::app);
time_t now;
time(&now);
tm _tm;
char mbstr[100];
localtime_s(&_tm, &now);
strftime(mbstr, sizeof(mbstr), "%F %T", &_tm);
log << mbstr << ' ' << item.message;
#ifdef WIN32
long long pos = log.tellp();
if (pos > 10 * 1024 * 1024)
{
log.close();
string::size_type pos = fname.find('.', 0);
if (pos != string::npos)
fname.erase(pos);
wstring zfname = A2U(fname + to_string(now) + ".zip");
HZIP hz = CreateZip((void*)zfname.c_str(), 0, ZIP_FILENAME);
wstring zname = A2U(item.file);
ZRESULT rst = ZipAdd(hz, zname.c_str(), (void*)wfname.c_str(), 0, ZIP_FILENAME);
CloseZip(hz);
::DeleteFile(wfname.c_str());
}
#endif
}
Sleep(10);
}
}
TextLog& TextLog::Singleton()
{
static TextLog log;
return log;
}
TextLog::~TextLog()
{
}
void TextLog::Start()
{
m_RunFlag = true;
m_thrdLog = std::thread(&TextLog::ProcRun, std::ref(*this));
}
void TextLog::Stop()
{
m_RunFlag = false;
if (m_thrdLog.joinable())
{
m_thrdLog.join();
}
}
void TextLog::Write(const string& fname, const string& msg, int level)
{
if (level >= m_filterLev)
{
std::lock_guard<std::mutex> lock(m_logMutex);
m_logQue.emplace_back(fname, msg, level);
}
}