11 struct strftime_cache {
16 static void log_(char const *log_type, FILE *stream, char const *fmt,
18 static struct strftime_cache strft;
20 time_t t = time(nullptr);
21 if (t != strft.time) {
24 struct tm *tmp = localtime_r(&t, &tm);
25 strftime(strft.buf, sizeof(strft.buf), "%Y-%m-%dT%H:%M:%S", tmp);
28 fputs(program_invocation_short_name, stream);
30 fputs(strft.buf, stream);
32 fputs(log_type, stream);
34 vfprintf(stream, fmt, args);
38 void lognotice(char const *fmt, ...) {
41 log_("notice", stdout, fmt, a);
45 void logerror(char const *fmt, ...) {
48 log_("error", stderr, fmt, a);
52 void fatal(char const *fmt, ...) {
55 log_("fatal", stderr, fmt, a);
61 void logdebug(char const *fmt, ...) {
64 log_("debug", stdout, fmt, a);
69 void Poller::add_fd(int fd, std::function<int(int)> handler) {
70 pfds.emplace_back(pollfd{.fd = fd, .events = POLLIN, .revents = 0});
71 handlers.emplace_back(std::move(handler));
74 int Poller::check_events() {
76 if ((ret = poll(this->pfds.data(), this->pfds.size(), -1)) != -1 &&
78 for (unsigned i = 0; i < pfds.size(); i++) {
79 if ((pfds[i].revents & POLLIN) != 0) {
80 if (handlers[i](pfds[i].fd) == -1) {
84 pfds[i].events = POLLIN;
91 unique_fd::~unique_fd() {