util: add ability to disable afb logging macro usage
[staging/windowmanager.git] / src / util.hpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef WM_UTIL_HPP
18 #define WM_UTIL_HPP
19
20 #include <functional>
21 #include <thread>
22 #include <vector>
23
24 #include <sys/poll.h>
25
26 #ifndef DO_NOT_USE_AFB
27 extern "C" {
28 #include <afb/afb-binding.h>
29 };
30 #endif
31
32 #define CONCAT_(X, Y) X##Y
33 #define CONCAT(X, Y) CONCAT_(X, Y)
34
35 #ifdef __GNUC__
36 #define ATTR_FORMAT(stringindex, firsttocheck) \
37    __attribute__((format(printf, stringindex, firsttocheck)))
38 #define ATTR_NORETURN __attribute__((noreturn))
39 #else
40 #define ATTR_FORMAT(stringindex, firsttocheck)
41 #define ATTR_NORETURN
42 #endif
43
44 #ifdef AFB_BINDING_VERSION
45 #define lognotice(...) AFB_NOTICE(__VA_ARGS__)
46 #define logerror(...) AFB_ERROR(__VA_ARGS__)
47 #define fatal(...)            \
48    do {                       \
49       AFB_ERROR(__VA_ARGS__); \
50       abort();                \
51    } while (0)
52 #else
53 #define lognotice(...)
54 #define logerror(...)
55 #define fatal(...)            \
56    do {                       \
57       abort();                \
58    } while (0)
59 #endif
60
61 #ifdef DEBUG_OUTPUT
62 #ifdef AFB_BINDING_VERSION
63 #define logdebug(...) AFB_DEBUG(__VA_ARGS__)
64 #else
65 #define logdebug(...)
66 #endif
67 #else
68 #define logdebug(...)
69 #endif
70
71 #ifndef SCOPE_TRACING
72 #define ST()
73 #define STN(N)
74 #else
75 #define ST() \
76     ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
77 #define STN(N) \
78     ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
79
80 struct ScopeTrace {
81    thread_local static int indent;
82    char const *f{};
83    explicit ScopeTrace(char const *func);
84    ~ScopeTrace();
85 };
86 #endif
87
88 //      _                   _                 _                       __     _
89 //  ___| |_ _ __ _   _  ___| |_   _   _ _ __ (_) __ _ _   _  ___     / _| __| |
90 // / __| __| '__| | | |/ __| __| | | | | '_ \| |/ _` | | | |/ _ \   | |_ / _` |
91 // \__ \ |_| |  | |_| | (__| |_  | |_| | | | | | (_| | |_| |  __/   |  _| (_| |
92 // |___/\__|_|   \__,_|\___|\__|  \__,_|_| |_|_|\__, |\__,_|\___|___|_|  \__,_|
93 //                                                 |_|         |_____|
94 struct unique_fd {
95    int fd{-1};
96    unique_fd() = default;
97    explicit unique_fd(int f) : fd{f} {}
98    operator int() const { return fd; }
99    ~unique_fd();
100    unique_fd(unique_fd const &) = delete;
101    unique_fd &operator=(unique_fd const &) = delete;
102    unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
103    unique_fd &operator=(unique_fd &&o) {
104       std::swap(this->fd, o.fd);
105       return *this;
106    }
107 };
108
109 #endif  // !WM_UTIL_HPP