OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
buffer.c
Go to the documentation of this file.
1 #include "olog.h"
2 #include "olog/buffer.h"
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #define BUFFER_INCREASE 2
8 
9 static int olog_backend_buffer_print(olog *olog, olog_backend *backend, uint8_t severity, va_list args) {
10  olog_buffer *data = (olog_buffer*) (backend->data);
11  if (!data || !data->buffer_start) {
12  return OLOG_EXPLODED;
13  }
14  va_list args_tmp;
15  va_copy(args_tmp, args);
16 
17  const char* format = va_arg(args_tmp, const char*);
18  size_t max_to_print = data->buffer_size_max - data->buffer_size;
19  int ret = vsnprintf(data->buffer_current, max_to_print, format, args_tmp);
20  if (ret < 0) {
21  return OLOG_EXPLODED;
22  }
23  if ((size_t) ret >= max_to_print) {
24  void *tmp_ptr;
25  tmp_ptr = realloc(data->buffer_start, data->buffer_size_max * BUFFER_INCREASE);
26  if (tmp_ptr) {
27  data->buffer_start = tmp_ptr;
28  data->buffer_current = data->buffer_start + data->buffer_size;
29  data->buffer_size_max *= BUFFER_INCREASE;
30  return olog_backend_buffer_print(olog, backend, severity, args);
31  } else {
32  free(data->buffer_start);
33  data->buffer_start = NULL;
34  data->buffer_current = NULL;
35  data->buffer_size_max = 0;
36  data->buffer_size = 0;
37  return 1;
38  }
39  } else {
40  data->buffer_current += ret;
41  data->buffer_size += ret;
42  }
43  return ret < 0 ? OLOG_EXPLODED : 0;
44 }
45 
46 static int olog_backend_buffer_destroy(olog *olog, olog_backend *backend) {
47  if (backend->data) {
48  olog_buffer *data = (olog_buffer*) (backend->data);
49  if (data->buffer_start) {
50  free(data->buffer_start);
51  }
52  free(backend->data);
53  }
54  return 0;
55 }
56 static int olog_backend_has_error(olog *olog, olog_backend *backend) {
57  if (backend->data == NULL) {
58  return 1;
59  }
60  olog_buffer *data = (olog_buffer*) (backend->data);
61  return data->buffer_start == NULL;
62 }
63 static int olog_backend_print_error(olog *olog, olog_backend *backend, FILE* stream) {
64  if (backend->data == NULL) {
65  fprintf(stream, "Couldn't malloc object\n");
66  return 1;
67  }
68  olog_buffer *data = (olog_buffer*) (backend->data);
69  if (data->buffer_start == NULL) {
70  fprintf(stream, "Couldn't malloc buffer\n");
71  return 1;
72  }
73  return 0;
74 }
75 
76 olog_backend olog_backend_buffer(size_t initial_size, int8_t min_log_level, int8_t max_log_level) {
77  olog_buffer *data = malloc(sizeof(olog_buffer));
78  data->buffer_start = malloc(sizeof(char) * initial_size);
79  data->buffer_current = data->buffer_start;
80  data->buffer_size_max = initial_size;
81  data->buffer_size = 0;
82  *data->buffer_start = '\0';
83  olog_backend ret = {
84  .min_log_level = min_log_level,
85  .max_log_level = max_log_level,
86  .data = data,
87  .print_callback = olog_backend_buffer_print,
88  .destroy_callback = olog_backend_buffer_destroy,
89  .has_error_callback = olog_backend_has_error,
90  .print_error_callback = olog_backend_print_error
91  };
92  return ret;
93 }
94 
96  olog_buffer *data = (olog_buffer*) backend->data;
97  data->buffer_current = data->buffer_start;
98  data->buffer_size = 0;
99  *data->buffer_start = '\0';
100 }
#define BUFFER_INCREASE
Definition: buffer.c:7
#define NULL
Definition: decode_rs.h:63
int8_t min_log_level
Minimum log level to receive.
Definition: olog.h:66
A simple logger, capable of dispatching log events to multiple end points.
Definition: olog.c:11
olog_backend olog_backend_buffer(size_t initial_size, int8_t min_log_level, int8_t max_log_level)
Definition: buffer.c:76
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
void * data
Storage for backend use.
Definition: olog.h:84
#define OLOG_EXPLODED
Definition: olog.h:53
void olog_backend_buffer_clear(olog_backend *backend)
Definition: buffer.c:95