Remove the DB() makro, replace with logdebug() where sensible
[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 <sys/poll.h>
22
23 #include <vector>
24
25 extern "C" {
26 #include <afb/afb-binding.h>
27 };
28
29 #ifdef __GNUC__
30 #define ATTR_FORMAT(stringindex, firsttocheck) \
31    __attribute__((format(printf, stringindex, firsttocheck)))
32 #define ATTR_NORETURN __attribute__((noreturn))
33 #else
34 #define ATTR_FORMAT(stringindex, firsttocheck)
35 #define ATTR_NORETURN
36 #endif
37
38 #define lognotice(...) AFB_NOTICE(__VA_ARGS__)
39 #define logerror(...) AFB_ERROR(__VA_ARGS__)
40 #define fatal(...)            \
41    do {                       \
42       AFB_ERROR(__VA_ARGS__); \
43       abort();                \
44    } while (0)
45
46 #ifdef DEBUG_OUTPUT
47 #define logdebug(...) AFB_DEBUG(__VA_ARGS__)
48 #else
49 #define logdebug(...)
50 #endif
51
52 //      _                   _                 _                       __     _
53 //  ___| |_ _ __ _   _  ___| |_   _   _ _ __ (_) __ _ _   _  ___     / _| __| |
54 // / __| __| '__| | | |/ __| __| | | | | '_ \| |/ _` | | | |/ _ \   | |_ / _` |
55 // \__ \ |_| |  | |_| | (__| |_  | |_| | | | | | (_| | |_| |  __/   |  _| (_| |
56 // |___/\__|_|   \__,_|\___|\__|  \__,_|_| |_|_|\__, |\__,_|\___|___|_|  \__,_|
57 //                                                 |_|         |_____|
58 struct unique_fd {
59    int fd{-1};
60    unique_fd() = default;
61    explicit unique_fd(int f) : fd{f} {}
62    operator int() const { return fd; }
63    ~unique_fd();
64    unique_fd(unique_fd const &) = delete;
65    unique_fd &operator=(unique_fd const &) = delete;
66    unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
67    unique_fd &operator=(unique_fd &&o) {
68       std::swap(this->fd, o.fd);
69       return *this;
70    }
71 };
72
73 #endif  // !WM_UTIL_HPP