level.h
/**
*
*/ #ifndef __zlog_level_h
#define __zlog_level_h #include "stdio.h"
#include "zc_defs.h" typedef struct zlog_level_s {
int int_level;
char str_uppercase[MAXLEN_PATH + ];
char str_lowercase[MAXLEN_PATH + ];
size_t str_len;
int syslog_level;
} zlog_level_t; zlog_level_t *zlog_level_new(char *line);
void zlog_level_del(zlog_level_t *a_level);
void zlog_level_profile(zlog_level_t *a_level, int flag); #endif
level.c
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <syslog.h> #include "zc_defs.h"
#include "level.h" void zlog_level_profile(zlog_level_t *a_level, int flag){
zc_assert(a_level,);
zc_profile(flag, "---level[%p][%d,%s,%s,%d,%d]---",
a_level,
a_level->int_level,
a_level->str_uppercase,
a_level->str_lowercase,
(int) a_level->str_len,
a_level->syslog_level);
return;
} void zlog_level_del(zlog_level_t *a_level){
zc_assert(a_level,);
free(a_level);
zc_debug("zlog_level_del[%p]", a_level);
return;
} static int syslog_level_atoi(char *str){
//notice -187
zc_assert(str, -);
if(STRICMP(str, ==, "LOG_EMERG")){
return LOG_EMERG;
}
if(STRICMP(str, ==, "LOG_ALERT")){
return LOG_ALERT;
}
if(STRICMP(str, ==, "LOG_CRIT")){
return LOG_CRIT;
}
if(STRICMP(str, ==, "LOG_ERR")){
return LOG_ERR;
}
if(STRICMP(str, ==, "LOG_WARNING")){
return LOG_WARNING;
}
if(STRICMP(str, ==, "LOG_NOTICE")){
return LOG_NOTICE;
}
if(STRICMP(str, ==, "LOG_INFO")){
return LOG_INFO;
}
if(STRICMP(str, ==, "LOG_DEBUG")){
return LOG_DEBUG;
}
zc_error("wrong syslog level[%s]", str);
return -;
} /* line: TRACE = 10, LOG_ERR */
zlog_level_t *zlog_level_new(char *line){
zlog_level_t *a_level = NULL;
int i, nscan, l = ;
char str[MAXLEN_CFG_LINE + ];
char sl[MAXLEN_CFG_LINE + ]; zc_assert(line, NULL); memset(str, 0x00, sizeof(str));
memset(sl, 0x00, sizeof(str)); nscan = sscanf(line, " %[^= \t] = %d ,%s", str, &l, sl);
if(nscan < ){
zc_error("level[%s], syntax wrong", line);
return NULL;
} //check level and str
if((l < ) || (l > )){
zc_error("l[%d] not in [0, 255], wrong", l);
return NULL;
}
if(str[] == '\0'){
zc_error("str[0] == 0");
return NULL;
} a_level = calloc(, sizeof(zlog_level_t));
if(!a_level){
zc_error("calloc fail, errno[%d]", errno);
return NULL;
}
a_level->int_level = l; //fill syslog level
if(sl[] == '\0'){
a_level->syslog_level = LOG_DEBUG;
}else{
a_level->syslog_level = syslog_level_atoi(sl);
if(a_level->syslog_level == -){
zc_error("syslog_level_atoi fail");
goto err;
}
} //strncpy and toupper(str)
for(i = ; (i < sizeof(a_level->str_uppercase) - ) && str[i] != '\0'; i++){
a_level->str_uppercase[i] = toupper(str[i]);
a_level->str_lowercase[i] = tolower(str[i]);
} //notice: overflow
if(str[i] != '\0'){
zc_error("not enough space for str, str[%s] > %d", str, i);
goto err;
}else{
a_level->str_uppercase[i] = '\0';
a_level->str_lowercase[i] = '\0';
} a_level->str_len = i; return a_level;
err:
zc_error("line[%s]", line);
zlog_level_del(a_level);
return NULL;
}
test_level.c
#include "zc_defs.h"
#include "level.h" #include "zc_profile.c"
#include "level.c" int main(){
zlog_level_t *a_level = zlog_level_new("TRACE = 10, LOG_ERR"); zlog_level_profile(a_level, ZC_DEBUG); zlog_level_del(a_level);
}