log.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. */
  6. #ifndef _NET_BATMAN_ADV_LOG_H_
  7. #define _NET_BATMAN_ADV_LOG_H_
  8. #include "main.h"
  9. #include <linux/atomic.h>
  10. #include <linux/bitops.h>
  11. #include <linux/compiler.h>
  12. #include <linux/printk.h>
  13. #ifdef CONFIG_BATMAN_ADV_DEBUG
  14. int batadv_debug_log_setup(struct batadv_priv *bat_priv);
  15. void batadv_debug_log_cleanup(struct batadv_priv *bat_priv);
  16. #else
  17. static inline int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  18. {
  19. return 0;
  20. }
  21. static inline void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  22. {
  23. }
  24. #endif
  25. /**
  26. * enum batadv_dbg_level - available log levels
  27. */
  28. enum batadv_dbg_level {
  29. /** @BATADV_DBG_BATMAN: OGM and TQ computations related messages */
  30. BATADV_DBG_BATMAN = BIT(0),
  31. /** @BATADV_DBG_ROUTES: route added / changed / deleted */
  32. BATADV_DBG_ROUTES = BIT(1),
  33. /** @BATADV_DBG_TT: translation table messages */
  34. BATADV_DBG_TT = BIT(2),
  35. /** @BATADV_DBG_BLA: bridge loop avoidance messages */
  36. BATADV_DBG_BLA = BIT(3),
  37. /** @BATADV_DBG_DAT: ARP snooping and DAT related messages */
  38. BATADV_DBG_DAT = BIT(4),
  39. /** @BATADV_DBG_MCAST: multicast related messages */
  40. BATADV_DBG_MCAST = BIT(6),
  41. /** @BATADV_DBG_TP_METER: throughput meter messages */
  42. BATADV_DBG_TP_METER = BIT(7),
  43. /** @BATADV_DBG_ALL: the union of all the above log levels */
  44. BATADV_DBG_ALL = 255,
  45. };
  46. #ifdef CONFIG_BATMAN_ADV_DEBUG
  47. int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
  48. __printf(2, 3);
  49. /**
  50. * _batadv_dbg() - Store debug output with(out) rate limiting
  51. * @type: type of debug message
  52. * @bat_priv: the bat priv with all the mesh interface information
  53. * @ratelimited: whether output should be rate limited
  54. * @fmt: format string
  55. * @arg: variable arguments
  56. */
  57. #define _batadv_dbg(type, bat_priv, ratelimited, fmt, arg...) \
  58. do { \
  59. struct batadv_priv *__batpriv = (bat_priv); \
  60. if (atomic_read(&__batpriv->log_level) & (type) && \
  61. (!(ratelimited) || net_ratelimit())) \
  62. batadv_debug_log(__batpriv, fmt, ## arg); \
  63. } \
  64. while (0)
  65. #else /* !CONFIG_BATMAN_ADV_DEBUG */
  66. __printf(4, 5)
  67. static inline void _batadv_dbg(int type __always_unused,
  68. struct batadv_priv *bat_priv __always_unused,
  69. int ratelimited __always_unused,
  70. const char *fmt __always_unused, ...)
  71. {
  72. }
  73. #endif
  74. /**
  75. * batadv_dbg() - Store debug output without rate limiting
  76. * @type: type of debug message
  77. * @bat_priv: the bat priv with all the mesh interface information
  78. * @arg: format string and variable arguments
  79. */
  80. #define batadv_dbg(type, bat_priv, arg...) \
  81. _batadv_dbg(type, bat_priv, 0, ## arg)
  82. /**
  83. * batadv_dbg_ratelimited() - Store debug output with rate limiting
  84. * @type: type of debug message
  85. * @bat_priv: the bat priv with all the mesh interface information
  86. * @arg: format string and variable arguments
  87. */
  88. #define batadv_dbg_ratelimited(type, bat_priv, arg...) \
  89. _batadv_dbg(type, bat_priv, 1, ## arg)
  90. /**
  91. * batadv_info() - Store message in debug buffer and print it to kmsg buffer
  92. * @net_dev: the mesh interface net device
  93. * @fmt: format string
  94. * @arg: variable arguments
  95. */
  96. #define batadv_info(net_dev, fmt, arg...) \
  97. do { \
  98. struct net_device *_netdev = (net_dev); \
  99. struct batadv_priv *_batpriv = netdev_priv(_netdev); \
  100. batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg); \
  101. pr_info("%s: " fmt, _netdev->name, ## arg); \
  102. } while (0)
  103. /**
  104. * batadv_err() - Store error in debug buffer and print it to kmsg buffer
  105. * @net_dev: the mesh interface net device
  106. * @fmt: format string
  107. * @arg: variable arguments
  108. */
  109. #define batadv_err(net_dev, fmt, arg...) \
  110. do { \
  111. struct net_device *_netdev = (net_dev); \
  112. struct batadv_priv *_batpriv = netdev_priv(_netdev); \
  113. batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg); \
  114. pr_err("%s: " fmt, _netdev->name, ## arg); \
  115. } while (0)
  116. #endif /* _NET_BATMAN_ADV_LOG_H_ */