Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bugfix: let OS release zlog_thread_t when thread exit, simple zlog_re…

…load() and zlog_fini()
  • Loading branch information...
commit 15631df3f82bfc8309e6c83211cfa8e1c3088602 1 parent ab49e4d
@HardySimpson authored
View
7 TODO
@@ -1,19 +1,21 @@
[p] 使用valgrind测试性能
[ ] hzlog的可定制
+[ ] hex那段重写,内置到buf内,参考od的设计
[ ] 分类匹配的可定制化, rcat
[ ] 自行管理文件缓存,替代stdio
+[ ] 减少dynamic文件名open的次数,通过日期改变智能推断, file_table?
[ ] async file输出的增加
[ ] 兼容性问题 zlog.h内
[ ] 增加trace级别
-[ ] hex那段重写,内置到buf内
[ ] gettid()
[ ] 性能对比, log4x, pantheios, glog
[ ] perl, python, go, c++支持
[ ] redis对接,协议设计
[ ] 和rsyslog对接的问题
[ ] linux fsync->fdatasync, open..
-[ ] 减少dynamic文件名open的次数,通过日期改变智能推断, file_table?
+--- 1.2.4 ---
+[o] 不再维持thread_list链表,每次写日志时判断配置有无更新,来刷新每线程的缓存大小
--- 1.2.0 ---
[o] rotate和zip的方案
[o] 把一部分static变量化为全局变量,减少接口参数
@@ -73,7 +75,6 @@
[o] 文件的权限设置
[o] 输出函数自定义
--- 0.9 ---
-[o] 研究ilog读配置文件, buf
[o] 采用更加面向对象的方法来写
[o] 使用arraylist来代替linklist为内部数据结构
[o] 改进配置文件的格式
View
8 src/makefile
@@ -19,7 +19,6 @@ OBJ= \
rule.o \
spec.o \
thread.o \
- thread_list.o \
zc_arraylist.o \
zc_hashtable.o \
zc_profile.o \
@@ -101,9 +100,6 @@ spec.o: spec.c fmacros.h spec.h event.h zc_defs.h zc_profile.h \
mdc.h level_list.h level.h
thread.o: thread.c zc_defs.h zc_profile.h zc_arraylist.h zc_hashtable.h \
zc_xplatform.h zc_util.h event.h buf.h thread.h mdc.h
-thread_list.o: thread_list.c zc_defs.h zc_profile.h zc_arraylist.h \
- zc_hashtable.h zc_xplatform.h zc_util.h thread_list.h thread.h event.h \
- buf.h mdc.h
zc_arraylist.o: zc_arraylist.c zc_defs.h zc_profile.h zc_arraylist.h \
zc_hashtable.h zc_xplatform.h zc_util.h
zc_hashtable.o: zc_hashtable.c zc_defs.h zc_profile.h zc_arraylist.h \
@@ -114,7 +110,7 @@ zc_util.o: zc_util.c zc_defs.h zc_profile.h zc_arraylist.h zc_hashtable.h \
zlog-chk-conf.o: zlog-chk-conf.c fmacros.h zlog.h
zlog.o: zlog.c fmacros.h conf.h zc_defs.h zc_profile.h zc_arraylist.h \
zc_hashtable.h zc_xplatform.h zc_util.h format.h thread.h event.h buf.h \
- mdc.h rotater.h category_table.h category.h thread_list.h record_table.h \
+ mdc.h rotater.h category_table.h category.h record_table.h \
record.h rule.h
$(DYLIBNAME): $(OBJ)
@@ -129,7 +125,7 @@ static: $(STLIBNAME)
# Binaries:
zlog-chk-conf: zlog-chk-conf.o $(STLIBNAME)
- $(CC) -o $@ $(REAL_CFLAGS) $(REAL_LDFLAGS) zlog-chk-conf.o -lzlog
+ $(CC) -o $@ $(REAL_CFLAGS) zlog-chk-conf.o -static -L. -lzlog $(REAL_LDFLAGS)
.c.o:
$(CC) -std=c99 -pedantic -c $(REAL_CFLAGS) $<
View
89 src/thread.c
@@ -72,7 +72,7 @@ void zlog_thread_del(zlog_thread_t * a_thread)
return;
}
-zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max)
+zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max)
{
zlog_thread_t *a_thread;
@@ -82,6 +82,8 @@ zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max)
return NULL;
}
+ a_thread->init_version = init_version;
+
a_thread->mdc = zlog_mdc_new();
if (!a_thread->mdc) {
zc_error("zlog_mdc_new fail");
@@ -134,84 +136,43 @@ zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max)
}
/*******************************************************************************/
-int zlog_thread_update_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max)
+int zlog_thread_resize_msg_buf(zlog_thread_t * a_thread, int init_version, size_t buf_size_min, size_t buf_size_max)
{
+ zlog_buf_t *pre_msg_buf_new = NULL;
+ zlog_buf_t *msg_buf_new = NULL;
zc_assert(a_thread, -1);
- /* 1st, mv msg_buf msg_buf_backup */
- if (a_thread->pre_msg_buf_backup) zlog_buf_del(a_thread->pre_msg_buf_backup);
- if (a_thread->msg_buf_backup) zlog_buf_del(a_thread->msg_buf_backup);
- a_thread->pre_msg_buf_backup = a_thread->pre_msg_buf;
- a_thread->msg_buf_backup = a_thread->msg_buf;
-
+ if ( (a_thread->msg_buf->size_min == buf_size_min)
+ && (a_thread->msg_buf->size_max == buf_size_max)) {
+ zc_debug("buf size not changed, no need resize");
+ a_thread->init_version = init_version;
+ return 0;
+ }
- /* 2nd, gen new buf */
- a_thread->pre_msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
- if (!a_thread->pre_msg_buf) {
+ pre_msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
+ if (!pre_msg_buf_new) {
zc_error("zlog_buf_new fail");
goto err;
}
- a_thread->msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
- if (!a_thread->msg_buf) {
+ msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
+ if (!msg_buf_new) {
zc_error("zlog_buf_new fail");
goto err;
}
+ zlog_buf_del(a_thread->pre_msg_buf);
+ a_thread->pre_msg_buf = pre_msg_buf_new;
+
+ zlog_buf_del(a_thread->msg_buf);
+ a_thread->msg_buf = msg_buf_new;
+
+ a_thread->init_version = init_version;
return 0;
err:
- if (a_thread->pre_msg_buf) zlog_buf_del(a_thread->pre_msg_buf);
- if (a_thread->msg_buf) zlog_buf_del(a_thread->msg_buf);
- a_thread->pre_msg_buf = NULL;
- a_thread->msg_buf = NULL;
+ if (pre_msg_buf_new) zlog_buf_del(pre_msg_buf_new);
+ if (msg_buf_new) zlog_buf_del(msg_buf_new);
return -1;
}
-void zlog_thread_commit_msg_buf(zlog_thread_t * a_thread)
-{
- zc_assert(a_thread, );
- if (!a_thread->pre_msg_buf_backup && !a_thread->msg_buf_backup) {
- zc_warn("backup is null, never update before");
- return;
- }
-
- if (a_thread->pre_msg_buf_backup) zlog_buf_del(a_thread->pre_msg_buf_backup);
- a_thread->pre_msg_buf_backup = NULL;
- if (a_thread->msg_buf_backup) zlog_buf_del(a_thread->msg_buf_backup);
- a_thread->msg_buf_backup = NULL;
- return;
-}
-
-void zlog_thread_rollback_msg_buf(zlog_thread_t * a_thread)
-{
- zc_assert(a_thread,);
- if (!a_thread->pre_msg_buf_backup || !a_thread->msg_buf_backup) {
- zc_warn("backup is null, never update before");
- return;
- }
-
- if (a_thread->pre_msg_buf) {
- /* update success */
- zlog_buf_del(a_thread->pre_msg_buf);
- a_thread->pre_msg_buf = a_thread->pre_msg_buf_backup;
- a_thread->pre_msg_buf_backup = NULL;
- } else {
- /* update fail */
- a_thread->pre_msg_buf = a_thread->pre_msg_buf_backup;
- a_thread->pre_msg_buf_backup = NULL;
- }
-
- if (a_thread->msg_buf) {
- /* update success */
- zlog_buf_del(a_thread->msg_buf);
- a_thread->msg_buf = a_thread->msg_buf_backup;
- a_thread->msg_buf_backup = NULL;
- } else {
- /* update fail */
- a_thread->msg_buf = a_thread->msg_buf_backup;
- a_thread->msg_buf_backup = NULL;
- }
- return;
-}
-
/*******************************************************************************/
View
10 src/thread.h
@@ -26,6 +26,7 @@
#include "mdc.h"
typedef struct {
+ int init_version;
zlog_mdc_t *mdc;
zlog_event_t *event;
@@ -34,18 +35,13 @@ typedef struct {
zlog_buf_t *archive_path_buf;
zlog_buf_t *pre_msg_buf;
zlog_buf_t *msg_buf;
-
- zlog_buf_t *pre_msg_buf_backup;
- zlog_buf_t *msg_buf_backup;
} zlog_thread_t;
void zlog_thread_del(zlog_thread_t * a_thread);
void zlog_thread_profile(zlog_thread_t * a_thread, int flag);
-zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max);
+zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max);
-int zlog_thread_update_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max);
-void zlog_thread_commit_msg_buf(zlog_thread_t * a_thread);
-void zlog_thread_rollback_msg_buf(zlog_thread_t * a_thread);
+int zlog_thread_resize_msg_buf(zlog_thread_t * a_thread, int init_version, size_t buf_size_min, size_t buf_size_max);
#endif
View
131 src/thread_list.c
@@ -1,131 +0,0 @@
-/*
- * This file is part of the zlog Library.
- *
- * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
- *
- * The zlog Library is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * The zlog Library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the zlog Library. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <errno.h>
-#include <pthread.h>
-#include "zc_defs.h"
-#include "thread_list.h"
-
-
-void zlog_thread_list_profile(zc_arraylist_t * threads, int flag)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads,);
- zc_profile(flag, "--thread_list[%p]--", threads);
- zc_arraylist_foreach(threads, i, a_thread) {
- zlog_thread_profile(a_thread, flag);
- }
- return;
-}
-
-/*******************************************************************************/
-void zlog_thread_list_del(zc_arraylist_t * threads)
-{
- zc_assert(threads,);
- zc_arraylist_del(threads);
- zc_debug("zlog_thread_list_del[%p]", threads);
- return;
-}
-
-zc_arraylist_t *zlog_thread_list_new(void)
-{
- zc_arraylist_t *threads;
-
- threads = zc_arraylist_new((zc_arraylist_del_fn)zlog_thread_del);
- if (!threads) {
- zc_error("zc_arraylist_new fail");
- return NULL;
- } else {
- zlog_thread_list_profile(threads, ZC_DEBUG);
- return threads;
- }
-}
-
-/*******************************************************************************/
-int zlog_thread_list_update_msg_buf(zc_arraylist_t * threads, size_t buf_size_min, size_t buf_size_max)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads, -1);
- zc_arraylist_foreach(threads, i, a_thread) {
- if (zlog_thread_update_msg_buf(a_thread, buf_size_min, buf_size_max)) {
- zc_error("zlog_thread_update_msg_buf fail, try rollback");
- return -1;
- }
- }
-
- return 0;
-}
-
-void zlog_thread_list_commit_msg_buf(zc_arraylist_t * threads)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads,);
- zc_arraylist_foreach(threads, i, a_thread) {
- zlog_thread_commit_msg_buf(a_thread);
- }
- return;
-}
-
-void zlog_thread_list_rollback_msg_buf(zc_arraylist_t * threads)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads,);
- zc_arraylist_foreach(threads, i, a_thread) {
- zlog_thread_rollback_msg_buf(a_thread);
- }
- return;
-}
-
-/*******************************************************************************/
-zlog_thread_t *zlog_thread_list_new_thread(zc_arraylist_t * threads, pthread_key_t key,
- size_t buf_size_min, size_t buf_size_max)
-{
- int rc;
- zlog_thread_t *a_thread;
- a_thread = zlog_thread_new(buf_size_min, buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_new fail");
- return NULL;
- }
-
- if (zc_arraylist_add(threads, a_thread)) {
- zc_error("zc_arraylist_put fail");
- goto err;
- }
-
- rc = pthread_setspecific(key, a_thread);
- if (rc) {
- zc_error("pthread_setspecific fail, rc[%d]");
- goto err;
- }
-
- return a_thread;
-err:
- zlog_thread_del(a_thread);
- return NULL;
-}
-/*******************************************************************************/
View
38 src/thread_list.h
@@ -1,38 +0,0 @@
-/*
- * This file is part of the zlog Library.
- *
- * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
- *
- * The zlog Library is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * The zlog Library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the zlog Library. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef __zlog_thread_list_h
-#define __zlog_thread_list_h
-
-#include <pthread.h>
-#include "zc_defs.h"
-#include "thread.h"
-
-zc_arraylist_t *zlog_thread_list_new(void);
-void zlog_thread_list_del(zc_arraylist_t *threads);
-void zlog_thread_list_profile(zc_arraylist_t *threads, int flag);
-
-int zlog_thread_list_update_msg_buf(zc_arraylist_t * threads, size_t buf_size_min, size_t buf_size_max);
-void zlog_thread_list_commit_msg_buf(zc_arraylist_t * threads);
-void zlog_thread_list_rollback_msg_buf(zc_arraylist_t * threads);
-
-zlog_thread_t *zlog_thread_list_new_thread(zc_arraylist_t * threads, pthread_key_t key,
- size_t buf_size_min, size_t buf_size_max);
-
-#endif
View
230 src/zlog.c
@@ -27,7 +27,6 @@
#include "conf.h"
#include "category_table.h"
-#include "thread_list.h"
#include "record_table.h"
#include "mdc.h"
#include "zc_defs.h"
@@ -39,19 +38,16 @@ extern char *zlog_git_sha1;
static pthread_rwlock_t zlog_env_lock = PTHREAD_RWLOCK_INITIALIZER;
zlog_conf_t *zlog_env_conf;
static pthread_key_t zlog_thread_key;
-static zc_arraylist_t *zlog_env_threads;
static zc_hashtable_t *zlog_env_categories;
static zc_hashtable_t *zlog_env_records;
static zlog_category_t *zlog_default_category;
static size_t zlog_env_reload_conf_count;
-static int zlog_env_init_flag = -1;
+static int zlog_env_init_version = -1;
/*******************************************************************************/
/* inner no need thread-safe */
static void zlog_fini_inner(void)
{
pthread_key_delete(zlog_thread_key);
- if (zlog_env_threads) zlog_thread_list_del(zlog_env_threads);
- zlog_env_threads = NULL;
if (zlog_env_categories) zlog_category_table_del(zlog_env_categories);
zlog_env_categories = NULL;
zlog_default_category = NULL;
@@ -65,7 +61,6 @@ static void zlog_fini_inner(void)
static int zlog_init_inner(const char *confpath)
{
int rc;
- zlog_thread_t *a_thread;
zlog_env_conf = zlog_conf_new(confpath);
if (!zlog_env_conf) {
@@ -79,34 +74,20 @@ static int zlog_init_inner(const char *confpath)
goto err;
}
- /* clean up is done by arraylist of threads, not by OS */
- rc = pthread_key_create(&zlog_thread_key, NULL);
+ /* clean up is done by OS */
+ rc = pthread_key_create(&zlog_thread_key,
+ (void (*) (void *)) zlog_thread_del);
if (rc) {
zc_error("pthread_key_create fail, rc[%d]", rc);
goto err;
}
- zlog_env_threads = zlog_thread_list_new();
- if (!zlog_env_threads) {
- zc_error("zlog_thread_list_new fail");
- goto err;
- }
-
- /* create zlog_thread in init thread */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto err;
- }
-
zlog_env_records = zlog_record_table_new();
if (!zlog_env_records) {
zc_error("zlog_record_table_new fail");
goto err;
}
-
return 0;
err:
zlog_fini_inner();
@@ -127,7 +108,7 @@ int zlog_init(const char *confpath)
return -1;
}
- if (zlog_env_init_flag > 0) {
+ if (zlog_env_init_version > 0) {
zc_error("already init, use zlog_reload pls");
goto err;
}
@@ -137,7 +118,7 @@ int zlog_init(const char *confpath)
goto err;
}
- zlog_env_init_flag = 1;
+ zlog_env_init_version = 1;
zc_debug("------zlog_init success end------");
rc = pthread_rwlock_unlock(&zlog_env_lock);
@@ -168,7 +149,7 @@ int dzlog_init(const char *confpath, const char *cname)
return -1;
}
- if (zlog_env_init_flag > 0) {
+ if (zlog_env_init_version > 0) {
zc_error("already init, use zlog_reload pls");
goto err;
}
@@ -187,7 +168,7 @@ int dzlog_init(const char *confpath, const char *cname)
goto err;
}
- zlog_env_init_flag = 1;
+ zlog_env_init_version = 1;
zc_debug("------dzlog_init success end------");
rc = pthread_rwlock_unlock(&zlog_env_lock);
@@ -212,7 +193,6 @@ int zlog_reload(const char *confpath)
int i = 0;
zlog_conf_t *new_conf = NULL;
zlog_rule_t *a_rule;
- int t_up = 0;
int c_up = 0;
zc_debug("------zlog_reload start------");
@@ -222,7 +202,7 @@ int zlog_reload(const char *confpath)
return -1;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("never zlog_init before");
goto quit;
}
@@ -262,27 +242,12 @@ int zlog_reload(const char *confpath)
c_up = 1;
}
- if ((new_conf->buf_size_min != zlog_env_conf->buf_size_min) ||
- (new_conf->buf_size_max != zlog_env_conf->buf_size_max) ) {
- if (zlog_thread_list_update_msg_buf(zlog_env_threads,
- new_conf->buf_size_min, new_conf->buf_size_max)) {
- t_up = 0;
- zc_error("zlog_thread_list_update_msg_buf fail");
- goto err;
- } else {
- t_up = 1;
- }
- } else {
- t_up = 0;
- }
-
- zlog_env_init_flag++;
+ zlog_env_init_version++;
if (c_up) zlog_category_table_commit_rules(zlog_env_categories);
- if (t_up) zlog_thread_list_commit_msg_buf(zlog_env_threads);
zlog_conf_del(zlog_env_conf);
zlog_env_conf = new_conf;
- zc_debug("------zlog_reload [%d] times end, success------", zlog_env_init_flag);
+ zc_debug("------zlog_reload [%d] times end, success------", zlog_env_init_version);
rc = pthread_rwlock_unlock(&zlog_env_lock);
if (rc) {
zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
@@ -294,8 +259,7 @@ int zlog_reload(const char *confpath)
zc_warn("zlog_reload fail, use old conf file, still working");
if (new_conf) zlog_conf_del(new_conf);
if (c_up) zlog_category_table_rollback_rules(zlog_env_categories);
- if (t_up) zlog_thread_list_rollback_msg_buf(zlog_env_threads);
- zc_error("------zlog_reload [%d] times end, fail------", zlog_env_init_flag);
+ zc_error("------zlog_reload [%d] times end, fail------", zlog_env_init_version);
rc = pthread_rwlock_unlock(&zlog_env_lock);
if (rc) {
zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
@@ -323,13 +287,13 @@ void zlog_fini(void)
return;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before finish, must zlog_init fisrt");
goto exit;
}
zlog_fini_inner();
- zlog_env_init_flag = 0;
+ zlog_env_init_version = 0;
exit:
zc_debug("------zlog_fini end------");
@@ -354,7 +318,7 @@ zlog_category_t *zlog_get_category(const char *cname)
return NULL;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
a_category = NULL;
goto err;
@@ -398,7 +362,7 @@ int dzlog_set_category(const char *cname)
return -1;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto err;
}
@@ -429,6 +393,36 @@ int dzlog_set_category(const char *cname)
return -1;
}
/*******************************************************************************/
+#define zlog_fetch_thread(fail_goto) do { \
+ int rd = 0; \
+ a_thread = pthread_getspecific(zlog_thread_key); \
+ if (!a_thread) { \
+ a_thread = zlog_thread_new(zlog_env_init_version, \
+ zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max); \
+ if (!a_thread) { \
+ zc_error("zlog_thread_new fail"); \
+ goto fail_goto; \
+ } \
+ \
+ rd = pthread_setspecific(zlog_thread_key, a_thread); \
+ if (rd) { \
+ zlog_thread_del(a_thread); \
+ zc_error("pthread_setspecific fail, rd[%d]", rd); \
+ goto fail_goto; \
+ } \
+ } \
+ \
+ if (a_thread->init_version != zlog_env_init_version) { \
+ rd = zlog_thread_resize_msg_buf(a_thread, zlog_env_init_version, \
+ zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max); \
+ if (rd) { \
+ zc_error("zlog_thread_resize_msg_buf fail, rd[%d]", rd); \
+ goto fail_goto; \
+ } \
+ } \
+} while (0)
+
+/*******************************************************************************/
int zlog_put_mdc(const char *key, const char *value)
{
int rc = 0;
@@ -443,31 +437,12 @@ int zlog_put_mdc(const char *key, const char *value)
return -1;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto err;
}
- a_thread = pthread_getspecific(zlog_thread_key);
- if (!a_thread) {
- rc = pthread_rwlock_unlock(&zlog_env_lock);
- if (rc) {
- zc_error("pthread_rwlock_unlock fail, rc[%d]", rc);
- goto err;
- }
- rc = pthread_rwlock_wrlock(&zlog_env_lock);
- if (rc) {
- zc_error("pthread_rwlock_unlock fail, rc[%d]", rc);
- goto err;
- }
- /* change to wrlock, create and associate */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto err;
- }
- }
+ zlog_fetch_thread(err);
if (zlog_mdc_put(a_thread->mdc, key, value)) {
zc_error("zlog_mdc_put fail, key[%s], value[%s]", key, value);
@@ -503,7 +478,7 @@ char *zlog_get_mdc(char *key)
return NULL;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto err;
}
@@ -548,7 +523,7 @@ void zlog_remove_mdc(char *key)
return;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
@@ -581,7 +556,7 @@ void zlog_clean_mdc(void)
return;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
@@ -626,24 +601,12 @@ void vzlog(zlog_category_t * category,
pthread_rwlock_rdlock(&zlog_env_lock);
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
- a_thread = pthread_getspecific(zlog_thread_key);
- if (!a_thread) {
- pthread_rwlock_unlock(&zlog_env_lock);
-
- pthread_rwlock_wrlock(&zlog_env_lock);
- /* change to wrlock, create and associate */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto exit;
- }
- }
+ zlog_fetch_thread(exit);
zlog_event_set_fmt(a_thread->event,
category->name, category->name_len,
@@ -685,24 +648,12 @@ void hzlog(zlog_category_t *category,
pthread_rwlock_rdlock(&zlog_env_lock);
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
- a_thread = pthread_getspecific(zlog_thread_key);
- if (!a_thread) {
- pthread_rwlock_unlock(&zlog_env_lock);
-
- pthread_rwlock_wrlock(&zlog_env_lock);
- /* change to wrlock, create and associate */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto exit;
- }
- }
+ zlog_fetch_thread(exit);
zlog_event_set_hex(a_thread->event,
category->name, category->name_len,
@@ -745,7 +696,7 @@ void vdzlog(const char *file, size_t filelen,
pthread_rwlock_rdlock(&zlog_env_lock);
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
@@ -757,19 +708,7 @@ void vdzlog(const char *file, size_t filelen,
goto exit;
}
- a_thread = pthread_getspecific(zlog_thread_key);
- if (!a_thread) {
- pthread_rwlock_unlock(&zlog_env_lock);
-
- pthread_rwlock_wrlock(&zlog_env_lock);
- /* change to wrlock, create and associate */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto exit;
- }
- }
+ zlog_fetch_thread(exit);
zlog_event_set_fmt(a_thread->event,
zlog_default_category->name, zlog_default_category->name_len,
@@ -810,7 +749,7 @@ void hdzlog(const char *file, size_t filelen,
pthread_rwlock_rdlock(&zlog_env_lock);
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
@@ -822,19 +761,7 @@ void hdzlog(const char *file, size_t filelen,
goto exit;
}
- a_thread = pthread_getspecific(zlog_thread_key);
- if (!a_thread) {
- pthread_rwlock_unlock(&zlog_env_lock);
-
- pthread_rwlock_wrlock(&zlog_env_lock);
- /* change to wrlock, create and associate */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto exit;
- }
- }
+ zlog_fetch_thread(exit);
zlog_event_set_hex(a_thread->event,
zlog_default_category->name, zlog_default_category->name_len,
@@ -877,24 +804,12 @@ void zlog(zlog_category_t * category,
pthread_rwlock_rdlock(&zlog_env_lock);
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
- a_thread = pthread_getspecific(zlog_thread_key);
- if (!a_thread) {
- pthread_rwlock_unlock(&zlog_env_lock);
-
- pthread_rwlock_wrlock(&zlog_env_lock);
- /* change to wrlock, create and associate */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto exit;
- }
- }
+ zlog_fetch_thread(exit);
va_start(args, format);
zlog_event_set_fmt(a_thread->event,
@@ -903,6 +818,7 @@ void zlog(zlog_category_t * category,
format, args);
if (zlog_category_output(category, a_thread)) {
zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
+ va_end(args);
goto exit;
}
va_end(args);
@@ -936,7 +852,7 @@ void dzlog(const char *file, size_t filelen, const char *func, size_t funclen, l
pthread_rwlock_rdlock(&zlog_env_lock);
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto exit;
}
@@ -948,19 +864,7 @@ void dzlog(const char *file, size_t filelen, const char *func, size_t funclen, l
goto exit;
}
- a_thread = pthread_getspecific(zlog_thread_key);
- if (!a_thread) {
- pthread_rwlock_unlock(&zlog_env_lock);
-
- pthread_rwlock_wrlock(&zlog_env_lock);
- /* change to wrlock, create and associate */
- a_thread = zlog_thread_list_new_thread(zlog_env_threads, zlog_thread_key,
- zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_list_new_thread fail");
- goto exit;
- }
- }
+ zlog_fetch_thread(exit);
va_start(args, format);
zlog_event_set_fmt(a_thread->event,
@@ -970,6 +874,7 @@ void dzlog(const char *file, size_t filelen, const char *func, size_t funclen, l
if (zlog_category_output(zlog_default_category, a_thread)) {
zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
+ va_end(args);
goto exit;
}
va_end(args);
@@ -1002,10 +907,9 @@ void zlog_profile(void)
return;
}
zc_warn("------zlog_profile start------ ");
- zc_warn("init_flag:[%d]", zlog_env_init_flag);
+ zc_warn("init_flag:[%d]", zlog_env_init_version);
zlog_conf_profile(zlog_env_conf, ZC_WARN);
zlog_record_table_profile(zlog_env_records, ZC_WARN);
- zlog_thread_list_profile(zlog_env_threads, ZC_WARN);
zlog_category_table_profile(zlog_env_categories, ZC_WARN);
if (zlog_default_category) {
zc_warn("-default_category-");
@@ -1037,7 +941,7 @@ int zlog_set_record(const char *rname, zlog_record_fn record_output)
return -1;
}
- if (zlog_env_init_flag < 0) {
+ if (zlog_env_init_version < 0) {
zc_error("before use, must zlog_init first!!!");
goto zlog_set_record_exit;
}
View
4 test/test_press_zlog.conf
@@ -9,7 +9,7 @@ default format = "%D.%us %-6V %p:%T:%F:%L %m%n"
#*.* | /usr/bin/cronolog press%Y%m%d.log
# 2.081s 2.190s 1.420s
-#*.* "press.log"
+*.* "press.log"
# 2.371s 1.850s 2.610s
#*.* "press.log",10M
@@ -21,6 +21,6 @@ default format = "%D.%us %-6V %p:%T:%F:%L %m%n"
#*.* "press%d(%Y%m%d).log",1M*5
# 4.602s 2.710s 6.050s
-*.* "press.%d(%F).log",1MB ~ "press.#2r.log"
+#*.* "press.%d(%F).log",1MB ~ "press.#2r.log"
# 4.774s 2.730s 6.120s
Please sign in to comment.
Something went wrong with that request. Please try again.