• 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 <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    
    #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 <stdio.h>
    #include <string.h>
    #include <time.h>
    
    #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
    ", 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
    ", 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);
    }
  • 相关阅读:
    地址路由使用
    SpringMVC的各种参数绑定方式
    SpringMVC 的 Controller 返回各种视图的处理方式
    jQuery过滤选择器:not()方法使用介绍
    js数组的操作
    ligerui grid行编辑示例
    ilter()和find()的区别
    java 文件字节输出流
    java 文件字节输入流
    java 创建一个File文件对象
  • 原文地址:https://www.cnblogs.com/bai-jimmy/p/5410084.html
Copyright © 2020-2023  润新知