Zlog study notes (level)

Source: Internet
Author: User
Tags syslog

Level.h

/** * */#ifndef __zlog_level_h#define__zlog_level_h#include"stdio.h"#include"Zc_defs.h"typedefstructzlog_level_s {intInt_level; CharStr_uppercase[maxlen_path +1]; CharStr_lowercase[maxlen_path +1];        size_t Str_len; intSyslog_level;} zlog_level_t;zlog_level_t*zlog_level_new (Char*Line );voidZlog_level_del (zlog_level_t *a_level);voidZlog_level_profile (zlog_level_t *a_level,intflag);#endif
View Code

Level.c

#include <string.h>#include<stdlib.h>#include<errno.h>#include<syslog.h>#include"Zc_defs.h"#include"level.h"voidZlog_level_profile (zlog_level_t *a_level,intflag)        {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;}voidZlog_level_del (zlog_level_t *a_level)        {Zc_assert (a_level,);  Free(A_level); Zc_debug ("zlog_level_del[%p]", A_level); return;}Static intSyslog_level_atoi (Char*str) {        //notice-187Zc_assert (str,-187); if(stricmp (str, = =,"Log_emerg")){                returnLog_emerg; }        if(stricmp (str, = =,"Log_alert")){                returnLog_alert; }        if(stricmp (str, = =,"Log_crit")){                returnLog_crit; }        if(stricmp (str, = =,"Log_err")){                returnLog_err; }        if(stricmp (str, = =,"log_warning")){                returnlog_warning; }        if(stricmp (str, = =,"Log_notice")){                returnLog_notice; }        if(stricmp (str, = =,"Log_info")){                returnLog_info; }        if(stricmp (str, = =,"Log_debug")){                returnLog_debug; } zc_error ("wrong syslog level[%s]", str); return-187;}/*Line:trace = ten, Log_err*/zlog_level_t*zlog_level_new (Char*Line ) {zlog_level_t*a_level =NULL; intI, nscan, L =0; CharStr[maxlen_cfg_line +1]; CharSl[maxlen_cfg_line +1];        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 <2) {Zc_error ("level[%s], syntax wrong", line); returnNULL; }        //check level and str        if((L <0) || (L >255) {zc_error ("l[%d] Not in [0, 255], wrong", L); returnNULL; }        if(str[0] ==' /') {Zc_error ("str[0] = = 0"); returnNULL; } a_level=calloc(1,sizeof(zlog_level_t)); if(!a_level) {Zc_error ("calloc fail, errno[%d]", errno); returnNULL; } 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 = =-187) {Zc_error ("Syslog_level_atoi fail"); Gotoerr; }        }        //strncpy and ToUpper (str)         for(i =0; (I <sizeof(a_level->str_uppercase)-1) && Str[i]! =' /'; i++) {A_level->str_uppercase[i] =ToUpper (Str[i]); A_level->str_lowercase[i] =ToLower (Str[i]); }        //Notice:overflow        if(Str[i]! =' /') {Zc_error ("Not enough space for STR, str[%s] >%d", str, i); Gotoerr; }Else{a_level->str_uppercase[i] =' /'; A_level->str_lowercase[i] =' /'; } a_level->str_len =i; returnA_level;err:zc_error ("line[%s]", line);        Zlog_level_del (A_level); returnNULL;}
View Code

Test_level.c

#include"Zc_defs.h"#include"level.h"#include"zc_profile.c"#include"level.c"intMain () {zlog_level_t*a_level = Zlog_level_new ("TRACE = ten, Log_err");        Zlog_level_profile (A_level, zc_debug); Zlog_level_del (A_level);}

Zlog study notes (level)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.