Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables
cdr_custom.c File Reference

Custom Comma Separated Value CDR records. More...

#include "asterisk.h"
#include <time.h>
#include "asterisk/paths.h"
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/threadstorage.h"
#include "asterisk/strings.h"
Include dependency graph for cdr_custom.c:

Go to the source code of this file.

Data Structures

struct  cdr_custom_config
 
struct  sinks
 

Macros

#define CONFIG   "cdr_custom.conf"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_THREADSTORAGE_CUSTOM_SCOPE (custom_buf, NULL, ast_free_ptr, static)
 
static int custom_log (struct ast_cdr *cdr)
 
static void free_config (void)
 
static int load_config (void)
 
static enum ast_module_load_result load_module (void)
 
static int reload (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable Comma Separated Values CDR Backend" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, .requires = "cdr", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const char name [] = "cdr-custom"
 
static struct sinks sinks = AST_RWLIST_HEAD_INIT_VALUE
 

Detailed Description

Custom Comma Separated Value CDR records.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Logs in LOG_DIR/cdr_custom

Definition in file cdr_custom.c.

Macro Definition Documentation

◆ CONFIG

#define CONFIG   "cdr_custom.conf"

Definition at line 61 of file cdr_custom.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 238 of file cdr_custom.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 238 of file cdr_custom.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 238 of file cdr_custom.c.

◆ AST_THREADSTORAGE_CUSTOM_SCOPE()

AST_THREADSTORAGE_CUSTOM_SCOPE ( custom_buf  ,
NULL  ,
ast_free_ptr  ,
static   
)

◆ custom_log()

static int custom_log ( struct ast_cdr cdr)
static

Definition at line 132 of file cdr_custom.c.

133{
134 struct ast_channel *dummy;
135 struct ast_str *str;
137
138 /* Batching saves memory management here. Otherwise, it's the same as doing an allocation and free each time. */
139 if (!(str = ast_str_thread_get(&custom_buf, 16))) {
140 return -1;
141 }
142
144 if (!dummy) {
145 ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
146 return -1;
147 }
148
149 /* We need to dup here since the cdr actually belongs to the other channel,
150 so when we release this channel we don't want the CDR getting cleaned
151 up prematurely. */
153
155
157 FILE *out;
158
160
161 /* Even though we have a lock on the list, we could be being chased by
162 another thread and this lock ensures that we won't step on anyone's
163 toes. Once each CDR backend gets it's own thread, this lock can be
164 removed. */
165 ast_mutex_lock(&config->lock);
166
167 /* Because of the absolutely unconditional need for the
168 highest reliability possible in writing billing records,
169 we open write and close the log file each time */
170 if ((out = fopen(config->filename, "a"))) {
171 fputs(ast_str_buffer(str), out);
172 fflush(out); /* be particularly anal here */
173 fclose(out);
174 } else {
175 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
176 }
177
178 ast_mutex_unlock(&config->lock);
179 }
180
182
184
185 return 0;
186}
const char * str
Definition app_jack.c:150
#define ast_log
Definition astobj2.c:42
struct ast_cdr * ast_cdr_dup(struct ast_cdr *cdr)
Duplicate a public CDR.
Definition cdr.c:3131
static const char config[]
static void dummy(char *unused,...)
#define ast_channel_unref(c)
Decrease channel reference count.
Definition channel.h:3018
void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
#define ast_dummy_channel_alloc()
Create a fake channel structure.
Definition channel.h:1328
#define LOG_ERROR
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition linkedlists.h:78
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define ast_mutex_unlock(a)
Definition lock.h:197
#define ast_mutex_lock(a)
Definition lock.h:196
int errno
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
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
Main Channel structure associated with a channel.
Support for dynamic strings.
Definition strings.h:623
struct cdr_custom_config::@105 list
FILE * out
Definition utils/frame.c:33

References ast_cdr_dup(), ast_channel_cdr_set(), ast_channel_unref, ast_dummy_channel_alloc, AST_LIST_TRAVERSE, ast_log, ast_mutex_lock, ast_mutex_unlock, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_str_buffer(), ast_str_substitute_variables(), ast_str_thread_get(), config, dummy(), errno, cdr_custom_config::list, LOG_ERROR, out, and str.

Referenced by load_module(), and unload_module().

◆ free_config()

static void free_config ( void  )
static

Definition at line 78 of file cdr_custom.c.

79{
80 struct cdr_custom_config *sink;
81
82 while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
83 ast_mutex_destroy(&sink->lock);
85 ast_free(sink);
86 }
87}
#define ast_free(a)
Definition astmm.h:180
#define AST_RWLIST_REMOVE_HEAD
#define ast_mutex_destroy(a)
Definition lock.h:195
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
ast_mutex_t lock
Definition cdr_custom.c:72

References ast_free, ast_mutex_destroy, AST_RWLIST_REMOVE_HEAD, ast_string_field_free_memory, cdr_custom_config::list, and cdr_custom_config::lock.

Referenced by reload(), and unload_module().

◆ load_config()

static int load_config ( void  )
static

Definition at line 89 of file cdr_custom.c.

90{
91 struct ast_config *cfg;
92 struct ast_variable *var;
93 struct ast_flags config_flags = { 0 };
94 int res = 0;
95
96 cfg = ast_config_load(CONFIG, config_flags);
97 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
98 ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
99 return -1;
100 }
101
102 var = ast_variable_browse(cfg, "mappings");
103 while (var) {
104 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
106
107 if (!sink) {
108 ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
109 res = -2;
110 break;
111 }
112
113 ast_string_field_build(sink, format, "%s\n", var->value);
114 if (var->name[0] == '/') {
115 ast_string_field_build(sink, filename, "%s", var->name);
116 } else {
118 }
119 ast_mutex_init(&sink->lock);
120
122 } else {
123 ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
124 }
125 var = var->next;
126 }
128
129 return res;
130}
#define var
Definition ast_expr2f.c:605
static const char name[]
Definition cdr_custom.c:65
#define CONFIG
Definition cdr_custom.c:61
#define ast_config_load(filename, flags)
Load a config file.
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition extconf.c:1287
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition extconf.c:1213
#define LOG_NOTICE
#define AST_RWLIST_INSERT_TAIL
#define ast_mutex_init(pmutex)
Definition lock.h:193
const char * ast_config_AST_LOG_DIR
Definition options.c:160
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
Structure used to handle boolean flags.
Definition utils.h:220
Structure for variables, used for configurations and for channel variables.
const ast_string_field filename
Definition cdr_custom.c:71
const ast_string_field format
Definition cdr_custom.c:71

References ast_calloc_with_stringfields, ast_config_AST_LOG_DIR, ast_config_destroy(), ast_config_load, ast_log, ast_mutex_init, AST_RWLIST_INSERT_TAIL, ast_string_field_build, ast_strlen_zero(), ast_variable_browse(), CONFIG, CONFIG_STATUS_FILEINVALID, cdr_custom_config::filename, cdr_custom_config::format, cdr_custom_config::list, cdr_custom_config::lock, LOG_ERROR, LOG_NOTICE, name, and var.

Referenced by load_module(), and reload().

◆ load_module()

static enum ast_module_load_result load_module ( void  )
static

Definition at line 205 of file cdr_custom.c.

206{
207 if (AST_RWLIST_WRLOCK(&sinks)) {
208 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
210 }
211
212 load_config();
216}
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition cdr.c:3076
static int custom_log(struct ast_cdr *cdr)
Definition cdr_custom.c:132
static int load_config(void)
Definition cdr_custom.c:89
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition linkedlists.h:52
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
const char * description
Definition module.h:366

References ast_cdr_register(), ast_log, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, custom_log(), ast_module_info::description, load_config(), LOG_ERROR, and name.

◆ reload()

static int reload ( void  )
static

Definition at line 218 of file cdr_custom.c.

219{
220 if (AST_RWLIST_WRLOCK(&sinks)) {
221 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
223 }
224
225 free_config();
226 load_config();
229}
static void free_config(void)
Definition cdr_custom.c:78

References ast_log, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, free_config(), load_config(), and LOG_ERROR.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 188 of file cdr_custom.c.

189{
191 return -1;
192 }
193
194 if (AST_RWLIST_WRLOCK(&sinks)) {
196 ast_log(LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
197 return -1;
198 }
199
200 free_config();
202 return 0;
203}
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition cdr.c:3121

References ast_cdr_register(), ast_cdr_unregister(), ast_log, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, custom_log(), ast_module_info::description, free_config(), LOG_ERROR, and name.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable Comma Separated Values CDR Backend" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, .requires = "cdr", }
static

Definition at line 238 of file cdr_custom.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 238 of file cdr_custom.c.

◆ name

const char name[] = "cdr-custom"
static

Definition at line 65 of file cdr_custom.c.

Referenced by load_config(), load_module(), and unload_module().

◆ sinks