pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
output_internal.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2023 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__log_output_entries[];
147 extern GOptionEntry pcmk__none_output_entries[];
148 extern GOptionEntry pcmk__text_output_entries[];
149 extern GOptionEntry pcmk__xml_output_entries[];
150 
152 pcmk__output_t *pcmk__mk_log_output(char **argv);
155 pcmk__output_t *pcmk__mk_xml_output(char **argv);
156 
157 #define PCMK__SUPPORTED_FORMAT_HTML { "html", pcmk__mk_html_output, pcmk__html_output_entries }
158 #define PCMK__SUPPORTED_FORMAT_LOG { "log", pcmk__mk_log_output, pcmk__log_output_entries }
159 #define PCMK__SUPPORTED_FORMAT_NONE { PCMK__VALUE_NONE, pcmk__mk_none_output, \
160  pcmk__none_output_entries }
161 #define PCMK__SUPPORTED_FORMAT_TEXT { "text", pcmk__mk_text_output, pcmk__text_output_entries }
162 #define PCMK__SUPPORTED_FORMAT_XML { "xml", pcmk__mk_xml_output, pcmk__xml_output_entries }
163 
176  const char *fmt_name;
177 
186  bool quiet;
187 
194  gchar *request;
195 
202  FILE *dest;
203 
210  GHashTable *messages;
211 
219  void *priv;
220 
235  bool (*init) (pcmk__output_t *out);
236 
246  void (*free_priv) (pcmk__output_t *out);
247 
282  void (*finish) (pcmk__output_t *out, crm_exit_t exit_status, bool print,
283  void **copy_dest);
284 
300  void (*reset) (pcmk__output_t *out);
301 
313  void (*register_message) (pcmk__output_t *out, const char *message_id,
314  pcmk__message_fn_t fn);
315 
331  int (*message) (pcmk__output_t *out, const char *message_id, ...);
332 
342  void (*subprocess_output) (pcmk__output_t *out, int exit_status,
343  const char *proc_stdout, const char *proc_stderr);
344 
353  void (*version) (pcmk__output_t *out, bool extended);
354 
375  int (*info) (pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
376 
394  int (*transient) (pcmk__output_t *out, const char *format, ...)
395  G_GNUC_PRINTF(2, 3);
396 
412  void (*err) (pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
413 
422  void (*output_xml) (pcmk__output_t *out, const char *name, const char *buf);
423 
443  void (*begin_list) (pcmk__output_t *out, const char *singular_noun,
444  const char *plural_noun, const char *format, ...)
445  G_GNUC_PRINTF(4, 5);
446 
456  void (*list_item) (pcmk__output_t *out, const char *name, const char *format, ...)
457  G_GNUC_PRINTF(3, 4);
458 
472 
483  void (*end_list) (pcmk__output_t *out);
484 
496  bool (*is_quiet) (pcmk__output_t *out);
497 
504  void (*spacer) (pcmk__output_t *out);
505 
516  void (*progress) (pcmk__output_t *out, bool end);
517 
534  void (*prompt) (const char *prompt, bool echo, char **dest);
535 };
536 
549 int
550 pcmk__call_message(pcmk__output_t *out, const char *message_id, ...);
551 
565 
582 int pcmk__output_new(pcmk__output_t **out, const char *fmt_name,
583  const char *filename, char **argv);
584 
601 int
602 pcmk__register_format(GOptionGroup *group, const char *name,
603  pcmk__output_factory_t create,
604  const GOptionEntry *options);
605 
617 void
618 pcmk__register_formats(GOptionGroup *group,
619  const pcmk__supported_format_t *table);
620 
626 void
628 
640 void
641 pcmk__register_message(pcmk__output_t *out, const char *message_id,
642  pcmk__message_fn_t fn);
643 
655 void
657  const pcmk__message_entry_t *table);
658 
659 /* Functions that are useful for implementing custom message formatters */
660 
676 void
677 pcmk__indented_printf(pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
678 
693 void
694 pcmk__indented_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2, 0);
695 
696 
709 void
710 pcmk__formatted_printf(pcmk__output_t *out, const char *format, ...) G_GNUC_PRINTF(2, 3);
711 
725 void
726 pcmk__formatted_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2, 0);
727 
737 void
738 pcmk__text_prompt(const char *prompt, bool echo, char **dest);
739 
748 uint8_t
750 
763 void
764 pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level);
765 
766 void pcmk__output_set_log_filter(pcmk__output_t *out, const char *file,
767  const char *function, uint32_t line,
768  uint32_t tags);
769 
770 
782 xmlNodePtr
783 pcmk__output_xml_create_parent(pcmk__output_t *out, const char *name, ...)
784 G_GNUC_NULL_TERMINATED;
785 
794 void
795 pcmk__output_xml_add_node_copy(pcmk__output_t *out, xmlNodePtr node);
796 
806 xmlNodePtr
807 pcmk__output_create_xml_node(pcmk__output_t *out, const char *name, ...)
808 G_GNUC_NULL_TERMINATED;
809 
819 xmlNodePtr
820 pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content);
821 
836 void
838 
853 void
855 
870 xmlNodePtr
872 
886 xmlNodePtr
887 pcmk__output_create_html_node(pcmk__output_t *out, const char *element_name, const char *id,
888  const char *class_name, const char *text);
889 
905 void
906 pcmk__html_add_header(const char *name, ...)
907 G_GNUC_NULL_TERMINATED;
908 
918 void pcmk__output_and_clear_error(GError **error, pcmk__output_t *out);
919 
920 int pcmk__xml_output_new(pcmk__output_t **out, xmlNodePtr *xml);
921 void pcmk__xml_output_finish(pcmk__output_t *out, xmlNodePtr *xml);
923 int pcmk__text_output_new(pcmk__output_t **out, const char *filename);
924 
948 static inline int
949 pcmk__output_select_rc(int old_rc, int new_rc)
950 {
951  switch (new_rc) {
952  case pcmk_rc_no_output:
953  return old_rc;
954  case pcmk_rc_ok:
955  switch (old_rc) {
956  case pcmk_rc_no_output:
957  return new_rc;
958  default:
959  return old_rc;
960  }
961  default:
962  return new_rc;
963  }
964 }
965 
966 #if defined(PCMK__UNIT_TESTING)
967 /* If we are building libcrmcommon_test.a, add this accessor function so we can
968  * inspect the internal formatters hash table.
969  */
970 GHashTable *pcmk__output_formatters(void);
971 #endif
972 
973 #define PCMK__OUTPUT_SPACER_IF(out_obj, cond) \
974  if (cond) { \
975  out->spacer(out); \
976  }
977 
978 #define PCMK__OUTPUT_LIST_HEADER(out_obj, cond, retcode, title...) \
979  if (retcode == pcmk_rc_no_output) { \
980  PCMK__OUTPUT_SPACER_IF(out_obj, cond); \
981  retcode = pcmk_rc_ok; \
982  out_obj->begin_list(out_obj, NULL, NULL, title); \
983  }
984 
985 #define PCMK__OUTPUT_LIST_FOOTER(out_obj, retcode) \
986  if (retcode == pcmk_rc_ok) { \
987  out_obj->end_list(out_obj); \
988  }
989 
990 #ifdef __cplusplus
991 }
992 #endif
993 
994 #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:509
pcmk__output_t * pcmk__mk_text_output(char **argv)
Definition: output_text.c:301
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:196
void void void void void pcmk__text_prompt(const char *prompt, bool echo, char **dest)
Definition: output_text.c:402
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:455
const char * fmt_name
The name of this output formatter.
bool(* is_quiet)(pcmk__output_t *out)
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:442
void pcmk__register_message(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
Definition: output.c:188
struct pcmk__message_entry_s pcmk__message_entry_t
pcmk__output_t * pcmk__mk_html_output(char **argv)
Definition: output_html.c:395
xmlNodePtr pcmk__output_xml_peek_parent(pcmk__output_t *out)
Definition: output_xml.c:535
enum crm_exit_e crm_exit_t
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:394
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:434
void pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level)
Definition: output_log.c:369
GOptionEntry pcmk__html_output_entries[]
Definition: output_html.c:44
pcmk__output_t * pcmk__mk_log_output(char **argv)
Definition: output_log.c:319
GOptionEntry pcmk__xml_output_entries[]
Definition: output_xml.c:29
int pcmk__call_message(pcmk__output_t *out, const char *message_id,...)
Definition: output.c:166
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:145
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:116
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:236
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
GOptionEntry pcmk__log_output_entries[]
Definition: output_log.c:19
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:478
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:126
void pcmk__output_xml_pop_parent(pcmk__output_t *out)
Definition: output_xml.c:522
Function and executable result codes.
pcmk__output_t * pcmk__mk_xml_output(char **argv)
Definition: output_xml.c:403
FILE * dest
Where output should be written.
void pcmk__output_free(pcmk__output_t *out)
Definition: output.c:28
void pcmk__unregister_formats(void)
Definition: output.c:158
GOptionEntry pcmk__text_output_entries[]
Definition: output_text.c:20
GOptionEntry pcmk__none_output_entries[]
Definition: output_none.c:18
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:207
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__xml_output_finish(pcmk__output_t *out, xmlNodePtr *xml)
Definition: output.c:258
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:111
void(* progress)(pcmk__output_t *out, bool end)
const char * parent
Definition: cib.c:27
uint8_t pcmk__output_get_log_level(const pcmk__output_t *out)
Definition: output_log.c:357
int pcmk__text_output_new(pcmk__output_t **out, const char *filename)
Definition: output.c:301
xmlNodePtr pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content)
Definition: output_xml.c:497
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:272
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:460