/src/pacemaker/lib/common/output_log.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2025 the Pacemaker project contributors |
3 | | * |
4 | | * The version control history for this file may have further details. |
5 | | * |
6 | | * This source code is licensed under the GNU Lesser General Public License |
7 | | * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. |
8 | | */ |
9 | | |
10 | | #include <crm_internal.h> |
11 | | #include <crm/common/cmdline_internal.h> |
12 | | |
13 | | #include <ctype.h> |
14 | | #include <stdarg.h> |
15 | | #include <stdint.h> |
16 | | #include <stdlib.h> |
17 | | #include <stdio.h> |
18 | | |
19 | | typedef struct private_data_s { |
20 | | /* gathered in log_begin_list */ |
21 | | GQueue/*<char*>*/ *prefixes; |
22 | | uint8_t log_level; |
23 | | const char *function; |
24 | | const char *file; |
25 | | uint32_t line; |
26 | | uint32_t tags; |
27 | | } private_data_t; |
28 | | |
29 | | /*! |
30 | | * \internal |
31 | | * \brief Log a message using output object's log level and filters |
32 | | * |
33 | | * \param[in] priv Output object's private_data_t |
34 | | * \param[in] fmt printf(3)-style format string |
35 | | * \param[in] args... Format string arguments |
36 | | */ |
37 | 0 | #define logger(priv, fmt, args...) do { \ |
38 | 0 | qb_log_from_external_source(pcmk__s((priv)->function, __func__), \ |
39 | 0 | pcmk__s((priv)->file, __FILE__), fmt, (priv)->log_level, \ |
40 | 0 | (((priv)->line == 0)? __LINE__ : (priv)->line), (priv)->tags, \ |
41 | 0 | ##args); \ |
42 | 0 | } while (0); |
43 | | |
44 | | /*! |
45 | | * \internal |
46 | | * \brief Log a message using an explicit log level and output object's filters |
47 | | * |
48 | | * \param[in] priv Output object's private_data_t |
49 | | * \param[in] level Log level |
50 | | * \param[in] fmt printf(3)-style format string |
51 | | * \param[in] ap Variadic arguments |
52 | | */ |
53 | 0 | #define logger_va(priv, level, fmt, ap) do { \ |
54 | 0 | qb_log_from_external_source_va(pcmk__s((priv)->function, __func__), \ |
55 | 0 | pcmk__s((priv)->file, __FILE__), fmt, level, \ |
56 | 0 | (((priv)->line == 0)? __LINE__ : (priv)->line), (priv)->tags, \ |
57 | 0 | ap); \ |
58 | 0 | } while (0); |
59 | | |
60 | | static void |
61 | | log_subprocess_output(pcmk__output_t *out, int exit_status, |
62 | 0 | const char *proc_stdout, const char *proc_stderr) { |
63 | | /* This function intentionally left blank */ |
64 | 0 | } |
65 | | |
66 | | static void |
67 | 0 | log_free_priv(pcmk__output_t *out) { |
68 | 0 | private_data_t *priv = NULL; |
69 | |
|
70 | 0 | if (out == NULL || out->priv == NULL) { |
71 | 0 | return; |
72 | 0 | } |
73 | | |
74 | 0 | priv = out->priv; |
75 | |
|
76 | 0 | g_queue_free(priv->prefixes); |
77 | 0 | free(priv); |
78 | 0 | out->priv = NULL; |
79 | 0 | } |
80 | | |
81 | | static bool |
82 | 0 | log_init(pcmk__output_t *out) { |
83 | 0 | private_data_t *priv = NULL; |
84 | |
|
85 | 0 | pcmk__assert(out != NULL); |
86 | | |
87 | | /* If log_init was previously called on this output struct, just return. */ |
88 | 0 | if (out->priv != NULL) { |
89 | 0 | return true; |
90 | 0 | } |
91 | | |
92 | 0 | out->priv = calloc(1, sizeof(private_data_t)); |
93 | 0 | if (out->priv == NULL) { |
94 | 0 | return false; |
95 | 0 | } |
96 | | |
97 | 0 | priv = out->priv; |
98 | |
|
99 | 0 | priv->prefixes = g_queue_new(); |
100 | 0 | priv->log_level = LOG_INFO; |
101 | |
|
102 | 0 | return true; |
103 | 0 | } |
104 | | |
105 | | static void |
106 | 0 | log_finish(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest) { |
107 | | /* This function intentionally left blank */ |
108 | 0 | } |
109 | | |
110 | | static void |
111 | 0 | log_reset(pcmk__output_t *out) { |
112 | 0 | pcmk__assert(out != NULL); |
113 | |
|
114 | 0 | out->dest = freopen(NULL, "w", out->dest); |
115 | 0 | pcmk__assert(out->dest != NULL); |
116 | |
|
117 | 0 | log_free_priv(out); |
118 | 0 | log_init(out); |
119 | 0 | } |
120 | | |
121 | | static void |
122 | | log_version(pcmk__output_t *out) |
123 | 0 | { |
124 | 0 | private_data_t *priv = NULL; |
125 | |
|
126 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL)); |
127 | 0 | priv = out->priv; |
128 | |
|
129 | 0 | logger(priv, "Pacemaker " PACEMAKER_VERSION); |
130 | 0 | logger(priv, |
131 | 0 | "Written by Andrew Beekhof and the Pacemaker project contributors"); |
132 | 0 | } |
133 | | |
134 | | G_GNUC_PRINTF(2, 3) |
135 | | static void |
136 | | log_err(pcmk__output_t *out, const char *format, ...) |
137 | 0 | { |
138 | 0 | va_list ap; |
139 | 0 | private_data_t *priv = NULL; |
140 | |
|
141 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL)); |
142 | 0 | priv = out->priv; |
143 | | |
144 | | /* Error output does not get indented, to separate it from other |
145 | | * potentially indented list output. |
146 | | */ |
147 | 0 | va_start(ap, format); |
148 | 0 | logger_va(priv, LOG_ERR, format, ap); |
149 | 0 | va_end(ap); |
150 | 0 | } |
151 | | |
152 | | static void |
153 | 0 | log_output_xml(pcmk__output_t *out, const char *name, const char *buf) { |
154 | 0 | xmlNodePtr node = NULL; |
155 | 0 | private_data_t *priv = NULL; |
156 | |
|
157 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL)); |
158 | 0 | priv = out->priv; |
159 | |
|
160 | 0 | node = pcmk__xe_create(NULL, name); |
161 | 0 | pcmk__xe_set_content(node, "%s", buf); |
162 | 0 | do_crm_log_xml(priv->log_level, name, node); |
163 | 0 | free(node); |
164 | 0 | } |
165 | | |
166 | | G_GNUC_PRINTF(4, 5) |
167 | | static void |
168 | | log_begin_list(pcmk__output_t *out, const char *singular_noun, const char *plural_noun, |
169 | 0 | const char *format, ...) { |
170 | 0 | int len = 0; |
171 | 0 | va_list ap; |
172 | 0 | char* buffer = NULL; |
173 | 0 | private_data_t *priv = NULL; |
174 | |
|
175 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL)); |
176 | 0 | priv = out->priv; |
177 | |
|
178 | 0 | va_start(ap, format); |
179 | 0 | len = vasprintf(&buffer, format, ap); |
180 | 0 | pcmk__assert(len >= 0); |
181 | 0 | va_end(ap); |
182 | | |
183 | | /* Don't skip empty prefixes, |
184 | | * otherwise there will be mismatch |
185 | | * in the log_end_list */ |
186 | 0 | if(strcmp(buffer, "") == 0) { |
187 | | /* nothing */ |
188 | 0 | } |
189 | |
|
190 | 0 | g_queue_push_tail(priv->prefixes, buffer); |
191 | 0 | } |
192 | | |
193 | | G_GNUC_PRINTF(3, 4) |
194 | | static void |
195 | | log_list_item(pcmk__output_t *out, const char *name, const char *format, ...) |
196 | 0 | { |
197 | 0 | gsize old_len = 0; |
198 | 0 | va_list ap; |
199 | 0 | private_data_t *priv = NULL; |
200 | 0 | GString *buffer = g_string_sized_new(128); |
201 | |
|
202 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL) && (format != NULL)); |
203 | 0 | priv = out->priv; |
204 | | |
205 | | // Message format: [<prefix1>[: <prefix2>...]: ]][<name>: ]<body> |
206 | |
|
207 | 0 | for (const GList *iter = priv->prefixes->head; iter != NULL; |
208 | 0 | iter = iter->next) { |
209 | |
|
210 | 0 | pcmk__g_strcat(buffer, (const char *) iter->data, ": ", NULL); |
211 | 0 | } |
212 | |
|
213 | 0 | if (!pcmk__str_empty(name)) { |
214 | 0 | pcmk__g_strcat(buffer, name, ": ", NULL); |
215 | 0 | } |
216 | |
|
217 | 0 | old_len = buffer->len; |
218 | 0 | va_start(ap, format); |
219 | 0 | g_string_append_vprintf(buffer, format, ap); |
220 | 0 | va_end(ap); |
221 | |
|
222 | 0 | if (buffer->len > old_len) { |
223 | | // Don't log a message with an empty body |
224 | 0 | logger(priv, "%s", buffer->str); |
225 | 0 | } |
226 | |
|
227 | 0 | g_string_free(buffer, TRUE); |
228 | 0 | } |
229 | | |
230 | | static void |
231 | 0 | log_end_list(pcmk__output_t *out) { |
232 | 0 | private_data_t *priv = NULL; |
233 | |
|
234 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL)); |
235 | 0 | priv = out->priv; |
236 | |
|
237 | 0 | if (priv->prefixes == NULL) { |
238 | 0 | return; |
239 | 0 | } |
240 | 0 | pcmk__assert(priv->prefixes->tail != NULL); |
241 | |
|
242 | 0 | free((char *)priv->prefixes->tail->data); |
243 | 0 | g_queue_pop_tail(priv->prefixes); |
244 | 0 | } |
245 | | |
246 | | G_GNUC_PRINTF(2, 3) |
247 | | static int |
248 | | log_info(pcmk__output_t *out, const char *format, ...) |
249 | 0 | { |
250 | 0 | va_list ap; |
251 | 0 | private_data_t *priv = NULL; |
252 | |
|
253 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL)); |
254 | 0 | priv = out->priv; |
255 | | |
256 | | /* Informational output does not get indented, to separate it from other |
257 | | * potentially indented list output. |
258 | | */ |
259 | 0 | va_start(ap, format); |
260 | 0 | logger_va(priv, priv->log_level, format, ap); |
261 | 0 | va_end(ap); |
262 | |
|
263 | 0 | return pcmk_rc_ok; |
264 | 0 | } |
265 | | |
266 | | G_GNUC_PRINTF(2, 3) |
267 | | static int |
268 | | log_transient(pcmk__output_t *out, const char *format, ...) |
269 | 0 | { |
270 | 0 | va_list ap; |
271 | 0 | private_data_t *priv = NULL; |
272 | |
|
273 | 0 | pcmk__assert((out != NULL) && (out->priv != NULL)); |
274 | 0 | priv = out->priv; |
275 | |
|
276 | 0 | va_start(ap, format); |
277 | 0 | logger_va(priv, QB_MAX(priv->log_level, LOG_DEBUG), format, ap); |
278 | 0 | va_end(ap); |
279 | |
|
280 | 0 | return pcmk_rc_ok; |
281 | 0 | } |
282 | | |
283 | | static bool |
284 | 0 | log_is_quiet(pcmk__output_t *out) { |
285 | 0 | return false; |
286 | 0 | } |
287 | | |
288 | | static void |
289 | 0 | log_spacer(pcmk__output_t *out) { |
290 | | /* This function intentionally left blank */ |
291 | 0 | } |
292 | | |
293 | | static void |
294 | 0 | log_progress(pcmk__output_t *out, bool end) { |
295 | | /* This function intentionally left blank */ |
296 | 0 | } |
297 | | |
298 | | static void |
299 | 0 | log_prompt(const char *prompt, bool echo, char **dest) { |
300 | | /* This function intentionally left blank */ |
301 | 0 | } |
302 | | |
303 | | pcmk__output_t * |
304 | 0 | pcmk__mk_log_output(char **argv) { |
305 | 0 | pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t)); |
306 | |
|
307 | 0 | if (retval == NULL) { |
308 | 0 | return NULL; |
309 | 0 | } |
310 | | |
311 | 0 | retval->fmt_name = "log"; |
312 | 0 | retval->request = pcmk__quote_cmdline(argv); |
313 | |
|
314 | 0 | retval->init = log_init; |
315 | 0 | retval->free_priv = log_free_priv; |
316 | 0 | retval->finish = log_finish; |
317 | 0 | retval->reset = log_reset; |
318 | |
|
319 | 0 | retval->register_message = pcmk__register_message; |
320 | 0 | retval->message = pcmk__call_message; |
321 | |
|
322 | 0 | retval->subprocess_output = log_subprocess_output; |
323 | 0 | retval->version = log_version; |
324 | 0 | retval->info = log_info; |
325 | 0 | retval->transient = log_transient; |
326 | 0 | retval->err = log_err; |
327 | 0 | retval->output_xml = log_output_xml; |
328 | |
|
329 | 0 | retval->begin_list = log_begin_list; |
330 | 0 | retval->list_item = log_list_item; |
331 | 0 | retval->end_list = log_end_list; |
332 | |
|
333 | 0 | retval->is_quiet = log_is_quiet; |
334 | 0 | retval->spacer = log_spacer; |
335 | 0 | retval->progress = log_progress; |
336 | 0 | retval->prompt = log_prompt; |
337 | |
|
338 | 0 | return retval; |
339 | 0 | } |
340 | | |
341 | | /*! |
342 | | * \internal |
343 | | * \brief Get the log level for a log output object |
344 | | * |
345 | | * This returns 0 if the output object is not of log format. |
346 | | * |
347 | | * \param[in] out Output object |
348 | | * |
349 | | * \return Current log level for \p out |
350 | | */ |
351 | | uint8_t |
352 | | pcmk__output_get_log_level(const pcmk__output_t *out) |
353 | 0 | { |
354 | 0 | pcmk__assert(out != NULL); |
355 | |
|
356 | 0 | if (pcmk__str_eq(out->fmt_name, "log", pcmk__str_none)) { |
357 | 0 | private_data_t *priv = out->priv; |
358 | |
|
359 | 0 | pcmk__assert(priv != NULL); |
360 | 0 | return priv->log_level; |
361 | 0 | } |
362 | 0 | return 0; |
363 | 0 | } |
364 | | |
365 | | /*! |
366 | | * \internal |
367 | | * \brief Set the log level for a log output object |
368 | | * |
369 | | * This does nothing if the output object is not of log format. |
370 | | * |
371 | | * \param[in,out] out Output object |
372 | | * \param[in] log_level Log level constant (\c LOG_ERR, etc.) to use |
373 | | * |
374 | | * \note \c LOG_INFO is used by default for new \c pcmk__output_t objects. |
375 | | * \note Almost all formatted output messages respect this setting. However, |
376 | | * <tt>out->err</tt> always logs at \c LOG_ERR. |
377 | | */ |
378 | | void |
379 | | pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level) |
380 | 0 | { |
381 | 0 | pcmk__assert(out != NULL); |
382 | |
|
383 | 0 | if (pcmk__str_eq(out->fmt_name, "log", pcmk__str_none)) { |
384 | 0 | private_data_t *priv = out->priv; |
385 | |
|
386 | 0 | pcmk__assert(priv != NULL); |
387 | 0 | priv->log_level = log_level; |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | | /*! |
392 | | * \internal |
393 | | * \brief Set the file, function, line, and tags used to filter log output |
394 | | * |
395 | | * This does nothing if the output object is not of log format. |
396 | | * |
397 | | * \param[in,out] out Output object |
398 | | * \param[in] file File name to filter with (or NULL for default) |
399 | | * \param[in] function Function name to filter with (or NULL for default) |
400 | | * \param[in] line Line number to filter with (or 0 for default) |
401 | | * \param[in] tags Tags to filter with (or 0 for none) |
402 | | * |
403 | | * \note Custom filters should generally be used only in short areas of a single |
404 | | * function. When done, callers should call this function again with |
405 | | * NULL/0 arguments to reset the filters. |
406 | | */ |
407 | | void |
408 | | pcmk__output_set_log_filter(pcmk__output_t *out, const char *file, |
409 | | const char *function, uint32_t line, uint32_t tags) |
410 | 0 | { |
411 | 0 | pcmk__assert(out != NULL); |
412 | |
|
413 | 0 | if (pcmk__str_eq(out->fmt_name, "log", pcmk__str_none)) { |
414 | 0 | private_data_t *priv = out->priv; |
415 | |
|
416 | 0 | pcmk__assert(priv != NULL); |
417 | 0 | priv->file = file; |
418 | 0 | priv->function = function; |
419 | 0 | priv->line = line; |
420 | 0 | priv->tags = tags; |
421 | 0 | } |
422 | 0 | } |