xref: /openbmc/linux/tools/perf/ui/util.c (revision 8520a98d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2aca7a94dSNamhyung Kim #include "util.h"
32b75863bSLuke Mujica #include "../util/debug.h"
48520a98dSArnaldo Carvalho de Melo #include <stdio.h>
5ba47a142SNamhyung Kim 
6ba47a142SNamhyung Kim /*
7ba47a142SNamhyung Kim  * Default error logging functions
8ba47a142SNamhyung Kim  */
9ba47a142SNamhyung Kim static int perf_stdio__error(const char *format, va_list args)
10aca7a94dSNamhyung Kim {
11ba47a142SNamhyung Kim 	fprintf(stderr, "Error:\n");
12ba47a142SNamhyung Kim 	vfprintf(stderr, format, args);
13ba47a142SNamhyung Kim 	return 0;
14aca7a94dSNamhyung Kim }
15aca7a94dSNamhyung Kim 
16ba47a142SNamhyung Kim static int perf_stdio__warning(const char *format, va_list args)
17aca7a94dSNamhyung Kim {
18ba47a142SNamhyung Kim 	fprintf(stderr, "Warning:\n");
19ba47a142SNamhyung Kim 	vfprintf(stderr, format, args);
20ba47a142SNamhyung Kim 	return 0;
21aca7a94dSNamhyung Kim }
22aca7a94dSNamhyung Kim 
23ba47a142SNamhyung Kim static struct perf_error_ops default_eops =
24aca7a94dSNamhyung Kim {
25ba47a142SNamhyung Kim 	.error		= perf_stdio__error,
26ba47a142SNamhyung Kim 	.warning	= perf_stdio__warning,
27aca7a94dSNamhyung Kim };
28aca7a94dSNamhyung Kim 
29ba47a142SNamhyung Kim static struct perf_error_ops *perf_eops = &default_eops;
30aca7a94dSNamhyung Kim 
31ba47a142SNamhyung Kim 
32ba47a142SNamhyung Kim int ui__error(const char *format, ...)
33aca7a94dSNamhyung Kim {
34ba47a142SNamhyung Kim 	int ret;
35ba47a142SNamhyung Kim 	va_list args;
36aca7a94dSNamhyung Kim 
37ba47a142SNamhyung Kim 	va_start(args, format);
38ba47a142SNamhyung Kim 	ret = perf_eops->error(format, args);
39ba47a142SNamhyung Kim 	va_end(args);
40aca7a94dSNamhyung Kim 
41ba47a142SNamhyung Kim 	return ret;
42aca7a94dSNamhyung Kim }
43aca7a94dSNamhyung Kim 
44aca7a94dSNamhyung Kim int ui__warning(const char *format, ...)
45aca7a94dSNamhyung Kim {
46ba47a142SNamhyung Kim 	int ret;
47aca7a94dSNamhyung Kim 	va_list args;
48aca7a94dSNamhyung Kim 
49aca7a94dSNamhyung Kim 	va_start(args, format);
50ba47a142SNamhyung Kim 	ret = perf_eops->warning(format, args);
51aca7a94dSNamhyung Kim 	va_end(args);
52ba47a142SNamhyung Kim 
53ba47a142SNamhyung Kim 	return ret;
54aca7a94dSNamhyung Kim }
55aca7a94dSNamhyung Kim 
56ba47a142SNamhyung Kim /**
57ba47a142SNamhyung Kim  * perf_error__register - Register error logging functions
58ba47a142SNamhyung Kim  * @eops: The pointer to error logging function struct
59ba47a142SNamhyung Kim  *
60ba47a142SNamhyung Kim  * Register UI-specific error logging functions. Before calling this,
61ba47a142SNamhyung Kim  * other logging functions should be unregistered, if any.
62ba47a142SNamhyung Kim  */
63ba47a142SNamhyung Kim int perf_error__register(struct perf_error_ops *eops)
64ba47a142SNamhyung Kim {
65ba47a142SNamhyung Kim 	if (perf_eops != &default_eops)
66ba47a142SNamhyung Kim 		return -1;
67ba47a142SNamhyung Kim 
68ba47a142SNamhyung Kim 	perf_eops = eops;
69ba47a142SNamhyung Kim 	return 0;
70ba47a142SNamhyung Kim }
71ba47a142SNamhyung Kim 
72ba47a142SNamhyung Kim /**
73ba47a142SNamhyung Kim  * perf_error__unregister - Unregister error logging functions
74ba47a142SNamhyung Kim  * @eops: The pointer to error logging function struct
75ba47a142SNamhyung Kim  *
76ba47a142SNamhyung Kim  * Unregister already registered error logging functions.
77ba47a142SNamhyung Kim  */
78ba47a142SNamhyung Kim int perf_error__unregister(struct perf_error_ops *eops)
79ba47a142SNamhyung Kim {
80ba47a142SNamhyung Kim 	if (perf_eops != eops)
81ba47a142SNamhyung Kim 		return -1;
82ba47a142SNamhyung Kim 
83ba47a142SNamhyung Kim 	perf_eops = &default_eops;
84ba47a142SNamhyung Kim 	return 0;
85aca7a94dSNamhyung Kim }
86