-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasync_logging.h
79 lines (59 loc) · 1.54 KB
/
async_logging.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
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
#ifndef __YDX_ASYNC_LOGGING_H__
#define __YDX_ASYNC_LOGGING_H__
#include "ydx_countdown_lauch.h"
#include "thread.h"
#include "ydx_mutex.h"
#include "log_stream.h"
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
namespace ydx
{
class AsyncLogging : boost::noncopyable
{
AsyncLogging(const std::string& basename = "",
size_t rollSize = 1000 * 1000 * 5,
int flushInterval = 3);
~AsyncLogging()
{
if (running_)
{
stop();
}
}
void append(const char* logline, int len);
void start()
{
running_ = true;
thread_.start();
launch_.wait();
}
void stop()
{
running_ = false;
condition_.notify();
thread_.join();
}
private:
// declare but not define, prevent compiler-synthesized functions
AsyncLogging(const AsyncLogging&); // ptr_container
void operator=(const AsyncLogging&); // ptr_container
void threadFunc();
typedef ydx::FixedBuffer<ydx::kLargeBuffer> FIXED_BUFFER;
typedef boost::ptr_vector<FIXED_BUFFER> FIXED_BUFFER_VECTOR;
typedef FIXED_BUFFER_VECTOR::auto_type FIXED_BUFFER_MEMBER_PTR;
const int flush_interval_; //日志刷新间隔
bool running_; //日志线程运行标识
std::string basename_; //日志文件主名称
size_t roll_size_; //多大切换写新文件
ydx::Thread thread_;
ydx::CountDownLauch launch_; //等待信号启动
ydx::MutexLock mutex_;
ydx::Condition condition_;
FIXED_BUFFER_MEMBER_PTR current_buf_;
FIXED_BUFFER_MEMBER_PTR next_buf_;
FIXED_BUFFER_VECTOR buffer_vector_;
};
}
#endif