-
Notifications
You must be signed in to change notification settings - Fork 1
/
asynclog.h
51 lines (41 loc) · 986 Bytes
/
asynclog.h
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
#pragma once
#ifndef ASYNCLOG_H
#define ASYNCLOG_H
#include <vector>
#include <memory>
#include <functional>
#include <stdlib.h>
#include <unistd.h>
#include "mutex.h"
#include "condition.h"
#include "thread.h"
#include "fixedbuffer.h"
#include "logfile.h"
static const size_t kFixedBufferSize = 4096;
class AsyncLog
{
public:
typedef FixedBuffer<kFixedBufferSize> Buffer;
typedef std::unique_ptr<Buffer> BufferPtr;
AsyncLog();
~AsyncLog();
AsyncLog(const AsyncLog &) = delete;
AsyncLog & operator= (const AsyncLog &) = delete;
void Start();
void Stop();
void Sync();
void Append(const char * data, size_t writeSize);
void ThreadFunc();
void NotifyLog();
private:
bool m_bRunning;
std::unique_ptr<Mutex> m_mutex;
std::unique_ptr<Condition> m_mainThreadRunCond;
std::unique_ptr<Condition> m_subThreadFlushCond;
Thread m_thread;
BufferPtr m_curBuff;
BufferPtr m_nextBuff;
std::vector<BufferPtr> m_buffers;
std::unique_ptr<LogFile> m_logFilePtr;
};
#endif