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

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,