This commit is contained in:
Rongsong Shen 2025-11-04 17:00:43 +08:00
parent cccea1141d
commit 2836d196da
12 changed files with 144 additions and 25 deletions

18
src/flake.lock generated
View file

@ -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": {

View file

@ -8,6 +8,7 @@
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <tlog.h>
#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);

View file

@ -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,

View file

@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include "unity.h"
#include <unity/unity.h>
extern void test_xstrings(void);
extern void test_zarray(void);

View file

@ -1,8 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <unity/unity.h>
#include "xce/errnox.h"
#include "unity.h"
#include "utils.h"
static void test_error_setget(void) {

View file

@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include "unity.h"
#include <unity/unity.h>
#include "htab.h"
static htab_t htab;

View file

@ -2,8 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unity/unity.h>
#include "unity.h"
#include "htab.h"
static htab_t htab;

View file

@ -1,8 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unity/unity.h>
#include "unity.h"
#include "utils.h"
#include "xce/types.h"

View file

@ -1,9 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unity/unity.h>
#include "objmgr.h"
#include "unity.h"
#include "xstring.h"
void test_xstring_create(void)

View file

@ -1,8 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unity/unity.h>
#include "unity.h"
#include "zarray.h"
static void test_zarray_create(void) {

View file

@ -15,6 +15,8 @@
#include <execinfo.h>
#endif
#include <tlog.h>
#include "config.h"
#include "controller.capnp.h"
#include <capnp_c.h>
@ -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,

View file

@ -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 ];