blob: 7d98763c0444dac729e880d28c1596a059bf860c [file] [log] [blame]
Rob Clarkd81871772016-11-05 11:08:07 -04001/*
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
26#ifndef DRM_PRINT_H_
27#define DRM_PRINT_H_
28
29#include <linux/seq_file.h>
30#include <linux/device.h>
31
32/**
33 * DOC: print
34 *
35 * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
36 * debug code to be used for both debugfs and printk logging.
37 *
38 * For example::
39 *
40 * void log_some_info(struct drm_printer *p)
41 * {
42 * drm_printf(p, "foo=%d\n", foo);
43 * drm_printf(p, "bar=%d\n", bar);
44 * }
45 *
46 * #ifdef CONFIG_DEBUG_FS
47 * void debugfs_show(struct seq_file *f)
48 * {
49 * struct drm_printer p = drm_seq_file_printer(f);
50 * log_some_info(&p);
51 * }
52 * #endif
53 *
54 * void some_other_function(...)
55 * {
56 * struct drm_printer p = drm_info_printer(drm->dev);
57 * log_some_info(&p);
58 * }
59 */
60
61/**
62 * struct drm_printer - drm output "stream"
Rob Clarkd81871772016-11-05 11:08:07 -040063 *
64 * Do not use struct members directly. Use drm_printer_seq_file(),
65 * drm_printer_info(), etc to initialize. And drm_printf() for output.
66 */
67struct drm_printer {
Daniel Vetter3d387d92016-12-28 17:42:09 +010068 /* private: */
Rob Clarkd81871772016-11-05 11:08:07 -040069 void (*printfn)(struct drm_printer *p, struct va_format *vaf);
70 void *arg;
Daniel Vetter3d387d92016-12-28 17:42:09 +010071 const char *prefix;
Rob Clarkd81871772016-11-05 11:08:07 -040072};
73
74void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
75void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
Daniel Vetter3d387d92016-12-28 17:42:09 +010076void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
Rob Clarkd81871772016-11-05 11:08:07 -040077
Rob Clarkd81871772016-11-05 11:08:07 -040078void drm_printf(struct drm_printer *p, const char *f, ...);
79
80
81/**
82 * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
Daniel Vetterea0dd852016-12-29 21:48:26 +010083 * @f: the &struct seq_file to output to
Rob Clarkd81871772016-11-05 11:08:07 -040084 *
85 * RETURNS:
86 * The &drm_printer object
87 */
88static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
89{
90 struct drm_printer p = {
91 .printfn = __drm_printfn_seq_file,
92 .arg = f,
93 };
94 return p;
95}
96
97/**
98 * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
Daniel Vetterea0dd852016-12-29 21:48:26 +010099 * @dev: the &struct device pointer
Rob Clarkd81871772016-11-05 11:08:07 -0400100 *
101 * RETURNS:
102 * The &drm_printer object
103 */
104static inline struct drm_printer drm_info_printer(struct device *dev)
105{
106 struct drm_printer p = {
107 .printfn = __drm_printfn_info,
108 .arg = dev,
109 };
110 return p;
111}
112
Daniel Vetter3d387d92016-12-28 17:42:09 +0100113/**
114 * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
115 * @prefix: debug output prefix
116 *
117 * RETURNS:
118 * The &drm_printer object
119 */
120static inline struct drm_printer drm_debug_printer(const char *prefix)
121{
122 struct drm_printer p = {
123 .printfn = __drm_printfn_debug,
124 .prefix = prefix
125 };
126 return p;
127}
Rob Clarkd81871772016-11-05 11:08:07 -0400128#endif /* DRM_PRINT_H_ */