pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
output_text.c
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 <stdarg.h>
11 #include <stdlib.h>
12 #include <crm/crm.h>
13 #include <crm/common/output.h>
14 #include <glib.h>
15 
16 static gboolean fancy = FALSE;
17 
18 GOptionEntry pcmk__text_output_entries[] = {
19  { "text-fancy", 0, 0, G_OPTION_ARG_NONE, &fancy,
20  "Use more highly formatted output (requires --output-as=text)",
21  NULL },
22 
23  { NULL }
24 };
25 
26 typedef struct text_list_data_s {
27  unsigned int len;
28  char *singular_noun;
29  char *plural_noun;
31 
32 typedef struct private_data_s {
33  GQueue *parent_q;
35 
36 static void
37 text_free_priv(pcmk__output_t *out) {
38  private_data_t *priv = out->priv;
39 
40  if (priv == NULL) {
41  return;
42  }
43 
44  g_queue_free(priv->parent_q);
45  free(priv);
46 }
47 
48 static bool
49 text_init(pcmk__output_t *out) {
50  private_data_t *priv = NULL;
51 
52  /* If text_init was previously called on this output struct, just return. */
53  if (out->priv != NULL) {
54  return true;
55  } else {
56  out->priv = calloc(1, sizeof(private_data_t));
57  if (out->priv == NULL) {
58  return false;
59  }
60 
61  priv = out->priv;
62  }
63 
64  priv->parent_q = g_queue_new();
65  return true;
66 }
67 
68 static void
69 text_finish(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest) {
70  /* This function intentionally left blank */
71 }
72 
73 static void
74 text_reset(pcmk__output_t *out) {
75  CRM_ASSERT(out != NULL);
76 
77  text_free_priv(out);
78  text_init(out);
79 }
80 
81 static void
82 text_subprocess_output(pcmk__output_t *out, int exit_status,
83  const char *proc_stdout, const char *proc_stderr) {
84  if (proc_stdout != NULL) {
85  fprintf(out->dest, "%s\n", proc_stdout);
86  }
87 
88  if (proc_stderr != NULL) {
89  fprintf(out->dest, "%s\n", proc_stderr);
90  }
91 }
92 
93 static void
94 text_version(pcmk__output_t *out, bool extended) {
95  if (extended) {
96  fprintf(out->dest, "Pacemaker %s (Build: %s): %s\n", PACEMAKER_VERSION, BUILD_VERSION, CRM_FEATURES);
97  } else {
98  fprintf(out->dest, "Pacemaker %s\n", PACEMAKER_VERSION);
99  fprintf(out->dest, "Written by Andrew Beekhof\n");
100  }
101 }
102 
103 G_GNUC_PRINTF(2, 3)
104 static void
105 text_err(pcmk__output_t *out, const char *format, ...) {
106  va_list ap;
107  int len = 0;
108 
109  va_start(ap, format);
110 
111  /* Informational output does not get indented, to separate it from other
112  * potentially indented list output.
113  */
114  len = vfprintf(stderr, format, ap);
115  CRM_ASSERT(len >= 0);
116  va_end(ap);
117 
118  /* Add a newline. */
119  fprintf(stderr, "\n");
120 }
121 
122 G_GNUC_PRINTF(2, 3)
123 static void
124 text_info(pcmk__output_t *out, const char *format, ...) {
125  va_list ap;
126  int len = 0;
127 
128  va_start(ap, format);
129 
130  /* Informational output does not get indented, to separate it from other
131  * potentially indented list output.
132  */
133  len = vfprintf(out->dest, format, ap);
134  CRM_ASSERT(len >= 0);
135  va_end(ap);
136 
137  /* Add a newline. */
138  fprintf(out->dest, "\n");
139 }
140 
141 static void
142 text_output_xml(pcmk__output_t *out, const char *name, const char *buf) {
143  private_data_t *priv = out->priv;
144 
145  CRM_ASSERT(priv != NULL);
146  pcmk__indented_printf(out, "%s", buf);
147 }
148 
149 G_GNUC_PRINTF(4, 5)
150 static void
151 text_begin_list(pcmk__output_t *out, const char *singular_noun, const char *plural_noun,
152  const char *format, ...) {
153  private_data_t *priv = out->priv;
154  text_list_data_t *new_list = NULL;
155  va_list ap;
156 
157  CRM_ASSERT(priv != NULL);
158 
159  va_start(ap, format);
160 
161  if (fancy && format) {
162  pcmk__indented_vprintf(out, format, ap);
163  fprintf(out->dest, ":\n");
164  }
165 
166  va_end(ap);
167 
168  new_list = calloc(1, sizeof(text_list_data_t));
169  new_list->len = 0;
170  new_list->singular_noun = singular_noun == NULL ? NULL : strdup(singular_noun);
171  new_list->plural_noun = plural_noun == NULL ? NULL : strdup(plural_noun);
172 
173  g_queue_push_tail(priv->parent_q, new_list);
174 }
175 
176 G_GNUC_PRINTF(3, 4)
177 static void
178 text_list_item(pcmk__output_t *out, const char *id, const char *format, ...) {
179  private_data_t *priv = out->priv;
180  va_list ap;
181 
182  CRM_ASSERT(priv != NULL);
183 
184  va_start(ap, format);
185 
186  if (fancy) {
187  if (id != NULL) {
188  /* Not really a good way to do this all in one call, so make it two.
189  * The first handles the indentation and list styling. The second
190  * just prints right after that one.
191  */
192  pcmk__indented_printf(out, "%s: ", id);
193  vfprintf(out->dest, format, ap);
194  } else {
195  pcmk__indented_vprintf(out, format, ap);
196  }
197  } else {
198  pcmk__indented_vprintf(out, format, ap);
199  }
200 
201  fputc('\n', out->dest);
202  va_end(ap);
203 
204  out->increment_list(out);
205 }
206 
207 static void
208 text_increment_list(pcmk__output_t *out) {
209  private_data_t *priv = out->priv;
210  gpointer tail;
211 
212  CRM_ASSERT(priv != NULL);
213  tail = g_queue_peek_tail(priv->parent_q);
214  CRM_ASSERT(tail != NULL);
215  ((text_list_data_t *) tail)->len++;
216 }
217 
218 static void
219 text_end_list(pcmk__output_t *out) {
220  private_data_t *priv = out->priv;
221  text_list_data_t *node = NULL;
222 
223  CRM_ASSERT(priv != NULL);
224  node = g_queue_pop_tail(priv->parent_q);
225 
226  if (node->singular_noun != NULL && node->plural_noun != NULL) {
227  if (node->len == 1) {
228  pcmk__indented_printf(out, "%d %s found\n", node->len, node->singular_noun);
229  } else {
230  pcmk__indented_printf(out, "%d %s found\n", node->len, node->plural_noun);
231  }
232  }
233 
234  free(node);
235 }
236 
238 pcmk__mk_text_output(char **argv) {
239  pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
240 
241  if (retval == NULL) {
242  return NULL;
243  }
244 
245  retval->fmt_name = "text";
246  retval->request = argv == NULL ? NULL : g_strjoinv(" ", argv);
247  retval->supports_quiet = true;
248 
249  retval->init = text_init;
250  retval->free_priv = text_free_priv;
251  retval->finish = text_finish;
252  retval->reset = text_reset;
253 
255  retval->message = pcmk__call_message;
256 
257  retval->subprocess_output = text_subprocess_output;
258  retval->version = text_version;
259  retval->info = text_info;
260  retval->err = text_err;
261  retval->output_xml = text_output_xml;
262 
263  retval->begin_list = text_begin_list;
264  retval->list_item = text_list_item;
265  retval->increment_list = text_increment_list;
266  retval->end_list = text_end_list;
267 
268  return retval;
269 }
270 
271 G_GNUC_PRINTF(2, 0)
272 void
273 pcmk__indented_vprintf(pcmk__output_t *out, const char *format, va_list args) {
274  int len = 0;
275 
276  if (fancy) {
277  int level = 0;
278  private_data_t *priv = out->priv;
279 
280  CRM_ASSERT(priv != NULL);
281 
282  level = g_queue_get_length(priv->parent_q);
283 
284  for (int i = 0; i < level; i++) {
285  fprintf(out->dest, " ");
286  }
287 
288  if (level > 0) {
289  fprintf(out->dest, "* ");
290  }
291  }
292 
293  len = vfprintf(out->dest, format, args);
294  CRM_ASSERT(len >= 0);
295 }
296 
297 G_GNUC_PRINTF(2, 3)
298 void
299 pcmk__indented_printf(pcmk__output_t *out, const char *format, ...) {
300  va_list ap;
301 
302  va_start(ap, format);
303  pcmk__indented_vprintf(out, format, ap);
304  va_end(ap);
305 }
bool(* init)(pcmk__output_t *out)
Definition: output.h:215
pcmk__output_t * pcmk__mk_text_output(char **argv)
Definition: output_text.c:238
A dumping ground.
Formatted output for pacemaker tools.
void(* register_message)(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
Definition: output.h:293
const char * fmt_name
The name of this output formatter.
Definition: output.h:157
void(* reset)(pcmk__output_t *out)
Definition: output.h:280
void void pcmk__indented_vprintf(pcmk__output_t *out, const char *format, va_list args) G_GNUC_PRINTF(2
void(*) void(* err)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
Definition: output.h:361
bool supports_quiet
Does this formatter support a special quiet mode?
Definition: output.h:174
int(* message)(pcmk__output_t *out, const char *message_id,...)
Definition: output.h:311
enum crm_exit_e crm_exit_t
#define PACEMAKER_VERSION
Definition: config.h:509
void * priv
Implementation-specific private data.
Definition: output.h:199
int pcmk__call_message(pcmk__output_t *out, const char *message_id,...)
Definition: output.c:116
void(* end_list)(pcmk__output_t *out)
Definition: output.h:432
void(* begin_list)(pcmk__output_t *out, const char *singular_noun, const char *plural_noun, const char *format,...) G_GNUC_PRINTF(4
Definition: output.h:392
#define BUILD_VERSION
Definition: config.h:8
struct private_data_s private_data_t
void(* subprocess_output)(pcmk__output_t *out, int exit_status, const char *proc_stdout, const char *proc_stderr)
Definition: output.h:322
FILE * dest
Where output should be written.
Definition: output.h:182
void(*) void(*) void(* increment_list)(pcmk__output_t *out)
Definition: output.h:420
void(* info)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
Definition: output.h:347
GOptionEntry pcmk__text_output_entries[]
Definition: output_text.c:18
#define CRM_ASSERT(expr)
Definition: results.h:42
void(* finish)(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest)
Definition: output.h:262
void(* free_priv)(pcmk__output_t *out)
Definition: output.h:226
gchar * request
A copy of the request that generated this output.
Definition: output.h:165
void pcmk__indented_printf(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
This structure contains everything that makes up a single output formatter.
Definition: output.h:153
struct text_list_data_s text_list_data_t
void(* version)(pcmk__output_t *out, bool extended)
Definition: output.h:333
void(*) void(*) void(* output_xml)(pcmk__output_t *out, const char *name, const char *buf)
Definition: output.h:371
char * name
Definition: pcmk_fence.c:30
#define CRM_FEATURES
Definition: config.h:35
void pcmk__register_message(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
Definition: output.c:134
void(*) void(* list_item)(pcmk__output_t *out, const char *name, const char *format,...) G_GNUC_PRINTF(3
Definition: output.h:405