Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
logger_category.c File Reference
#include "asterisk.h"
#include "asterisk/cli.h"
#include "asterisk/conversions.h"
#include "asterisk/logger_category.h"
#include "asterisk/vector.h"
Include dependency graph for logger_category.c:

Go to the source code of this file.

Data Structures

struct  categories_level_t
 
struct  category_t
 

Functions

char * ast_debug_category_complete (const char *const *argv, int argc, const char *word, int state)
 Add a unique (no duplicates) result to a request for completion for debug categories. More...
 
int ast_debug_category_is_allowed (int sublevel, uintmax_t ids)
 Check if a debug category is enabled, and allowed to output. More...
 
uintmax_t ast_debug_category_register (const char *name)
 Register a debug level logger category. More...
 
int ast_debug_category_set_sublevel (const char *name, int sublevel)
 Set the debug category's sublevel. More...
 
int ast_debug_category_set_sublevels (const char *const *names, size_t size, int default_sublevel)
 Set one or more debug category's sublevel. More...
 
int ast_debug_category_unregister (const char *name)
 Un-register a debug level logger category. More...
 
int ast_logger_category_load (void)
 Load/Initialize system wide logger category functionality. More...
 
int ast_logger_category_unload (void)
 Unload system wide logger category functionality. More...
 
 AST_VECTOR_RW (categories_t, struct category_t *)
 
static int casecmp_by_name (const struct category_t *category, const char *name)
 
static char * category_complete (struct categories_level_t *level, const char *const *argv, int argc, const char *word, int state)
 
static int category_is_allowed (int sublevel, struct categories_level_t *level, uintmax_t ids)
 
static uintmax_t category_register (struct categories_level_t *level, const char *name)
 
static int category_set_sublevel (struct category_t *category, struct categories_level_t *level, const char *name, int sublevel)
 
static int category_set_sublevels (struct categories_level_t *level, const char *const *names, size_t size, int default_sublevel)
 
static int category_unregister (struct categories_level_t *level, const char *name)
 
static int cmp_by_name (const struct category_t *category, const char *name)
 
static uintmax_t get_next_id (struct categories_level_t *level)
 Retrieve the next available id. More...
 

Variables

static struct categories_level_t debug_categories
 

Function Documentation

◆ ast_debug_category_complete()

char* ast_debug_category_complete ( const char *const *  argv,
int  argc,
const char *  word,
int  state 
)

Add a unique (no duplicates) result to a request for completion for debug categories.

Parameters
argvA list of already completed options
argcThe number of already completed options
wordThe word to complete
stateThe state
Return values
0Success, -1 Failure
Since
16.14
17.8
18.0

Definition at line 300 of file logger_category.c.

References category_complete().

Referenced by handle_debug_category().

302 {
303  return category_complete(&debug_categories, argv, argc, word, state);
304 }
static struct categories_level_t debug_categories
short word
static char * category_complete(struct categories_level_t *level, const char *const *argv, int argc, const char *word, int state)

◆ ast_debug_category_is_allowed()

int ast_debug_category_is_allowed ( int  sublevel,
uintmax_t  ids 
)

Check if a debug category is enabled, and allowed to output.

Note
If more than one id is specified then if even one is allowed "true" is returned.
Parameters
sublevelCurrent set sublevel must be this sublevel or less
idsOne or more unique category ids to check
Return values
1if allowed, 0 if not allowed
Since
16.14
17.8
18.0

Definition at line 306 of file logger_category.c.

References category_is_allowed().

307 {
308  return category_is_allowed(sublevel, &debug_categories, ids);
309 }
static int category_is_allowed(int sublevel, struct categories_level_t *level, uintmax_t ids)
static struct categories_level_t debug_categories

◆ ast_debug_category_register()

uintmax_t ast_debug_category_register ( const char *  name)

Register a debug level logger category.

Parameters
nameThe name of the category
idThe unique id of the category
Return values
0if failed to register/retrieve an id. Otherwise it returns the id for the registered category.
Since
16.14
17.8
18.0

Definition at line 279 of file logger_category.c.

References category_register().

Referenced by ast_rtp_engine_init(), and ast_stun_init().

280 {
282 }
static struct categories_level_t debug_categories
static const char name[]
Definition: cdr_mysql.c:74
static uintmax_t category_register(struct categories_level_t *level, const char *name)

◆ ast_debug_category_set_sublevel()

int ast_debug_category_set_sublevel ( const char *  name,
int  sublevel 
)

Set the debug category's sublevel.

Statements are output at a specified sublevel. Typically any number greater than or equal to 0. Other acceptable values include AST_LOG_CATEGORY_ENABLED and AST_LOG_CATEGORY_DISABLED.

Parameters
nameThe name of the category
sublevelThe debug sublevel output number
Return values
0Success, -1 Failure
Since
16.14
17.8
18.0

Definition at line 289 of file logger_category.c.

References category_set_sublevel(), and NULL.

Referenced by handle_cli_rtcp_set_debug(), handle_cli_rtp_set_debug(), handle_cli_stun_set_debug(), rtcp_do_debug_ip(), and rtp_do_debug_ip().

290 {
291  return category_set_sublevel(NULL, &debug_categories, name, sublevel);
292 }
#define NULL
Definition: resample.c:96
static struct categories_level_t debug_categories
static const char name[]
Definition: cdr_mysql.c:74
static int category_set_sublevel(struct category_t *category, struct categories_level_t *level, const char *name, int sublevel)

◆ ast_debug_category_set_sublevels()

int ast_debug_category_set_sublevels ( const char *const *  names,
size_t  size,
int  default_sublevel 
)

Set one or more debug category's sublevel.

Accepts an array of category names, and optional associated sublevels. Sublevels can be associated with a name by using a ':' as a separator. For example:

<category name>="">:<category sublevel>="">

The given default sublevel is used if no sublevel is associated with a name.

Parameters
namesAn array of category names
sizeThe size of the array (number of elements)
default_sublevelThe sublevel value to use if one is not associated with a name
Return values
0Success, -1 Failure
Since
16.14
17.8
18.0

Definition at line 294 of file logger_category.c.

References category_set_sublevels().

Referenced by handle_debug_category().

296 {
297  return category_set_sublevels(&debug_categories, names, size, default_sublevel);
298 }
static int category_set_sublevels(struct categories_level_t *level, const char *const *names, size_t size, int default_sublevel)
static struct categories_level_t debug_categories

◆ ast_debug_category_unregister()

int ast_debug_category_unregister ( const char *  name)

Un-register a debug level logger category.

Return values
0Success, -1 Failure
Since
16.14
17.8
18.0

Definition at line 284 of file logger_category.c.

References category_unregister().

Referenced by rtp_engine_shutdown(), and stun_shutdown().

285 {
287 }
static struct categories_level_t debug_categories
static const char name[]
Definition: cdr_mysql.c:74
static int category_unregister(struct categories_level_t *level, const char *name)

◆ ast_logger_category_load()

int ast_logger_category_load ( void  )

Load/Initialize system wide logger category functionality.

Return values
0Success, -1 Failure
Since
16.14
17.8
18.0

Definition at line 317 of file logger_category.c.

References AST_VECTOR_RW_INIT, and categories_level_t::categories.

Referenced by init_logger().

318 {
320  return -1;
321  }
322 
323  return 0;
324 }
#define AST_VECTOR_RW_INIT(vec, size)
Initialize a vector with a read/write lock.
Definition: vector.h:158
struct categories_t categories
static struct categories_level_t debug_categories

◆ ast_logger_category_unload()

int ast_logger_category_unload ( void  )

Unload system wide logger category functionality.

Return values
0Success, -1 Failure
Since
16.14
17.8
18.0

Definition at line 311 of file logger_category.c.

References AST_VECTOR_RW_FREE, and categories_level_t::categories.

Referenced by close_logger().

312 {
314  return 0;
315 }
struct categories_t categories
#define AST_VECTOR_RW_FREE(vec)
Deallocates this locked vector.
Definition: vector.h:202
static struct categories_level_t debug_categories

◆ AST_VECTOR_RW()

AST_VECTOR_RW ( categories_t  ,
struct category_t  
)

◆ casecmp_by_name()

static int casecmp_by_name ( const struct category_t category,
const char *  name 
)
static

Definition at line 113 of file logger_category.c.

References category_t::name.

Referenced by category_set_sublevel().

114 {
115  return !strcasecmp(category->name, name);
116 }
static const char name[]
Definition: cdr_mysql.c:74
char name[0]

◆ category_complete()

static char* category_complete ( struct categories_level_t level,
const char *const *  argv,
int  argc,
const char *  word,
int  state 
)
static

Definition at line 196 of file logger_category.c.

References ast_cli_completion_add(), ast_strdup, AST_VECTOR_GET, AST_VECTOR_RW_RDLOCK, AST_VECTOR_RW_UNLOCK, AST_VECTOR_SIZE, categories_level_t::categories, category_t::name, and NULL.

Referenced by ast_debug_category_complete().

198 {
199  int wordlen = strlen(word);
200  int which = 0;
201  int i, j;
202 
204 
205  if (argc == AST_VECTOR_SIZE(&level->categories)) {
207  return NULL;
208  }
209 
210  for (i = 0; i < AST_VECTOR_SIZE(&level->categories); ++i) {
211  struct category_t *category = AST_VECTOR_GET(&level->categories, i);
212 
213  if (!strncasecmp(word, category->name, wordlen) && (++which > state)) {
214  /* Check to see if one is already been included */
215  for (j = 0; j < argc; ++j) {
216  if (!strncasecmp(category->name, argv[j], strlen(category->name))) {
217  break;
218  }
219  }
220 
221  if (j != argc) {
222  continue;
223  }
224 
225  if (state != -1) {
227  return ast_strdup(category->name);
228  }
229 
230  if (ast_cli_completion_add(ast_strdup(category->name))) {
232  return NULL;
233  }
234  }
235  }
236 
238  return NULL;
239 }
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
#define AST_VECTOR_RW_RDLOCK(vec)
Obtain read lock on vector.
Definition: vector.h:880
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
struct categories_t categories
#define NULL
Definition: resample.c:96
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
char name[0]
int ast_cli_completion_add(char *value)
Add a result to a request for completion options.
Definition: main/cli.c:2726
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611
short word

◆ category_is_allowed()

static int category_is_allowed ( int  sublevel,
struct categories_level_t level,
uintmax_t  ids 
)
static

Definition at line 241 of file logger_category.c.

References AST_LOG_CATEGORY_DISABLED, AST_LOG_CATEGORY_ENABLED, AST_VECTOR_GET, AST_VECTOR_RW_RDLOCK, AST_VECTOR_RW_UNLOCK, AST_VECTOR_SIZE, categories_level_t::categories, category_t::id, categories_level_t::state, and category_t::sublevel.

Referenced by ast_debug_category_is_allowed().

242 {
243  /* Check level, and potentially allow but only if there is a matching state enabled */
244  if (ids & level->state) {
245  int i;
246 
247  if (sublevel == AST_LOG_CATEGORY_ENABLED || sublevel == 0) {
248  /* If at least one id is enabled then always allow these sublevels */
249  return 1;
250  }
251 
253  for (i = 0; i < AST_VECTOR_SIZE(&level->categories); ++i) {
254  struct category_t *category = AST_VECTOR_GET(&level->categories, i);
255 
256  /*
257  * If there is at least one matching category, and that category is enabled
258  * or its sub-level is at or above the given sub-level then allow.
259  */
260  if ((category->id & ids) && category->sublevel != AST_LOG_CATEGORY_DISABLED &&
261  (category->sublevel == AST_LOG_CATEGORY_ENABLED || category->sublevel >= sublevel)) {
263  return 1;
264  }
265  }
267  }
268 
269  return 0;
270 }
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
#define AST_VECTOR_RW_RDLOCK(vec)
Obtain read lock on vector.
Definition: vector.h:880
struct categories_t categories
uintmax_t id
#define AST_LOG_CATEGORY_DISABLED
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
#define AST_LOG_CATEGORY_ENABLED
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

◆ category_register()

static uintmax_t category_register ( struct categories_level_t level,
const char *  name 
)
static

Definition at line 66 of file logger_category.c.

References ast_calloc, ast_log, AST_LOG_CATEGORY_DISABLED, AST_VECTOR_APPEND, AST_VECTOR_GET_INDEX, AST_VECTOR_RW_UNLOCK, AST_VECTOR_RW_WRLOCK, categories_level_t::categories, cmp_by_name(), get_next_id(), category_t::id, LOG_ERROR, category_t::name, and category_t::sublevel.

Referenced by ast_debug_category_register().

67 {
68  int i;
69  struct category_t *category;
70 
72 
74  if (i >= 0) {
76  ast_log(LOG_ERROR, "Cannot register logger category '%s'. "
77  "Name already used for type.\n", name);
78  return 0;
79  }
80 
81  category = ast_calloc(1, sizeof(*category) + strlen(name) + 1);
82  if (!category) {
84  return 0;
85  }
86 
87  category->id = get_next_id(level);
89  strcpy(category->name, name); /* Safe */
90 
91  if (AST_VECTOR_APPEND(&level->categories, category)) {
93  ast_log(LOG_ERROR, "Cannot register logger category '%s'. "
94  "Unable to append.\n", name);
95  return 0;
96  }
97 
99  return category->id;
100 }
#define AST_VECTOR_GET_INDEX(vec, value, cmp)
Get the 1st index from a vector that matches the given comparison.
Definition: vector.h:721
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
static int cmp_by_name(const struct category_t *category, const char *name)
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
struct categories_t categories
#define ast_log
Definition: astobj2.c:42
uintmax_t id
#define LOG_ERROR
Definition: logger.h:285
#define AST_LOG_CATEGORY_DISABLED
static const char name[]
Definition: cdr_mysql.c:74
#define AST_VECTOR_RW_WRLOCK(vec)
Obtain write lock on vector.
Definition: vector.h:890
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
char name[0]
static uintmax_t get_next_id(struct categories_level_t *level)
Retrieve the next available id.

◆ category_set_sublevel()

static int category_set_sublevel ( struct category_t category,
struct categories_level_t level,
const char *  name,
int  sublevel 
)
static

Definition at line 118 of file logger_category.c.

References AST_LOG_CATEGORY_DISABLED, AST_VECTOR_GET_CMP, AST_VECTOR_RW_UNLOCK, AST_VECTOR_RW_WRLOCK, casecmp_by_name(), categories_level_t::categories, category_t::id, categories_level_t::state, and category_t::sublevel.

Referenced by ast_debug_category_set_sublevel(), and category_set_sublevels().

120 {
121  int locked = 0;
122 
123  if (!category) {
124  struct category_t **obj;
125 
126  if (!name) {
127  return -1;
128  }
129 
130  locked = !AST_VECTOR_RW_WRLOCK(&level->categories);
131  if (!locked) {
132  return -1;
133  }
134 
136  if (!obj) {
138  return -1;
139  }
140 
141  category = *obj;
142  }
143 
144  category->sublevel = sublevel;
145 
146  if (category->sublevel == AST_LOG_CATEGORY_DISABLED) {
147  level->state &= ~category->id;
148  } else {
149  level->state |= category->id;
150  }
151 
152  if (locked) {
154  }
155 
156  return 0;
157 }
static int casecmp_by_name(const struct category_t *category, const char *name)
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
struct categories_t categories
uintmax_t id
#define AST_LOG_CATEGORY_DISABLED
static const char name[]
Definition: cdr_mysql.c:74
#define AST_VECTOR_RW_WRLOCK(vec)
Obtain write lock on vector.
Definition: vector.h:890
#define AST_VECTOR_GET_CMP(vec, value, cmp)
Get an element from a vector that matches the given comparison.
Definition: vector.h:733

◆ category_set_sublevels()

static int category_set_sublevels ( struct categories_level_t level,
const char *const *  names,
size_t  size,
int  default_sublevel 
)
static

Definition at line 159 of file logger_category.c.

References ast_copy_string(), ast_str_to_int(), AST_VECTOR_CALLBACK_VOID, AST_VECTOR_RW_UNLOCK, AST_VECTOR_RW_WRLOCK, categories_level_t::categories, category_set_sublevel(), len(), category_t::name, NULL, categories_level_t::state, and category_t::sublevel.

Referenced by ast_debug_category_set_sublevels().

161 {
162  int i;
163 
164  if (!names || !size) {
165  level->state = default_sublevel;
168  level, NULL, default_sublevel);
170  return 0;
171  }
172 
173  for (i = 0; i < size; ++i) {
174  const char *sublevel;
175  int num = default_sublevel;
176 
177  sublevel = strchr(names[i], ':');
178  if (sublevel) {
179  size_t len = ++sublevel - names[i];
180  char name[len];
181 
182  if (*sublevel && ast_str_to_int(sublevel, &num)) {
183  continue;
184  }
185 
186  ast_copy_string(name, names[i], len);
187  category_set_sublevel(NULL, level, name, num);
188  } else {
189  category_set_sublevel(NULL, level, names[i], num);
190  }
191  }
192 
193  return 0;
194 }
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
struct categories_t categories
#define NULL
Definition: resample.c:96
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static const char name[]
Definition: cdr_mysql.c:74
#define AST_VECTOR_RW_WRLOCK(vec)
Obtain write lock on vector.
Definition: vector.h:890
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static int category_set_sublevel(struct category_t *category, struct categories_level_t *level, const char *name, int sublevel)
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:865

◆ category_unregister()

static int category_unregister ( struct categories_level_t level,
const char *  name 
)
static

Definition at line 102 of file logger_category.c.

References ast_free, AST_VECTOR_REMOVE_CMP_UNORDERED, AST_VECTOR_RW_UNLOCK, AST_VECTOR_RW_WRLOCK, categories_level_t::categories, and cmp_by_name().

Referenced by ast_debug_category_unregister().

103 {
104  int res;
105 
109 
110  return res;
111 }
#define AST_VECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup)
Remove an element from a vector that matches the given comparison.
Definition: vector.h:488
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
static int cmp_by_name(const struct category_t *category, const char *name)
struct categories_t categories
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
#define AST_VECTOR_RW_WRLOCK(vec)
Obtain write lock on vector.
Definition: vector.h:890

◆ cmp_by_name()

static int cmp_by_name ( const struct category_t category,
const char *  name 
)
static

Definition at line 61 of file logger_category.c.

References category_t::name.

Referenced by category_register(), and category_unregister().

62 {
63  return !strcmp(category->name, name);
64 }
static const char name[]
Definition: cdr_mysql.c:74
char name[0]

◆ get_next_id()

static uintmax_t get_next_id ( struct categories_level_t level)
static

Retrieve the next available id.

Ids must be a power of 2. This allows for fast lookup, and "or'ing" of ids in order to permit multiple categories in a log statement.

Definition at line 47 of file logger_category.c.

References categories_level_t::id_pool.

Referenced by category_register().

48 {
49  if (level->id_pool == 0) {
50  level->id_pool = 1;
51  } else if (level->id_pool >= (UINTMAX_MAX / 2)) {
52  /* No more ids left*/
53  return 0;
54  } else {
55  level->id_pool <<= 1;
56  }
57 
58  return level->id_pool;
59 }

Variable Documentation

◆ debug_categories

struct categories_level_t debug_categories
static

Definition at line 272 of file logger_category.c.