From 2836d196da3d327115823703f7064b57a040b8ee Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Tue, 4 Nov 2025 17:00:43 +0800 Subject: [PATCH] tlog --- src/flake.lock | 18 ++--- src/include/utils.h | 12 ++- src/lib/meson.build | 4 +- src/lib/testsuite/test.c | 2 +- src/lib/testsuite/test_error.c | 2 +- src/lib/testsuite/test_htab.c | 2 +- src/lib/testsuite/test_htab_int.c | 2 +- src/lib/testsuite/test_utils.c | 2 +- src/lib/testsuite/test_xstring.c | 2 +- src/lib/testsuite/test_zarray.c | 2 +- src/lib/utils.c | 117 +++++++++++++++++++++++++++++- src/nixSupport/xce.nix | 4 +- 12 files changed, 144 insertions(+), 25 deletions(-) diff --git a/src/flake.lock b/src/flake.lock index 791eb2b1..ab60de4c 100644 --- a/src/flake.lock +++ b/src/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1761907660, - "narHash": "sha256-kJ8lIZsiPOmbkJypG+B5sReDXSD1KGu2VEPNqhRa/ew=", + "lastModified": 1762111121, + "narHash": "sha256-4vhDuZ7OZaZmKKrnDpxLZZpGIJvAeMtK6FKLJYUtAdw=", "owner": "nixos", "repo": "nixpkgs", - "rev": "2fb006b87f04c4d3bdf08cfdbc7fab9c13d94a15", + "rev": "b3d51a0365f6695e7dd5cdf3e180604530ed33b4", "type": "github" }, "original": { @@ -36,11 +36,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1755615617, - "narHash": "sha256-HMwfAJBdrr8wXAkbGhtcby1zGFvs+StOp19xNsbqdOg=", + "lastModified": 1762111121, + "narHash": "sha256-4vhDuZ7OZaZmKKrnDpxLZZpGIJvAeMtK6FKLJYUtAdw=", "owner": "nixos", "repo": "nixpkgs", - "rev": "20075955deac2583bb12f07151c2df830ef346b4", + "rev": "b3d51a0365f6695e7dd5cdf3e180604530ed33b4", "type": "github" }, "original": { @@ -77,11 +77,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1762160937, - "narHash": "sha256-L0wBnhZxneDKm9ym7XroRexjuio62sVV0xi5sgAMiaA=", + "lastModified": 1762246108, + "narHash": "sha256-99qtmUXV4UZcUc8klD+n0MCBU52DwxpqHf/KjXcd4vQ=", "owner": "shen390s", "repo": "my-nix-channel", - "rev": "3eb8b50d886f197ec5b842aadb022b026c9b2bfd", + "rev": "de44a16949de901790ae712c1a1cc39ccc960fae", "type": "github" }, "original": { diff --git a/src/include/utils.h b/src/include/utils.h index 38b4edcc..395b54ea 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "controller.capnp.h" #include "xce/errnox.h" @@ -57,6 +58,14 @@ (code)); \ } while (0) +#define TRACE(...) tlog_debug3(__VA_ARGS__) + +#define INFO(...) tlog_info(__VA_ARGS__) + +#define ERROR(...) tlog_error(__VA_ARGS__) + +#define WARN(...) tlog_warn(__VA_ARGS__) + #define PERROR(s) xce_perror(s) typedef struct { @@ -66,9 +75,6 @@ typedef struct { int code; } ectx_t; -#define TRACE(...) -#define WARN(...) - extern capn_text chars_to_text(const char*); extern int parse_argv(char*, char***); extern void free_argv(char**, int); diff --git a/src/lib/meson.build b/src/lib/meson.build index 3da3a592..21596a59 100644 --- a/src/lib/meson.build +++ b/src/lib/meson.build @@ -37,10 +37,10 @@ capnpc_c_prog = find_program('capnpc-c') thread_dep = dependency('threads') c_capnproto_dep = dependency('c-capnproto') -zlog_dep = dependency('zlog') +tinylog_dep = dependency('tinylog') unity_dep = dependency('unity') -libxce_deps = [c_capnproto_dep, zlog_dep, thread_dep] +libxce_deps = [c_capnproto_dep, tinylog_dep, thread_dep] libxce = static_library('xce', libxce_src, diff --git a/src/lib/testsuite/test.c b/src/lib/testsuite/test.c index 23553142..732363e6 100644 --- a/src/lib/testsuite/test.c +++ b/src/lib/testsuite/test.c @@ -2,7 +2,7 @@ #include #include -#include "unity.h" +#include extern void test_xstrings(void); extern void test_zarray(void); diff --git a/src/lib/testsuite/test_error.c b/src/lib/testsuite/test_error.c index 92d7be37..db71cfcd 100644 --- a/src/lib/testsuite/test_error.c +++ b/src/lib/testsuite/test_error.c @@ -1,8 +1,8 @@ #include #include +#include #include "xce/errnox.h" -#include "unity.h" #include "utils.h" static void test_error_setget(void) { diff --git a/src/lib/testsuite/test_htab.c b/src/lib/testsuite/test_htab.c index 537f695e..b49f038e 100644 --- a/src/lib/testsuite/test_htab.c +++ b/src/lib/testsuite/test_htab.c @@ -2,7 +2,7 @@ #include #include -#include "unity.h" +#include #include "htab.h" static htab_t htab; diff --git a/src/lib/testsuite/test_htab_int.c b/src/lib/testsuite/test_htab_int.c index a206f203..ff86850d 100644 --- a/src/lib/testsuite/test_htab_int.c +++ b/src/lib/testsuite/test_htab_int.c @@ -2,8 +2,8 @@ #include #include #include +#include -#include "unity.h" #include "htab.h" static htab_t htab; diff --git a/src/lib/testsuite/test_utils.c b/src/lib/testsuite/test_utils.c index 1216369a..558b5b2a 100644 --- a/src/lib/testsuite/test_utils.c +++ b/src/lib/testsuite/test_utils.c @@ -1,8 +1,8 @@ #include #include #include +#include -#include "unity.h" #include "utils.h" #include "xce/types.h" diff --git a/src/lib/testsuite/test_xstring.c b/src/lib/testsuite/test_xstring.c index 2c20b70d..d75db0eb 100644 --- a/src/lib/testsuite/test_xstring.c +++ b/src/lib/testsuite/test_xstring.c @@ -1,9 +1,9 @@ #include #include #include +#include #include "objmgr.h" -#include "unity.h" #include "xstring.h" void test_xstring_create(void) diff --git a/src/lib/testsuite/test_zarray.c b/src/lib/testsuite/test_zarray.c index 4a68cfb3..5b7788df 100644 --- a/src/lib/testsuite/test_zarray.c +++ b/src/lib/testsuite/test_zarray.c @@ -1,8 +1,8 @@ #include #include #include +#include -#include "unity.h" #include "zarray.h" static void test_zarray_create(void) { diff --git a/src/lib/utils.c b/src/lib/utils.c index b6aeafe2..2c2bbf47 100755 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -15,6 +15,8 @@ #include #endif +#include + #include "config.h" #include "controller.capnp.h" #include @@ -210,12 +212,123 @@ char* frame_symbol(int k) int xce_vprintf(const char* fmt, va_list ap) { - int rc = 0; + int rc; - /* FIXME: */ + if (tlog_get_root() == NULL) { + rc = vfprintf(stdout, fmt, ap); + } else { + rc = tlog_vext(TLOG_INFO, __FILE__, __LINE__, + __func__, NULL, fmt, ap); + } return rc; } +int name_to_level(char* lvlname) +{ + static struct { + char* lvlstr; + int lvl; + } levels[] = { + { "debug", TLOG_DEBUG }, + { "debug2", TLOG_DEBUG2 }, + { "debug3", TLOG_DEBUG3 }, + { "info", TLOG_INFO }, + { "notice", TLOG_NOTICE }, + { "warn", TLOG_WARN }, + { "error", TLOG_ERROR }, + { "fatal", TLOG_FATAL }, + { NULL, -1 } + }; + int i; + + for (i = 0; levels[i].lvlstr != NULL; i++) { + if (strcasecmp(levels[i].lvlstr, lvlname) == 0) { + return levels[i].lvl; + } + } + + return TLOG_INFO; +} + +static int my_tlog_format(char* buff, + int maxlen, + struct tlog_loginfo* info, + void* userptr, + const char* fmt, + va_list ap) +{ + int len = 0; + int total_len = 0; + struct tlog_time* tm = &info->time; + + xce_use(userptr); + + /* format prefix */ + if (info->level < TLOG_INFO) { + len = snprintf(buff, maxlen, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d] [%5d] [%6s] [%17s:%-4d] ", + tm->year, tm->mon, tm->mday, tm->hour, tm->min, tm->sec, tm->usec / 1000, getpid(), + tlog_get_level_string(info->level), info->file, info->line); + } else { + len = snprintf(buff, maxlen, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d] [%5d] [%6s] [%22s] ", + tm->year, tm->mon, tm->mday, tm->hour, tm->min, tm->sec, tm->usec / 1000, getpid(), + tlog_get_level_string(info->level), info->func); + } + + if (len < 0 || len >= maxlen) { + return -1; + } + buff += len; + total_len += len; + maxlen -= len; + + /* format log message */ + len = vsnprintf(buff, maxlen, fmt, ap); + if (len < 0 || len == maxlen) { + return -1; + } + buff += len; + total_len += len; + + /* return total length */ + return total_len; +} +int mylog_init(char* logdir, + char* id, + char* loglvl, + int max_log_size, + int max_log_count) +{ + char logfile[XCE_MAXPATH_LEN]; + int flag = 0; + int rc = 0; + + snprintf(logfile, sizeof(logfile), + "%s/%s.log", logdir, id); + if (isatty(STDERR_FILENO)) { + flag = TLOG_SCREEN_COLOR; + } + + rc = tlog_init(logfile, max_log_size, + max_log_count, 0, flag); + if (rc >= 0) { + rc = tlog_setlevel(name_to_level(loglvl)); + } + + tlog_reg_format_func(my_tlog_format); + + return rc; +} + +int mylog_setlevel(char* level) +{ + return tlog_setlevel(name_to_level(level)); +} + +void mylog_fini() +{ + tlog_exit(); +} + static _Thread_local ectx_t ectx; void set_error(char* file, diff --git a/src/nixSupport/xce.nix b/src/nixSupport/xce.nix index dffb394c..4a4f4aea 100644 --- a/src/nixSupport/xce.nix +++ b/src/nixSupport/xce.nix @@ -35,8 +35,8 @@ stdenv.mkDerivation { pkgs.sqlite pkgs.hwloc pkgs.linux-pam - pkgs.zlog_with_pkgconf - pkgs.unity-test + pkgs.tinylog + pkgs.unity_test_with_color ]; build-system = [ pkgs.meson ];