pacemaker  3.0.0-d8340737c4
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__CRM_COMMON_OUTPUT_INTERNAL__H
11 #define PCMK__CRM_COMMON_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 
148 pcmk__output_t *pcmk__mk_log_output(char **argv);
151 pcmk__output_t *pcmk__mk_xml_output(char **argv);
152 
153 #define PCMK__SUPPORTED_FORMAT_HTML { "html", pcmk__mk_html_output, pcmk__html_output_entries }
154 #define PCMK__SUPPORTED_FORMAT_LOG { "log", pcmk__mk_log_output, NULL }
155 #define PCMK__SUPPORTED_FORMAT_NONE { PCMK_VALUE_NONE, pcmk__mk_none_output, NULL }
156 #define PCMK__SUPPORTED_FORMAT_TEXT { "text", pcmk__mk_text_output, NULL }
157 #define PCMK__SUPPORTED_FORMAT_XML { "xml", pcmk__mk_xml_output, NULL }
158 
171  const char *fmt_name;
172 
181  bool quiet;
182 
189  gchar *request;
190 
197  FILE *dest;
198 
205  GHashTable *messages;
206 
214  void *priv;
215 
230  bool (*init) (pcmk__output_t *out);
231 
241  void (*free_priv) (pcmk__output_t *out);
242 
277  void (*finish) (pcmk__output_t *out, crm_exit_t exit_status, bool print,
278  void **copy_dest);
279 
295  void (*reset) (pcmk__output_t *out);
296 
308  void (*register_message) (pcmk__output_t *out, const char *message_id,
309  pcmk__message_fn_t fn);
310 
326  int (*message) (pcmk__output_t *out, const char *message_id, ...);
327 
337  void (*subprocess_output) (pcmk__output_t *out, int exit_status,
338  const char *proc_stdout, const char *proc_stderr);
339 
348  void (*version) (pcmk__output_t *out, bool extended);
349 
370  int (*info) (pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
371 
389  int (*transient) (pcmk__output_t *out, const char *format, ...)
390  G_GNUC_PRINTF(2, 3);
391 
407  void (*err) (pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
408 
417  void (*output_xml) (pcmk__output_t *out, const char *name, const char *buf);
418 
438  void (*begin_list) (pcmk__output_t *out, const char *singular_noun,
439  const char *plural_noun, const char *format, ...)
440  G_GNUC_PRINTF(4, 5);
441 
451  void (*list_item) (pcmk__output_t *out, const char *name, const char *format, ...)
452  G_GNUC_PRINTF(3, 4);
453 
467 
478  void (*end_list) (pcmk__output_t *out);
479 
491  bool (*is_quiet) (pcmk__output_t *out);
492 
499  void (*spacer) (pcmk__output_t *out);
500 
511  void (*progress) (pcmk__output_t *out, bool end);
512 
529  void (*prompt) (const char *prompt, bool echo, char **dest);
530 };
531 
544 int
545 pcmk__call_message(pcmk__output_t *out, const char *message_id, ...);
546 
560 
577 int pcmk__output_new(pcmk__output_t **out, const char *fmt_name,
578  const char *filename, char **argv);
579 
596 int
597 pcmk__register_format(GOptionGroup *group, const char *name,
598  pcmk__output_factory_t create,
599  const GOptionEntry *options);
600 
612 void
613 pcmk__register_formats(GOptionGroup *group,
614  const pcmk__supported_format_t *table);
615 
621 void
623 
635 void
636 pcmk__register_message(pcmk__output_t *out, const char *message_id,
637  pcmk__message_fn_t fn);
638 
650 void
652  const pcmk__message_entry_t *table);
653 
654 /* Functions that are useful for implementing custom message formatters */
655 
656 void pcmk__output_text_set_fancy(pcmk__output_t *out, bool enabled);
657 
673 void
674 pcmk__indented_printf(pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
675 
690 void
691 pcmk__indented_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2, 0);
692 
693 
706 void
707 pcmk__formatted_printf(pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
708 
722 void
723 pcmk__formatted_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2, 0);
724 
734 void
735 pcmk__text_prompt(const char *prompt, bool echo, char **dest);
736 
737 uint8_t
739 
740 void
741 pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level);
742 
743 void pcmk__output_set_log_filter(pcmk__output_t *out, const char *file,
744  const char *function, uint32_t line,
745  uint32_t tags);
746 
747 
759 xmlNodePtr
760 pcmk__output_xml_create_parent(pcmk__output_t *out, const char *name, ...)
761 G_GNUC_NULL_TERMINATED;
762 
771 void
772 pcmk__output_xml_add_node_copy(pcmk__output_t *out, xmlNodePtr node);
773 
783 xmlNodePtr
784 pcmk__output_create_xml_node(pcmk__output_t *out, const char *name, ...)
785 G_GNUC_NULL_TERMINATED;
786 
796 xmlNodePtr
797 pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content);
798 
813 void
815 
830 void
832 
847 xmlNodePtr
849 
863 xmlNodePtr
864 pcmk__output_create_html_node(pcmk__output_t *out, const char *element_name, const char *id,
865  const char *class_name, const char *text);
866 
867 xmlNode *pcmk__html_create(xmlNode *parent, const char *name, const char *id,
868  const char *class_name);
869 
889 void
890 pcmk__html_add_header(const char *name, ...)
891 G_GNUC_NULL_TERMINATED;
892 
902 void pcmk__output_and_clear_error(GError **error, pcmk__output_t *out);
903 
904 int pcmk__xml_output_new(pcmk__output_t **out, xmlNodePtr *xml);
905 void pcmk__xml_output_finish(pcmk__output_t *out, crm_exit_t exit_status, xmlNodePtr *xml);
907 int pcmk__text_output_new(pcmk__output_t **out, const char *filename);
908 
921 // @COMPAT This can be removed when `crm_mon -X` and daemon metadata are removed
923 
933 // @COMPAT This can be removed when `crm_mon -X` and daemon metadata are removed
935 
948 
972 static inline int
973 pcmk__output_select_rc(int old_rc, int new_rc)
974 {
975  switch (new_rc) {
976  case pcmk_rc_no_output:
977  return old_rc;
978  case pcmk_rc_ok:
979  switch (old_rc) {
980  case pcmk_rc_no_output:
981  return new_rc;
982  default:
983  return old_rc;
984  }
985  default:
986  return new_rc;
987  }
988 }
989 
990 #if defined(PCMK__UNIT_TESTING)
991 /* If we are building libcrmcommon_test.a, add this accessor function so we can
992  * inspect the internal formatters hash table.
993  */
994 GHashTable *pcmk__output_formatters(void);
995 #endif
996 
997 #define PCMK__OUTPUT_SPACER_IF(out_obj, cond) \
998  if (cond) { \
999  out->spacer(out); \
1000  }
1001 
1002 #define PCMK__OUTPUT_LIST_HEADER(out_obj, cond, retcode, title...) \
1003  if (retcode == pcmk_rc_no_output) { \
1004  PCMK__OUTPUT_SPACER_IF(out_obj, cond); \
1005  retcode = pcmk_rc_ok; \
1006  out_obj->begin_list(out_obj, NULL, NULL, title); \
1007  }
1008 
1009 #define PCMK__OUTPUT_LIST_FOOTER(out_obj, retcode) \
1010  if (retcode == pcmk_rc_ok) { \
1011  out_obj->end_list(out_obj); \
1012  }
1013 
1014 #ifdef __cplusplus
1015 }
1016 #endif
1017 
1018 #endif // PCMK__CRM_COMMON_OUTPUT_INTERNAL__H
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:311
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:458
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
xmlNode * pcmk__html_create(xmlNode *parent, const char *name, const char *id, const char *class_name)
Definition: output_html.c:483
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
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
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:383
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