标签:
level_list.h
/** * */ #ifndef __zlog_level_list_h #define __zlog_level_list_h zc_arraylist_t *zlog_level_list_new(void); void zlog_level_list_del(zc_arraylist_t *levels); void zlog_level_list_profile(zc_arraylist_t *levels, int flag); /* notice */ int zlog_level_list_set(zc_arraylist_t *levels, char *line); /* notice */ zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l); /* notice */ int zlog_level_init_atoi(zc_arraylist_t *level, char *str); #endif
level_list.c
#include <stdio.h> #include <ctype.h> #include <errno.h> #include "zc_defs.h" #include "level.h" #include "level_list.h" void zlog_level_list_profile(zc_arraylist_t *levels, int flag){ int i; zlog_level_t *a_level; zc_assert(levels,); zc_profile(flag, "---level_list[%p]", levels); zc_arraylist_foreach(levels, i, a_level){ /*skip empty slots*/ if(a_level){ zlog_level_profile(a_level, flag); } } return ; } //---------------------------------------------------------------------- void zlog_level_list_del(zc_arraylist_t *levels){ zc_assert(levels,); zc_arraylist_del(levels); zc_debug("zc_level_list_del[%p]", levels); return ; } int zlog_level_list_set(zc_arraylist_t *levels, char *line){ zlog_level_t *a_level; a_level = zlog_level_new(line); if(!a_level){ zc_error("zlog_level_new fail"); return -1; } if(zc_arraylist_set(levels, a_level->int_level, a_level)){ zc_error("zc_arraylist_set fail"); goto err; } return 0; err: zc_error("line[%s]", line); zlog_level_del(a_level); return -1; } static int zlog_level_list_set_default(zc_arraylist_t *levels){ return zlog_level_list_set(levels, "* = 0, LOG_INFO") || zlog_level_list_set(levels, "DEBUG = 20, LOG_DEBUG") || zlog_level_list_set(levels, "INFO = 40, LOG_INFO") || zlog_level_list_set(levels, "WARN = 80, LOG_WARNING") || zlog_level_list_set(levels, "ERROR = 100, LOG_ERR") || zlog_level_list_set(levels, "FATAL = 120, LOG_ALERT") || zlog_level_list_set(levels, "UNKNOWN = 254, LOG_ERR") || zlog_level_list_set(levels, "! = 255, LOG_INFO"); } zc_arraylist_t *zlog_level_list_new(void){ zc_arraylist_t *levels; levels = zc_arraylist_new((zc_arraylist_del_fn) *zlog_level_del); if(!levels){ zc_error("zc_arraylist_new fail"); return NULL; } if(zlog_level_list_set_default(levels)){ zc_error("zlog_level_set_default faile"); goto err; } //zlog_level_list_profile(levels, ZC_DEBUG); return levels; err: zc_arraylist_del(levels); return NULL; } zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l){ zlog_level_t *a_level; #if 0 #endif a_level = zc_arraylist_get(levels, l); if(a_level){ return a_level; }else{ zc_error("l[%d] not in (0, 254), or has no level defined," "see configure file define, set to UNKOWN", l); return zc_arraylist_get(levels, 254); } } //------------------------------------------------------------------ int zlog_level_list_atoi(zc_arraylist_t *levels, char *str){ int i; zlog_level_t *a_level; if(str == NULL || *str == ‘\0‘){ zc_error("str is [%s], can‘t find level", str); return -1; } zc_arraylist_foreach(levels, i, a_level){ if(a_level && STRICMP(str, ==, a_level->str_uppercase)){ return i; } } zc_error("str[%s] can‘t found in level list", str); return -1; }
测试 test_level_list.c
#include "zc_defs.h" #include "level.h" #include "level_list.h" #include "zc_profile.c" #include "zc_arraylist.c" #include "level.c" #include "level_list.c" int main(){ zc_arraylist_t *levels = zlog_level_list_new(); zlog_level_list_set(levels, "FATAL = 110, LOG_ERR"); zlog_level_list_profile(levels, ZC_DEBUG); zlog_level_t *a_level = zlog_level_list_get(levels, 110); zlog_level_list_del(levels); }
标签:
原文地址:http://www.cnblogs.com/bai-jimmy/p/5396295.html