drm_print.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2016 Red Hat
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors:
  23. * Rob Clark <robdclark@gmail.com>
  24. */
  25. #ifndef DRM_PRINT_H_
  26. #define DRM_PRINT_H_
  27. #include <linux/compiler.h>
  28. #include <linux/printk.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/device.h>
  31. /**
  32. * DOC: print
  33. *
  34. * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
  35. * debug code to be used for both debugfs and printk logging.
  36. *
  37. * For example::
  38. *
  39. * void log_some_info(struct drm_printer *p)
  40. * {
  41. * drm_printf(p, "foo=%d\n", foo);
  42. * drm_printf(p, "bar=%d\n", bar);
  43. * }
  44. *
  45. * #ifdef CONFIG_DEBUG_FS
  46. * void debugfs_show(struct seq_file *f)
  47. * {
  48. * struct drm_printer p = drm_seq_file_printer(f);
  49. * log_some_info(&p);
  50. * }
  51. * #endif
  52. *
  53. * void some_other_function(...)
  54. * {
  55. * struct drm_printer p = drm_info_printer(drm->dev);
  56. * log_some_info(&p);
  57. * }
  58. */
  59. /**
  60. * struct drm_printer - drm output "stream"
  61. *
  62. * Do not use struct members directly. Use drm_printer_seq_file(),
  63. * drm_printer_info(), etc to initialize. And drm_printf() for output.
  64. */
  65. struct drm_printer {
  66. /* private: */
  67. void (*printfn)(struct drm_printer *p, struct va_format *vaf);
  68. void *arg;
  69. const char *prefix;
  70. };
  71. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
  72. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
  73. void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
  74. __printf(2, 3)
  75. void drm_printf(struct drm_printer *p, const char *f, ...);
  76. /**
  77. * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
  78. * @f: the &struct seq_file to output to
  79. *
  80. * RETURNS:
  81. * The &drm_printer object
  82. */
  83. static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
  84. {
  85. struct drm_printer p = {
  86. .printfn = __drm_printfn_seq_file,
  87. .arg = f,
  88. };
  89. return p;
  90. }
  91. /**
  92. * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
  93. * @dev: the &struct device pointer
  94. *
  95. * RETURNS:
  96. * The &drm_printer object
  97. */
  98. static inline struct drm_printer drm_info_printer(struct device *dev)
  99. {
  100. struct drm_printer p = {
  101. .printfn = __drm_printfn_info,
  102. .arg = dev,
  103. };
  104. return p;
  105. }
  106. /**
  107. * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
  108. * @prefix: debug output prefix
  109. *
  110. * RETURNS:
  111. * The &drm_printer object
  112. */
  113. static inline struct drm_printer drm_debug_printer(const char *prefix)
  114. {
  115. struct drm_printer p = {
  116. .printfn = __drm_printfn_debug,
  117. .prefix = prefix
  118. };
  119. return p;
  120. }
  121. #endif /* DRM_PRINT_H_ */