-
Notifications
You must be signed in to change notification settings - Fork 7
/
logwriter.h
124 lines (101 loc) · 2.91 KB
/
logwriter.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
*/
#ifndef _LOGWRITER_H_
#define _LOGWRITER_H_
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/epoll.h>
template <typename InputBuffer>
class LogWriter {
public:
LogWriter(const char *dst, InputBuffer *inbuffer, bool isStream = false);
~LogWriter();
bool run();
bool stop();
private:
static int open(const char *addr, bool isStream);
private:
bool isStream_;
const char *dst_;
InputBuffer *inbuffer_;
int fd_;
char buffer_[InputBuffer::nbuffer];
bool quit_;
};
template <typename InputBuffer>
LogWriter<InputBuffer>::LogWriter(const char *dst, InputBuffer *inbuffer, bool isStream)
: isStream_(isStream), dst_(dst), inbuffer_(inbuffer), fd_(-1), quit_(false) { }
template <typename InputBuffer>
LogWriter<InputBuffer>::~LogWriter()
{
if (fd_ != -1) close(fd_);
}
template <typename InputBuffer>
int LogWriter<InputBuffer>::open(const char *dst, bool isStream)
{
int fd;
if ((fd = socket(AF_UNIX, isStream ? SOCK_STREAM : SOCK_DGRAM, 0)) < 0) {
fprintf(stderr, "socket() error, %d:%s\n", errno, strerror(errno));
return -1;
}
struct sockaddr_un un;
socklen_t len;
memset(&un, 0x00, sizeof(un));
un.sun_family = AF_UNIX;
sprintf(un.sun_path, "/var/tmp/%05d", getpid());
len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
unlink(un.sun_path);
/* unlink tmp file, the file will not be really unlinked until closed */
if (bind(fd, (struct sockaddr *)(&un), len) < 0) {
fprintf(stderr, "bind(%s) error, %d:%s\n", un.sun_path, errno, strerror(errno));
unlink(un.sun_path);
close(fd);
return -1;
}
unlink(un.sun_path);
memset(&un, 0x00, sizeof(un));
un.sun_family = AF_UNIX;
strcpy(un.sun_path, dst);
len = offsetof(struct sockaddr_un, sun_path) + strlen(dst);
if (connect(fd, (struct sockaddr*)(&un), len) < 0) {
fprintf(stderr, "connect(%s) error, %d:%s\n", dst, errno, strerror(errno));
close(fd);
return -1;
}
return fd;
}
template <typename InputBuffer>
bool LogWriter<InputBuffer>::run()
{
while (!quit_) {
int fd = open(dst_, isStream_);
if (fd == -1) {
sleep(1);
continue;
}
while (!quit_) {
size_t n = inbuffer_->read(buffer_, InputBuffer::nbuffer);
size_t pos = 0;
ssize_t nn = 0;
while((nn = send(fd, buffer_ + pos, n - pos, MSG_NOSIGNAL)) > 0) {
pos += nn;
}
if (nn == -1 && errno != EAGAIN) break;
}
close(fd);
}
return true;
}
template <typename InputBuffer>
bool LogWriter<InputBuffer>::stop()
{
quit_ = true;
return true;
}
#endif