zlog学习笔记(mdc)

mdc.h

#ifndef __zlog_mdc_h
#define __zlog_mdc_h

#include "zc_defs.h"

typedef struct zlog_mdc_s zlog_mdc_t;
struct zlog_mdc_s{
        zc_hashtable_t *tab;
};

zlog_mdc_t *zlog_mdc_new(void);
void zlog_mdc_del(zlog_mdc_t *a_mdc);
void zlog_mdc_profile(zlog_mdc_t *a_mdc, int flag);

void zlog_mdc_clean(zlog_mdc_t *a_mdc);
int zlog_mdc_put(zlog_mdc_t *a_mdc, const char *key, const char *value);
char *zlog_mdc_get(zlog_mdc_t *a_mdc, const char *key);
void zlog_mdc_remove(zlog_mdc_t *a_mdc, const char *key);

typedef struct zlog_mdc_kv_s{
        char key[MAXLEN_PATH + 1];
        char value[MAXLEN_PATH + 1];
        size_t value_len;
}zlog_mdc_kv_t;

zlog_mdc_kv_t *zlog_mdc_get_kv(zlog_mdc_t *a_mdc, const char *key);

#endif
View Code

 

mdc.c

/**
 * 文件作用
 */

#include 
#include 
#include 

#include "mdc.h"
#include "zc_defs.h"

void zlog_mdc_profile(zlog_mdc_t *a_mdc, int flag){
        zc_assert(a_mdc,);

        zc_hashtable_entry_t *a_entry;
        zlog_mdc_kv_t *a_mdc_kv;

        zc_hashtable_foreach(a_mdc->tab, a_entry){
                a_mdc_kv = a_entry->value;
                zc_profile(flag, "---mdc_kv[%p][%s]-[%s]---",
                                a_mdc_kv,
                                a_mdc_kv->key,
                                a_mdc_kv->value
                );
        }
        return;
}

//-------------------------------------------------------------------------
void zlog_mdc_del(zlog_mdc_t *a_mdc){
        zc_assert(a_mdc,);
        if(a_mdc->tab){
                zc_hashtable_del(a_mdc->tab);
        }
        free(a_mdc);
        zc_debug("zlog_mdc_del[%p]", a_mdc);
        return;
}

static void zlog_mdc_kv_del(zlog_mdc_kv_t *a_mdc_kv){
        free(a_mdc_kv);
        zc_debug("zlog_mdc_kv_del[%p]", a_mdc_kv);
        return;
} 

/**
 *      struct zlog_mdc_kv_s{
 *              char key[MAXLEN_PATH + 1];
 *              char value[MAXLEN_PATH + 1];
 *              size_t value_len;
 *      }
 */
static zlog_mdc_kv_t *zlog_mdc_kv_new(const char *key, const char *value){
        zlog_mdc_kv_t *a_mdc_kv;

        a_mdc_kv = (zlog_mdc_kv_t *)calloc(1, sizeof(zlog_mdc_kv_t));
        if(!a_mdc_kv){
                zc_error("calloc fail, errno[%d]", errno);
                return NULL;
        }

        snprintf(a_mdc_kv->key, sizeof(a_mdc_kv->key), "%s", key);
        a_mdc_kv->value_len = snprintf(a_mdc_kv->value, sizeof(a_mdc_kv->value), "%s", value);
        return a_mdc_kv;
}


/**
 * struct zlog_mdc_s{
 *              zc_hashtable_t *tab;
 * }
 */
zlog_mdc_t *zlog_mdc_new(void){
        zlog_mdc_t *a_mdc;

        a_mdc = calloc(1, sizeof(zlog_mdc_t));
        if(!a_mdc){
                zc_error("calloc fail, errno[%d]", errno);
                return NULL;
        }

        a_mdc->tab = zc_hashtable_new(
                20,
                zc_hashtable_str_hash,
                zc_hashtable_str_equal,
                NULL,
                (zc_hashtable_del_fn) zlog_mdc_kv_del
        );
        if(!a_mdc->tab){
                zc_error("zc_hashtable_new_fail");
                goto err;
        }

        return a_mdc;
err:
        zlog_mdc_del(a_mdc);
        return NULL;
}

int zlog_mdc_put(zlog_mdc_t *a_mdc, const char *key, const char *value){
        zlog_mdc_kv_t *a_mdc_kv;

        a_mdc_kv = zlog_mdc_kv_new(key, value);
        if(!a_mdc_kv){
                zc_error("zlog_new_kv_new failed");
                return -1;
        }

        if(zc_hashtable_put(a_mdc->tab, a_mdc_kv->key, a_mdc_kv)){
                zc_error("zc_hashtable_put fail");
                zlog_mdc_kv_del(a_mdc_kv);
                return -1;
        }

        return 0;
}

char *zlog_mdc_get(zlog_mdc_t *a_mdc, const char *key){
        zlog_mdc_kv_t *a_mdc_kv;

        a_mdc_kv = zc_hashtable_get(a_mdc->tab, key);
        if(!a_mdc_kv){
                zc_error("zc_hashtable_get fail");
                return NULL;
        }else{
                return a_mdc_kv->value;
        }
}

zlog_mdc_kv_t *zlog_mdc_get_kv(zlog_mdc_t *a_mdc, const char *key){
        zlog_mdc_kv_t *a_mdc_kv;

        a_mdc_kv = zc_hashtable_get(a_mdc->tab, key);
        if(!a_mdc_kv){
                zc_error("zc_hashtable_get fail");
                return NULL;
        }else{
                return a_mdc_kv;
        }
}

void zlog_mdc_remove(zlog_mdc_t *a_mdc, const char *key){
        zc_hashtable_remove(a_mdc->tab, key);
        return;
}

void zlog_mdc_clean(zlog_mdc_t *a_mdc){
        zc_hashtable_clean(a_mdc->tab);
        return;
}
View Code

 

test_mdc.c

#include 
#include <string.h>
#include 

#include "zc_defs.h"
#include "zc_hashtable.h"
#include "mdc.h"

#include "zc_profile.c"

#include "mdc.c"
#include "zc_hashtable.c"

int main(){
        zlog_mdc_t *a_mdc;
        a_mdc = zlog_mdc_new();
        if(!a_mdc){
                fprintf(stderr, "%s", "zlog_mdc_new fail");
                return -1;
        }

        zlog_mdc_put(a_mdc, "name", "jimmy");
        char *name = zlog_mdc_get(a_mdc, "name");
        printf("name = %s\n", name);

        time_t tt;
        struct tm timer;
        char time_str[20];
        time(&tt);
        localtime_r(&tt, &timer);
        strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", &timer);
        zlog_mdc_put(a_mdc, "time", time_str);
        char *ps = zlog_mdc_get(a_mdc, "time");
        printf("time = %s\n", ps);

        zlog_mdc_profile(a_mdc, ZC_DEBUG);
        zlog_mdc_clean(a_mdc);
        zlog_mdc_profile(a_mdc, ZC_DEBUG);

        zlog_mdc_del(a_mdc);
}

 

转载于:https://www.cnblogs.com/bai-jimmy/p/5410084.html

你可能感兴趣的:(zlog学习笔记(mdc))