Commit 5895237d authored by Hardy Simpson's avatar Hardy Simpson

optimize: a little safe opt

parent 55294ed4
......@@ -166,13 +166,9 @@ zlog_buf_t *zlog_buf_new(size_t buf_size_min, size_t buf_size_max,
/*******************************************************************************/
int zlog_buf_printf(zlog_buf_t * a_buf, const char *format, ...)
{
int rc = 0;
int rc;
va_list args;
if (format == NULL) {
return 0;
}
va_start(args, format);
rc = zlog_buf_vprintf(a_buf, format, args);
va_end(args);
......@@ -256,15 +252,11 @@ static int zlog_buf_resize(zlog_buf_t * a_buf, size_t increment)
int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args)
{
int rc = 0;
int rc;
va_list ap;
size_t size_left;
int nwrite;
if (format == NULL) {
return 0;
}
if (a_buf->size_real < 0) {
zc_error("pre-use of zlog_buf_resize fail, so can't convert");
return -1;
......@@ -320,12 +312,13 @@ int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args)
/*******************************************************************************/
int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len)
{
int rc = 0;
size_t size_left;
#if 0
if (str_len <= 0 || str == NULL) {
return 0;
}
#endif
if (a_buf->size_real < 0) {
zc_error("pre-use of zlog_buf_resize fail, so can't convert");
......@@ -334,6 +327,7 @@ int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len)
size_left = a_buf->size_real - (a_buf->end - a_buf->start);
if (str_len > size_left - 1) {
int rc;
zc_debug("size_left not enough, resize");
rc = zlog_buf_resize(a_buf, str_len - size_left + 1);
if (rc > 0) {
......@@ -369,9 +363,11 @@ int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
size_t source_len = 0;
size_t space_len = 0;
#if 0
if (str_len <= 0 || str == NULL) {
return 0;
}
#endif
if (a_buf->size_real < 0) {
zc_error("pre-use of zlog_buf_resize fail, so can't convert");
......@@ -454,14 +450,9 @@ int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
int zlog_buf_strftime(zlog_buf_t * a_buf, const char *time_fmt, size_t time_len,
const struct tm *a_tm)
{
int rc = 0;
size_t size_left;
size_t nwrite;
if (time_len <= 0) {
return 0;
}
if (a_buf->size_real < 0) {
zc_error("pre-use of zlog_buf_resize fail, so can't convert");
return -1;
......@@ -469,6 +460,7 @@ int zlog_buf_strftime(zlog_buf_t * a_buf, const char *time_fmt, size_t time_len,
size_left = a_buf->size_real - (a_buf->end - a_buf->start);
if (time_len > size_left - 1) {
int rc;
zc_debug("size_left not enough, resize");
rc = zlog_buf_resize(a_buf, time_len - size_left + 1);
if (rc > 0) {
......@@ -495,8 +487,7 @@ int zlog_buf_strftime(zlog_buf_t * a_buf, const char *time_fmt, size_t time_len,
*(a_buf->end) = '\0';
if (nwrite <= 0) {
zc_error
("strftime maybe failed or output 0 char, nwrite[%d], time_fmt[%s]",
zc_error("strftime maybe failed or output 0 char, nwrite[%d], time_fmt[%s]",
nwrite, time_fmt);
}
......
......@@ -51,7 +51,7 @@ typedef struct {
zlog_event_cmd generate_cmd;
struct timeval time_stamp;
time_t last_sec;
volatile time_t last_sec;
struct tm local_time;
char us[6 + 1];
char time_fmt_msus[MAXLEN_CFG_LINE + 1];
......
......@@ -177,7 +177,7 @@ zlog_format_t *zlog_format_new(char *line, zc_arraylist_t *levels)
*/
int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread)
{
int rc = 0;
int rc;
int i;
zlog_spec_t *a_spec;
......@@ -185,11 +185,10 @@ int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread)
zc_arraylist_foreach(a_format->pattern_specs, i, a_spec) {
rc = zlog_spec_gen_msg(a_spec, a_thread);
switch (rc) {
case -1:
if (rc < 0) {
zc_error("zlog_spec_gen_msg fail");
return -1;
case 1:
} else if (rc > 0) {
zc_error("zlog_spec_gen_msg, buffer is full");
break;
}
......
......@@ -135,7 +135,7 @@ zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l)
if (!a_level) {
/* empty slot */
zc_error("l[%d] in (0,254), but has no level defined,"
"see configure file define, set ot UNKOWN", l);
"see configure file define, set to UNKOWN", l);
a_level = zc_arraylist_get(levels, 254);
}
......
......@@ -39,8 +39,7 @@
#include "zc_defs.h"
typedef int (*zlog_rule_output_fn) (zlog_rule_t * a_rule,
zlog_thread_t * a_thread);
typedef int (*zlog_rule_output_fn) (zlog_rule_t * a_rule, zlog_thread_t * a_thread);
struct zlog_rule_s {
char category[MAXLEN_CFG_LINE + 1];
......@@ -136,7 +135,7 @@ static int zlog_rule_output_static_file_single(zlog_rule_t * a_rule, zlog_thread
return -1;
}
if (a_rule->fsync_period && ++a_rule->fsync_count > a_rule->fsync_period) {
if (a_rule->fsync_period && ++a_rule->fsync_count >= a_rule->fsync_period) {
a_rule->fsync_count = 0;
if (fflush(a_rule->static_file_stream)) {
zc_error("fflush[%p] fail, errno[%d]", a_rule->static_file_stream, errno);
......@@ -174,7 +173,7 @@ static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule, zlog_thread
return -1;
}
if (a_rule->fsync_period && ++a_rule->fsync_count > a_rule->fsync_period) {
if (a_rule->fsync_period && ++a_rule->fsync_count >= a_rule->fsync_period) {
a_rule->fsync_count = 0;
if (fflush(a_rule->static_file_stream)) {
zc_error("fflush[%p] fail, errno[%d]", a_rule->static_file_stream, errno);
......@@ -212,7 +211,7 @@ static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule, zlog_thread
}
a_rule->static_file_descriptor = open(a_rule->file_path,
a_rule->file_open_flags | O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
if (a_rule->static_file_descriptor < 0) {
zc_error("open file[%s] fail, errno[%d]", a_rule->file_path, errno);
rc = -1;
......@@ -292,7 +291,7 @@ static int zlog_rule_output_dynamic_file_single(zlog_rule_t * a_rule, zlog_threa
return -1;
}
if (a_rule->fsync_period && ++a_rule->fsync_count > a_rule->fsync_period) {
if (a_rule->fsync_period && ++a_rule->fsync_count >= a_rule->fsync_period) {
a_rule->fsync_count = 0;
if (fsync(fd)) zc_error("fsync[%d] fail, errno[%d]", fd, errno);
}
......@@ -340,7 +339,7 @@ static int zlog_rule_output_dynamic_file_rotate(zlog_rule_t * a_rule, zlog_threa
return -1;
}
if (a_rule->fsync_period && ++a_rule->fsync_count > a_rule->fsync_period) {
if (a_rule->fsync_period && ++a_rule->fsync_count >= a_rule->fsync_period) {
a_rule->fsync_count = 0;
if (fsync(fd)) zc_error("fsync[%d] fail, errno[%d]", fd, errno);
}
......@@ -667,6 +666,10 @@ zlog_rule_t *zlog_rule_new(char *line,
rc = -1;
goto zlog_rule_new_exit;
}
/* no need to fsync, as file is opened by O_SYNC, write immediately */
a_rule->fsync_period = 0;
p = file_path + 2;
a_rule->file_open_flags = O_SYNC;
/* fall through */
......@@ -701,7 +704,13 @@ zlog_rule_t *zlog_rule_new(char *line,
}
/* try to figure out if the log file path is dynamic or static */
if (strchr(a_rule->file_path, '%') == NULL) {
if (strchr(a_rule->file_path, '%') == NULL
&& (a_rule->file_open_flags != O_SYNC)) {
/* no % means static, using cached FILE* to fwrite
* but if O_SYNC('-' before path),
* dynamic open write close each time is fast and safe
*/
if (a_rule->file_max_size <= 0) {
a_rule->output = zlog_rule_output_static_file_single;
} else {
......@@ -710,7 +719,7 @@ zlog_rule_t *zlog_rule_new(char *line,
}
a_rule->static_file_descriptor = open(a_rule->file_path,
a_rule->file_open_flags | O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
if (a_rule->static_file_descriptor < 0) {
rc = -1;
zc_error("open file[%s] fail, errno[%d]", a_rule->file_path, errno);
......
......@@ -88,7 +88,7 @@ void zlog_spec_profile(zlog_spec_t * a_spec, int flag)
/* implementation of write function */
static int zlog_spec_write_time_direct(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
/* only when need fetch time, do it once */
if (!a_thread->event->time_stamp.tv_sec) {
......@@ -117,7 +117,7 @@ static int zlog_spec_write_time_direct(zlog_spec_t * a_spec, zlog_thread_t * a_t
static int zlog_spec_write_time_msus(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int i;
int rc = 0;
int rc;
/* only when need fetch time, do it once */
if (!a_thread->event->time_stamp.tv_sec) {
......@@ -158,7 +158,7 @@ static int zlog_spec_write_time_msus(zlog_spec_t * a_spec, zlog_thread_t * a_thr
static int zlog_spec_write_mdc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
zlog_mdc_kv_t *a_mdc_kv;
a_mdc_kv = zlog_mdc_get_kv(a_thread->mdc, a_spec->mdc_key);
......@@ -177,7 +177,7 @@ static int zlog_spec_write_mdc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, z
static int zlog_spec_write_str(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
rc = zlog_buf_append(a_buf, a_spec->str, a_spec->len);
if (rc) {
......@@ -190,7 +190,7 @@ static int zlog_spec_write_str(zlog_spec_t * a_spec, zlog_thread_t * a_thread, z
static int zlog_spec_write_category(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
rc = zlog_buf_append(a_buf, a_thread->event->category_name,
a_thread->event->category_name_len);
......@@ -203,7 +203,9 @@ static int zlog_spec_write_category(zlog_spec_t * a_spec, zlog_thread_t * a_thre
static int zlog_spec_write_srcfile(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
zc_assert(a_thread->event->file, -1);
rc = zlog_buf_append(a_buf, a_thread->event->file, a_thread->event->file_len);
if (rc) {
......@@ -215,19 +217,13 @@ static int zlog_spec_write_srcfile(zlog_spec_t * a_spec, zlog_thread_t * a_threa
static int zlog_spec_write_srcfile_neat(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
char *p;
char *end;
end = (char*)a_thread->event->file + strlen(a_thread->event->file) - 1;
for (p = end; *p != '/' && p > a_thread->event->file; p--);
/* empty */
if (*p == '/') {
p++;
}
p = strrchr(a_thread->event->file, '/') + 1;
zc_assert(p, -1);
rc = zlog_buf_append(a_buf, p, end - p + 1);
rc = zlog_buf_append(a_buf, p, (char*)a_thread->event->file + a_thread->event->file_len - p);
if (rc) {
zc_error("zlog_buf_append maybe fail or overflow");
return rc;
......@@ -237,11 +233,11 @@ static int zlog_spec_write_srcfile_neat(zlog_spec_t * a_spec, zlog_thread_t * a_
static int zlog_spec_write_srcline(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
rc = zlog_buf_printf(a_buf, "%ld", a_thread->event->line);
if (rc) {
zc_error("zlog_buf_append maybe fail or overflow");
zc_error("zlog_buf_printf maybe fail or overflow");
return rc;
}
return 0;
......@@ -249,7 +245,9 @@ static int zlog_spec_write_srcline(zlog_spec_t * a_spec, zlog_thread_t * a_threa
static int zlog_spec_write_srcfunc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
zc_assert(a_thread->event->func, -1);
rc = zlog_buf_append(a_buf, a_thread->event->func, a_thread->event->func_len);
if (rc) {
......@@ -262,7 +260,7 @@ static int zlog_spec_write_srcfunc(zlog_spec_t * a_spec, zlog_thread_t * a_threa
static int zlog_spec_write_hostname(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
rc = zlog_buf_append(a_buf, a_thread->event->host_name, a_thread->event->host_name_len);
if (rc) {
......@@ -274,7 +272,7 @@ static int zlog_spec_write_hostname(zlog_spec_t * a_spec, zlog_thread_t * a_thre
static int zlog_spec_write_newline(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
rc = zlog_buf_append(a_buf, FILE_NEWLINE, FILE_NEWLINE_LEN);
if (rc) {
......@@ -286,7 +284,7 @@ static int zlog_spec_write_newline(zlog_spec_t * a_spec, zlog_thread_t * a_threa
static int zlog_spec_write_percent(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
rc = zlog_buf_append(a_buf, "%", 1);
if (rc) {
......@@ -298,7 +296,7 @@ static int zlog_spec_write_percent(zlog_spec_t * a_spec, zlog_thread_t * a_threa
static int zlog_spec_write_pid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
if (!a_thread->event->pid) {
a_thread->event->pid = getpid();
......@@ -314,7 +312,7 @@ static int zlog_spec_write_pid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, z
static int zlog_spec_write_tid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
/* don't need to get tid again, as tmap_new_thread fetch it already */
/* and fork not change tid */
......@@ -329,10 +327,11 @@ static int zlog_spec_write_tid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, z
static int zlog_spec_write_level_lowercase(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
zlog_level_t *a_level;
a_level = zlog_level_list_get(a_spec->levels, a_thread->event->level);
zc_assert(a_level, -1);
rc = zlog_buf_append(a_buf, a_level->str_lowercase, a_level->str_len);
if (rc) {
......@@ -344,10 +343,11 @@ static int zlog_spec_write_level_lowercase(zlog_spec_t * a_spec, zlog_thread_t *
static int zlog_spec_write_level_uppercase(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
zlog_level_t *a_level;
a_level = zlog_level_list_get(a_spec->levels, a_thread->event->level);
zc_assert(a_level, -1);
rc = zlog_buf_append(a_buf, a_level->str_uppercase, a_level->str_len);
if (rc) {
......@@ -359,16 +359,14 @@ static int zlog_spec_write_level_uppercase(zlog_spec_t * a_spec, zlog_thread_t *
static int zlog_spec_write_usrmsg(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
int rc = 0;
int rc;
if (a_thread->event->generate_cmd == ZLOG_FMT) {
if (a_thread->event->str_format == NULL) {
rc = zlog_buf_printf(a_buf, "format=(null)");
if (rc) {
zc_error
("zlog_buf_printf maybe fail or overflow");
zc_error ("zlog_buf_printf maybe fail or overflow");
return rc;
}
return 0;
......@@ -456,8 +454,7 @@ static int zlog_spec_write_usrmsg(zlog_spec_t * a_spec, zlog_thread_t * a_thread
hex_buf + line_offset * 16 +
byte_offset);
if (c >= 32 && c <= 126) {
rc = zlog_buf_printf(a_buf,
"%c", c);
rc = zlog_buf_printf(a_buf, "%c", c);
if (rc) {
goto zlog_hex_exit;
}
......@@ -509,20 +506,13 @@ int zlog_spec_gen_msg(zlog_spec_t * a_spec, zlog_thread_t * a_thread)
static int zlog_spec_gen_msg_direct(zlog_spec_t * a_spec, zlog_thread_t * a_thread)
{
int rc = 0;
/* no need to reprint %1.2d here */
rc = a_spec->write_buf(a_spec, a_thread, a_thread->msg_buf);
if (rc) {
zc_error("a_spec->gen_buf fail,or buffer is full");
return rc;
}
return 0;
return a_spec->write_buf(a_spec, a_thread, a_thread->msg_buf);
}
static int zlog_spec_gen_msg_reformat(zlog_spec_t * a_spec, zlog_thread_t * a_thread)
{
int rc = 0;
int rc;
zlog_buf_restart(a_thread->pre_msg_buf);
......@@ -556,7 +546,7 @@ int zlog_spec_gen_path(zlog_spec_t * a_spec, zlog_thread_t * a_thread)
static int zlog_spec_gen_path_direct(zlog_spec_t * a_spec, zlog_thread_t * a_thread)
{
int rc = 0;
int rc;
/* no need to reprint %1.2d here */
rc = a_spec->write_buf(a_spec, a_thread, a_thread->path_buf);
......@@ -572,7 +562,7 @@ static int zlog_spec_gen_path_direct(zlog_spec_t * a_spec, zlog_thread_t * a_thr
static int zlog_spec_gen_path_reformat(zlog_spec_t * a_spec, zlog_thread_t * a_thread)
{
int rc = 0;
int rc;
zlog_buf_restart(a_thread->pre_path_buf);
......@@ -758,6 +748,7 @@ zlog_spec_t *zlog_spec_new(char *pattern_start, char **pattern_next, zc_arraylis
} else if (STRNCMP(p, ==, "d()", 3)) {
/* with () but without detail time format,
* keep a_spec->time_fmt=="" */
strcpy(a_spec->time_fmt, ZLOG_DEFAULT_TIME_FMT);
p += 3;
} else {
nscan =
......
......@@ -66,13 +66,6 @@ void zc_arraylist_del(zc_arraylist_t * a_list)
return;
}
void *zc_arraylist_get(zc_arraylist_t * a_list, int i)
{
if (i >= a_list->len)
return NULL;
return a_list->array[i];
}
static int zc_arraylist_expand_inner(zc_arraylist_t * a_list, int max)
{
void *tmp;
......
......@@ -36,7 +36,6 @@ typedef struct {
zc_arraylist_t *zc_arraylist_new(zc_arraylist_del_fn del);
void zc_arraylist_del(zc_arraylist_t * a_list);
void *zc_arraylist_get(zc_arraylist_t * a_list, int i);
int zc_arraylist_set(zc_arraylist_t * a_list, int i, void *data);
int zc_arraylist_add(zc_arraylist_t * a_list, void *data);
int zc_arraylist_sortadd(zc_arraylist_t * a_list, zc_arraylist_cmp_fn cmp,
......@@ -44,6 +43,9 @@ int zc_arraylist_sortadd(zc_arraylist_t * a_list, zc_arraylist_cmp_fn cmp,
int zc_arraylist_len(zc_arraylist_t * a_list);
#define zc_arraylist_get(a_list, i) \
(i >= a_list->len) ? NULL : a_list->array[i]
#define zc_arraylist_foreach(a_list, i, a_unit) \
for(i = 0, a_unit = a_list->array[0]; i < a_list->len && (a_unit = a_list->array[i], 1) ; i++)
......
......@@ -171,10 +171,16 @@ zc_hashtable_entry_t *zc_hashtable_get_entry(zc_hashtable_t * a_table,
void *zc_hashtable_get(zc_hashtable_t * a_table, void *a_key)
{
unsigned int i;
zc_hashtable_entry_t *p;
p = zc_hashtable_get_entry(a_table, a_key);
return p ? p->value : NULL;
i = a_table->hash(a_key) % a_table->tab_size;
for (p = (a_table->tab)[i]; p; p = p->next) {
if (a_table->equal(a_key, p->key))
return p->value;
}
return NULL;
}
int zc_hashtable_put(zc_hashtable_t * a_table, void *a_key, void *a_value)
......@@ -328,19 +334,10 @@ int zc_hashtable_str_equal(void *key1, void *key2)
unsigned int zc_hashtable_tid_hash(void *ptid)
{
pthread_t tid;
tid = *((pthread_t *) ptid);
return (unsigned int)tid;
return (unsigned int) *((pthread_t *) ptid);
}
int zc_hashtable_tid_equal(void *ptid1, void *ptid2)
{
pthread_t tid1;
pthread_t tid2;
tid1 = *((pthread_t *) ptid1);
tid2 = *((pthread_t *) ptid2);
return pthread_equal(tid1, tid2);
return pthread_equal(*((pthread_t *) ptid1), *((pthread_t *) ptid2));
}
[formats]
simple = "%m%n"
simple = "%D %m%n"
[rules]
my_cat.* >stdout;simple
my_cat.* >stdout;
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