Commit 9c7f727b authored by Hardy Simpson's avatar Hardy Simpson

bugfix: after zlog_reload(), zlog_set_record still useful

parent ac3e3560
......@@ -33,11 +33,13 @@ zlog.pc
press.log*
callgrind*
err.log
doc/*
!doc/*.lyx
!doc/*.conf
!doc/*.txt
test_*
!test_*.c
!test_*.conf
!test_*.h
doc/*.lyx~
doc/*.pdf
doc/obj.think
doc/zlog.3
doc/zlogtest.xls
docdir = $(datadir)/doc/$(PACKAGE)
man3dir = $(mandir)/man3
doc_DATA = UsersGuide-EN.pdf \
UsersGuide-CN.pdf \
GettingStart-EN.txt \
GettingStart-CN.txt
EXTRA_DIST = $(doc_DATA) \
UsersGuide-EN.lyx \
UsersGuide-CN.lyx
UsersGuide-EN.pdf : UsersGuide-EN.lyx
lyx -f -e pdf2 $^
UsersGuide-CN.pdf : UsersGuide-CN.lyx
lyx -f -e pdf4 $^
clean-local :
-rm -f *.pdf
This diff is collapsed.
This diff is collapsed.
......@@ -36,6 +36,10 @@ libzlog_la_SOURCES = \
thread.h \
thread_table.c \
thread_table.h \
record.c \
record.h \
record_table.c \
record_table.h \
category.c \
category.h \
category_table.c \
......
......@@ -27,13 +27,13 @@
void zlog_category_table_profile(zc_hashtable_t * categories, int flag)
{
zc_hashtable_entry_t *a_entry;
zlog_category_t *a_categoryegory;
zlog_category_t *a_category;
zc_assert(categories,);
zc_profile(flag, "-category_table[%p]-", categories);
zc_hashtable_foreach(categories, a_entry) {
a_categoryegory = (zlog_category_t *) a_entry->value;
zlog_category_profile(a_categoryegory, flag);
a_category = (zlog_category_t *) a_entry->value;
zlog_category_profile(a_category, flag);
}
return;
}
......
/*
* 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 "zc_defs.h"
#include "record.h"
void zlog_record_profile(zlog_record_t *a_record, int flag)
{
zc_assert(a_record,);
zc_profile(flag, "--record:[%p][%s:%p]--", a_record, a_record->name, a_record->output);
return;
}
void zlog_record_del(zlog_record_t *a_record)
{
zc_assert(a_record,);
free(a_record);
zc_debug("zlog_record_del[%p]", a_record);
return;
}
zlog_record_t *zlog_record_new(char *name, zlog_record_fn output)
{
zlog_record_t *a_record;
int rc = 0;
zc_assert(name, NULL);
zc_assert(output, NULL);
a_record = calloc(1, sizeof(zlog_record_t));
if (!a_record) {
zc_error("calloc fail, errno[%d]", errno);
return NULL;
}
if (strlen(name) > sizeof(a_record->name) - 1) {
zc_error("name[%s] is too long", name);
goto _exit;
}
strcpy(a_record->name, name);
a_record->output = output;
_exit:
if (rc) {
zlog_record_del(a_record);
return NULL;
} else {
zlog_record_profile(a_record, ZC_DEBUG);
return a_record;
}
}
/*
* 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_record_h
#define __zlog_record_h
#include "zc_defs.h"
/* record is user-defined output function and it's name from configure file */
typedef int (*zlog_record_fn)(char *param, char *msg, size_t msg_len);
typedef struct zlog_record_s {
char name[MAXLEN_PATH + 1];
zlog_record_fn output;
} zlog_record_t;
zlog_record_t *zlog_record_new(char *name, zlog_record_fn output);
void zlog_record_del(zlog_record_t *a_record);
void zlog_record_profile(zlog_record_t *a_record, int flag);
#endif
/*
* 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 <string.h>
#include <stdlib.h>
#include <errno.h>
#include "zc_defs.h"
#include "record_table.h"
void zlog_record_table_profile(zc_hashtable_t * records, int flag)
{
zc_hashtable_entry_t *a_entry;
zlog_record_t *a_record;
zc_assert(records,);
zc_profile(flag, "-record_table[%p]-", records);
zc_hashtable_foreach(records, a_entry) {
a_record = (zlog_record_t *) a_entry->value;
zlog_record_profile(a_record, flag);
}
return;
}
/*******************************************************************************/
void zlog_record_table_del(zc_hashtable_t * records)
{
zc_assert(records,);
zc_hashtable_del(records);
zc_debug("zlog_record_table_del[%p]", records);
return;
}
zc_hashtable_t *zlog_record_table_new(void)
{
zc_hashtable_t *records;
records = zc_hashtable_new(20,
(zc_hashtable_hash_fn) zc_hashtable_str_hash,
(zc_hashtable_equal_fn) zc_hashtable_str_equal,
NULL, (zc_hashtable_del_fn) zlog_record_del);
if (!records) {
zc_error("zc_hashtable_new fail");
return NULL;
} else {
zlog_record_table_profile(records, ZC_DEBUG);
return records;
}
}
/*******************************************************************************/
/*
* 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_record_table_h
#define __zlog_record_table_h
#include "zc_defs.h"
#include "record.h"
zc_hashtable_t *zlog_record_table_new(void);
void zlog_record_table_del(zc_hashtable_t * records);
void zlog_record_table_profile(zc_hashtable_t * records, int flag);
#endif
......@@ -66,9 +66,9 @@ struct zlog_rule_s {
zlog_rule_output_fn output;
zlog_format_t *format;
char user_defined_str1[MAXLEN_PATH + 1];
char user_defined_str2[MAXLEN_PATH + 1];
zlog_record_fn record;
char record_name[MAXLEN_PATH + 1];
char record_param[MAXLEN_PATH + 1];
zlog_record_fn record_output;
};
void zlog_rule_profile(zlog_rule_t * a_rule, int flag)
......@@ -89,9 +89,9 @@ void zlog_rule_profile(zlog_rule_t * a_rule, int flag)
a_rule->file_max_count,
a_rule->syslog_facility,
a_rule->format,
a_rule->user_defined_str1,
a_rule->user_defined_str2,
a_rule->record);
a_rule->record_name,
a_rule->record_param,
a_rule->record_output);
if (a_rule->dynamic_file_specs) {
zc_arraylist_foreach(a_rule->dynamic_file_specs, i, a_spec) {
......@@ -320,7 +320,7 @@ static int zlog_rule_output_syslog(zlog_rule_t * a_rule, zlog_thread_t * a_threa
return 0;
}
static int zlog_rule_output_user_record(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
static int zlog_rule_output_record(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
{
int rc = 0;
char *msg;
......@@ -335,15 +335,15 @@ static int zlog_rule_output_user_record(zlog_rule_t * a_rule, zlog_thread_t * a_
msg = a_thread->msg_buf->start;
msg_len = a_thread->msg_buf->end - a_thread->msg_buf->start;
if (a_rule->record) {
rc = a_rule->record(a_rule->user_defined_str2, msg, msg_len);
if (a_rule->record_output) {
rc = a_rule->record_output(a_rule->record_param, msg, msg_len);
if (rc) {
zc_error("a_rule->record fail");
return -1;
}
} else {
zc_error("user defined record funcion for [%s,%s] not set, no outpu",
a_rule->user_defined_str1, a_rule->user_defined_str2);
zc_error("user defined record funcion for [%s,%s] not set, no output",
a_rule->record_name, a_rule->record_param);
return -1;
}
return 0;
......@@ -686,9 +686,9 @@ zlog_rule_t *zlog_rule_new(char *line,
goto zlog_rule_new_exit;
}
} else if (file_path[0] == '$') {
sscanf(file_path + 1, "%s", a_rule->user_defined_str1);
sscanf(file_limit, " %[^; ]", a_rule->user_defined_str2);
a_rule->output = zlog_rule_output_user_record;
sscanf(file_path + 1, "%s", a_rule->record_name);
sscanf(file_limit, " %[^; ]", a_rule->record_param);
a_rule->output = zlog_rule_output_record;
} else {
zc_error("the 1st char[%c] of file_path[%s] is wrong",
file_path[0], file_path);
......@@ -797,12 +797,15 @@ int zlog_rule_match_category(zlog_rule_t * a_rule, char *category)
/*******************************************************************************/
int zlog_rule_set_record(zlog_rule_t * a_rule, char *str1, zlog_record_fn record)
int zlog_rule_set_record(zlog_rule_t * a_rule, zc_hashtable_t *records)
{
if (a_rule->output != zlog_rule_output_user_record) return 0;
zlog_record_t *a_record;
if (STRCMP(str1, ==, a_rule->user_defined_str1)) {
a_rule->record = record;
if (a_rule->output != zlog_rule_output_record) return 0;
a_record = zc_hashtable_get(records, a_rule->record_name);
if (a_record) {
a_rule->record_output = a_record->output;
}
return 0;
}
......@@ -30,6 +30,7 @@
#include "format.h"
#include "thread.h"
#include "rotater.h"
#include "record.h"
typedef struct zlog_rule_s zlog_rule_t;
......@@ -47,7 +48,6 @@ int zlog_rule_output(zlog_rule_t * a_rule, zlog_thread_t * a_thread);
int zlog_rule_match_category(zlog_rule_t * a_rule, char *category);
int zlog_rule_is_wastebin(zlog_rule_t * a_rule);
typedef int (*zlog_record_fn)(char *str2, char *msg, size_t msg_len);
int zlog_rule_set_record(zlog_rule_t * a_rule, char *str1, zlog_record_fn record);
int zlog_rule_set_record(zlog_rule_t * a_rule, zc_hashtable_t *records);
#endif
......@@ -180,14 +180,14 @@ int zlog_thread_update_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, si
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) {
if (!a_thread->pre_msg_buf_backup && !a_thread->msg_buf_backup) {
zc_warn("backup is null, never update before");
return;
}
zlog_buf_del(a_thread->pre_msg_buf_backup);
if (a_thread->pre_msg_buf_backup) zlog_buf_del(a_thread->pre_msg_buf_backup);
a_thread->pre_msg_buf_backup = NULL;
zlog_buf_del(a_thread->msg_buf_backup);
if (a_thread->msg_buf_backup) zlog_buf_del(a_thread->msg_buf_backup);
a_thread->msg_buf_backup = NULL;
return;
}
......
......@@ -26,6 +26,7 @@
#include "conf.h"
#include "category_table.h"
#include "thread_table.h"
#include "record_table.h"
#include "mdc.h"
#include "zc_defs.h"
#include "rule.h"
......@@ -35,6 +36,7 @@ static pthread_rwlock_t zlog_env_lock = PTHREAD_RWLOCK_INITIALIZER;
static zlog_conf_t *zlog_env_conf;
static zc_hashtable_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 int zlog_env_init_flag = 0;
/*******************************************************************************/
......@@ -43,6 +45,7 @@ static void zlog_fini_inner(void)
{
if (zlog_env_threads) zc_hashtable_del(zlog_env_threads);
if (zlog_env_categories) zc_hashtable_del(zlog_env_categories);
if (zlog_env_records) zc_hashtable_del(zlog_env_records);
if (zlog_env_conf) zlog_conf_del(zlog_env_conf);
zlog_env_threads = NULL;
zlog_env_categories = NULL;
......@@ -79,6 +82,13 @@ static int zlog_init_inner(char *confpath)
goto zlog_init_inner_exit;
}
zlog_env_records = zlog_record_table_new();
if (!zlog_env_records) {
zc_error("zlog_record_table_new fail");
rc = -1;
goto zlog_init_inner_exit;
}
zlog_conf_get_buf_size(zlog_env_conf, &buf_size_min, &buf_size_max);
a_thread = zlog_thread_table_new_thread(zlog_env_threads,
......@@ -189,11 +199,14 @@ int zlog_reload(char *confpath)
{
int rc = 0;
int rd = 0;
int i = 0;
zlog_conf_t *new_conf = NULL;
zlog_rule_t *a_rule;
size_t new_buf_size_min;
size_t new_buf_size_max;
size_t old_buf_size_min;
size_t old_buf_size_max;
int do_thread_flag;
zc_debug("------zlog_reload start------");
rd = pthread_rwlock_wrlock(&zlog_env_lock);
......@@ -220,8 +233,11 @@ int zlog_reload(char *confpath)
goto zlog_reload_exit;
}
rc = zlog_category_table_update_rules(zlog_env_categories,
zlog_conf_get_rules(new_conf));
zc_arraylist_foreach(zlog_conf_get_rules(new_conf), i, a_rule) {
zlog_rule_set_record(a_rule, zlog_env_records);
}
rc = zlog_category_table_update_rules(zlog_env_categories, zlog_conf_get_rules(new_conf));
if (rc) {
zc_error("zlog_category_table_update fail");
rc = -1;
......@@ -231,8 +247,8 @@ int zlog_reload(char *confpath)
zlog_conf_get_buf_size(zlog_env_conf, &old_buf_size_min, &old_buf_size_max);
zlog_conf_get_buf_size(new_conf, &new_buf_size_min, &new_buf_size_max);
if ((new_buf_size_min != old_buf_size_min)
|| (new_buf_size_max != old_buf_size_max) ) {
if ((new_buf_size_min != old_buf_size_min) || (new_buf_size_max != old_buf_size_max) ) {
do_thread_flag = 1;
rc = zlog_thread_table_update_msg_buf(zlog_env_threads,
new_buf_size_min, new_buf_size_max);
if (rc) {
......@@ -240,6 +256,8 @@ int zlog_reload(char *confpath)
rc = -1;
goto zlog_reload_exit;
}
} else {
do_thread_flag = 0;
}
zlog_env_init_flag++;
......@@ -250,10 +268,10 @@ int zlog_reload(char *confpath)
zc_warn("zlog_reload fail, use old conf file, still working");
if (new_conf) zlog_conf_del(new_conf);
zlog_category_table_rollback_rules(zlog_env_categories);
zlog_thread_table_rollback_msg_buf(zlog_env_threads);
if (do_thread_flag) zlog_thread_table_rollback_msg_buf(zlog_env_threads);
} else {
zlog_category_table_commit_rules(zlog_env_categories);
zlog_thread_table_commit_msg_buf(zlog_env_threads);
if (do_thread_flag) zlog_thread_table_commit_msg_buf(zlog_env_threads);
zlog_conf_del(zlog_env_conf);
zlog_env_conf = new_conf;
}
......@@ -783,6 +801,7 @@ void zlog_profile(void)
zc_warn("------zlog_profile start------ ");
zc_warn("init_flag:[%d]", zlog_env_init_flag);
zlog_conf_profile(zlog_env_conf, ZC_WARN);
zlog_record_table_profile(zlog_env_records, ZC_WARN);
zlog_thread_table_profile(zlog_env_threads, ZC_WARN);
zlog_category_table_profile(zlog_env_categories, ZC_WARN);
if (zlog_default_category) {
......@@ -798,13 +817,16 @@ void zlog_profile(void)
return;
}
/*******************************************************************************/
int zlog_set_record(char *str1, zlog_record_fn record)
int zlog_set_record(char *name, zlog_record_fn record_output)
{
int rc = 0;
int rd = 0;
zlog_rule_t *a_rule;
zlog_record_t *a_record;
int i = 0;
zc_assert(str1, -1);
zc_assert(name, -1);
zc_assert(record_output, -1);
rd = pthread_rwlock_wrlock(&zlog_env_lock);
if (rd) {
......@@ -817,8 +839,22 @@ int zlog_set_record(char *str1, zlog_record_fn record)
goto zlog_set_record_exit;
}
a_record = zlog_record_new(name, record_output);
if (!a_record) {
rc = -1;
zc_error("zlog_record_new fail");
goto zlog_set_record_exit;
}
rc = zc_hashtable_put(zlog_env_records, a_record->name, a_record);
if (rc) {
zlog_record_del(a_record);
zc_error("zc_hashtable_put fail");
goto zlog_set_record_exit;
}
zc_arraylist_foreach(zlog_conf_get_rules(zlog_env_conf), i, a_rule) {
zlog_rule_set_record(a_rule, str1, record);
zlog_rule_set_record(a_rule, zlog_env_records);
}
zlog_set_record_exit:
......@@ -827,5 +863,5 @@ int zlog_set_record(char *str1, zlog_record_fn record)
zc_error("pthread_rwlock_unlock fail, rd=[%d]", rd);
return -1;
}
return 0;
return rc;
}
......@@ -36,8 +36,6 @@ void zlog_profile(void);
zlog_category_t *zlog_get_category(char *cname);
int zlog_put_mdc(char *key, char *value);
char *zlog_get_mdc(char *key);
void zlog_remove_mdc(char *key);
......@@ -63,8 +61,8 @@ void vdzlog(const char *file, size_t filelen, const char *func, size_t funclen,
void hdzlog(const char *file, size_t filelen, const char *func, size_t funclen, long line, int level,
const void *buf, size_t buflen);
typedef int (*zlog_record_fn)(char *str2, char *msg, size_t msg_len);
int zlog_set_record(char *str1, zlog_record_fn record);
typedef int (*zlog_record_fn)(char *param, char *msg, size_t msglen);
int zlog_set_record(char *name, zlog_record_fn record);
/******* useful macros, can be redefined at user's h file **********/
......
......@@ -22,7 +22,7 @@ noinst_PROGRAMS = \
test_level \
test_leak \
test_default \
test_user_record \
test_record \
test_profile
test_tmp_SOURCES = test_tmp.c
......
......@@ -10,6 +10,7 @@ exe = \
test_level \
test_leak \
test_mdc \
test_record \
test_press_zlog \
test_press_printf \
test_press_syslog \
......
......@@ -20,9 +20,9 @@
#include <stdio.h>
#include "zlog.h"
int mystd(char *str2, char *msg, size_t msg_len)
int output(char *str2, char *msg, size_t msg_len)
{
printf("[mystd]:[%s", msg);
printf("[mystd]:[%s][%s][%ld]\n", str2, msg, (long)msg_len);
return 0;
}
......@@ -31,13 +31,13 @@ int main(int argc, char** argv)
int rc;
zlog_category_t *zc;
rc = zlog_init("test_user_record.conf");
rc = zlog_init("test_record.conf");
if (rc) {
printf("init failed\n");
return -1;
}
zlog_set_record("mystd", mystd);
zlog_set_record("myoutput", output);
zc = zlog_get_category("my_cat");
if (!zc) {
......@@ -47,9 +47,6 @@ int main(int argc, char** argv)
}
ZLOG_INFO(zc, "hello, zlog");
zlog_profile();
zlog_fini();
return 0;
}
[formats]
simple = "%m%n"
[rules]
my_cat.* $mystd;simple
my_cat.* $myoutput, myparam;simple
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment