Asterisk - The Open Source Telephony Project GIT-master-80b953f
Loading...
Searching...
No Matches
Functions
writers.c File Reference

Backend output functions. More...

#include "cdrel.h"
Include dependency graph for writers.c:

Go to the source code of this file.

Functions

 AST_THREADSTORAGE_CUSTOM_SCOPE (custom_buf, NULL, ast_free_ptr, static)
 
static int database_writer (struct cdrel_config *config, struct cdrel_values *values)
 
static int dsv_appender (struct cdrel_config *config, struct cdrel_values *values, struct ast_str **str)
 
static int dsv_writer (struct cdrel_config *config, struct cdrel_values *values)
 
static int json_writer (struct cdrel_config *config, struct cdrel_values *values)
 
int load_writers (void)
 
int write_record_to_database (struct cdrel_config *config, struct cdrel_values *values)
 
int write_record_to_file (struct cdrel_config *config, struct ast_str *record)
 

Detailed Description

Backend output functions.

Author
George Joseph gjose.nosp@m.ph@s.nosp@m.angom.nosp@m.a.co.nosp@m.m

The writers all take a vector of cdrel_value objects and write them to the output file or database.

Definition in file writers.c.

Function Documentation

◆ AST_THREADSTORAGE_CUSTOM_SCOPE()

AST_THREADSTORAGE_CUSTOM_SCOPE ( custom_buf  ,
NULL  ,
ast_free_ptr  ,
static   
)

◆ database_writer()

static int database_writer ( struct cdrel_config config,
struct cdrel_values values 
)
static

Definition at line 216 of file writers.c.

217{
218 return write_record_to_database(config, values);
219}
static const char config[]
int write_record_to_database(struct cdrel_config *config, struct cdrel_values *values)
Definition writers.c:169

References config, cdrel_value::values, and write_record_to_database().

Referenced by load_writers().

◆ dsv_appender()

static int dsv_appender ( struct cdrel_config config,
struct cdrel_values values,
struct ast_str **  str 
)
static

Definition at line 81 of file writers.c.

82{
83 int ix = 0;
84 int res = 0;
85
86 for (ix = 0; ix < AST_VECTOR_SIZE(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}
const char * str
Definition app_jack.c:150
@ cdrel_type_string
Definition cdrel.h:73
const char * string
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition strings.h:1139
union cdrel_value::@454 values
int value
Definition syslog.c:37
#define ast_assert(a)
Definition utils.h:779
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition vector.h:620
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition vector.h:691

References ast_assert, ast_str_append(), AST_VECTOR_GET, AST_VECTOR_SIZE, cdrel_type_string, config, str, value, and cdrel_value::values.

Referenced by dsv_writer().

◆ dsv_writer()

static int dsv_writer ( struct cdrel_config config,
struct cdrel_values values 
)
static

Definition at line 108 of file writers.c.

109{
110 int res = 0;
111 struct ast_str *str = ast_str_thread_get(&custom_buf, 1024);
112
114
115 res = dsv_appender(config, values, &str);
116 if (res < 0) {
117 return -1;
118 }
119
121}
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition strings.h:693
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition strings.h:909
Support for dynamic strings.
Definition strings.h:623
int write_record_to_file(struct cdrel_config *config, struct ast_str *record)
Definition writers.c:50
static int dsv_appender(struct cdrel_config *config, struct cdrel_values *values, struct ast_str **str)
Definition writers.c:81

References ast_str_reset(), ast_str_thread_get(), config, dsv_appender(), str, and write_record_to_file().

Referenced by load_writers().

◆ json_writer()

static int json_writer ( struct cdrel_config config,
struct cdrel_values values 
)
static

Definition at line 135 of file writers.c.

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
155}
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition strings.h:1113
char * field_name
Definition cdrel.h:174

References ast_assert, ast_str_append(), ast_str_set(), ast_str_thread_get(), AST_VECTOR_GET, AST_VECTOR_SIZE, cdrel_type_string, config, str, value, cdrel_value::values, and write_record_to_file().

Referenced by load_writers().

◆ load_writers()

int load_writers ( void  )

Definition at line 221 of file writers.c.

222{
223 ast_debug(1, "Loading Writers\n");
227
228 return 0;
229}
@ cdrel_format_json
Definition cdrel.h:49
@ cdrel_format_dsv
Definition cdrel.h:48
@ cdrel_format_sql
Definition cdrel.h:50
cdrel_backend_writer cdrel_backend_writers[cdrel_format_type_end]
#define ast_debug(level,...)
Log a DEBUG message.
static int dsv_writer(struct cdrel_config *config, struct cdrel_values *values)
Definition writers.c:108
static int json_writer(struct cdrel_config *config, struct cdrel_values *values)
Definition writers.c:135
static int database_writer(struct cdrel_config *config, struct cdrel_values *values)
Definition writers.c:216

References ast_debug, cdrel_backend_writers, cdrel_format_dsv, cdrel_format_json, cdrel_format_sql, database_writer(), dsv_writer(), and json_writer().

Referenced by load_module().

◆ write_record_to_database()

int write_record_to_database ( struct cdrel_config config,
struct cdrel_values values 
)

Definition at line 169 of file writers.c.

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}
#define ast_log
Definition astobj2.c:42
#define RECORD_TYPE_STR(_rt)
Definition cdrel.h:42
#define LOG_ERROR
#define ast_mutex_unlock(a)
Definition lock.h:197
#define ast_mutex_lock(a)
Definition lock.h:196

References ast_assert, ast_debug, ast_log, ast_mutex_lock, ast_mutex_unlock, AST_VECTOR_GET, AST_VECTOR_SIZE, cdrel_type_string, config, LOG_ERROR, RECORD_TYPE_STR, value, and cdrel_value::values.

Referenced by database_writer(), and log_legacy_database_record().

◆ write_record_to_file()

int write_record_to_file ( struct cdrel_config config,
struct ast_str record 
)

Definition at line 50 of file writers.c.

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 }
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}
int errno
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
FILE * out
Definition utils/frame.c:33

References ast_log, ast_mutex_lock, ast_mutex_unlock, ast_str_buffer(), config, errno, LOG_ERROR, out, and RECORD_TYPE_STR.

Referenced by dsv_writer(), json_writer(), and log_legacy_dsv_record().