2010-03-26 01:33:40 +00:00
|
|
|
/* Debugging/Logging support code */
|
|
|
|
|
|
|
|
/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
* (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/* \addtogroup logging
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* \file logging.c */
|
|
|
|
|
2010-03-26 15:51:31 +00:00
|
|
|
#include "../config.h"
|
|
|
|
|
2010-03-26 01:33:40 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2011-03-09 12:05:08 +00:00
|
|
|
#include <ctype.h>
|
2010-03-26 15:51:31 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_STRINGS_H
|
2010-03-26 01:33:40 +00:00
|
|
|
#include <strings.h>
|
2010-03-26 15:51:31 +00:00
|
|
|
#endif
|
2010-03-26 01:33:40 +00:00
|
|
|
#include <time.h>
|
2015-03-17 09:21:15 +00:00
|
|
|
#include <sys/time.h>
|
2010-03-26 01:33:40 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/utils.h>
|
|
|
|
#include <osmocom/core/logging.h>
|
2010-03-26 01:33:40 +00:00
|
|
|
|
2011-03-09 12:05:08 +00:00
|
|
|
#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
|
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
struct log_info *osmo_log_info;
|
2010-03-26 01:33:40 +00:00
|
|
|
|
2010-03-26 13:24:24 +00:00
|
|
|
static struct log_context log_context;
|
|
|
|
static void *tall_log_ctx = NULL;
|
2011-02-18 19:37:04 +00:00
|
|
|
LLIST_HEAD(osmo_log_target_list);
|
2010-03-26 01:33:40 +00:00
|
|
|
|
2011-03-09 12:05:08 +00:00
|
|
|
#define LOGLEVEL_DEFS 6 /* Number of loglevels.*/
|
|
|
|
|
|
|
|
static const struct value_string loglevel_strs[LOGLEVEL_DEFS+1] = {
|
2013-03-19 08:55:28 +00:00
|
|
|
{ 0, "EVERYTHING" },
|
2010-03-26 01:33:40 +00:00
|
|
|
{ LOGL_DEBUG, "DEBUG" },
|
|
|
|
{ LOGL_INFO, "INFO" },
|
|
|
|
{ LOGL_NOTICE, "NOTICE" },
|
|
|
|
{ LOGL_ERROR, "ERROR" },
|
|
|
|
{ LOGL_FATAL, "FATAL" },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
#define INT2IDX(x) (-1*(x)-1)
|
|
|
|
static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
|
|
|
|
[INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
|
|
|
|
.name = "DLGLOBAL",
|
|
|
|
.description = "Library-internal global log family",
|
|
|
|
.loglevel = LOGL_NOTICE,
|
|
|
|
.enabled = 1,
|
|
|
|
},
|
2011-09-26 09:22:21 +00:00
|
|
|
[INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
|
|
|
|
.name = "DLLAPD",
|
|
|
|
.description = "LAPD in libosmogsm",
|
2011-06-27 08:51:37 +00:00
|
|
|
.loglevel = LOGL_NOTICE,
|
|
|
|
.enabled = 1,
|
|
|
|
},
|
2011-07-19 12:31:44 +00:00
|
|
|
[INT2IDX(DLINP)] = {
|
2011-07-29 09:43:39 +00:00
|
|
|
.name = "DLINP",
|
2011-07-07 17:46:38 +00:00
|
|
|
.description = "A-bis Intput Subsystem",
|
|
|
|
.loglevel = LOGL_NOTICE,
|
|
|
|
.enabled = 1,
|
|
|
|
},
|
2011-07-19 12:31:44 +00:00
|
|
|
[INT2IDX(DLMUX)] = {
|
2011-07-29 09:43:39 +00:00
|
|
|
.name = "DLMUX",
|
2011-07-07 17:46:38 +00:00
|
|
|
.description = "A-bis B-Subchannel TRAU Frame Multiplex",
|
|
|
|
.loglevel = LOGL_NOTICE,
|
|
|
|
.enabled = 1,
|
|
|
|
},
|
2011-07-19 12:31:44 +00:00
|
|
|
[INT2IDX(DLMI)] = {
|
2011-07-29 09:43:39 +00:00
|
|
|
.name = "DLMI",
|
2011-07-07 17:46:38 +00:00
|
|
|
.description = "A-bis Input Driver for Signalling",
|
|
|
|
.enabled = 0, .loglevel = LOGL_NOTICE,
|
|
|
|
},
|
2011-07-19 12:31:44 +00:00
|
|
|
[INT2IDX(DLMIB)] = {
|
2011-07-29 09:43:39 +00:00
|
|
|
.name = "DLMIB",
|
2011-07-07 17:46:38 +00:00
|
|
|
.description = "A-bis Input Driver for B-Channels (voice)",
|
|
|
|
.enabled = 0, .loglevel = LOGL_NOTICE,
|
|
|
|
},
|
2011-10-28 01:53:50 +00:00
|
|
|
[INT2IDX(DLSMS)] = {
|
|
|
|
.name = "DLSMS",
|
|
|
|
.description = "Layer3 Short Message Service (SMS)",
|
|
|
|
.enabled = 1, .loglevel = LOGL_NOTICE,
|
|
|
|
.color = "\033[1;38m",
|
|
|
|
},
|
2014-08-20 17:58:13 +00:00
|
|
|
[INT2IDX(DLCTRL)] = {
|
|
|
|
.name = "DLCTRL",
|
|
|
|
.description = "Control Interface",
|
|
|
|
.enabled = 1, .loglevel = LOGL_NOTICE,
|
|
|
|
},
|
2014-12-04 13:35:21 +00:00
|
|
|
[INT2IDX(DLGTP)] = {
|
|
|
|
.name = "DLGTP",
|
|
|
|
.description = "GPRS GTP library",
|
|
|
|
.enabled = 1, .loglevel = LOGL_NOTICE,
|
|
|
|
},
|
2015-11-02 14:17:50 +00:00
|
|
|
[INT2IDX(DLSTATS)] = {
|
|
|
|
.name = "DLSTATS",
|
|
|
|
.description = "Statistics messages and logging",
|
|
|
|
.enabled = 1, .loglevel = LOGL_NOTICE,
|
|
|
|
},
|
2011-06-27 08:29:17 +00:00
|
|
|
};
|
|
|
|
|
2012-12-06 20:25:27 +00:00
|
|
|
/*! \brief descriptive string for each log level */
|
2011-03-09 12:05:08 +00:00
|
|
|
/* You have to keep this in sync with the structure loglevel_strs. */
|
|
|
|
const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = {
|
2014-12-28 17:27:38 +00:00
|
|
|
"Don't use. It doesn't log anything",
|
2011-03-09 12:05:08 +00:00
|
|
|
"Log debug messages and higher levels",
|
|
|
|
"Log informational messages and higher levels",
|
|
|
|
"Log noticable messages and higher levels",
|
|
|
|
"Log error messages and higher levels",
|
|
|
|
"Log only fatal messages",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
/* special magic for negative (library-internal) log subsystem numbers */
|
|
|
|
static int subsys_lib2index(int subsys)
|
|
|
|
{
|
|
|
|
return (subsys * -1) + (osmo_log_info->num_cat_user-1);
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Parse a human-readable log level into a numeric value */
|
2010-03-26 13:24:24 +00:00
|
|
|
int log_parse_level(const char *lvl)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
return get_string_value(loglevel_strs, lvl);
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief convert a numeric log level into human-readable string */
|
2010-05-11 09:19:40 +00:00
|
|
|
const char *log_level_str(unsigned int lvl)
|
|
|
|
{
|
|
|
|
return get_value_string(loglevel_strs, lvl);
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief parse a human-readable log category into numeric form
|
|
|
|
* \param[in] category human-readable log category name
|
|
|
|
* \returns numeric category value, or -EINVAL otherwise
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
int log_parse_category(const char *category)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2010-05-19 17:54:00 +00:00
|
|
|
for (i = 0; i < osmo_log_info->num_cat; ++i) {
|
2011-06-27 08:29:17 +00:00
|
|
|
if (osmo_log_info->cat[i].name == NULL)
|
|
|
|
continue;
|
2010-05-19 17:54:00 +00:00
|
|
|
if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
|
2010-03-26 13:05:43 +00:00
|
|
|
return i;
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief parse the log category mask
|
|
|
|
* \param[in] target log target to be configured
|
|
|
|
* \param[in] _mask log category mask string
|
|
|
|
*
|
2010-03-26 01:33:40 +00:00
|
|
|
* The format can be this: category1:category2:category3
|
|
|
|
* or category1,2:category2,3:...
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_parse_category_mask(struct log_target* target, const char *_mask)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char *mask = strdup(_mask);
|
|
|
|
char *category_token = NULL;
|
|
|
|
|
|
|
|
/* Disable everything to enable it afterwards */
|
2011-06-27 08:29:17 +00:00
|
|
|
for (i = 0; i < osmo_log_info->num_cat; ++i)
|
2010-03-26 01:33:40 +00:00
|
|
|
target->categories[i].enabled = 0;
|
|
|
|
|
|
|
|
category_token = strtok(mask, ":");
|
|
|
|
do {
|
2010-05-19 17:54:00 +00:00
|
|
|
for (i = 0; i < osmo_log_info->num_cat; ++i) {
|
2012-09-21 15:44:58 +00:00
|
|
|
size_t length, cat_length;
|
2010-03-26 01:33:40 +00:00
|
|
|
char* colon = strstr(category_token, ",");
|
2012-09-21 15:44:58 +00:00
|
|
|
|
|
|
|
if (!osmo_log_info->cat[i].name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
length = strlen(category_token);
|
|
|
|
cat_length = strlen(osmo_log_info->cat[i].name);
|
2011-08-11 11:24:18 +00:00
|
|
|
|
|
|
|
/* Use longest length not to match subocurrences. */
|
|
|
|
if (cat_length > length)
|
|
|
|
length = cat_length;
|
2010-03-26 01:33:40 +00:00
|
|
|
|
|
|
|
if (colon)
|
|
|
|
length = colon - category_token;
|
|
|
|
|
2010-05-19 17:54:00 +00:00
|
|
|
if (strncasecmp(osmo_log_info->cat[i].name,
|
|
|
|
category_token, length) == 0) {
|
2010-03-26 01:33:40 +00:00
|
|
|
int level = 0;
|
|
|
|
|
|
|
|
if (colon)
|
|
|
|
level = atoi(colon+1);
|
|
|
|
|
2010-03-26 13:05:43 +00:00
|
|
|
target->categories[i].enabled = 1;
|
|
|
|
target->categories[i].loglevel = level;
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((category_token = strtok(NULL, ":")));
|
|
|
|
|
|
|
|
free(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* color(int subsys)
|
|
|
|
{
|
2010-05-19 17:54:00 +00:00
|
|
|
if (subsys < osmo_log_info->num_cat)
|
|
|
|
return osmo_log_info->cat[subsys].color;
|
2010-03-26 01:33:40 +00:00
|
|
|
|
2010-03-26 01:45:03 +00:00
|
|
|
return NULL;
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 08:35:30 +00:00
|
|
|
static const char* category_name(int subsys)
|
|
|
|
{
|
|
|
|
if (subsys < osmo_log_info->num_cat)
|
|
|
|
return osmo_log_info->cat[subsys].name;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-26 13:24:24 +00:00
|
|
|
static void _output(struct log_target *target, unsigned int subsys,
|
2012-07-12 07:26:25 +00:00
|
|
|
unsigned int level, const char *file, int line, int cont,
|
2011-02-17 14:52:39 +00:00
|
|
|
const char *format, va_list ap)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
char buf[4096];
|
2011-05-03 20:32:43 +00:00
|
|
|
int ret, len = 0, offset = 0, rem = sizeof(buf);
|
2010-03-26 01:33:40 +00:00
|
|
|
|
|
|
|
/* are we using color */
|
|
|
|
if (target->use_color) {
|
2010-03-26 01:45:03 +00:00
|
|
|
const char *c = color(subsys);
|
|
|
|
if (c) {
|
2014-12-05 09:29:45 +00:00
|
|
|
ret = snprintf(buf + offset, rem, "%s", c);
|
2011-05-03 20:32:43 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2010-03-26 01:45:03 +00:00
|
|
|
}
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
if (!cont) {
|
2014-12-05 08:35:30 +00:00
|
|
|
if (target->print_ext_timestamp) {
|
|
|
|
struct tm tm;
|
2015-03-17 09:21:15 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
localtime_r(&tv.tv_sec, &tm);
|
|
|
|
ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
|
2014-12-05 08:35:30 +00:00
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
2015-03-17 09:21:15 +00:00
|
|
|
tm.tm_hour, tm.tm_min, tm.tm_sec,
|
|
|
|
(int)(tv.tv_usec / 1000));
|
2014-12-05 08:35:30 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
|
|
|
} else if (target->print_timestamp) {
|
2010-03-26 01:33:40 +00:00
|
|
|
char *timestr;
|
|
|
|
time_t tm;
|
|
|
|
tm = time(NULL);
|
|
|
|
timestr = ctime(&tm);
|
|
|
|
timestr[strlen(timestr)-1] = '\0';
|
2011-05-03 20:32:43 +00:00
|
|
|
ret = snprintf(buf + offset, rem, "%s ", timestr);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
2014-12-05 08:35:30 +00:00
|
|
|
if (target->print_category) {
|
|
|
|
ret = snprintf(buf + offset, rem, "%s ", category_name(subsys));
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
|
|
|
}
|
2012-09-11 09:24:51 +00:00
|
|
|
if (target->print_filename) {
|
|
|
|
ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ",
|
|
|
|
subsys, file, line);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
|
|
|
}
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
2011-05-03 20:32:43 +00:00
|
|
|
ret = vsnprintf(buf + offset, rem, format, ap);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
|
|
|
|
|
|
|
ret = snprintf(buf + offset, rem, "%s",
|
|
|
|
target->use_color ? "\033[0;m" : "");
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
|
|
|
err:
|
|
|
|
buf[sizeof(buf)-1] = '\0';
|
|
|
|
target->output(target, level, buf);
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 13:45:16 +00:00
|
|
|
static inline int map_subsys(int subsys)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2011-06-27 08:29:17 +00:00
|
|
|
if (subsys < 0)
|
|
|
|
subsys = subsys_lib2index(subsys);
|
|
|
|
|
|
|
|
if (subsys > osmo_log_info->num_cat)
|
|
|
|
subsys = DLGLOBAL;
|
2015-12-21 13:45:16 +00:00
|
|
|
return subsys;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int check_log_to_target(struct log_target *tar, int subsys, int level)
|
|
|
|
{
|
|
|
|
struct log_category *category;
|
|
|
|
|
|
|
|
category = &tar->categories[subsys];
|
|
|
|
|
|
|
|
/* subsystem is not supposed to be logged */
|
|
|
|
if (!category->enabled)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check the global log level */
|
|
|
|
if (tar->loglevel != 0 && level < tar->loglevel)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check the category log level */
|
|
|
|
if (tar->loglevel == 0 && category->loglevel != 0 &&
|
|
|
|
level < category->loglevel)
|
|
|
|
return 0;
|
|
|
|
|
2016-01-15 15:49:06 +00:00
|
|
|
/* Apply filters here... if that becomes messy we will
|
|
|
|
* need to put filters in a list and each filter will
|
|
|
|
* say stop, continue, output */
|
|
|
|
if ((tar->filter_map & LOG_FILTER_ALL) != 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (osmo_log_info->filter_fn)
|
|
|
|
return osmo_log_info->filter_fn(&log_context, tar);
|
|
|
|
|
2015-12-21 13:45:16 +00:00
|
|
|
/* TODO: Check the filter/selector too? */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief vararg version of logging function */
|
|
|
|
void osmo_vlogp(int subsys, int level, const char *file, int line,
|
|
|
|
int cont, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
struct log_target *tar;
|
|
|
|
|
|
|
|
subsys = map_subsys(subsys);
|
2011-06-27 08:29:17 +00:00
|
|
|
|
2011-02-18 19:37:04 +00:00
|
|
|
llist_for_each_entry(tar, &osmo_log_target_list, entry) {
|
2010-03-26 01:33:40 +00:00
|
|
|
int output = 0;
|
logging: fix corrupted output
Harald reported a problem in the logging:
http://lists.osmocom.org/pipermail/openbsc/2011-May/002896.html
Reverting 81e9636454294ae10ef9bc8bf149dd0248afce76 seems to
fix the problem. However, that workaround looks ugly.
Holger gives us another clue on what was wrong:
http://lists.osmocom.org/pipermail/openbsc/2011-May/002905.html
While digging in the manpage, I found this:
"The functions vprintf(), vfprintf(), vsprintf(), vsnprintf()
are equivalent to the functions printf(), fprintf(), sprintf(),
snprintf(), respectively, except that they are called with a
va_list instead of a variable number of arguments. These functions
do not call the va_end macro. Consequently, the value of ap is
undefined after the call. The application should call va_end(ap)
itself afterwards."
2011-05-18 23:40:43 +00:00
|
|
|
va_list bp;
|
2010-03-26 01:33:40 +00:00
|
|
|
|
2015-12-21 13:45:16 +00:00
|
|
|
if (!check_log_to_target(tar, subsys, level))
|
2010-03-26 01:33:40 +00:00
|
|
|
continue;
|
|
|
|
|
logging: fix corrupted output
Harald reported a problem in the logging:
http://lists.osmocom.org/pipermail/openbsc/2011-May/002896.html
Reverting 81e9636454294ae10ef9bc8bf149dd0248afce76 seems to
fix the problem. However, that workaround looks ugly.
Holger gives us another clue on what was wrong:
http://lists.osmocom.org/pipermail/openbsc/2011-May/002905.html
While digging in the manpage, I found this:
"The functions vprintf(), vfprintf(), vsprintf(), vsnprintf()
are equivalent to the functions printf(), fprintf(), sprintf(),
snprintf(), respectively, except that they are called with a
va_list instead of a variable number of arguments. These functions
do not call the va_end macro. Consequently, the value of ap is
undefined after the call. The application should call va_end(ap)
itself afterwards."
2011-05-18 23:40:43 +00:00
|
|
|
/* According to the manpage, vsnprintf leaves the value of ap
|
|
|
|
* in undefined state. Since _output uses vsnprintf and it may
|
|
|
|
* be called several times, we have to pass a copy of ap. */
|
|
|
|
va_copy(bp, ap);
|
2011-07-02 19:51:32 +00:00
|
|
|
_output(tar, subsys, level, file, line, cont, format, bp);
|
logging: fix corrupted output
Harald reported a problem in the logging:
http://lists.osmocom.org/pipermail/openbsc/2011-May/002896.html
Reverting 81e9636454294ae10ef9bc8bf149dd0248afce76 seems to
fix the problem. However, that workaround looks ugly.
Holger gives us another clue on what was wrong:
http://lists.osmocom.org/pipermail/openbsc/2011-May/002905.html
While digging in the manpage, I found this:
"The functions vprintf(), vfprintf(), vsprintf(), vsnprintf()
are equivalent to the functions printf(), fprintf(), sprintf(),
snprintf(), respectively, except that they are called with a
va_list instead of a variable number of arguments. These functions
do not call the va_end macro. Consequently, the value of ap is
undefined after the call. The application should call va_end(ap)
itself afterwards."
2011-05-18 23:40:43 +00:00
|
|
|
va_end(bp);
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 20:25:27 +00:00
|
|
|
/*! \brief logging function used by DEBUGP() macro */
|
2012-07-12 07:26:25 +00:00
|
|
|
void logp(int subsys, const char *file, int line, int cont,
|
2010-03-26 13:24:24 +00:00
|
|
|
const char *format, ...)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2011-08-27 12:33:19 +00:00
|
|
|
osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
|
2010-03-26 01:33:40 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2012-12-06 20:25:27 +00:00
|
|
|
/*! \brief logging function used by LOGP() macro */
|
2012-07-12 07:26:25 +00:00
|
|
|
void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2011-08-27 12:33:19 +00:00
|
|
|
osmo_vlogp(subsys, level, file, line, cont, format, ap);
|
2010-03-26 01:33:40 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Register a new log target with the logging core
|
|
|
|
* \param[in] target Log target to be registered
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_add_target(struct log_target *target)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2011-02-18 19:37:04 +00:00
|
|
|
llist_add_tail(&target->entry, &osmo_log_target_list);
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Unregister a log target from the logging core
|
|
|
|
* \param[in] target Log target to be unregistered
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_del_target(struct log_target *target)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
llist_del(&target->entry);
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Reset (clear) the logging context */
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_reset_context(void)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2010-03-26 13:24:24 +00:00
|
|
|
memset(&log_context, 0, sizeof(log_context));
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Set the logging context
|
|
|
|
* \param[in] ctx_nr logging context number
|
|
|
|
* \param[in] value value to which the context is to be set
|
|
|
|
*
|
|
|
|
* A logging context is something like the subscriber identity to which
|
|
|
|
* the currently processed message relates, or the BTS through which it
|
|
|
|
* was received. As soon as this data is known, it can be set using
|
|
|
|
* this function. The main use of context information is for logging
|
|
|
|
* filters.
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
int log_set_context(uint8_t ctx_nr, void *value)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2010-03-26 13:24:24 +00:00
|
|
|
if (ctx_nr > LOG_MAX_CTX)
|
2010-03-26 01:33:40 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2010-03-26 13:24:24 +00:00
|
|
|
log_context.ctx[ctx_nr] = value;
|
2010-03-26 01:33:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Enable the \ref LOG_FILTER_ALL log filter
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] all enable (1) or disable (0) the ALL filter
|
|
|
|
*
|
|
|
|
* When the \ref LOG_FILTER_ALL filter is enabled, all log messages will
|
|
|
|
* be printed. It acts as a wildcard. Setting it to \a 1 means there
|
|
|
|
* is no filtering.
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_set_all_filter(struct log_target *target, int all)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
if (all)
|
2010-03-26 13:24:24 +00:00
|
|
|
target->filter_map |= LOG_FILTER_ALL;
|
2010-03-26 01:33:40 +00:00
|
|
|
else
|
2010-03-26 13:24:24 +00:00
|
|
|
target->filter_map &= ~LOG_FILTER_ALL;
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Enable or disable the use of colored output
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] use_color Use color (1) or don't use color (0)
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_set_use_color(struct log_target *target, int use_color)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
target->use_color = use_color;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Enable or disable printing of timestamps while logging
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] print_timestamp Enable (1) or disable (0) timestamps
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_set_print_timestamp(struct log_target *target, int print_timestamp)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
target->print_timestamp = print_timestamp;
|
|
|
|
}
|
|
|
|
|
2014-12-05 08:35:30 +00:00
|
|
|
/*! \brief Enable or disable printing of extended timestamps while logging
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] print_timestamp Enable (1) or disable (0) timestamps
|
|
|
|
*
|
|
|
|
* When both timestamp and extended timestamp is enabled then only
|
|
|
|
* the extended timestamp will be used. The format of the timestamp
|
|
|
|
* is YYYYMMDDhhmmssnnn.
|
|
|
|
*/
|
|
|
|
void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
|
|
|
|
{
|
|
|
|
target->print_ext_timestamp = print_timestamp;
|
|
|
|
}
|
|
|
|
|
2012-09-11 09:24:51 +00:00
|
|
|
/*! \brief Enable or disable printing of the filename while logging
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] print_filename Enable (1) or disable (0) filenames
|
|
|
|
*/
|
|
|
|
void log_set_print_filename(struct log_target *target, int print_filename)
|
|
|
|
{
|
|
|
|
target->print_filename = print_filename;
|
|
|
|
}
|
|
|
|
|
2014-12-05 08:35:30 +00:00
|
|
|
/*! \brief Enable or disable printing of the category name
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] print_catname Enable (1) or disable (0) filenames
|
|
|
|
*
|
|
|
|
* Print the category/subsys name in front of every log message.
|
|
|
|
*/
|
|
|
|
void log_set_print_category(struct log_target *target, int print_category)
|
|
|
|
{
|
|
|
|
target->print_category = print_category;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Set the global log level for a given log target
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] log_level New global log level
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_set_log_level(struct log_target *target, int log_level)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
|
|
|
target->loglevel = log_level;
|
|
|
|
}
|
|
|
|
|
2012-12-06 20:25:27 +00:00
|
|
|
/*! \brief Set a category filter on a given log target
|
|
|
|
* \param[in] target Log target to be affected
|
|
|
|
* \param[in] category Log category to be affected
|
|
|
|
* \param[in] enable whether to enable or disable the filter
|
|
|
|
* \param[in] level Log level of the filter
|
|
|
|
*/
|
2010-03-26 13:24:24 +00:00
|
|
|
void log_set_category_filter(struct log_target *target, int category,
|
2010-03-26 01:33:40 +00:00
|
|
|
int enable, int level)
|
|
|
|
{
|
2010-05-19 17:54:00 +00:00
|
|
|
if (category >= osmo_log_info->num_cat)
|
2010-03-26 01:33:40 +00:00
|
|
|
return;
|
|
|
|
target->categories[category].enabled = !!enable;
|
|
|
|
target->categories[category].loglevel = level;
|
|
|
|
}
|
|
|
|
|
2011-02-17 14:52:39 +00:00
|
|
|
static void _file_output(struct log_target *target, unsigned int level,
|
|
|
|
const char *log)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2010-08-25 12:55:44 +00:00
|
|
|
fprintf(target->tgt_file.out, "%s", log);
|
|
|
|
fflush(target->tgt_file.out);
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Create a new log target skeleton */
|
2010-03-26 13:24:24 +00:00
|
|
|
struct log_target *log_target_create(void)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2010-03-26 13:24:24 +00:00
|
|
|
struct log_target *target;
|
2010-03-26 14:04:03 +00:00
|
|
|
unsigned int i;
|
2010-03-26 01:33:40 +00:00
|
|
|
|
2010-03-26 13:24:24 +00:00
|
|
|
target = talloc_zero(tall_log_ctx, struct log_target);
|
2010-03-26 01:33:40 +00:00
|
|
|
if (!target)
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
target->categories = talloc_zero_array(target,
|
|
|
|
struct log_category,
|
|
|
|
osmo_log_info->num_cat);
|
|
|
|
if (!target->categories) {
|
|
|
|
talloc_free(target);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-26 01:33:40 +00:00
|
|
|
INIT_LLIST_HEAD(&target->entry);
|
2010-03-26 14:04:03 +00:00
|
|
|
|
|
|
|
/* initialize the per-category enabled/loglevel from defaults */
|
2010-05-19 17:54:00 +00:00
|
|
|
for (i = 0; i < osmo_log_info->num_cat; i++) {
|
2010-03-26 14:04:03 +00:00
|
|
|
struct log_category *cat = &target->categories[i];
|
2010-05-19 17:54:00 +00:00
|
|
|
cat->enabled = osmo_log_info->cat[i].enabled;
|
|
|
|
cat->loglevel = osmo_log_info->cat[i].loglevel;
|
2010-03-26 14:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* global settings */
|
2010-03-26 01:33:40 +00:00
|
|
|
target->use_color = 1;
|
|
|
|
target->print_timestamp = 0;
|
2012-09-11 09:24:51 +00:00
|
|
|
target->print_filename = 1;
|
2010-03-26 14:04:03 +00:00
|
|
|
|
|
|
|
/* global log level */
|
2010-03-26 01:33:40 +00:00
|
|
|
target->loglevel = 0;
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Create the STDERR log target */
|
2010-03-26 13:24:24 +00:00
|
|
|
struct log_target *log_target_create_stderr(void)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2010-03-26 16:04:40 +00:00
|
|
|
/* since C89/C99 says stderr is a macro, we can safely do this! */
|
|
|
|
#ifdef stderr
|
2010-03-26 13:24:24 +00:00
|
|
|
struct log_target *target;
|
2010-03-26 01:33:40 +00:00
|
|
|
|
2010-03-26 13:24:24 +00:00
|
|
|
target = log_target_create();
|
2010-03-26 01:33:40 +00:00
|
|
|
if (!target)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-18 19:37:04 +00:00
|
|
|
target->type = LOG_TGT_TYPE_STDERR;
|
2010-08-25 12:55:44 +00:00
|
|
|
target->tgt_file.out = stderr;
|
|
|
|
target->output = _file_output;
|
2010-03-26 01:33:40 +00:00
|
|
|
return target;
|
2010-03-26 16:04:40 +00:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif /* stderr */
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Create a new file-based log target
|
|
|
|
* \param[in] fname File name of the new log file
|
|
|
|
* \returns Log target in case of success, NULL otherwise
|
|
|
|
*/
|
2010-08-25 17:10:50 +00:00
|
|
|
struct log_target *log_target_create_file(const char *fname)
|
|
|
|
{
|
|
|
|
struct log_target *target;
|
|
|
|
|
|
|
|
target = log_target_create();
|
|
|
|
if (!target)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-18 19:37:04 +00:00
|
|
|
target->type = LOG_TGT_TYPE_FILE;
|
2010-08-25 17:10:50 +00:00
|
|
|
target->tgt_file.out = fopen(fname, "a");
|
|
|
|
if (!target->tgt_file.out)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
target->output = _file_output;
|
|
|
|
|
|
|
|
target->tgt_file.fname = talloc_strdup(target, fname);
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Find a registered log target
|
|
|
|
* \param[in] type Log target type
|
|
|
|
* \param[in] fname File name
|
|
|
|
* \returns Log target (if found), NULL otherwise
|
|
|
|
*/
|
2011-02-18 19:37:04 +00:00
|
|
|
struct log_target *log_target_find(int type, const char *fname)
|
|
|
|
{
|
|
|
|
struct log_target *tgt;
|
|
|
|
|
|
|
|
llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
|
|
|
|
if (tgt->type != type)
|
|
|
|
continue;
|
|
|
|
if (tgt->type == LOG_TGT_TYPE_FILE) {
|
|
|
|
if (!strcmp(fname, tgt->tgt_file.fname))
|
|
|
|
return tgt;
|
|
|
|
} else
|
|
|
|
return tgt;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Unregister, close and delete a log target */
|
2010-08-25 17:10:50 +00:00
|
|
|
void log_target_destroy(struct log_target *target)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* just in case, to make sure we don't have any references */
|
|
|
|
log_del_target(target);
|
|
|
|
|
|
|
|
if (target->output == &_file_output) {
|
2010-09-17 12:38:17 +00:00
|
|
|
/* since C89/C99 says stderr is a macro, we can safely do this! */
|
|
|
|
#ifdef stderr
|
2010-08-25 17:10:50 +00:00
|
|
|
/* don't close stderr */
|
2010-09-17 12:38:17 +00:00
|
|
|
if (target->tgt_file.out != stderr)
|
|
|
|
#endif
|
|
|
|
{
|
2010-08-25 17:10:50 +00:00
|
|
|
fclose(target->tgt_file.out);
|
|
|
|
target->tgt_file.out = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(target);
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief close and re-open a log file (for log file rotation) */
|
2010-08-25 17:10:50 +00:00
|
|
|
int log_target_file_reopen(struct log_target *target)
|
|
|
|
{
|
|
|
|
fclose(target->tgt_file.out);
|
|
|
|
|
|
|
|
target->tgt_file.out = fopen(target->tgt_file.fname, "a");
|
|
|
|
if (!target->tgt_file.out)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
/* we assume target->output already to be set */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-18 18:01:40 +00:00
|
|
|
/*! \brief close and re-open a log file (for log file rotation) */
|
|
|
|
int log_targets_reopen(void)
|
|
|
|
{
|
|
|
|
struct log_target *tar;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
llist_for_each_entry(tar, &osmo_log_target_list, entry) {
|
|
|
|
switch (tar->type) {
|
|
|
|
case LOG_TGT_TYPE_FILE:
|
|
|
|
if (log_target_file_reopen(tar) < 0)
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Generates the logging command string for VTY
|
|
|
|
* \param[in] unused_info Deprecated parameter, no longer used!
|
|
|
|
*/
|
2011-06-27 12:19:16 +00:00
|
|
|
const char *log_vty_command_string(const struct log_info *unused_info)
|
2010-05-11 14:39:22 +00:00
|
|
|
{
|
2011-06-27 12:19:16 +00:00
|
|
|
struct log_info *info = osmo_log_info;
|
2011-03-09 12:05:08 +00:00
|
|
|
int len = 0, offset = 0, ret, i, rem;
|
|
|
|
int size = strlen("logging level () ()") + 1;
|
2010-05-11 14:39:22 +00:00
|
|
|
char *str;
|
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
for (i = 0; i < info->num_cat; i++) {
|
|
|
|
if (info->cat[i].name == NULL)
|
|
|
|
continue;
|
2011-03-09 12:05:08 +00:00
|
|
|
size += strlen(info->cat[i].name) + 1;
|
2011-06-27 08:29:17 +00:00
|
|
|
}
|
2010-05-11 14:39:22 +00:00
|
|
|
|
2011-03-09 12:05:08 +00:00
|
|
|
for (i = 0; i < LOGLEVEL_DEFS; i++)
|
|
|
|
size += strlen(loglevel_strs[i].str) + 1;
|
|
|
|
|
|
|
|
rem = size;
|
2011-05-03 20:32:42 +00:00
|
|
|
str = talloc_zero_size(tall_log_ctx, size);
|
2010-05-11 14:39:22 +00:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2011-03-29 15:03:56 +00:00
|
|
|
ret = snprintf(str + offset, rem, "logging level (all|");
|
2011-03-09 12:05:08 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2011-03-28 17:24:22 +00:00
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2011-03-09 12:05:08 +00:00
|
|
|
|
|
|
|
for (i = 0; i < info->num_cat; i++) {
|
2011-06-27 08:29:17 +00:00
|
|
|
if (info->cat[i].name) {
|
|
|
|
int j, name_len = strlen(info->cat[i].name)+1;
|
|
|
|
char name[name_len];
|
2010-05-11 14:39:22 +00:00
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
for (j = 0; j < name_len; j++)
|
|
|
|
name[j] = tolower(info->cat[i].name[j]);
|
2011-03-09 12:05:08 +00:00
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
name[name_len-1] = '\0';
|
|
|
|
ret = snprintf(str + offset, rem, "%s|", name+1);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
|
|
|
}
|
2011-03-09 12:05:08 +00:00
|
|
|
}
|
|
|
|
offset--; /* to remove the trailing | */
|
|
|
|
rem++;
|
|
|
|
|
|
|
|
ret = snprintf(str + offset, rem, ") (");
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2011-03-28 17:24:22 +00:00
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2011-03-09 12:05:08 +00:00
|
|
|
|
|
|
|
for (i = 0; i < LOGLEVEL_DEFS; i++) {
|
|
|
|
int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
|
|
|
|
char loglevel_str[loglevel_str_len];
|
|
|
|
|
|
|
|
for (j = 0; j < loglevel_str_len; j++)
|
|
|
|
loglevel_str[j] = tolower(loglevel_strs[i].str[j]);
|
|
|
|
|
|
|
|
loglevel_str[loglevel_str_len-1] = '\0';
|
|
|
|
ret = snprintf(str + offset, rem, "%s|", loglevel_str);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2011-03-28 17:24:22 +00:00
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2011-03-09 12:05:08 +00:00
|
|
|
}
|
|
|
|
offset--; /* to remove the trailing | */
|
|
|
|
rem++;
|
|
|
|
|
|
|
|
ret = snprintf(str + offset, rem, ")");
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2011-03-28 17:24:22 +00:00
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2011-03-09 12:05:08 +00:00
|
|
|
err:
|
2011-05-03 20:32:48 +00:00
|
|
|
str[size-1] = '\0';
|
2010-05-11 14:39:22 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Generates the logging command description for VTY
|
|
|
|
* \param[in] unused_info Deprecated parameter, no longer used!
|
|
|
|
*/
|
2011-06-27 12:19:16 +00:00
|
|
|
const char *log_vty_command_description(const struct log_info *unused_info)
|
2010-05-11 14:39:22 +00:00
|
|
|
{
|
2011-06-27 12:19:16 +00:00
|
|
|
struct log_info *info = osmo_log_info;
|
2010-05-11 14:39:22 +00:00
|
|
|
char *str;
|
2011-03-09 12:05:08 +00:00
|
|
|
int i, ret, len = 0, offset = 0, rem;
|
|
|
|
unsigned int size =
|
|
|
|
strlen(LOGGING_STR
|
|
|
|
"Set the log level for a specified category\n") + 1;
|
2010-05-11 14:39:22 +00:00
|
|
|
|
2011-06-27 08:29:17 +00:00
|
|
|
for (i = 0; i < info->num_cat; i++) {
|
|
|
|
if (info->cat[i].name == NULL)
|
|
|
|
continue;
|
2011-03-09 12:05:08 +00:00
|
|
|
size += strlen(info->cat[i].description) + 1;
|
2011-06-27 08:29:17 +00:00
|
|
|
}
|
2010-05-11 14:39:22 +00:00
|
|
|
|
2011-03-09 12:05:08 +00:00
|
|
|
for (i = 0; i < LOGLEVEL_DEFS; i++)
|
|
|
|
size += strlen(loglevel_descriptions[i]) + 1;
|
|
|
|
|
2011-05-03 20:32:32 +00:00
|
|
|
size += strlen("Global setting for all subsystems") + 1;
|
2011-03-09 12:05:08 +00:00
|
|
|
rem = size;
|
2011-05-03 20:32:42 +00:00
|
|
|
str = talloc_zero_size(tall_log_ctx, size);
|
2010-05-11 14:39:22 +00:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2011-03-09 12:05:08 +00:00
|
|
|
ret = snprintf(str + offset, rem, LOGGING_STR
|
|
|
|
"Set the log level for a specified category\n");
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2011-03-28 17:24:22 +00:00
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2011-03-09 12:05:08 +00:00
|
|
|
|
2011-05-03 20:32:32 +00:00
|
|
|
ret = snprintf(str + offset, rem,
|
|
|
|
"Global setting for all subsystems\n");
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
|
|
|
|
2010-05-11 14:39:22 +00:00
|
|
|
for (i = 0; i < info->num_cat; i++) {
|
2011-06-27 08:29:17 +00:00
|
|
|
if (info->cat[i].name == NULL)
|
|
|
|
continue;
|
2011-03-09 12:05:08 +00:00
|
|
|
ret = snprintf(str + offset, rem, "%s\n",
|
|
|
|
info->cat[i].description);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2011-03-28 17:24:22 +00:00
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2010-05-11 14:39:22 +00:00
|
|
|
}
|
2011-03-09 12:05:08 +00:00
|
|
|
for (i = 0; i < LOGLEVEL_DEFS; i++) {
|
|
|
|
ret = snprintf(str + offset, rem, "%s\n",
|
|
|
|
loglevel_descriptions[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2011-03-28 17:24:22 +00:00
|
|
|
OSMO_SNPRINTF_RET(ret, rem, offset, len);
|
2011-03-09 12:05:08 +00:00
|
|
|
}
|
|
|
|
err:
|
2011-05-03 20:32:48 +00:00
|
|
|
str[size-1] = '\0';
|
2010-05-11 14:39:22 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:14:17 +00:00
|
|
|
/*! \brief Initialize the Osmocom logging core
|
|
|
|
* \param[in] inf Information regarding logging categories
|
|
|
|
* \param[in] ctx \ref talloc context for logging allocations
|
|
|
|
* \returns 0 in case of success, negative in case of error
|
|
|
|
*/
|
2011-06-27 08:29:17 +00:00
|
|
|
int log_init(const struct log_info *inf, void *ctx)
|
2010-03-26 01:33:40 +00:00
|
|
|
{
|
2011-06-27 08:29:17 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
tall_log_ctx = talloc_named_const(ctx, 1, "logging");
|
|
|
|
if (!tall_log_ctx)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
|
|
|
|
if (!osmo_log_info)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-12-29 18:38:01 +00:00
|
|
|
osmo_log_info->filter_fn = inf->filter_fn;
|
2011-06-27 08:29:17 +00:00
|
|
|
osmo_log_info->num_cat_user = inf->num_cat;
|
|
|
|
/* total number = number of user cat + library cat */
|
2011-06-27 12:19:16 +00:00
|
|
|
osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
|
2011-06-27 08:29:17 +00:00
|
|
|
|
|
|
|
osmo_log_info->cat = talloc_zero_array(osmo_log_info,
|
|
|
|
struct log_info_cat,
|
|
|
|
osmo_log_info->num_cat);
|
|
|
|
if (!osmo_log_info->cat) {
|
|
|
|
talloc_free(osmo_log_info);
|
|
|
|
osmo_log_info = NULL;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy over the user part */
|
|
|
|
for (i = 0; i < inf->num_cat; i++) {
|
2012-09-11 08:31:29 +00:00
|
|
|
memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
|
|
|
|
&inf->cat[i],
|
2011-06-27 08:29:17 +00:00
|
|
|
sizeof(struct log_info_cat));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy over the library part */
|
2011-06-27 12:00:03 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
|
2011-06-27 12:19:16 +00:00
|
|
|
unsigned int cn = osmo_log_info->num_cat_user + i;
|
2012-09-11 08:31:29 +00:00
|
|
|
memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
|
2011-06-27 12:00:03 +00:00
|
|
|
&internal_cat[i], sizeof(struct log_info_cat));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-03-26 01:33:40 +00:00
|
|
|
}
|
2011-08-17 12:14:17 +00:00
|
|
|
|
2015-11-17 10:52:24 +00:00
|
|
|
/*! \brief Check whether a log entry will be generated.
|
|
|
|
* \returns != 0 if a log entry might get generated by at least one target */
|
|
|
|
int log_check_level(int subsys, unsigned int level)
|
|
|
|
{
|
|
|
|
struct log_target *tar;
|
|
|
|
|
2015-12-21 13:45:16 +00:00
|
|
|
subsys = map_subsys(subsys);
|
2015-11-17 10:52:24 +00:00
|
|
|
|
|
|
|
/* TODO: The following could/should be cached (update on config) */
|
|
|
|
|
|
|
|
llist_for_each_entry(tar, &osmo_log_target_list, entry) {
|
2015-12-21 13:45:16 +00:00
|
|
|
if (!check_log_to_target(tar, subsys, level))
|
2015-11-17 10:52:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* This might get logged (ignoring filters) */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are sure, that this will not be logged. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|