util: logging to the afb daemon
[staging/windowmanager.git] / src / util.hpp
1 #ifndef WM_UTIL_HPP
2 #define WM_UTIL_HPP
3
4 #include <functional>
5 #include <sys/poll.h>
6 #include <vector>
7
8 extern "C" {
9 #define AFB_BINDING_VERSION 2
10 #include <afb/afb-binding.h>
11 };
12
13 #ifdef __GNUC__
14 #define ATTR_FORMAT(stringindex, firsttocheck) \
15    __attribute__((format(printf, stringindex, firsttocheck)))
16 #define ATTR_NORETURN __attribute__((noreturn))
17 #else
18 #define ATTR_FORMAT(stringindex, firsttocheck)
19 #define ATTR_NORETURN
20 #endif
21
22 #define lognotice(...) AFB_NOTICE(__VA_ARGS__)
23 #define logerror(...) AFB_ERROR(__VA_ARGS__)
24 #define fatal(...)            \
25    do {                       \
26       AFB_ERROR(__VA_ARGS__); \
27       abort();                \
28    } while (0)
29
30 #ifdef DEBUG_OUTPUT
31 #define logdebug(...) AFB_DEBUG(__VA_ARGS__)
32 #else
33 #define logdebug(...)
34 #endif
35
36 //      _                   _                 _                       __     _
37 //  ___| |_ _ __ _   _  ___| |_   _   _ _ __ (_) __ _ _   _  ___     / _| __| |
38 // / __| __| '__| | | |/ __| __| | | | | '_ \| |/ _` | | | |/ _ \   | |_ / _` |
39 // \__ \ |_| |  | |_| | (__| |_  | |_| | | | | | (_| | |_| |  __/   |  _| (_| |
40 // |___/\__|_|   \__,_|\___|\__|  \__,_|_| |_|_|\__, |\__,_|\___|___|_|  \__,_|
41 //                                                 |_|         |_____|
42 struct unique_fd {
43    int fd{-1};
44    unique_fd() = default;
45    explicit unique_fd(int f) : fd{f} {}
46    operator int() const { return fd; }
47    ~unique_fd();
48    unique_fd(unique_fd const &) = delete;
49    unique_fd &operator=(unique_fd const &) = delete;
50    unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
51    unique_fd &operator=(unique_fd &&o) {
52       std::swap(this->fd, o.fd);
53       return *this;
54    }
55 };
56
57 //      _                   _     ____       _ _
58 //  ___| |_ _ __ _   _  ___| |_  |  _ \ ___ | | | ___ _ __
59 // / __| __| '__| | | |/ __| __| | |_) / _ \| | |/ _ \ '__|
60 // \__ \ |_| |  | |_| | (__| |_  |  __/ (_) | | |  __/ |
61 // |___/\__|_|   \__,_|\___|\__| |_|   \___/|_|_|\___|_|
62 //
63 struct Poller {
64    std::vector<std::function<int(int)>> handlers;
65    std::vector<struct pollfd> pfds;
66
67    Poller() = default;
68    void add_fd(int fd, std::function<int(int)> handler);
69    int check_events();
70 };
71
72 #endif  // !WM_UTIL_HPP