pacemaker  2.1.6-802a72226b
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 
777 xmlNodePtr
778 pcmk__output_xml_create_parent(pcmk__output_t *out, const char *name, ...)
779 G_GNUC_NULL_TERMINATED;
780 
789 void
790 pcmk__output_xml_add_node_copy(pcmk__output_t *out, xmlNodePtr node);
791 
801 xmlNodePtr
802 pcmk__output_create_xml_node(pcmk__output_t *out, const char *name, ...)
803 G_GNUC_NULL_TERMINATED;
804 
814 xmlNodePtr
815 pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content);
816 
831 void
833 
848 void
850 
865 xmlNodePtr
867 
881 xmlNodePtr
882 pcmk__output_create_html_node(pcmk__output_t *out, const char *element_name, const char *id,
883  const char *class_name, const char *text);
884 
900 void
901 pcmk__html_add_header(const char *name, ...)
902 G_GNUC_NULL_TERMINATED;
903 
913 void pcmk__output_and_clear_error(GError **error, pcmk__output_t *out);
914 
915 int pcmk__xml_output_new(pcmk__output_t **out, xmlNodePtr *xml);
916 void pcmk__xml_output_finish(pcmk__output_t *out, xmlNodePtr *xml);
918 int pcmk__text_output_new(pcmk__output_t **out, const char *filename);
919 
943 static inline int
944 pcmk__output_select_rc(int old_rc, int new_rc)
945 {
946  switch (new_rc) {
947  case pcmk_rc_no_output:
948  return old_rc;
949  case pcmk_rc_ok:
950  switch (old_rc) {
951  case pcmk_rc_no_output:
952  return new_rc;
953  default:
954  return old_rc;
955  }
956  default:
957  return new_rc;
958  }
959 }
960 
961 #if defined(PCMK__UNIT_TESTING)
962 /* If we are building libcrmcommon_test.a, add this accessor function so we can
963  * inspect the internal formatters hash table.
964  */
965 GHashTable *pcmk__output_formatters(void);
966 #endif
967 
968 #define PCMK__OUTPUT_SPACER_IF(out_obj, cond) \
969  if (cond) { \
970  out->spacer(out); \
971  }
972 
973 #define PCMK__OUTPUT_LIST_HEADER(out_obj, cond, retcode, title...) \
974  if (retcode == pcmk_rc_no_output) { \
975  PCMK__OUTPUT_SPACER_IF(out_obj, cond); \
976  retcode = pcmk_rc_ok; \
977  out_obj->begin_list(out_obj, NULL, NULL, title); \
978  }
979 
980 #define PCMK__OUTPUT_LIST_FOOTER(out_obj, retcode) \
981  if (retcode == pcmk_rc_ok) { \
982  out_obj->end_list(out_obj); \
983  }
984 
985 #ifdef __cplusplus
986 }
987 #endif
988 
989 #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:505
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:24
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:438
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:531
enum crm_exit_e crm_exit_t
int(* info)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
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:345
GOptionEntry pcmk__html_output_entries[]
Definition: output_html.c:44
pcmk__output_t * pcmk__mk_log_output(char **argv)
Definition: output_log.c:295
GOptionEntry pcmk__xml_output_entries[]
Definition: output_xml.c:25
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:18
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:474
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:518
Function and executable result codes.
pcmk__output_t * pcmk__mk_xml_output(char **argv)
Definition: output_xml.c:399
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:25
uint8_t pcmk__output_get_log_level(const pcmk__output_t *out)
Definition: output_log.c:333
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:493
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:456