blob: ca4d7c6321f28fcea1286720f2da65a2db0395cf [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
Joe Perches3c6d6e02017-02-15 15:33:18 -080029#include <linux/compiler.h>
30#include <linux/printk.h>
Rob Clarkd81871772016-11-05 11:08:07 -040031#include <linux/seq_file.h>
32#include <linux/device.h>
33
34/**
35 * DOC: print
36 *
37 * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
38 * debug code to be used for both debugfs and printk logging.
39 *
40 * For example::
41 *
42 * void log_some_info(struct drm_printer *p)
43 * {
44 * drm_printf(p, "foo=%d\n", foo);
45 * drm_printf(p, "bar=%d\n", bar);
46 * }
47 *
48 * #ifdef CONFIG_DEBUG_FS
49 * void debugfs_show(struct seq_file *f)
50 * {
51 * struct drm_printer p = drm_seq_file_printer(f);
52 * log_some_info(&p);
53 * }
54 * #endif
55 *
56 * void some_other_function(...)
57 * {
58 * struct drm_printer p = drm_info_printer(drm->dev);
59 * log_some_info(&p);
60 * }
61 */
62
63/**
64 * struct drm_printer - drm output "stream"
Rob Clarkd81871772016-11-05 11:08:07 -040065 *
66 * Do not use struct members directly. Use drm_printer_seq_file(),
67 * drm_printer_info(), etc to initialize. And drm_printf() for output.
68 */
69struct drm_printer {
Daniel Vetter3d387d92016-12-28 17:42:09 +010070 /* private: */
Rob Clarkd81871772016-11-05 11:08:07 -040071 void (*printfn)(struct drm_printer *p, struct va_format *vaf);
72 void *arg;
Daniel Vetter3d387d92016-12-28 17:42:09 +010073 const char *prefix;
Rob Clarkd81871772016-11-05 11:08:07 -040074};
75
76void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
77void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
Daniel Vetter3d387d92016-12-28 17:42:09 +010078void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
Rob Clarkd81871772016-11-05 11:08:07 -040079
Joe Perches3c6d6e02017-02-15 15:33:18 -080080__printf(2, 3)
Rob Clarkd81871772016-11-05 11:08:07 -040081void drm_printf(struct drm_printer *p, const char *f, ...);
82
83
84/**
85 * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
Daniel Vetterea0dd852016-12-29 21:48:26 +010086 * @f: the &struct seq_file to output to
Rob Clarkd81871772016-11-05 11:08:07 -040087 *
88 * RETURNS:
89 * The &drm_printer object
90 */
91static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
92{
93 struct drm_printer p = {
94 .printfn = __drm_printfn_seq_file,
95 .arg = f,
96 };
97 return p;
98}
99
100/**
101 * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
Daniel Vetterea0dd852016-12-29 21:48:26 +0100102 * @dev: the &struct device pointer
Rob Clarkd81871772016-11-05 11:08:07 -0400103 *
104 * RETURNS:
105 * The &drm_printer object
106 */
107static inline struct drm_printer drm_info_printer(struct device *dev)
108{
109 struct drm_printer p = {
110 .printfn = __drm_printfn_info,
111 .arg = dev,
112 };
113 return p;
114}
115
Daniel Vetter3d387d92016-12-28 17:42:09 +0100116/**
117 * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
118 * @prefix: debug output prefix
119 *
120 * RETURNS:
121 * The &drm_printer object
122 */
123static inline struct drm_printer drm_debug_printer(const char *prefix)
124{
125 struct drm_printer p = {
126 .printfn = __drm_printfn_debug,
127 .prefix = prefix
128 };
129 return p;
130}
Rob Clarkd81871772016-11-05 11:08:07 -0400131#endif /* DRM_PRINT_H_ */