batman-adv: Add SPDX license identifier above copyright header
[sfrench/cifs-2.6.git] / net / batman-adv / log.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2007-2017  B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef _NET_BATMAN_ADV_LOG_H_
20 #define _NET_BATMAN_ADV_LOG_H_
21
22 #include "main.h"
23
24 #include <linux/bitops.h>
25 #include <linux/compiler.h>
26 #include <linux/printk.h>
27
28 #ifdef CONFIG_BATMAN_ADV_DEBUG
29
30 int batadv_debug_log_setup(struct batadv_priv *bat_priv);
31 void batadv_debug_log_cleanup(struct batadv_priv *bat_priv);
32
33 #else
34
35 static inline int batadv_debug_log_setup(struct batadv_priv *bat_priv)
36 {
37         return 0;
38 }
39
40 static inline void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
41 {
42 }
43
44 #endif
45
46 /**
47  * enum batadv_dbg_level - available log levels
48  * @BATADV_DBG_BATMAN: OGM and TQ computations related messages
49  * @BATADV_DBG_ROUTES: route added / changed / deleted
50  * @BATADV_DBG_TT: translation table messages
51  * @BATADV_DBG_BLA: bridge loop avoidance messages
52  * @BATADV_DBG_DAT: ARP snooping and DAT related messages
53  * @BATADV_DBG_NC: network coding related messages
54  * @BATADV_DBG_MCAST: multicast related messages
55  * @BATADV_DBG_TP_METER: throughput meter messages
56  * @BATADV_DBG_ALL: the union of all the above log levels
57  */
58 enum batadv_dbg_level {
59         BATADV_DBG_BATMAN       = BIT(0),
60         BATADV_DBG_ROUTES       = BIT(1),
61         BATADV_DBG_TT           = BIT(2),
62         BATADV_DBG_BLA          = BIT(3),
63         BATADV_DBG_DAT          = BIT(4),
64         BATADV_DBG_NC           = BIT(5),
65         BATADV_DBG_MCAST        = BIT(6),
66         BATADV_DBG_TP_METER     = BIT(7),
67         BATADV_DBG_ALL          = 255,
68 };
69
70 #ifdef CONFIG_BATMAN_ADV_DEBUG
71 int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
72 __printf(2, 3);
73
74 /* possibly ratelimited debug output */
75 #define _batadv_dbg(type, bat_priv, ratelimited, fmt, arg...)           \
76         do {                                                            \
77                 struct batadv_priv *__batpriv = (bat_priv);             \
78                 if (atomic_read(&__batpriv->log_level) & (type) &&      \
79                     (!(ratelimited) || net_ratelimit()))                \
80                         batadv_debug_log(__batpriv, fmt, ## arg);       \
81         }                                                               \
82         while (0)
83 #else /* !CONFIG_BATMAN_ADV_DEBUG */
84 __printf(4, 5)
85 static inline void _batadv_dbg(int type __always_unused,
86                                struct batadv_priv *bat_priv __always_unused,
87                                int ratelimited __always_unused,
88                                const char *fmt __always_unused, ...)
89 {
90 }
91 #endif
92
93 #define batadv_dbg(type, bat_priv, arg...) \
94         _batadv_dbg(type, bat_priv, 0, ## arg)
95 #define batadv_dbg_ratelimited(type, bat_priv, arg...) \
96         _batadv_dbg(type, bat_priv, 1, ## arg)
97
98 #define batadv_info(net_dev, fmt, arg...)                               \
99         do {                                                            \
100                 struct net_device *_netdev = (net_dev);                 \
101                 struct batadv_priv *_batpriv = netdev_priv(_netdev);    \
102                 batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg);      \
103                 pr_info("%s: " fmt, _netdev->name, ## arg);             \
104         } while (0)
105 #define batadv_err(net_dev, fmt, arg...)                                \
106         do {                                                            \
107                 struct net_device *_netdev = (net_dev);                 \
108                 struct batadv_priv *_batpriv = netdev_priv(_netdev);    \
109                 batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg);      \
110                 pr_err("%s: " fmt, _netdev->name, ## arg);              \
111         } while (0)
112
113 #endif /* _NET_BATMAN_ADV_LOG_H_ */