summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/sbc_harness/config/config.h4
-rw-r--r--cmd/sbc_harness/main.c6
-rw-r--r--cmd/srv9p/main.c1
-rw-r--r--lib9p/srv.c7
-rw-r--r--libcr/coroutine.c35
-rw-r--r--libdhcp/dhcp_client.c10
-rw-r--r--libhw/w5500.c7
-rw-r--r--libmisc/CMakeLists.txt1
-rw-r--r--libmisc/assert.c6
-rw-r--r--libmisc/include/libmisc/log.h31
-rw-r--r--libmisc/log.c19
-rw-r--r--libusb/usb_common.c9
12 files changed, 96 insertions, 40 deletions
diff --git a/cmd/sbc_harness/config/config.h b/cmd/sbc_harness/config/config.h
index b557287..98fc156 100644
--- a/cmd/sbc_harness/config/config.h
+++ b/cmd/sbc_harness/config/config.h
@@ -66,6 +66,10 @@
#define CONFIG_DHCP_OPT_SIZE 312 /* minimum of 312 */
#define CONFIG_DHCP_SELECTING_NS (5*NS_PER_S)
+/* USB KEYBOARD ***************************************************************/
+
+#define CONFIG_USB_COMMON_DEBUG 1 /* bool */
+
/* COROUTINE ******************************************************************/
#define CONFIG_COROUTINE_DEFAULT_STACK_SIZE (2*1024)
diff --git a/cmd/sbc_harness/main.c b/cmd/sbc_harness/main.c
index 17d32cf..23195b3 100644
--- a/cmd/sbc_harness/main.c
+++ b/cmd/sbc_harness/main.c
@@ -5,7 +5,6 @@
*/
#include <string.h> /* libc: for strlen() */
-#include <stdio.h> /* libc: for printf() */
#include <pico/stdlib.h> /* pico-sdk:pico_stdlib: for stdio_uart_init() */
#include <hardware/flash.h> /* pico-sdk:hardware_flash: for flash_get_unique_id() */
@@ -17,6 +16,9 @@
#include <libusb/usb_common.h>
#include <libdhcp/client.h>
+#define LOG_NAME MAIN
+#include <libmisc/log.h>
+
#include "usb_keyboard.h"
COROUTINE hello_world_cr(void *_chan) {
@@ -27,7 +29,7 @@ COROUTINE hello_world_cr(void *_chan) {
for (size_t i = 0;; i = (i+1) % strlen(msg)) {
int result = usb_keyboard_rpc_send_req(chan, (uint32_t)msg[i]);
if (result < 1) {
- printf("error!\n");
+ errorf("error sending rune U+%d", (uint32_t)msg[i]);
break;
}
}
diff --git a/cmd/srv9p/main.c b/cmd/srv9p/main.c
index d0a683a..be34daa 100644
--- a/cmd/srv9p/main.c
+++ b/cmd/srv9p/main.c
@@ -5,7 +5,6 @@
*/
#include <error.h>
-#include <stdio.h>
#include <lib9p/srv.h>
#include <libcr/coroutine.h>
diff --git a/lib9p/srv.c b/lib9p/srv.c
index 5bec0d6..a7c4293 100644
--- a/lib9p/srv.c
+++ b/lib9p/srv.c
@@ -6,7 +6,6 @@
#include <alloca.h>
#include <inttypes.h> /* for PRI* */
-#include <stdio.h> /* for printf() */
#include <string.h> /* for strerror() */
#include <libcr/coroutine.h>
@@ -16,6 +15,9 @@
#include <libmisc/assert.h>
#include <libmisc/vcall.h>
+#define LOG_NAME 9P_SRV
+#include <libmisc/log.h>
+
#define IMPLEMENTATION_FOR_LIB9P_SRV_H YES
#include <lib9p/srv.h>
#include "internal.h"
@@ -103,8 +105,7 @@ struct _lib9p_srv_req {
/* base utilities *************************************************************/
-#define nonrespond_errorf(fmt, ...) \
- printf("error: " fmt "\n" __VA_OPT__(,) __VA_ARGS__)
+#define nonrespond_errorf errorf
static uint32_t rerror_overhead_for_version(enum lib9p_version version,
uint8_t *scratch) {
diff --git a/libcr/coroutine.c b/libcr/coroutine.c
index 041b624..35a060d 100644
--- a/libcr/coroutine.c
+++ b/libcr/coroutine.c
@@ -6,12 +6,15 @@
#include <setjmp.h> /* for setjmp(), longjmp(), jmp_buf */
#include <stdint.h> /* for uint8_t */
-#include <stdio.h> /* for printf() */
#include <stdlib.h> /* for aligned_alloc(), free() */
#include <libmisc/assert.h>
+#define LOG_NAME COROUTINE
+#include <libmisc/log.h>
+
#include <libcr/coroutine.h>
+#undef COROUTINE
/* Configuration **************************************************************/
@@ -414,14 +417,6 @@ static cid_t coroutine_running = 0;
/* utility functions **********************************************************/
-#define errorf(...) printf("error: " __VA_ARGS__)
-#define infof(...) printf("info: " __VA_ARGS__)
-#if CONFIG_COROUTINE_DEBUG
- #define debugf(...) printf("dbg: " __VA_ARGS__)
-#else
- #define debugf(...)
-#endif
-
static inline const char* coroutine_state_str(enum coroutine_state state) {
assert(state < ARRAY_LEN(coroutine_state_strs));
return coroutine_state_strs[state];
@@ -473,7 +468,7 @@ cid_t coroutine_add_with_stack_size(size_t stack_size,
assert_cid_state(parent, state == CR_RUNNING);
assert(stack_size);
assert(fn);
- debugf("coroutine_add_with_stack_size(%zu, \"%s\", %p, %p)...\n",
+ debugf("coroutine_add_with_stack_size(%zu, \"%s\", %p, %p)...",
stack_size, name, fn, args);
cid_t child;
@@ -487,7 +482,7 @@ cid_t coroutine_add_with_stack_size(size_t stack_size,
return 0;
found:
}
- debugf("...child=%zu\n", child);
+ debugf("...child=%zu", child);
last_created = child;
@@ -516,8 +511,8 @@ cid_t coroutine_add_with_stack_size(size_t stack_size,
+ STACK_GUARD_SIZE
#endif
;
- debugf("...stack =%p\n", coroutine_table[child-1].stack);
- debugf("...stack_base=%p\n", stack_base);
+ debugf("...stack =%p", coroutine_table[child-1].stack);
+ debugf("...stack_base=%p", stack_base);
/* run until cr_begin() */
cr_plat_call_with_stack(stack_base, fn, args);
assert_notreached("should cr_begin() instead of returning");
@@ -543,7 +538,7 @@ cid_t coroutine_add(const char *name, cr_fn_t fn, void *args) {
/* coroutine_main() ***********************************************************/
__attribute__ ((noreturn)) void coroutine_main(void) {
- debugf("coroutine_main()\n");
+ debugf("coroutine_main()");
bool saved = cr_save_and_disable_interrupts();
assert(saved);
assert(!cr_is_in_intrhandler());
@@ -575,7 +570,7 @@ __attribute__ ((noreturn)) void coroutine_main(void) {
/* cr_*() *********************************************************************/
void cr_begin(void) {
- debugf("cid=%zu: cr_begin()\n", coroutine_running);
+ debugf("cid=%zu: cr_begin()", coroutine_running);
assert_cid_state(coroutine_running, state == CR_INITIALIZING);
bool saved = cr_save_and_disable_interrupts();
@@ -609,7 +604,7 @@ static inline void _cr_yield() {
}
void cr_yield(void) {
- debugf("cid=%zu: cr_yield()\n", coroutine_running);
+ debugf("cid=%zu: cr_yield()", coroutine_running);
assert(!cr_is_in_intrhandler());
assert_cid_state(coroutine_running, state == CR_RUNNING);
@@ -621,7 +616,7 @@ void cr_yield(void) {
}
void cr_pause_and_yield(void) {
- debugf("cid=%zu: cr_pause_and_yield()\n", coroutine_running);
+ debugf("cid=%zu: cr_pause_and_yield()", coroutine_running);
assert(!cr_is_in_intrhandler());
assert_cid_state(coroutine_running, state == CR_RUNNING);
@@ -632,7 +627,7 @@ void cr_pause_and_yield(void) {
}
__attribute__ ((noreturn)) void cr_exit(void) {
- debugf("cid=%zu: cr_exit()\n", coroutine_running);
+ debugf("cid=%zu: cr_exit()", coroutine_running);
assert(!cr_is_in_intrhandler());
assert_cid_state(coroutine_running, state == CR_RUNNING);
@@ -649,7 +644,7 @@ static void _cr_unpause(cid_t cid) {
}
void cr_unpause(cid_t cid) {
- debugf("cr_unpause(%zu)\n", cid);
+ debugf("cr_unpause(%zu)", cid);
assert(!cr_is_in_intrhandler());
assert_cid_state(coroutine_running, state == CR_RUNNING);
@@ -659,7 +654,7 @@ void cr_unpause(cid_t cid) {
}
void cr_unpause_from_intrhandler(cid_t cid) {
- debugf("cr_unpause_from_intrhandler(%zu)\n", cid);
+ debugf("cr_unpause_from_intrhandler(%zu)", cid);
assert(cr_is_in_intrhandler());
_cr_unpause(cid);
diff --git a/libdhcp/dhcp_client.c b/libdhcp/dhcp_client.c
index a7b1d49..34ce8ac 100644
--- a/libdhcp/dhcp_client.c
+++ b/libdhcp/dhcp_client.c
@@ -90,6 +90,9 @@
#include <libmisc/vcall.h>
#include <libhw/generic/alarmclock.h>
+#define LOG_NAME DHCP
+#include <libmisc/log.h>
+
#include <libdhcp/client.h>
#include "dhcp_common.h"
@@ -110,13 +113,6 @@
/* Implementation *************************************************************/
-#if CONFIG_DHCP_DEBUG
- #include <stdio.h>
- #define debugf(fmt, ...) printf(fmt "\n" __VA_OPT__(,) __VA_ARGS__)
-#else
- #define debugf(fmt, ...) ((void)0)
-#endif
-
enum requirement {
MUST,
MUST_NOT,
diff --git a/libhw/w5500.c b/libhw/w5500.c
index 79fc10e..d94a88d 100644
--- a/libhw/w5500.c
+++ b/libhw/w5500.c
@@ -67,8 +67,6 @@
* SPDX-License-Identifier: MIT
*/
-#include <stdio.h>
-
/* TODO: Write a <libhw/generic/gpio.h> to avoid w5500.c being
* pico-sdk-specific. */
#include <hardware/gpio.h> /* pico-sdk:hardware_gpio */
@@ -78,6 +76,9 @@
#include <libhw/generic/alarmclock.h> /* for sleep_*() */
+#define LOG_NAME W5500
+#include <libmisc/log.h> /* for errorf() */
+
#define IMPLEMENTATION_FOR_LIBHW_W5500_H YES
#include <libhw/w5500.h>
@@ -340,7 +341,7 @@ void _w5500_init(struct w5500 *chip,
w5500ll_write_sock_reg(chip->spidev, 0, mode, a);
uint8_t b = w5500ll_read_sock_reg(chip->spidev, 0, mode);
if (b != a)
- printf("SPI to W5500 does not appear to be functional: wrote:%d != read:%d\n", a, b);
+ errorf("SPI to W5500 does not appear to be functional: wrote:%d != read:%d", a, b);
}
w5500ll_write_sock_reg(chip->spidev, 0, mode, 0);
diff --git a/libmisc/CMakeLists.txt b/libmisc/CMakeLists.txt
index 68b6910..1394978 100644
--- a/libmisc/CMakeLists.txt
+++ b/libmisc/CMakeLists.txt
@@ -7,6 +7,7 @@ add_library(libmisc INTERFACE)
target_include_directories(libmisc SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_sources(libmisc INTERFACE
assert.c
+ log.c
)
target_compile_options(libmisc INTERFACE "$<$<COMPILE_LANGUAGE:C>:-fplan9-extensions>")
diff --git a/libmisc/assert.c b/libmisc/assert.c
index 911cea3..dbf25b7 100644
--- a/libmisc/assert.c
+++ b/libmisc/assert.c
@@ -5,9 +5,11 @@
*/
#include <stdbool.h> /* for bool, true, false */
-#include <stdio.h> /* for printf() */
#include <stdlib.h> /* for abort() */
+#define LOG_NAME ASSERT
+#include <libmisc/log.h> /* for errorf() */
+
#include <libmisc/assert.h>
#ifndef NDEBUG
@@ -18,7 +20,7 @@ void __assert_msg_fail(const char *expr,
static bool in_fail = false;
if (!in_fail) {
in_fail = true;
- printf("error: %s:%u:%s(): assertion \"%s\" failed%s%s\n",
+ errorf("%s:%u:%s(): assertion \"%s\" failed%s%s",
file, line, func,
expr,
msg ? ": " : "", msg);
diff --git a/libmisc/include/libmisc/log.h b/libmisc/include/libmisc/log.h
new file mode 100644
index 0000000..eb9db3b
--- /dev/null
+++ b/libmisc/include/libmisc/log.h
@@ -0,0 +1,31 @@
+/* libmisc/log.h - stdio logging
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _LIBMISC_LOG_H_
+#define _LIBMISC_LOG_H_
+
+#ifndef LOG_NAME
+ #error "each compilation unit that includes <libmisc/log.h> must define LOG_NAME"
+#endif
+
+#ifdef NDEBUG
+ #define _LOG_NDEBUG 1
+#else
+ #define _LOG_NDEBUG 0
+#endif
+#define _LOG_STR(x) #x
+#define __LOG_CAT3(a, b, c) a ## b ## c
+#define _LOG_CAT3(a, b, c) __LOG_CAT3(a, b, c)
+
+__attribute__((format(printf, 1, 2)))
+int _log_printf(const char *format, ...);
+
+#define errorf(fmt, ...) do { _log_printf("error: " _LOG_STR(LOG_NAME) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)
+#define infof(fmt, ...) do { _log_printf("info : " _LOG_STR(LOG_NAME) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)
+#define debugf(fmt, ...) do { if (_LOG_CAT3(CONFIG_, LOG_NAME, _DEBUG) && !_LOG_NDEBUG) \
+ _log_printf("debug: " _LOG_STR(LOG_NAME) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)
+
+#endif /* _LIBMISC_LOG_H_ */
diff --git a/libmisc/log.c b/libmisc/log.c
new file mode 100644
index 0000000..a1ec10f
--- /dev/null
+++ b/libmisc/log.c
@@ -0,0 +1,19 @@
+/* libmisc/log.c - stdio logging
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <stdio.h> /* for vprintf() */
+#include <stdarg.h> /* for va_list, va_start(), va_end() */
+
+#define LOG_NAME
+#include <libmisc/log.h>
+
+int _log_printf(const char *format, ...) {
+ va_list va;
+ va_start(va, format);
+ int ret = vprintf(format, va);
+ va_end(va);
+ return ret;
+}
diff --git a/libusb/usb_common.c b/libusb/usb_common.c
index 17dba0a..c9429e7 100644
--- a/libusb/usb_common.c
+++ b/libusb/usb_common.c
@@ -13,9 +13,14 @@
#include <libmisc/assert.h>
+#define LOG_NAME USB_COMMON
+#include <libmisc/log.h>
+
#include <libusb/tusb_helpers.h> /* for LANGID_*, TU_UTF16() */
#include <libusb/usb_common.h>
+#include "config.h"
+
/* Strings ********************************************************************/
/**
@@ -54,12 +59,12 @@ uint16_t const *tud_descriptor_string_cb(uint8_t strid, uint16_t langid) {
#pragma GCC diagnostic pop
break;
default:
- printf("GET STRING: unknown string id=%"PRIu8, strid);
+ debugf("GET STRING: unknown string id=%"PRIu8, strid);
return NULL;
}
break;
default:
- printf("GET STRING: unknown LANGID=%"PRIx16, langid);
+ debugf("GET STRING: unknown LANGID=%"PRIx16, langid);
return NULL;
}
}