logging: rename tgt_stdout to tgt_file
[osmocom-bb.git] / src / logging.c
index 7c50877..fe782a6 100644 (file)
@@ -37,7 +37,7 @@
 #include <osmocore/utils.h>
 #include <osmocore/logging.h>
 
-static const struct log_info *log_info;
+const struct log_info *osmo_log_info;
 
 static struct log_context log_context;
 static void *tall_log_ctx = NULL;
@@ -58,12 +58,17 @@ int log_parse_level(const char *lvl)
        return get_string_value(loglevel_strs, lvl);
 }
 
+const char *log_level_str(unsigned int lvl)
+{
+       return get_value_string(loglevel_strs, lvl);
+}
+
 int log_parse_category(const char *category)
 {
        int i;
 
-       for (i = 0; i < log_info->num_cat; ++i) {
-               if (!strcasecmp(log_info->cat[i].name+1, category))
+       for (i = 0; i < osmo_log_info->num_cat; ++i) {
+               if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
                        return i;
        }
 
@@ -87,15 +92,15 @@ void log_parse_category_mask(struct log_target* target, const char *_mask)
 
        category_token = strtok(mask, ":");
        do {
-               for (i = 0; i < log_info->num_cat; ++i) {
+               for (i = 0; i < osmo_log_info->num_cat; ++i) {
                        char* colon = strstr(category_token, ",");
                        int length = strlen(category_token);
 
                        if (colon)
                            length = colon - category_token;
 
-                       if (strncasecmp(log_info->cat[i].name, category_token,
-                                       length) == 0) {
+                       if (strncasecmp(osmo_log_info->cat[i].name,
+                                       category_token, length) == 0) {
                                int level = 0;
 
                                if (colon)
@@ -112,8 +117,8 @@ void log_parse_category_mask(struct log_target* target, const char *_mask)
 
 static const char* color(int subsys)
 {
-       if (subsys < log_info->num_cat)
-               return log_info->cat[subsys].color;
+       if (subsys < osmo_log_info->num_cat)
+               return osmo_log_info->cat[subsys].color;
 
        return NULL;
 }
@@ -159,7 +164,8 @@ static void _output(struct log_target *target, unsigned int subsys,
                sub[sizeof(sub)-1] = '\0';
        }
 
-       snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
+       snprintf(final, sizeof(final), "%s%s%s%s%s", col, tim, sub, buf,
+                target->use_color ? "\033[0;m" : "");
        final[sizeof(final)-1] = '\0';
        target->output(target, final);
 }
@@ -193,8 +199,8 @@ static void _logp(unsigned int subsys, int level, char *file, int line,
                 * say stop, continue, output */
                if ((tar->filter_map & LOG_FILTER_ALL) != 0)
                        output = 1;
-               else if (log_info->filter_fn)
-                       output = log_info->filter_fn(&log_context,
+               else if (osmo_log_info->filter_fn)
+                       output = osmo_log_info->filter_fn(&log_context,
                                                       tar);
 
                if (output) {
@@ -231,25 +237,6 @@ void logp2(unsigned int subsys, unsigned int level, char *file, int line, int co
        va_end(ap);
 }
 
-static char hexd_buff[4096];
-
-char *hexdump(const unsigned char *buf, int len)
-{
-       int i;
-       char *cur = hexd_buff;
-
-       hexd_buff[0] = 0;
-       for (i = 0; i < len; i++) {
-               int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
-               int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
-               if (rc <= 0)
-                       break;
-               cur += rc;
-       }
-       hexd_buff[sizeof(hexd_buff)-1] = 0;
-       return hexd_buff;
-}
-
 void log_add_target(struct log_target *target)
 {
        llist_add_tail(&target->entry, &target_list);
@@ -301,7 +288,7 @@ void log_set_log_level(struct log_target *target, int log_level)
 void log_set_category_filter(struct log_target *target, int category,
                               int enable, int level)
 {
-       if (category >= log_info->num_cat)
+       if (category >= osmo_log_info->num_cat)
                return;
        target->categories[category].enabled = !!enable;
        target->categories[category].loglevel = level;
@@ -309,10 +296,10 @@ void log_set_category_filter(struct log_target *target, int category,
 
 /* since C89/C99 says stderr is a macro, we can safely do this! */
 #ifdef stderr
-static void _stderr_output(struct log_target *target, const char *log)
+static void _file_output(struct log_target *target, const char *log)
 {
-       fprintf(target->tgt_stdout.out, "%s", log);
-       fflush(target->tgt_stdout.out);
+       fprintf(target->tgt_file.out, "%s", log);
+       fflush(target->tgt_file.out);
 }
 #endif
 
@@ -328,10 +315,10 @@ struct log_target *log_target_create(void)
        INIT_LLIST_HEAD(&target->entry);
 
        /* initialize the per-category enabled/loglevel from defaults */
-       for (i = 0; i < log_info->num_cat; i++) {
+       for (i = 0; i < osmo_log_info->num_cat; i++) {
                struct log_category *cat = &target->categories[i];
-               cat->enabled = log_info->cat[i].enabled;
-               cat->loglevel = log_info->cat[i].loglevel;
+               cat->enabled = osmo_log_info->cat[i].enabled;
+               cat->loglevel = osmo_log_info->cat[i].loglevel;
        }
 
        /* global settings */
@@ -353,16 +340,62 @@ struct log_target *log_target_create_stderr(void)
        if (!target)
                return NULL;
 
-       target->tgt_stdout.out = stderr;
-       target->output = _stderr_output;
+       target->tgt_file.out = stderr;
+       target->output = _file_output;
        return target;
 #else
        return NULL;
 #endif /* stderr */
 }
 
+const char *log_vty_level_string(struct log_info *info)
+{
+       const struct value_string *vs;
+       unsigned int len = 3; /* ()\0 */
+       char *str;
+
+       for (vs = loglevel_strs; vs->value || vs->str; vs++)
+               len += strlen(vs->str) + 1;
+
+       str = talloc_zero_size(NULL, len);
+       if (!str)
+               return NULL;
+
+       str[0] = '(';
+       for (vs = loglevel_strs; vs->value || vs->str; vs++) {
+               strcat(str, vs->str);
+               strcat(str, "|");
+       }
+       str[strlen(str)-1] = ')';
+
+       return str;
+}
+
+const char *log_vty_category_string(struct log_info *info)
+{
+       unsigned int len = 3;   /* "()\0" */
+       unsigned int i;
+       char *str;
+
+       for (i = 0; i < info->num_cat; i++)
+               len += strlen(info->cat[i].name) + 1;
+
+       str = talloc_zero_size(NULL, len);
+       if (!str)
+               return NULL;
+
+       str[0] = '(';
+       for (i = 0; i < info->num_cat; i++) {
+               strcat(str, info->cat[i].name+1);
+               strcat(str, "|");
+       }
+       str[strlen(str)-1] = ')';
+
+       return str;
+}
+
 void log_init(const struct log_info *cat)
 {
        tall_log_ctx = talloc_named_const(NULL, 1, "logging");
-       log_info = cat;
+       osmo_log_info = cat;
 }