OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
olog.c
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include "olog.h"
3 #include "olog/loader.h"
4 
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 struct olog {
13 };
14 
15 olog *olog_create(olog_backend *backends, FILE *stream) {
16  if (backends == NULL) {
17  if (stream) {
18  fprintf(stream, "backends cannot be NULL\n");
19  }
20  return NULL;
21  }
22  olog *o = malloc(sizeof(olog));
23  if (o == NULL) {
24  if (stream) {
25  fprintf(stream, "Couldn't malloc olog\n");
26  }
27  return NULL;
28  }
29 
30  for (int i = 0;; i++) {
31  if (!(backends[i].print_callback || backends[i].destroy_callback)) {
32  size_t backends_size = sizeof(olog_backend) * (i + 1);
33  o->backends = malloc(backends_size);
34  if (o->backends == NULL) {
35  free(o);
36  if (stream) {
37  fprintf(stream, "Couldn't malloc backends\n");
38  }
39  return NULL;
40  }
41  memcpy(o->backends, backends, backends_size);
42  break;
43  }
44  }
45 
46  if (stream) {
47  if (olog_print_error(o, stream)) {
48  olog_destroy(o);
49  return NULL;
50  }
51  } else {
52  if (olog_has_error(o)) {
53  olog_destroy(o);
54  return NULL;
55  }
56  }
57 
58  return o;
59 }
61  if (olog == NULL) {
63  }
64  for (int i = 0; olog->backends[i].print_callback || olog->backends[i].destroy_callback; i++) {
67  }
68  }
69  free(olog->backends);
70  free(olog);
71 }
72 
74  if (olog == NULL) {
76  }
77  for (int i = 0; olog->backends[i].print_callback || olog->backends[i].destroy_callback; i++) {
80  return true;
81  }
82  }
83  }
84  return false;
85 }
86 bool olog_print_error(olog *olog, FILE *stream) {
87  if (olog == NULL) {
89  }
90  bool ret = false;
91  for (int i = 0; olog->backends[i].print_callback || olog->backends[i].destroy_callback; i++) {
93  if (olog->backends[i].print_error_callback(olog, &olog->backends[i], stream)) {
94  ret = true;
95  }
96  }
97  }
98  return ret;
99 }
100 int olog_vprint(olog *olog, int8_t level, va_list args) {
101  if (olog == NULL) {
103  }
104  for (int i = 0; olog->backends[i].print_callback || olog->backends[i].destroy_callback; i++) {
106  va_list args_tmp;
107  va_copy(args_tmp, args);
108  int print_ret = olog->backends[i].print_callback(olog, &olog->backends[i], level, args_tmp);
109  if (print_ret != OLOG_CONTINUE) {
110  return print_ret;
111  }
112  }
113  }
114  return 0;
115 }
116 
117 int olog_print(olog *olog, int level, ...) {
118  va_list args;
119  va_start(args, level);
120  int ret = olog_vprint(olog, level, args);
121  va_end(args);
122  return ret;
123 }
124 
125 #define define_olog_print(name, level) \
126 int olog_ ## name(olog *olog, ...){ \
127  va_list args; \
128  va_start(args, olog); \
129  int ret = olog_vprint(olog, level, args); \
130  va_end(args); \
131  return ret; \
132 }
133 
142 
#define OLOG_INFO
Definition: olog.h:43
#define OLOG_WARNING
Definition: olog.h:45
#define NULL
Definition: decode_rs.h:63
olog * olog_create(olog_backend *backends, FILE *stream)
Definition: olog.c:15
int8_t min_log_level
Minimum log level to receive.
Definition: olog.h:66
#define OLOG_ERROR
Definition: olog.h:46
olog_print_error_callback print_error_callback
Called to print an error, if one exists.
Definition: olog.h:75
A simple logger, capable of dispatching log events to multiple end points.
olog_has_error_callback has_error_callback
Called to check if backend is valid.
Definition: olog.h:72
bool olog_print_error(olog *olog, FILE *stream)
Definition: olog.c:86
int olog_vprint(olog *olog, int8_t level, va_list args)
Definition: olog.c:100
#define OLOG_CONTINUE
Definition: olog.h:51
a context in which it is NOT documented to do so subscript error
Definition: HISTORY.txt:53
olog * olog_load_default()
Definition: olog_loader.c:12
#define OLOG_EMERGENCY
Definition: olog.h:49
Definition: olog.c:11
int olog_print(olog *olog, int level,...)
Definition: olog.c:117
#define OLOG_DEBUG
Definition: olog.h:42
bool olog_has_error(olog *olog)
Definition: olog.c:73
olog_destroy_callback destroy_callback
Called when olog object is destroyed.
Definition: olog.h:81
level
Definition: mapgen.py:186
int8_t max_log_level
Maximum log level to receive.
Definition: olog.h:69
#define OLOG_ALERT
Definition: olog.h:48
olog_print_callback print_callback
Called when given data to print.
Definition: olog.h:78
#define OLOG_NOTICE
Definition: olog.h:44
#define define_olog_print(name, level)
Definition: olog.c:125
#define OLOG_CRITICAL
Definition: olog.h:47
olog_backend * backends
Definition: olog.c:12
int i
Definition: decode_rs.h:71
void olog_destroy(olog *olog)
Definition: olog.c:60