tlog
This commit is contained in:
parent
cccea1141d
commit
2836d196da
12 changed files with 144 additions and 25 deletions
117
src/lib/utils.c
117
src/lib/utils.c
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue