Commit 35a68d34 authored by Hardy Simpson's avatar Hardy Simpson

feature: user-defined record fuction is done!

parent 21b77e39
......@@ -21,4 +21,4 @@ my_.INFO >stderr;
my_cat.!ERROR "/var/log/aa.log"
my_dog.=DEBUG >syslog, LOG_LOCAL0; simple
my_fish.warn |/var/pipe; normal
my_mice.* @user_define;
my_mice.* $user_define_str1 , user_define_str2 ; normal
......@@ -65,6 +65,10 @@ 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;
};
void zlog_rule_profile(zlog_rule_t * a_rule, int flag)
......@@ -73,7 +77,7 @@ void zlog_rule_profile(zlog_rule_t * a_rule, int flag)
zlog_spec_t *a_spec;
zc_assert(a_rule,);
zc_profile(flag, "---rule:[%p][%s%c%d]-[%s(0%o)|%p,%ld*%d|%d;%p]---",
zc_profile(flag, "---rule:[%p][%s%c%d]-[%s(0%o)|%p,%ld*%d|%d;%p][%s:%s:%p]---",
a_rule,
a_rule->category,
a_rule->compare_char,
......@@ -84,7 +88,10 @@ void zlog_rule_profile(zlog_rule_t * a_rule, int flag)
a_rule->file_max_size,
a_rule->file_max_count,
a_rule->syslog_facility,
a_rule->format);
a_rule->format,
a_rule->user_defined_str1,
a_rule->user_defined_str2,
a_rule->record);
if (a_rule->dynamic_file_specs) {
zc_arraylist_foreach(a_rule->dynamic_file_specs, i, a_spec) {
......@@ -95,8 +102,7 @@ void zlog_rule_profile(zlog_rule_t * a_rule, int flag)
}
/*******************************************************************************/
static int zlog_rule_output_static_file_single(zlog_rule_t * a_rule,
zlog_thread_t * a_thread)
static int zlog_rule_output_static_file_single(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
{
int rc = 0;
int fd = 0;
......@@ -131,8 +137,7 @@ static int zlog_rule_output_static_file_single(zlog_rule_t * a_rule,
return 0;
}
static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule,
zlog_thread_t * a_thread)
static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
{
int rc = 0;
int fd;
......@@ -180,8 +185,7 @@ static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule,
/* return 0 success
* return !=0 fail
*/
static int zlog_rule_gen_path(zlog_rule_t * a_rule, zlog_thread_t * a_thread,
char **file_path)
static int zlog_rule_gen_path(zlog_rule_t * a_rule, zlog_thread_t * a_thread, char **file_path)
{
int rc = 0;
int i;
......@@ -201,8 +205,7 @@ static int zlog_rule_gen_path(zlog_rule_t * a_rule, zlog_thread_t * a_thread,
return 0;
}
static int zlog_rule_output_dynamic_file_single(zlog_rule_t * a_rule,
zlog_thread_t * a_thread)
static int zlog_rule_output_dynamic_file_single(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
{
int rc = 0;
ssize_t nwrite = 0;
......@@ -242,8 +245,7 @@ static int zlog_rule_output_dynamic_file_single(zlog_rule_t * a_rule,
return 0;
}
static int zlog_rule_output_dynamic_file_rotate(zlog_rule_t * a_rule,
zlog_thread_t * a_thread)
static int zlog_rule_output_dynamic_file_rotate(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
{
int rc = 0;
ssize_t nwrite = 0;
......@@ -294,8 +296,7 @@ static int zlog_rule_output_dynamic_file_rotate(zlog_rule_t * a_rule,
return 0;
}
static int zlog_rule_output_syslog(zlog_rule_t * a_rule,
zlog_thread_t * a_thread)
static int zlog_rule_output_syslog(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
{
int rc = 0;
char *msg;
......@@ -319,6 +320,35 @@ static int zlog_rule_output_syslog(zlog_rule_t * a_rule,
return 0;
}
static int zlog_rule_output_user_record(zlog_rule_t * a_rule, zlog_thread_t * a_thread)
{
int rc = 0;
char *msg;
size_t msg_len;
rc = zlog_format_gen_msg(a_rule->format, a_thread);
if (rc) {
zc_error("zlog_format_gen_msg fail");
return -1;
}
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 (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);
return -1;
}
return 0;
}
static int zlog_rule_output_stdout(zlog_rule_t * a_rule,
zlog_thread_t * a_thread)
{
......@@ -655,6 +685,10 @@ zlog_rule_t *zlog_rule_new(char *line,
rc = -1;
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;
} else {
zc_error("the 1st char[%c] of file_path[%s] is wrong",
file_path[0], file_path);
......@@ -761,3 +795,14 @@ int zlog_rule_match_category(zlog_rule_t * a_rule, char *category)
return 0;
}
/*******************************************************************************/
int zlog_rule_set_record(zlog_rule_t * a_rule, char *str1, zlog_record_fn record)
{
if (a_rule->output != zlog_rule_output_user_record) return 0;
if (STRCMP(str1, ==, a_rule->user_defined_str1)) {
a_rule->record = record;
}
return 0;
}
......@@ -43,9 +43,11 @@ zlog_rule_t *zlog_rule_new(char *line,
void zlog_rule_del(zlog_rule_t * a_rule);
void zlog_rule_profile(zlog_rule_t * a_rule, int flag);
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);
#endif
......@@ -28,6 +28,7 @@
#include "thread_table.h"
#include "mdc.h"
#include "zc_defs.h"
#include "rule.h"
/*******************************************************************************/
static pthread_rwlock_t zlog_env_lock = PTHREAD_RWLOCK_INITIALIZER;
......@@ -796,3 +797,35 @@ void zlog_profile(void)
}
return;
}
/*******************************************************************************/
int zlog_set_record(char *str1, zlog_record_fn record)
{
int rd = 0;
zlog_rule_t *a_rule;
int i = 0;
zc_assert(str1, -1);
rd = pthread_rwlock_wrlock(&zlog_env_lock);
if (rd) {
zc_error("pthread_rwlock_rdlock fail, rd[%d]", rd);
return -1;
}
if (zlog_env_init_flag <= 0) {
zc_error("before use, must zlog_init first!!!");
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_set_record_exit:
rd = pthread_rwlock_unlock(&zlog_env_lock);
if (rd) {
zc_error("pthread_rwlock_unlock fail, rd=[%d]", rd);
return -1;
}
return 0;
}
......@@ -36,6 +36,8 @@ 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);
......@@ -61,6 +63,9 @@ 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);
/******* useful macros, can be redefined at user's h file **********/
typedef enum {
......
......@@ -22,6 +22,7 @@ noinst_PROGRAMS = \
test_level \
test_leak \
test_default \
test_user_record \
test_profile
test_tmp_SOURCES = test_tmp.c
......
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson@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 <stdio.h>
#include "zlog.h"
int mystd(char *str2, char *msg, size_t msg_len)
{
printf("[mystd]:[%s", msg);
return 0;
}
int main(int argc, char** argv)
{
int rc;
zlog_category_t *zc;
rc = zlog_init("test_user_record.conf");
if (rc) {
printf("init failed\n");
return -1;
}
zlog_set_record("mystd", mystd);
zc = zlog_get_category("my_cat");
if (!zc) {
printf("get cat fail\n");
zlog_fini();
return -2;
}
ZLOG_INFO(zc, "hello, zlog");
zlog_profile();
zlog_fini();
return 0;
}
[global]
strict init = true
buffer min = 1024
buffer max = 0
rotate lock file = /tmp/zlog.lock
[formats]
simple = "%m%n"
[rules ]
my_cat.* $mystd;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