blob: b5be6f10387f95a0b222b2011889aa3149a3d06c [file] [log] [blame]
Harald Weltebdf1b352019-05-17 10:21:45 +02001/* Integration of libosmocore into our bare-iron embedded target */
2
3#include <string.h>
4#include <stdio.h>
5#include <osmocom/core/utils.h>
6#include <osmocom/core/msgb.h>
7
8
9extern void *g_tall_ctx;
10void *g_msgb_ctx;
11
12/***********************************************************************
13 * Logging
14 ***********************************************************************/
15
16#include "logging.h"
17#include <osmocom/core/logging.h>
18
19static const struct log_info_cat log_info_cat[] = {
20 [DUSB] = {
21 .name = "USB",
22 .description = "USB Transport",
23 .enabled = 1,
24 .loglevel = LOGL_NOTICE,
25 },
26 [DCCID] = {
27 .name = "CCID",
28 .description = "CCID Core",
29 .color = "\033[1;35m",
30 .enabled = 1,
31 .loglevel = LOGL_DEBUG,
32 },
33};
34
35static const struct log_info log_info = {
36 .cat = log_info_cat,
37 .num_cat = ARRAY_SIZE(log_info_cat),
38};
39
40/* call-back function to the logging framework. We use this instead of the libosmocore
41 * provided target, as libosmocore wants to put 4kB on the stack. */
42static void stderr_raw_output(struct log_target *target, int subsys, unsigned int level,
43 const char *file, int line, int cont, const char *format, va_list ap)
44{
45 if (!cont) {
46 /* TODO: Timestamp? */
47 if (target->print_category)
48 fprintf(stderr, "%s ", log_category_name(subsys));
49
50 if (target->print_level)
51 fprintf(stderr, "%s ", log_level_str(level));
52
53 if (target->print_category_hex)
54 fprintf(stderr, "<%4.4x> ", subsys);
55
56 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
57 const char *bn;
58 switch (target->print_filename2) {
59 case LOG_FILENAME_NONE:
60 break;
61 case LOG_FILENAME_PATH:
62 fprintf(stderr, "%s:%d ", file, line);
63 break;
64 case LOG_FILENAME_BASENAME:
65 bn = strrchr(file, '/');
66 if (!bn || !bn[1])
67 bn = file;
68 else
69 bn++;
70 fprintf(stderr, "%s:%d ", bn, line);
71 break;
72 }
73 }
74 }
75 vfprintf(stderr, format, ap);
76 /* TODO: LOG_FILENAME_POS_LINE_END; we cannot modify the format string here :/ */
77 int fmt_len = strlen(format);
78 if (fmt_len && format[fmt_len-1] == '\n')
79 fputc('\r', stderr);
80}
81
82static struct log_target *log_target_create_stderr_raw(void)
83{
84 struct log_target *target;
85
86 target = log_target_create();
87 if (!target)
88 return NULL;
89
90 target->type = LOG_TGT_TYPE_STDERR;
91 target->raw_output = stderr_raw_output;
92 target->print_category = true;
93 target->print_level = true;
94
95 return target;
96}
97
98void libosmo_emb_init(void)
99{
100 struct log_target *stderr_target;
101
102 /* msgb */
103 g_msgb_ctx = talloc_pool(g_tall_ctx, 20480);
104 talloc_set_memlimit(g_msgb_ctx, 20480);
105 msgb_talloc_ctx_init(g_msgb_ctx, 0);
106
107 /* logging */
108 log_init(&log_info, g_tall_ctx);
109 stderr_target = log_target_create_stderr_raw();
110 log_add_target(stderr_target);
111 log_set_all_filter(stderr_target, 1);
112}