Debian Codegraph @

res/cdrel_custom/writers.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2026, Sangoma Technologies Corporation
5 *
6 * George Joseph <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 * \author George Joseph <[email protected]>
22 *
23 * \brief Backend output functions.
24 *
25 * The writers all take a vector of cdrel_value objects and write them to the output
26 * file or database.
27 *
28 */
29
30 #include "cdrel.h"
31
32 /*
33 * We can save some time and ast_str memory allocation work by allocating a single
34 * thread-local buffer and re-using it.
35 */
36 AST_THREADSTORAGE(custom_buf);
37
38 /*!
39 * \internal
40 * \brief Write a record to a text file
41 *
42 * Besides being used here, this function is also used by the legacy loggers
43 * that shortcut the advanced stuff.
44 *
45 * \param config The configuration object.
46 * \param record The data to write.
47 * \retval 0 on success
48 * \retval -1 on failure
49 */
50 int write_record_to_file(struct cdrel_config *config, struct ast_str *record)
51 {
52 FILE *out;
53 int res = 0;
54
55 ast_mutex_lock(&config->lock);
56 if ((out = fopen(config->output_filename, "a"))) {
57 res = fputs(ast_str_buffer(record), out);
58 fputs("\n", out);
59 fclose(out);
60 }
61 ast_mutex_unlock(&config->lock);
62
63 if (!out || res < 0) {
64 ast_log(LOG_ERROR, "Unable to write %s to file %s : %s\n",
65 RECORD_TYPE_STR(config->record_type), config->output_filename, strerror(errno));
66 return -1;
67 }
68 return 0;
69 }
70
71 /*!
72 * \internal
73 * \brief Concatenate and append a list of values to an ast_str
74 *
75 * \param config Config object
76 * \param values A vector of values
77 * \param str The ast_str to append to
78 * \retval 0 on success
79 * \retval -1 on failure
80 */
81 static int dsv_appender(struct cdrel_config *config, struct cdrel_values *values, struct ast_str **str)
82 {
83 int ix = 0;
84 int res = 0;
85
86 for (ix = 0; ix < AST_VECTOR_SIZE(values); ix++) {
87 struct cdrel_value *value = AST_VECTOR_GET(values, ix);
88 ast_assert(value->data_type == cdrel_type_string);
89
90 res = ast_str_append(str, -1, "%s%s", ix == 0 ? "" : config->separator, value->values.string);
91 if (res < 0) {
92 return -1;
93 }
94 }
95
96 return res;
97 }
98
99 /*!
100 * \internal
101 * \brief Write a DSV list of values to a text file
102 *
103 * \param config Config object
104 * \param values A vector of values
105 * \retval 0 on success
106 * \retval -1 on failure
107 */
108 static int dsv_writer(struct cdrel_config *config, struct cdrel_values *values)
109 {
110 int res = 0;
111 struct ast_str *str = ast_str_thread_get(&custom_buf, 1024);
112
113 ast_str_reset(str);
114
115 res = dsv_appender(config, values, &str);
116 if (res < 0) {
117 return -1;
118 }
119
120 return write_record_to_file(config, str);
121 }
122
123 /*!
124 * \internal
125 * \brief Write a list of values as a JSON object to a text file
126 *
127 * \param config Config object
128 * \param values A vector of values
129 * \retval 0 on success
130 * \retval -1 on failure
131 *
132 * \note We are intentionally NOT using the ast_json APIs here because
133 * they're expensive and these are simple objects.
134 */
135 static int json_writer(struct cdrel_config *config, struct cdrel_values *values)
136 {
137 int ix = 0;
138 int res = 0;
139 struct ast_str *str = ast_str_thread_get(&custom_buf, 1024);
140
141 ast_str_set(&str, -1, "%s", "{");
142
143 for (ix = 0; ix < AST_VECTOR_SIZE(values); ix++) {
144 struct cdrel_value *value = AST_VECTOR_GET(values, ix);
145 ast_assert(value->data_type == cdrel_type_string);
146
147 res = ast_str_append(&str, -1, "%s\"%s\":%s", ix == 0 ? "" : config->separator, value->field_name, value->values.string);
148 if (res < 0) {
149 return -1;
150 }
151 }
152 ast_str_append(&str, -1, "%s", "}");
153
154 return write_record_to_file(config, str);
155 }
156
157 /*!
158 * \internal
159 * \brief Write a record to a database
160 *
161 * Besides being used here, this function is also used by the legacy loggers
162 * that shortcut the advanced stuff.
163 *
164 * \param config The configuration object.
165 * \param record The data to write.
166 * \retval 0 on success
167 * \retval -1 on failure
168 */
169 int write_record_to_database(struct cdrel_config *config, struct cdrel_values *values)
170 {
171 int res = 0;
172 int ix;
173
174 ast_mutex_lock(&config->lock);
175 for (ix = 0; ix < AST_VECTOR_SIZE(values); ix++) {
176 struct cdrel_value *value = AST_VECTOR_GET(values, ix);
177 ast_assert(value->data_type == cdrel_type_string);
178 ast_debug(6, "%s '%s'\n", value->field_name, value->values.string);
179 res = sqlite3_bind_text(config->insert, ix + 1, value->values.string, -1, SQLITE_STATIC);
180 if (res != SQLITE_OK) {
181 ast_log(LOG_ERROR, "Unable to write %s to database %s. SQL bind for field %s:'%s'. Error: %s\n",
182 RECORD_TYPE_STR(config->record_type), config->output_filename,
183 value->field_name, value->values.string,
184 sqlite3_errmsg(config->db));
185 sqlite3_reset(config->insert);
186 ast_mutex_unlock(&config->lock);
187 return -1;
188 }
189 }
190
191 res = sqlite3_step(config->insert);
192 if (res != SQLITE_DONE) {
193 ast_log(LOG_ERROR, "Unable to write %s to database %s. Error: %s\n",
194 RECORD_TYPE_STR(config->record_type), config->output_filename,
195 sqlite3_errmsg(config->db));
196 sqlite3_reset(config->insert);
197 ast_mutex_unlock(&config->lock);
198 return -1;
199 }
200
201 sqlite3_reset(config->insert);
202 ast_mutex_unlock(&config->lock);
203
204 return res;
205 }
206
207 /*!
208 * \internal
209 * \brief Write a list of values to a database
210 *
211 * \param config Config object
212 * \param values A vector of values
213 * \retval 0 on success
214 * \retval -1 on failure
215 */
216 static int database_writer(struct cdrel_config *config, struct cdrel_values *values)
217 {
218 return write_record_to_database(config, values);
219 }
220
221 int load_writers(void)
222 {
223 ast_debug(1, "Loading Writers\n");
224 cdrel_backend_writers[cdrel_format_dsv] = dsv_writer;
225 cdrel_backend_writers[cdrel_format_json] = json_writer;
226 cdrel_backend_writers[cdrel_format_sql] = database_writer;
227
228 return 0;
229 }
230