pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
output_internal.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2024 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 #ifndef PCMK__OUTPUT_INTERNAL__H
11 #define PCMK__OUTPUT_INTERNAL__H
12 
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <libxml/tree.h>
17 #include <libxml/HTMLtree.h>
18 
19 #include <glib.h>
20 #include <crm/common/results.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
31 #if defined(PCMK__WITH_ATTRIBUTE_OUTPUT_ARGS)
32 #define PCMK__OUTPUT_ARGS(ARGS...) __attribute__((output_args(ARGS)))
33 #else
34 #define PCMK__OUTPUT_ARGS(ARGS...)
35 #endif
36 
38 
49 typedef pcmk__output_t * (*pcmk__output_factory_t)(char **argv);
50 
68 typedef int (*pcmk__message_fn_t)(pcmk__output_t *out, va_list args);
69 
91 typedef struct pcmk__message_entry_s {
98  const char *message_id;
99 
108  const char *fmt_name;
109 
116 
122 typedef struct pcmk__supported_format_s {
127  const char *name;
128 
133 
138  GOptionEntry *options;
140 
141 /* The following three blocks need to be updated each time a new base formatter
142  * is added.
143  */
144 
145 extern GOptionEntry pcmk__html_output_entries[];
146 extern GOptionEntry pcmk__text_output_entries[];
147 
149 pcmk__output_t *pcmk__mk_log_output(char **argv);
152 pcmk__output_t *pcmk__mk_xml_output(char **argv);
153 
154 #define PCMK__SUPPORTED_FORMAT_HTML { "html", pcmk__mk_html_output, pcmk__html_output_entries }
155 #define PCMK__SUPPORTED_FORMAT_LOG { "log", pcmk__mk_log_output, NULL }
156 #define PCMK__SUPPORTED_FORMAT_NONE { PCMK_VALUE_NONE, pcmk__mk_none_output, NULL }
157 #define PCMK__SUPPORTED_FORMAT_TEXT { "text", pcmk__mk_text_output, pcmk__text_output_entries }
158 #define PCMK__SUPPORTED_FORMAT_XML { "xml", pcmk__mk_xml_output, NULL }
159 
172  const char *fmt_name;
173 
182  bool quiet;
183 
190  gchar *request;
191 
198  FILE *dest;
199 
206  GHashTable *messages;
207 
215  void *priv;
216 
231  bool (*init) (pcmk__output_t *out);
232 
242  void (*free_priv) (pcmk__output_t *out);
243 
278  void (*finish) (pcmk__output_t *out, crm_exit_t exit_status, bool print,
279  void **copy_dest);
280 
296  void (*reset) (pcmk__output_t *out);
297 
309  void (*register_message) (pcmk__output_t *out, const char *message_id,
310  pcmk__message_fn_t fn);
311 
327  int (*message) (pcmk__output_t *out, const char *message_id, ...);
328 
338  void (*subprocess_output) (pcmk__output_t *out, int exit_status,
339  const char *proc_stdout, const char *proc_stderr);
340 
349  void (*version) (pcmk__output_t *out, bool extended);
350 
371  int (*info) (pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
372 
390  int (*transient) (pcmk__output_t *out, const char *format, ...)
391  G_GNUC_PRINTF(2, 3);
392 
408  void (*err) (pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
409 
418  void (*output_xml) (pcmk__output_t *out, const char *name, const char *buf);
419 
439  void (*begin_list) (pcmk__output_t *out, const char *singular_noun,
440  const char *plural_noun, const char *format, ...)
441  G_GNUC_PRINTF(4, 5);
442 
452  void (*list_item) (pcmk__output_t *out, const char *name, const char *format, ...)
453  G_GNUC_PRINTF(3, 4);
454 
468 
479  void (*end_list) (pcmk__output_t *out);
480 
492  bool (*is_quiet) (pcmk__output_t *out);
493 
500  void (*spacer) (pcmk__output_t *out);
501 
512  void (*progress) (pcmk__output_t *out, bool end);
513 
530  void (*prompt) (const char *prompt, bool echo, char **dest);
531 };
532 
545 int
546 pcmk__call_message(pcmk__output_t *out, const char *message_id, ...);
547 
561 
578 int pcmk__output_new(pcmk__output_t **out, const char *fmt_name,
579  const char *filename, char **argv);
580 
597 int
598 pcmk__register_format(GOptionGroup *group, const char *name,
599  pcmk__output_factory_t create,
600  const GOptionEntry *options);
601 
613 void
614 pcmk__register_formats(GOptionGroup *group,
615  const pcmk__supported_format_t *table);
616 
622 void
624 
636 void
637 pcmk__register_message(pcmk__output_t *out, const char *message_id,
638  pcmk__message_fn_t fn);
639 
651 void
653  const pcmk__message_entry_t *table);
654 
655 /* Functions that are useful for implementing custom message formatters */
656 
657 void pcmk__output_text_set_fancy(pcmk__output_t *out, bool enabled);
658 
674 void
675 pcmk__indented_printf(pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
676 
691 void
692 pcmk__indented_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2, 0);
693 
694 
707 void
708 pcmk__formatted_printf(pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
709 
723 void
724 pcmk__formatted_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2, 0);
725 
735 void
736 pcmk__text_prompt(const char *prompt, bool echo, char **dest);
737 
738 uint8_t
740 
741 void
742 pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level);
743 
744 void pcmk__output_set_log_filter(pcmk__output_t *out, const char *file,
745  const char *function, uint32_t line,
746  uint32_t tags);
747 
748 
760 xmlNodePtr
761 pcmk__output_xml_create_parent(pcmk__output_t *out, const char *name, ...)
762 G_GNUC_NULL_TERMINATED;
763 
772 void
773 pcmk__output_xml_add_node_copy(pcmk__output_t *out, xmlNodePtr node);
774 
784 xmlNodePtr
785 pcmk__output_create_xml_node(pcmk__output_t *out, const char *name, ...)
786 G_GNUC_NULL_TERMINATED;
787 
797 xmlNodePtr
798 pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content);
799 
814 void
816 
831 void
833 
848 xmlNodePtr
850 
864 xmlNodePtr
865 pcmk__output_create_html_node(pcmk__output_t *out, const char *element_name, const char *id,
866  const char *class_name, const char *text);
867 
868 xmlNode *pcmk__html_create(xmlNode *parent, const char *name, const char *id,
869  const char *class);
870 
890 void
891 pcmk__html_add_header(const char *name, ...)
892 G_GNUC_NULL_TERMINATED;
893 
903 void pcmk__output_and_clear_error(GError **error, pcmk__output_t *out);
904 
905 int pcmk__xml_output_new(pcmk__output_t **out, xmlNodePtr *xml);
906 void pcmk__xml_output_finish(pcmk__output_t *out, crm_exit_t exit_status, xmlNodePtr *xml);
908 int pcmk__text_output_new(pcmk__output_t **out, const char *filename);
909 
922 // @COMPAT This can be removed when `crm_mon -X` and daemon metadata are removed
924 
934 // @COMPAT This can be removed when `crm_mon -X` and daemon metadata are removed
936 
949 
973 static inline int
974 pcmk__output_select_rc(int old_rc, int new_rc)
975 {
976  switch (new_rc) {
977  case pcmk_rc_no_output:
978  return old_rc;
979  case pcmk_rc_ok:
980  switch (old_rc) {
981  case pcmk_rc_no_output:
982  return new_rc;
983  default:
984  return old_rc;
985  }
986  default:
987  return new_rc;
988  }
989 }
990 
991 #if defined(PCMK__UNIT_TESTING)
992 /* If we are building libcrmcommon_test.a, add this accessor function so we can
993  * inspect the internal formatters hash table.
994  */
995 GHashTable *pcmk__output_formatters(void);
996 #endif
997 
998 #define PCMK__OUTPUT_SPACER_IF(out_obj, cond) \
999  if (cond) { \
1000  out->spacer(out); \
1001  }
1002 
1003 #define PCMK__OUTPUT_LIST_HEADER(out_obj, cond, retcode, title...) \
1004  if (retcode == pcmk_rc_no_output) { \
1005  PCMK__OUTPUT_SPACER_IF(out_obj, cond); \
1006  retcode = pcmk_rc_ok; \
1007  out_obj->begin_list(out_obj, NULL, NULL, title); \
1008  }
1009 
1010 #define PCMK__OUTPUT_LIST_FOOTER(out_obj, retcode) \
1011  if (retcode == pcmk_rc_ok) { \
1012  out_obj->end_list(out_obj); \
1013  }
1014 
1015 #ifdef __cplusplus
1016 }
1017 #endif
1018 
1019 #endif
int(* pcmk__message_fn_t)(pcmk__output_t *out, va_list args)
void(* end_list)(pcmk__output_t *out)
void void void void pcmk__formatted_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2
void pcmk__output_xml_push_parent(pcmk__output_t *out, xmlNodePtr parent)
Definition: output_xml.c:548
pcmk__output_t * pcmk__mk_text_output(char **argv)
Definition: output_text.c:323
void void pcmk__indented_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2
pcmk__message_fn_t fn
The function to be called for message_id given a match on fmt_name. See comments on pcmk__message_fn_...
void pcmk__register_messages(pcmk__output_t *out, const pcmk__message_entry_t *table)
Definition: output.c:204
void void void void void pcmk__text_prompt(const char *prompt, bool echo, char **dest)
Definition: output_text.c:470
const char * name
Definition: cib.c:26
int(* message)(pcmk__output_t *out, const char *message_id,...)
void pcmk__html_add_header(const char *name,...) G_GNUC_NULL_TERMINATED
Definition: output_html.c:496
const char * fmt_name
The name of this output formatter.
bool(* is_quiet)(pcmk__output_t *out)
bool pcmk__output_get_legacy_xml(pcmk__output_t *out)
Definition: output_xml.c:593
void(* spacer)(pcmk__output_t *out)
xmlNodePtr pcmk__output_xml_create_parent(pcmk__output_t *out, const char *name,...) G_GNUC_NULL_TERMINATED
Definition: output_xml.c:478
void pcmk__register_message(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
Definition: output.c:196
struct pcmk__message_entry_s pcmk__message_entry_t
pcmk__output_t * pcmk__mk_html_output(char **argv)
Definition: output_html.c:410
xmlNodePtr pcmk__output_xml_peek_parent(pcmk__output_t *out)
Definition: output_xml.c:578
enum crm_exit_e crm_exit_t
void pcmk__xml_output_finish(pcmk__output_t *out, crm_exit_t exit_status, xmlNodePtr *xml)
Definition: output.c:271
int(* info)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
void pcmk__output_set_log_filter(pcmk__output_t *out, const char *file, const char *function, uint32_t line, uint32_t tags)
Definition: output_log.c:419
xmlNodePtr pcmk__output_create_html_node(pcmk__output_t *out, const char *element_name, const char *id, const char *class_name, const char *text)
Definition: output_html.c:449
void pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level)
Definition: output_log.c:390
GOptionEntry pcmk__html_output_entries[]
Definition: output_html.c:44
pcmk__output_t * pcmk__mk_log_output(char **argv)
Definition: output_log.c:315
int pcmk__call_message(pcmk__output_t *out, const char *message_id,...)
Definition: output.c:174
void void void pcmk__formatted_printf(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
GHashTable * messages
Custom messages that are currently registered on this formatter.
void(* prompt)(const char *prompt, bool echo, char **dest)
void * priv
Implementation-specific private data.
void pcmk__register_formats(GOptionGroup *group, const pcmk__supported_format_t *table)
Definition: output.c:153
void(* register_message)(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
GOptionEntry * options
Format-specific command line options. This can be NULL if no command line options should be supported...
pcmk__output_t * pcmk__mk_none_output(char **argv)
Definition: output_none.c:112
const char * fmt_name
The format type this handler is for.
int pcmk__xml_output_new(pcmk__output_t **out, xmlNodePtr *xml)
Definition: output.c:244
bool quiet
Should this formatter supress most output?
void(* free_priv)(pcmk__output_t *out)
bool(* init)(pcmk__output_t *out)
int(*) int(*) void(*) void(* output_xml)(pcmk__output_t *out, const char *name, const char *buf)
int(*) int(*) void(* err)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
void(* finish)(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest)
void pcmk__indented_printf(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
xmlNodePtr pcmk__output_create_xml_node(pcmk__output_t *out, const char *name,...) G_GNUC_NULL_TERMINATED
Definition: output_xml.c:515
pcmk__output_factory_t create
A function that creates a pcmk__output_t.
int pcmk__register_format(GOptionGroup *group, const char *name, pcmk__output_factory_t create, const GOptionEntry *options)
Definition: output.c:127
void pcmk__output_xml_pop_parent(pcmk__output_t *out)
Definition: output_xml.c:562
xmlNode * pcmk__html_create(xmlNode *parent, const char *name, const char *id, const char *class)
Definition: output_html.c:483
Function and executable result codes.
pcmk__output_t * pcmk__mk_xml_output(char **argv)
Definition: output_xml.c:439
FILE * dest
Where output should be written.
void pcmk__output_free(pcmk__output_t *out)
Definition: output.c:30
void pcmk__unregister_formats(void)
Definition: output.c:166
GOptionEntry pcmk__text_output_entries[]
Definition: output_text.c:24
void pcmk__output_enable_list_element(pcmk__output_t *out)
Definition: output_xml.c:627
pcmk__output_t *(* pcmk__output_factory_t)(char **argv)
const char * name
The name of this output formatter, which should match the fmt_name parameter in some pcmk__output_t s...
void(*) void(* list_item)(pcmk__output_t *out, const char *name, const char *format,...) G_GNUC_PRINTF(3
gchar * request
A copy of the request that generated this output.
void pcmk__output_and_clear_error(GError **error, pcmk__output_t *out)
Definition: output.c:215
This structure contains everything that makes up a single output formatter.
void(* version)(pcmk__output_t *out, bool extended)
void(* begin_list)(pcmk__output_t *out, const char *singular_noun, const char *plural_noun, const char *format,...) G_GNUC_PRINTF(4
void pcmk__output_text_set_fancy(pcmk__output_t *out, bool enabled)
Definition: output_text.c:395
void(* reset)(pcmk__output_t *out)
int pcmk__output_new(pcmk__output_t **out, const char *fmt_name, const char *filename, char **argv)
Definition: output.c:113
void(* progress)(pcmk__output_t *out, bool end)
void pcmk__output_set_legacy_xml(pcmk__output_t *out)
Definition: output_xml.c:610
const char * parent
Definition: cib.c:27
uint8_t pcmk__output_get_log_level(const pcmk__output_t *out)
Definition: output_log.c:363
int pcmk__text_output_new(pcmk__output_t **out, const char *filename)
Definition: output.c:320
xmlNodePtr pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content)
Definition: output_xml.c:536
const char * message_id
The message to be handled.
void(*) void(*) void(* increment_list)(pcmk__output_t *out)
void(* subprocess_output)(pcmk__output_t *out, int exit_status, const char *proc_stdout, const char *proc_stderr)
int pcmk__log_output_new(pcmk__output_t **out)
Definition: output.c:291
struct pcmk__supported_format_s pcmk__supported_format_t
void pcmk__output_xml_add_node_copy(pcmk__output_t *out, xmlNodePtr node)
Definition: output_xml.c:496