-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsnippet_filelog_main.cpp
88 lines (76 loc) · 2.43 KB
/
snippet_filelog_main.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
//
// g++ test_FileLog.cpp -lg3logger -lpthread
// Sink location: snippets
// the manpage of memfd_create says sys/mman.h, but the version of glibc
// installed on travis has it in sys/memfd.h
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
#include "g3sinks/FileLogSink.h"
#include <iostream>
#include <string.h>
// for open(2):
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_MMAN
#include <sys/mman.h>
#endif
#ifdef HAVE_MEMFD
#include <sys/memfd.h>
#endif
void content_to_stderr(int fd) {
ssize_t dataSize;
ssize_t usefulBufSize = 1234;
char buf[usefulBufSize + 1];
std::cerr << "buffer contents:" << std::endl;
lseek(fd, 0, SEEK_SET);
do {
dataSize = read(fd, buf, usefulBufSize);
if (dataSize > 0) {
buf[dataSize] = 0;
std::cerr << buf;
}
} while (dataSize > 0);
}
int new_mem_fd(const char* name) {
#ifdef MFD_CLOEXEC
int fd = memfd_create(name, MFD_CLOEXEC);
#else
// emulate memfd with a file that we unlink immediately
std::cerr << "Emulating memfd_create() in " << __FILE__ << std::endl;
int fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (unlink(name) != 0) {
std::cerr << "ERROR: unable to unlink " << name << " : " << strerror(errno) << std::endl;
exit(1);
}
#endif
if (fd == -1) {
std::cerr << "ERROR: unable to create the file descriptor. " << strerror(errno) << std::endl;
exit(1);
}
return fd;
}
int main(int argc, char** argv) {
int fd1 = new_mem_fd("memfile_1");
std::unique_ptr<g3::LogWorker> logworker{ g3::LogWorker::createLogWorker() };
auto sinkHandle = logworker->addSink(std::make_unique<FileLogSink>(fd1, false),
&FileLogSink::ReceiveLogMessage);
initializeLogging(logworker.get());
LOG(INFO) << "This content is written to the first memory file descriptor";
sleep(1); // TODO: wait for the logger to finish, in a cleaner way...
int fd2 = new_mem_fd("memfile_2");
std::future<void> to_wait = sinkHandle->call(&FileLogSink::Rotate, fd2, true);
to_wait.wait();
content_to_stderr(fd1);
close(fd1);
LOG(INFO) << "This content is written to the second memory file descriptor";
sleep(1); // TODO: wait for the logger to finish, in a cleaner way...
to_wait = sinkHandle->call(&FileLogSink::sync);
to_wait.wait();
content_to_stderr(fd2);
close(fd2);
return 0;
}