Asterisk - The Open Source Telephony Project  GIT-master-1f78ee9
Data Structures | Functions | Variables
cdr_syslog.c File Reference

syslog CDR logger More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/cdr.h"
#include "asterisk/pbx.h"
#include <syslog.h>

Go to the source code of this file.

Data Structures

struct  cdr_syslog_config
 
struct  sinks
 

Functions

static void __init_syslog_buf (void)
 
static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void free_config (void)
 
static int load_config (int reload)
 
static enum ast_module_load_result load_module (void)
 
static int reload (void)
 
static int syslog_log (struct ast_cdr *cdr)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable syslog CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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 CONFIG [] = "cdr_syslog.conf"
 
static const char name [] = "cdr-syslog"
 
static struct sinks sinks = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 
static struct ast_threadstorage syslog_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_syslog_buf , .custom_init = NULL , }
 

Detailed Description

syslog CDR logger

Author
Sean Bright sean@.nosp@m.mall.nosp@m.eable.nosp@m..com

See also

Definition in file cdr_syslog.c.

Function Documentation

◆ __init_syslog_buf()

static void __init_syslog_buf ( void  )
static

Definition at line 58 of file cdr_syslog.c.

62 {

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 296 of file cdr_syslog.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 296 of file cdr_syslog.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 296 of file cdr_syslog.c.

◆ free_config()

static void free_config ( void  )
static

Definition at line 75 of file cdr_syslog.c.

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

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

76 {
77  struct cdr_syslog_config *sink;
78 
79  while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
80  ast_mutex_destroy(&sink->lock);
82  ast_free(sink);
83  }
84 }
struct cdr_syslog_config::@99 list
#define ast_free(a)
Definition: astmm.h:182
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:843
ast_mutex_t lock
Definition: cdr_syslog.c:69
#define ast_mutex_destroy(a)
Definition: lock.h:186
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368

◆ load_config()

static int load_config ( int  reload)
static

Definition at line 134 of file cdr_syslog.c.

References ast_calloc_with_stringfields, ast_category_browse(), ast_config_destroy(), ast_config_load, ast_log, AST_LOG_ERROR, AST_LOG_WARNING, ast_mutex_init, AST_RWLIST_EMPTY, AST_RWLIST_INSERT_TAIL, ast_string_field_set, ast_strlen_zero, ast_syslog_facility(), ast_syslog_facility_name(), ast_syslog_priority(), ast_syslog_priority_name(), ast_variable_retrieve(), CONFIG, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, cdr_syslog_config::facility, cdr_syslog_config::format, free_config(), cdr_syslog_config::ident, cdr_syslog_config::list, cdr_syslog_config::lock, NULL, cdr_syslog_config::priority, and tmp().

Referenced by load_module(), and reload().

135 {
136  struct ast_config *cfg;
137  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
138  int default_facility = LOG_LOCAL4;
139  int default_priority = LOG_INFO;
140  const char *catg = NULL, *tmp;
141 
142  cfg = ast_config_load(CONFIG, config_flags);
145  "Unable to load %s. Not logging custom CSV CDRs to syslog.\n", CONFIG);
146  return -1;
147  } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
148  return 0;
149  }
150 
151  if (reload) {
152  free_config();
153  }
154 
155  if (!(ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "general", "facility")))) {
156  int facility = ast_syslog_facility(tmp);
157  if (facility < 0) {
159  "Invalid facility '%s' specified, defaulting to '%s'\n",
160  tmp, ast_syslog_facility_name(default_facility));
161  } else {
162  default_facility = facility;
163  }
164  }
165 
166  if (!(ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "general", "priority")))) {
167  int priority = ast_syslog_priority(tmp);
168  if (priority < 0) {
170  "Invalid priority '%s' specified, defaulting to '%s'\n",
171  tmp, ast_syslog_priority_name(default_priority));
172  } else {
173  default_priority = priority;
174  }
175  }
176 
177  while ((catg = ast_category_browse(cfg, catg))) {
178  struct cdr_syslog_config *sink;
179 
180  if (!strcasecmp(catg, "general")) {
181  continue;
182  }
183 
184  if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "template"))) {
186  "No 'template' parameter found for '%s'. Skipping.\n", catg);
187  continue;
188  }
189 
190  sink = ast_calloc_with_stringfields(1, struct cdr_syslog_config, 1024);
191 
192  if (!sink) {
194  "Unable to allocate memory for configuration settings.\n");
195  free_config();
196  break;
197  }
198 
199  ast_mutex_init(&sink->lock);
200  ast_string_field_set(sink, ident, catg);
201  ast_string_field_set(sink, format, tmp);
202 
203  if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "facility"))) {
204  sink->facility = default_facility;
205  } else {
206  int facility = ast_syslog_facility(tmp);
207  if (facility < 0) {
209  "Invalid facility '%s' specified for '%s,' defaulting to '%s'\n",
210  tmp, catg, ast_syslog_facility_name(default_facility));
211  } else {
212  sink->facility = facility;
213  }
214  }
215 
216  if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "priority"))) {
217  sink->priority = default_priority;
218  } else {
219  int priority = ast_syslog_priority(tmp);
220  if (priority < 0) {
222  "Invalid priority '%s' specified for '%s,' defaulting to '%s'\n",
223  tmp, catg, ast_syslog_priority_name(default_priority));
224  } else {
225  sink->priority = priority;
226  }
227  }
228 
230  }
231 
232  ast_config_destroy(cfg);
233 
234  return AST_RWLIST_EMPTY(&sinks) ? -1 : 0;
235 }
struct cdr_syslog_config::@99 list
int ast_syslog_priority(const char *priority)
Maps a syslog priority name from a string to a syslog priority constant.
Definition: syslog.c:126
#define CONFIG_STATUS_FILEINVALID
static int tmp()
Definition: bt_open.c:389
#define AST_LOG_WARNING
Definition: logger.h:279
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
Definition: stringfields.h:426
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3328
const char * ast_syslog_priority_name(int priority)
Maps a syslog priority constant to a string.
Definition: syslog.c:139
#define NULL
Definition: resample.c:96
static int reload(void)
Definition: cdr_syslog.c:272
static int priority
static void free_config(void)
Definition: cdr_syslog.c:75
#define ast_log
Definition: astobj2.c:42
#define ast_config_load(filename, flags)
Load a config file.
#define AST_LOG_ERROR
Definition: logger.h:290
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define CONFIG_STATUS_FILEUNCHANGED
#define AST_RWLIST_EMPTY
Definition: linkedlists.h:451
#define ast_strlen_zero(a)
Definition: muted.c:73
Structure used to handle boolean flags.
Definition: utils.h:199
static const char CONFIG[]
Definition: cdr_syslog.c:56
#define CONFIG_STATUS_FILEMISSING
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:694
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:740
ast_mutex_t lock
Definition: cdr_syslog.c:69
int ast_syslog_facility(const char *facility)
Maps a syslog facility name from a string to a syslog facility constant.
Definition: syslog.c:85
const ast_string_field ident
Definition: cdr_syslog.c:66
#define ast_mutex_init(pmutex)
Definition: lock.h:184
static snd_pcm_format_t format
Definition: chan_alsa.c:102
const char * ast_syslog_facility_name(int facility)
Maps a syslog facility constant to a string.
Definition: syslog.c:98
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ load_module()

static enum ast_module_load_result load_module ( void  )
static

Definition at line 254 of file cdr_syslog.c.

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

Referenced by reload().

255 {
256  int res;
257 
258  if (AST_RWLIST_WRLOCK(&sinks)) {
259  ast_log(AST_LOG_ERROR, "Unable to lock sink list. Load failed.\n");
261  }
262 
263  res = load_config(0);
265  if (res) {
267  }
270 }
const char * description
Definition: module.h:352
static int load_config(int reload)
Definition: cdr_syslog.c:134
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static const char name[]
Definition: cdr_syslog.c:60
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
Definition: logger.h:290
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:2943
static int syslog_log(struct ast_cdr *cdr)
Definition: cdr_syslog.c:86
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78

◆ reload()

static int reload ( void  )
static

Definition at line 272 of file cdr_syslog.c.

References ast_log, AST_LOG_ERROR, AST_MODFLAG_LOAD_ORDER, AST_MODPRI_CDR_DRIVER, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ASTERISK_GPL_KEY, free_config(), load_config(), load_module(), and unload_module().

273 {
274  int res;
275  if (AST_RWLIST_WRLOCK(&sinks)) {
276  ast_log(AST_LOG_ERROR, "Unable to lock sink list. Load failed.\n");
278  }
279 
280  if ((res = load_config(1))) {
281  free_config();
282  }
283 
285 
287 }
static int load_config(int reload)
Definition: cdr_syslog.c:134
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static void free_config(void)
Definition: cdr_syslog.c:75
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
Definition: logger.h:290
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78

◆ syslog_log()

static int syslog_log ( struct ast_cdr cdr)
static

Definition at line 86 of file cdr_syslog.c.

References ast_cdr_dup(), ast_channel_cdr_set(), ast_channel_unref, ast_dummy_channel_alloc, AST_LIST_TRAVERSE, ast_log, AST_LOG_ERROR, ast_mutex_lock, ast_mutex_unlock, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_str_buffer(), ast_str_substitute_variables(), ast_str_thread_get(), dummy(), cdr_syslog_config::facility, cdr_syslog_config::format, cdr_syslog_config::ident, cdr_syslog_config::list, cdr_syslog_config::lock, cdr_syslog_config::priority, str, and syslog_buf.

Referenced by load_module(), and unload_module().

87 {
88  struct ast_channel *dummy;
89  struct ast_str *str;
90  struct cdr_syslog_config *sink;
91 
92  /* Batching saves memory management here. Otherwise, it's the same as doing an
93  allocation and free each time. */
94  if (!(str = ast_str_thread_get(&syslog_buf, 16))) {
95  return -1;
96  }
97 
98  if (!(dummy = ast_dummy_channel_alloc())) {
99  ast_log(AST_LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
100  return -1;
101  }
102 
103  /* We need to dup here since the cdr actually belongs to the other channel,
104  so when we release this channel we don't want the CDR getting cleaned
105  up prematurely. */
106  ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
107 
109 
110  AST_LIST_TRAVERSE(&sinks, sink, list) {
111 
112  ast_str_substitute_variables(&str, 0, dummy, sink->format);
113 
114  /* Even though we have a lock on the list, we could be being chased by
115  another thread and this lock ensures that we won't step on anyone's
116  toes. Once each CDR backend gets it's own thread, this lock can be
117  removed. */
118  ast_mutex_lock(&sink->lock);
119 
120  openlog(sink->ident, LOG_CONS, sink->facility);
121  syslog(sink->priority, "%s", ast_str_buffer(str));
122  closelog();
123 
124  ast_mutex_unlock(&sink->lock);
125  }
126 
128 
129  ast_channel_unref(dummy);
130 
131  return 0;
132 }
struct cdr_syslog_config::@99 list
Main Channel structure associated with a channel.
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2873
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
static void dummy(char *unused,...)
Definition: chan_unistim.c:220
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
struct ast_cdr * ast_cdr_dup(struct ast_cdr *cdr)
Duplicate a public CDR.
Definition: cdr.c:2998
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
#define ast_mutex_lock(a)
Definition: lock.h:187
const char * str
Definition: app_jack.c:147
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_log
Definition: astobj2.c:42
void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
#define AST_LOG_ERROR
Definition: logger.h:290
#define ast_dummy_channel_alloc()
Create a fake channel structure.
Definition: channel.h:1266
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static struct ast_threadstorage syslog_buf
Definition: cdr_syslog.c:58
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
ast_mutex_t lock
Definition: cdr_syslog.c:69
const ast_string_field ident
Definition: cdr_syslog.c:66
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:861
const ast_string_field format
Definition: cdr_syslog.c:66
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 237 of file cdr_syslog.c.

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

Referenced by reload().

238 {
239  if (ast_cdr_unregister(name)) {
240  return -1;
241  }
242 
243  if (AST_RWLIST_WRLOCK(&sinks)) {
245  ast_log(AST_LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
246  return -1;
247  }
248 
249  free_config();
251  return 0;
252 }
const char * description
Definition: module.h:352
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:2988
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static void free_config(void)
Definition: cdr_syslog.c:75
static const char name[]
Definition: cdr_syslog.c:60
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
Definition: logger.h:290
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:2943
static int syslog_log(struct ast_cdr *cdr)
Definition: cdr_syslog.c:86

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable syslog CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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 296 of file cdr_syslog.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 296 of file cdr_syslog.c.

◆ CONFIG

const char CONFIG[] = "cdr_syslog.conf"
static

Definition at line 56 of file cdr_syslog.c.

Referenced by load_config().

◆ name

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

Definition at line 60 of file cdr_syslog.c.

Referenced by load_module(), and unload_module().

◆ sinks

struct sinks sinks = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
static

◆ syslog_buf

struct ast_threadstorage syslog_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_syslog_buf , .custom_init = NULL , }
static

Definition at line 58 of file cdr_syslog.c.

Referenced by syslog_log().