integrate libosmocore logging into firmware

We do so using our own 'raw' log target to avoid the 4k-on-stack
buffer of libosmocore _output() function.

Change-Id: I7a10b5b2b50bcee0154a1fa3fc43756aec836226
diff --git a/sysmoOCTSIM/gcc/Makefile b/sysmoOCTSIM/gcc/Makefile
index 3001015..640fdff 100644
--- a/sysmoOCTSIM/gcc/Makefile
+++ b/sysmoOCTSIM/gcc/Makefile
@@ -83,6 +83,7 @@
 gcc/gcc/startup_same54.o \
 hal/src/hal_usb_device.o \
 main.o \
+libosmo_emb.o \
 manual_test.o \
 talloc.o \
 usb_descriptors.o \
@@ -136,6 +137,7 @@
 "gcc/gcc/startup_same54.o" \
 "hal/src/hal_usb_device.o" \
 "main.o" \
+"libosmo_emb.o" \
 "manual_test.o" \
 "talloc.o" \
 "usb_descriptors.o" \
@@ -195,6 +197,7 @@
 "hal/src/hal_usart_async_rings.d" \
 "hpl/osc32kctrl/hpl_osc32kctrl.d" \
 "main.d" \
+"libosmo_emb.d" \
 "manual_test.d" \
 "talloc.d" \
 "usb_descriptors.d" \
diff --git a/sysmoOCTSIM/libosmo_emb.c b/sysmoOCTSIM/libosmo_emb.c
new file mode 100644
index 0000000..b5be6f1
--- /dev/null
+++ b/sysmoOCTSIM/libosmo_emb.c
@@ -0,0 +1,112 @@
+/* Integration of libosmocore into our bare-iron embedded target */
+
+#include <string.h>
+#include <stdio.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/msgb.h>
+
+
+extern void *g_tall_ctx;
+void *g_msgb_ctx;
+
+/***********************************************************************
+ * Logging
+ ***********************************************************************/
+
+#include "logging.h"
+#include <osmocom/core/logging.h>
+
+static const struct log_info_cat log_info_cat[] = {
+	[DUSB] = {
+		.name = "USB",
+		.description = "USB Transport",
+		.enabled = 1,
+		.loglevel = LOGL_NOTICE,
+	},
+	[DCCID] = {
+		.name = "CCID",
+		.description = "CCID Core",
+		.color = "\033[1;35m",
+		.enabled = 1,
+		.loglevel = LOGL_DEBUG,
+	},
+};
+
+static const struct log_info log_info = {
+	.cat = log_info_cat,
+	.num_cat = ARRAY_SIZE(log_info_cat),
+};
+
+/* call-back function to the logging framework. We use this instead of the libosmocore
+ * provided target, as libosmocore wants to put 4kB on the stack. */
+static void stderr_raw_output(struct log_target *target, int subsys, unsigned int level,
+			      const char *file, int line, int cont, const char *format, va_list ap)
+{
+	if (!cont) {
+		/* TODO: Timestamp? */
+		if (target->print_category)
+			fprintf(stderr, "%s ", log_category_name(subsys));
+
+		if (target->print_level)
+			fprintf(stderr, "%s ", log_level_str(level));
+
+		if (target->print_category_hex)
+			fprintf(stderr, "<%4.4x> ", subsys);
+
+		if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
+			const char *bn;
+			switch (target->print_filename2) {
+			case LOG_FILENAME_NONE:
+				break;
+			case LOG_FILENAME_PATH:
+				fprintf(stderr, "%s:%d ", file, line);
+				break;
+			case LOG_FILENAME_BASENAME:
+				bn = strrchr(file, '/');
+				if (!bn || !bn[1])
+					bn = file;
+				else
+					bn++;
+				fprintf(stderr, "%s:%d ", bn, line);
+				break;
+			}
+		}
+	}
+	vfprintf(stderr, format, ap);
+	/* TODO: LOG_FILENAME_POS_LINE_END; we cannot modify the format string here :/ */
+	int fmt_len = strlen(format);
+	if (fmt_len && format[fmt_len-1] == '\n')
+		fputc('\r', stderr);
+}
+
+static struct log_target *log_target_create_stderr_raw(void)
+{
+	struct log_target *target;
+
+	target = log_target_create();
+	if (!target)
+		return NULL;
+
+	target->type = LOG_TGT_TYPE_STDERR;
+	target->raw_output = stderr_raw_output;
+	target->print_category = true;
+	target->print_level = true;
+
+	return target;
+}
+
+void libosmo_emb_init(void)
+{
+	struct log_target *stderr_target;
+
+	/* msgb */
+	g_msgb_ctx = talloc_pool(g_tall_ctx, 20480);
+	talloc_set_memlimit(g_msgb_ctx, 20480);
+	msgb_talloc_ctx_init(g_msgb_ctx, 0);
+
+	/* logging */
+	log_init(&log_info, g_tall_ctx);
+	stderr_target = log_target_create_stderr_raw();
+	log_add_target(stderr_target);
+	log_set_all_filter(stderr_target, 1);
+}
diff --git a/sysmoOCTSIM/logging.h b/sysmoOCTSIM/logging.h
new file mode 100644
index 0000000..900f88e
--- /dev/null
+++ b/sysmoOCTSIM/logging.h
@@ -0,0 +1,8 @@
+#pragma once
+
+#include <osmocom/core/logging.h>
+
+enum {
+	DUSB,
+	DCCID
+};
diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c
index 80b52dc..01ea0a8 100644
--- a/sysmoOCTSIM/main.c
+++ b/sysmoOCTSIM/main.c
@@ -946,11 +946,12 @@
 }
 
 extern void testmode_init(void);
+extern void libosmo_emb_init(void);
 
 #include "talloc.h"
+#include "logging.h"
 #include <osmocom/core/msgb.h>
 void *g_tall_ctx;
-void *g_msgb_ctx;
 
 DEFUN(_talloc_report, cmd_talloc_report, "talloc-report", "Generate a talloc report")
 {
@@ -1039,8 +1040,10 @@
 	talloc_enable_null_tracking();
 	g_tall_ctx = talloc_named_const(NULL, 0, "global");
 	printf("g_tall_ctx=%p\r\n", g_tall_ctx);
-	g_msgb_ctx = talloc_pool(g_tall_ctx, 20480);
-	talloc_set_memlimit(g_msgb_ctx, 20480);
+
+	libosmo_emb_init();
+
+	LOGP(DUSB, LOGL_ERROR, "foobar usb\n");
 
 	command_print_prompt();
 	while (true) { // main loop