libamxc  1.10.3
C Generic Data Containers
amxc_variant_dump.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <amxc/amxc_string.h>
#include <amxc/amxc_variant_type.h>
#include <amxc/amxc_rbuffer.h>
#include <amxc/amxc_string_split.h>
#include <amxc_variant_priv.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Data Structures

struct  _amxc_log_var
 

Typedefs

typedef struct _amxc_log_var amxc_log_var_t
 
typedef int(* amxc_var_dump_fn_t) (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 

Functions

static void amxc_var_log_init (amxc_log_var_t *log, int fd, FILE *stream, size_t msg_length)
 
static int amxc_var_write (amxc_log_var_t *log, const char *line, size_t length)
 
static int amxc_var_dump_internal (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static void write_indentation (int indent, amxc_log_var_t *log)
 
static int variant_dump_type (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int variant_dump_null (UNUSED const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int variant_dump_char (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int variant_dump_default (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int variant_dump_list (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int variant_dump_htable (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int variant_dump_fd (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int variant_dump_ts (const amxc_var_t *const var, int indent, amxc_log_var_t *log)
 
static int amxc_var_dump_impl (const amxc_var_t *const var, int fd, FILE *stream)
 
int amxc_var_dump (const amxc_var_t *const var, int fd)
 Dumps the content of the variant in a human readable manner. More...
 
int amxc_var_dump_stream (const amxc_var_t *const var, FILE *stream)
 Dumps the content of the variant in a human readable manner. More...
 
int amxc_var_log (const amxc_var_t *const var)
 Logs the content of the variant in a human readable manner to syslog. More...
 

Typedef Documentation

◆ amxc_log_var_t

typedef struct _amxc_log_var amxc_log_var_t

◆ amxc_var_dump_fn_t

typedef int(* amxc_var_dump_fn_t) (const amxc_var_t *const var, int indent, amxc_log_var_t *log)

Definition at line 74 of file amxc_variant_dump.c.

Function Documentation

◆ amxc_var_dump_impl()

static int amxc_var_dump_impl ( const amxc_var_t *const  var,
int  fd,
FILE *  stream 
)
static

Definition at line 321 of file amxc_variant_dump.c.

321  {
322  int retval = 0;
323  amxc_log_var_t log;
324 
325  amxc_var_log_init(&log, fd, stream, 0);
326 
327  if(var == NULL) {
328  amxc_var_write(&log, "NULL\n", 5);
329  goto exit;
330  }
331 
332  retval = amxc_var_dump_internal(var, 0, &log);
333  when_true(amxc_var_write(&log, "\n", 1) == -1, exit);
334 
335 exit:
337  return retval;
338 }
#define when_true(x, l)
Definition: amxc_macros.h:134
static void amxc_var_log_init(amxc_log_var_t *log, int fd, FILE *stream, size_t msg_length)
static int amxc_var_dump_internal(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
static int amxc_var_write(amxc_log_var_t *log, const char *line, size_t length)
void amxc_string_clean(amxc_string_t *const string)
Frees the string buffer and reset length attributes.
Definition: amxc_string.c:189
amxc_string_t message
static amxc_var_t * var
Definition: test_issue_58.c:77

◆ amxc_var_dump_internal()

static int amxc_var_dump_internal ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 283 of file amxc_variant_dump.c.

285  {
286  int retval = -1;
307  NULL,
308  };
309 
311  retval = variant_dump_default(var, indent, log);
312  } else {
313  if(dumpfn[var->type_id] != NULL) {
314  retval = dumpfn[var->type_id](var, indent, log);
315  }
316  }
317 
318  return retval;
319 }
static int variant_dump_ts(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
static int variant_dump_null(UNUSED const amxc_var_t *const var, int indent, amxc_log_var_t *log)
static int variant_dump_default(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
static int variant_dump_list(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
static int variant_dump_htable(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
static int variant_dump_fd(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
int(* amxc_var_dump_fn_t)(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
static int variant_dump_char(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257
uint32_t type_id
Definition: amxc_variant.h:864

◆ amxc_var_log_init()

static void amxc_var_log_init ( amxc_log_var_t log,
int  fd,
FILE *  stream,
size_t  msg_length 
)
static

Definition at line 78 of file amxc_variant_dump.c.

78  {
79  log->fd = fd;
80  log->stream = stream;
81  amxc_string_init(&log->message, msg_length);
82 }
int amxc_string_init(amxc_string_t *const string, const size_t length)
Initializes a string.
Definition: amxc_string.c:163

◆ amxc_var_write()

static int amxc_var_write ( amxc_log_var_t log,
const char *  line,
size_t  length 
)
static

Definition at line 84 of file amxc_variant_dump.c.

86  {
87  int retval = 0;
88  if(log->fd != -1) {
89  retval = write(log->fd, line, length);
90  } else if(log->stream != NULL) {
91  retval = fwrite(line, 1, length, log->stream); // size is 1 to return the number of bytes like write()
92  } else {
93  retval = amxc_string_append(&log->message, line, length);
94  if(amxc_string_search(&log->message, "\n", 0) >= 0) {
95  syslog(LOG_DAEMON | LOG_DEBUG,
96  "%s", amxc_string_get(&log->message, 0));
98  }
99  }
100  return retval;
101 }
const char * amxc_string_get(const amxc_string_t *const string, const size_t offset)
Gets the content of the string buffer.
Definition: amxc_string.c:339
AMXC_INLINE int amxc_string_append(amxc_string_t *const string, const char *const text, const size_t length)
Appends text to the end of the current content of the string buffer.
Definition: amxc_string.h:920
void amxc_string_reset(amxc_string_t *const string)
Resets the buffer, reset the content to all 0.
Definition: amxc_string.c:203
int amxc_string_search(const amxc_string_t *const string, const char *needle, uint32_t start_pos)
Searches a sub-string in a string.
Definition: amxc_string.c:765

◆ variant_dump_char()

static int variant_dump_char ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 156 of file amxc_variant_dump.c.

158  {
159  const char* txt = amxc_var_constcast(cstring_t, var);
160  write_indentation(indent, log);
161  when_true(amxc_var_write(log, "\"", 1) == -1, exit);
162  if(txt != NULL) {
163  when_true(amxc_var_write(log, txt, strlen(txt)) == -1, exit);
164  }
165  when_true(amxc_var_write(log, "\"", 1) == -1, exit);
166 
167 exit:
168  return 0;
169 }
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
static void write_indentation(int indent, amxc_log_var_t *log)
#define amxc_var_constcast(type, var)
Takes the content from a variant.
Definition: amxc_variant.h:722

◆ variant_dump_default()

static int variant_dump_default ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 171 of file amxc_variant_dump.c.

173  {
174  int retval = -1;
175  char* text = amxc_var_dyncast(cstring_t, var);
176 
177  write_indentation(indent, log);
178 
179  if(text != NULL) {
180  when_true(amxc_var_write(log, text, strlen(text)) == -1, exit);
181  retval = 0;
182  } else {
183  retval = variant_dump_type(var, indent, log);
184  }
185 
186 exit:
187  free(text);
188  return retval;
189 }
static int variant_dump_type(const amxc_var_t *const var, int indent, amxc_log_var_t *log)
#define amxc_var_dyncast(type, var)
Dynamic cast a variant to a certain type.
Definition: amxc_variant.h:678

◆ variant_dump_fd()

static int variant_dump_fd ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 269 of file amxc_variant_dump.c.

271  {
272  variant_dump_type(var, indent, log);
273  return variant_dump_default(var, indent, log);
274 }

◆ variant_dump_htable()

static int variant_dump_htable ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 220 of file amxc_variant_dump.c.

222  {
225  const char* prev_key = NULL;
226  amxc_htable_it_t* it = NULL;
227 
228  when_true(amxc_var_write(log, "{\n", 2) == -1, exit);
229  indent += 4;
230  for(uint32_t i = 0; i < amxc_array_capacity(keys); i++) {
231  amxc_var_t* hvar = NULL;
232  const char* key
233  = (const char*) amxc_array_it_get_data(amxc_array_get_at(keys, i));
234  if((prev_key != NULL) && (key != NULL) && (strcmp(key, prev_key) == 0)) {
236  } else {
237  it = amxc_htable_get(htable, key);
238  }
239  prev_key = key;
240  hvar = amxc_var_from_htable_it(it);
241  write_indentation(indent, log);
242  if(key != NULL) {
243  when_true(amxc_var_write(log, key, strlen(key)) == -1, exit);
244  } else {
245  when_true(amxc_var_write(log, "UNKNOWN", 7) == -1, exit);
246  }
247  when_true(amxc_var_write(log, " = ", 3) == -1, exit);
248  if((amxc_var_type_of(hvar) == AMXC_VAR_ID_HTABLE) ||
249  ( amxc_var_type_of(hvar) == AMXC_VAR_ID_LIST)) {
250  amxc_var_dump_internal(hvar, indent, log);
251  } else {
252  amxc_var_dump_internal(hvar, 0, log);
253  }
254  if(i + 1 < amxc_array_capacity(keys)) {
255  when_true(amxc_var_write(log, ",\n", 2) == -1, exit);
256  } else {
257  when_true(amxc_var_write(log, "\n", 1) == -1, exit);
258  }
259  }
260  indent -= 4;
261  write_indentation(indent, log);
262  when_true(amxc_var_write(log, "}", 1) == -1, exit);
263 
264 exit:
265  amxc_array_delete(&keys, NULL);
266  return 0;
267 }
#define amxc_var_from_htable_it(ht_it)
Get the variant pointer from an amxc htable iterator.
Definition: amxc_variant.h:790
AMXC_INLINE void * amxc_array_it_get_data(const amxc_array_it_t *const it)
Gets the data pointer of array iterator.
Definition: amxc_array.h:729
AMXC_INLINE size_t amxc_array_capacity(const amxc_array_t *const array)
Gets the capacity of the array.
Definition: amxc_array.h:694
amxc_array_it_t * amxc_array_get_at(const amxc_array_t *const array, const unsigned int index)
Gets the item iterator for the given index.
Definition: amxc_array.c:504
void amxc_array_delete(amxc_array_t **array, const amxc_array_it_delete_t func)
Frees the previously allocated array.
Definition: amxc_array.c:213
amxc_htable_it_t * amxc_htable_it_get_next_key(const amxc_htable_it_t *const reference)
Gets the next iterator in the hash table with the same key.
amxc_array_t * amxc_htable_get_sorted_keys(const amxc_htable_t *const htable)
Creates an array containing all keys of the hash table.
Definition: amxc_htable.c:339
amxc_htable_it_t * amxc_htable_get(const amxc_htable_t *const htable, const char *const key)
Gets a hash table iterator from the hash table.
Definition: amxc_htable.c:278
#define AMXC_VAR_ID_LIST
Ambiorix Linked List variant id.
Definition: amxc_variant.h:206
#define AMXC_VAR_ID_HTABLE
Ambiorix Hash Table variant id.
Definition: amxc_variant.h:212
uint32_t amxc_var_type_of(const amxc_var_t *const var)
Gets the variant type id of a variant.
Definition: amxc_variant.c:668
The array structure.
Definition: amxc_array.h:162
The hash table iterator structure.
Definition: amxc_htable.h:138
The hash table structure.
Definition: amxc_htable.h:175
The variant struct definition.
Definition: amxc_variant.h:861
static amxc_htable_it_t it[2000]
static amxc_htable_t * htable

◆ variant_dump_list()

static int variant_dump_list ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 191 of file amxc_variant_dump.c.

193  {
194 
196 
197  when_true(amxc_var_write(log, "[\n", 2) == -1, exit);
198  indent += 4;
199  amxc_llist_for_each(it, list) {
201  if((amxc_var_type_of(lvar) == AMXC_VAR_ID_HTABLE) ||
202  ( amxc_var_type_of(lvar) == AMXC_VAR_ID_LIST)) {
203  write_indentation(indent, log);
204  }
205  amxc_var_dump_internal(lvar, indent, log);
206  if(amxc_llist_it_get_next(it) != NULL) {
207  when_true(amxc_var_write(log, ",\n", 2) == -1, exit);
208  } else {
209  when_true(amxc_var_write(log, "\n", 1) == -1, exit);
210  }
211  }
212  indent -= 4;
213  write_indentation(indent, log);
214  when_true(amxc_var_write(log, "]", 1) == -1, exit);
215 
216 exit:
217  return 0;
218 }
#define amxc_var_from_llist_it(ll_it)
Get the variant pointer from an amxc linked list iterator.
Definition: amxc_variant.h:799
AMXC_INLINE amxc_llist_it_t * amxc_llist_it_get_next(const amxc_llist_it_t *const reference)
Gets the next iterator in the list.
Definition: amxc_llist.h:824
#define amxc_llist_for_each(it, list)
Loops over the list from head to tail.
Definition: amxc_llist.h:253
The linked list structure.
Definition: amxc_llist.h:228

◆ variant_dump_null()

static int variant_dump_null ( UNUSED const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 146 of file amxc_variant_dump.c.

148  {
149  write_indentation(indent, log);
150  when_true(amxc_var_write(log, "<NULL>", 6) == -1, exit);
151 
152 exit:
153  return 0;
154 }

◆ variant_dump_ts()

static int variant_dump_ts ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 276 of file amxc_variant_dump.c.

278  {
279  variant_dump_type(var, indent, log);
280  return variant_dump_default(var, indent, log);
281 }

◆ variant_dump_type()

static int variant_dump_type ( const amxc_var_t *const  var,
int  indent,
amxc_log_var_t log 
)
static

Definition at line 116 of file amxc_variant_dump.c.

118  {
119  amxc_string_t txt;
120  char addr[64] = "";
121  const char* type_name = amxc_var_type_name_of(var);
122 
123  write_indentation(indent, log);
124  amxc_string_init(&txt, 64);
125  amxc_string_append(&txt, "<", 1);
126  if(type_name != NULL) {
127  amxc_string_append(&txt, type_name, strlen(type_name));
128  } else {
129  amxc_string_append(&txt, "UNKNOWN", 7);
130  }
131  amxc_string_append(&txt, ">:", 2);
133  snprintf(addr, 63, "%p:", var->data.data);
134  amxc_string_append(&txt, addr, strlen(addr));
135  }
137  amxc_string_get(&txt, 0),
138  amxc_string_text_length(&txt)) == -1,
139  exit);
140 
141 exit:
142  amxc_string_clean(&txt);
143  return 0;
144 }
AMXC_INLINE size_t amxc_string_text_length(const amxc_string_t *const string)
Gets the current size of the used string buffer.
Definition: amxc_string.h:997
#define AMXC_VAR_ID_ANY
Special variant id, typically used in cast or conversion functions.
Definition: amxc_variant.h:247
const char * amxc_var_type_name_of(const amxc_var_t *const var)
Gets the variant type name of a variant.
Definition: amxc_variant.c:672
The string structure.
Definition: amxc_string.h:103
void * data
Definition: amxc_variant.h:883

◆ write_indentation()

static void write_indentation ( int  indent,
amxc_log_var_t log 
)
static

Definition at line 107 of file amxc_variant_dump.c.

107  {
108  for(int i = 0; i < indent; i++) {
109  when_true(amxc_var_write(log, " ", 1) == -1, exit);
110  }
111 
112 exit:
113  return;
114 }