| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "cdrel.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
AST_THREADSTORAGE(custom_buf); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 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 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 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 |
|