1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * Copyright (C) 2010 Red Hat Inc, Steven Rostedt <[email protected]>
4 *
5 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include "event-utils.h"
13 #include "event-parse.h"
14 #include "kbuffer.h"
15
16 #define __weak __attribute__((weak))
17
18 static int log_level = TEP_LOG_CRITICAL;
19
20 /**
21 * tep_set_loglevel - set log level of the library
22 * @level: desired level of the library messages
23 */
tep_set_loglevel(enum tep_loglevel level)24 void tep_set_loglevel(enum tep_loglevel level)
25 {
26 log_level = level;
27 }
28
29 /**
30 * tep_vprint - print library log messages
31 * @name: name of the library.
32 * @level: severity of the log message. This parameter is not used in this implementation, but as
33 * the function is weak and can be overridden, having the log level could be useful
34 * for other implementations.
35 * @print_err: whether to print the errno, if non zero.
36 * @fmt: printf format string of the message.
37 * @ap: list of printf parameters.
38 *
39 * This function is used to print all messages from traceevent, tracefs and trace-cmd libraries.
40 * It is defined as weak, so the application that uses those libraries can override it in order
41 * to implement its own logic for printing library logs.
42 *
43 * Return the value of errno at the function enter.
44 */
tep_vprint(const char * name,enum tep_loglevel level,bool print_err,const char * fmt,va_list ap)45 int __weak tep_vprint(const char *name, enum tep_loglevel level,
46 bool print_err, const char *fmt, va_list ap)
47 {
48 return __tep_vprint(name, level, print_err, fmt, ap);
49 }
50
51 /**
52 * __tep_vprint - print library log messages
53 * @name: name of the library.
54 * @level: severity of the log message. This parameter is not used in this implementation, but as
55 * the function is weak and can be overridden, having the log level could be useful
56 * for other implementations.
57 * @print_err: whether to print the errno, if non zero.
58 * @fmt: printf format string of the message.
59 * @ap: list of printf parameters.
60 *
61 * This function is used to print all messages from traceevent, tracefs and trace-cmd libraries.
62 * It is defined as weak, so the application that uses those libraries can override it in order
63 * to implement its own logic for printing library logs.
64 *
65 * Return the value of errno at the function enter.
66 */
__tep_vprint(const char * name,enum tep_loglevel level,bool print_err,const char * fmt,va_list ap)67 int __tep_vprint(const char *name, enum tep_loglevel level,
68 bool print_err, const char *fmt, va_list ap)
69 {
70 int ret = errno;
71 FILE *fp = stdout;
72
73 if (level <= TEP_LOG_WARNING) {
74 fp = stderr;
75 if (errno && print_err) {
76 perror(name);
77 fprintf(stderr, " ");
78 }
79 }
80 vfprintf(fp, fmt, ap);
81 fprintf(fp, "\n");
82
83 return ret;
84 }
85
tep_warning(const char * fmt,...)86 void tep_warning(const char *fmt, ...)
87 {
88 va_list ap;
89
90 if (log_level < TEP_LOG_WARNING)
91 return;
92
93 va_start(ap, fmt);
94 tep_vprint("libtraceevent", TEP_LOG_WARNING, true, fmt, ap);
95 va_end(ap);
96 }
97
98
tep_info(const char * fmt,...)99 void tep_info(const char *fmt, ...)
100 {
101 va_list ap;
102
103 if (log_level < TEP_LOG_INFO)
104 return;
105
106 va_start(ap, fmt);
107 tep_vprint("libtraceevent", TEP_LOG_INFO, false, fmt, ap);
108 va_end(ap);
109 }
110
111 /* The below is for backward compatibility */
tep_vwarning(const char * name,const char * fmt,va_list ap)112 int __weak tep_vwarning(const char *name, const char *fmt, va_list ap)
113 {
114 return tep_vprint(name, TEP_LOG_WARNING, true, fmt, ap);
115 }
116
117 void pr_stat(const char *fmt, ...) __attribute__((weak, alias("tep_info")));
118 void __pr_stat(const char *fmt, ...) __attribute__((weak, alias("tep_info")));
119
__vpr_stat(const char * fmt,va_list ap)120 void __weak __vpr_stat(const char *fmt, va_list ap)
121 {
122 tep_vprint("libtraceevent", TEP_LOG_INFO, false, fmt, ap);
123 }
124
125 void vpr_stat(const char *fmt, va_list ap) __attribute__((weak, alias("__vpr_stat")));
126
127 /**
128 * tep_kbuffer - return an allocated kbuffer that can be used for the tep handle
129 * @tep: the handle that will work with the kbuffer descriptor
130 *
131 * Allocates and returns a new kbuffer.
132 * The return must be freed by kbuffer_free();
133 */
tep_kbuffer(struct tep_handle * tep)134 struct kbuffer *tep_kbuffer(struct tep_handle *tep)
135 {
136 enum kbuffer_endian endian;
137 int long_size;
138
139 long_size = tep_get_long_size(tep);
140
141 /* If the long_size is not set, then use the commit size */
142 if (!long_size)
143 long_size = tep_get_header_page_size(tep);
144
145 if (long_size == 8)
146 long_size = KBUFFER_LSIZE_8;
147 else
148 long_size = KBUFFER_LSIZE_4;
149
150 if (tep_is_file_bigendian(tep))
151 endian = KBUFFER_ENDIAN_BIG;
152 else
153 endian = KBUFFER_ENDIAN_LITTLE;
154
155 return kbuffer_alloc(long_size, endian);
156 }
157