drm_print.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/seq_file.h>
  28. #include <linux/device.h>
  29. /**
  30. * DOC: print
  31. *
  32. * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
  33. * debug code to be used for both debugfs and printk logging.
  34. *
  35. * For example::
  36. *
  37. * void log_some_info(struct drm_printer *p)
  38. * {
  39. * drm_printf(p, "foo=%d\n", foo);
  40. * drm_printf(p, "bar=%d\n", bar);
  41. * }
  42. *
  43. * #ifdef CONFIG_DEBUG_FS
  44. * void debugfs_show(struct seq_file *f)
  45. * {
  46. * struct drm_printer p = drm_seq_file_printer(f);
  47. * log_some_info(&p);
  48. * }
  49. * #endif
  50. *
  51. * void some_other_function(...)
  52. * {
  53. * struct drm_printer p = drm_info_printer(drm->dev);
  54. * log_some_info(&p);
  55. * }
  56. */
  57. /**
  58. * struct drm_printer - drm output "stream"
  59. *
  60. * Do not use struct members directly. Use drm_printer_seq_file(),
  61. * drm_printer_info(), etc to initialize. And drm_printf() for output.
  62. */
  63. struct drm_printer {
  64. /* private: */
  65. void (*printfn)(struct drm_printer *p, struct va_format *vaf);
  66. void *arg;
  67. const char *prefix;
  68. };
  69. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
  70. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
  71. void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
  72. void drm_printf(struct drm_printer *p, const char *f, ...);
  73. /**
  74. * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
  75. * @f: the &struct seq_file to output to
  76. *
  77. * RETURNS:
  78. * The &drm_printer object
  79. */
  80. static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
  81. {
  82. struct drm_printer p = {
  83. .printfn = __drm_printfn_seq_file,
  84. .arg = f,
  85. };
  86. return p;
  87. }
  88. /**
  89. * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
  90. * @dev: the &struct device pointer
  91. *
  92. * RETURNS:
  93. * The &drm_printer object
  94. */
  95. static inline struct drm_printer drm_info_printer(struct device *dev)
  96. {
  97. struct drm_printer p = {
  98. .printfn = __drm_printfn_info,
  99. .arg = dev,
  100. };
  101. return p;
  102. }
  103. /**
  104. * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
  105. * @prefix: debug output prefix
  106. *
  107. * RETURNS:
  108. * The &drm_printer object
  109. */
  110. static inline struct drm_printer drm_debug_printer(const char *prefix)
  111. {
  112. struct drm_printer p = {
  113. .printfn = __drm_printfn_debug,
  114. .prefix = prefix
  115. };
  116. return p;
  117. }
  118. #endif /* DRM_PRINT_H_ */