Commit c6979d1c authored by Hardy Simpson's avatar Hardy Simpson

code: smooth debug output, and bugfix buf.c

parent 838dc73e
......@@ -125,20 +125,21 @@ zlog_buf_t *zlog_buf_new(size_t buf_size_min, size_t buf_size_max, const char *t
a_buf->truncate_str_len = strlen(truncate_str);
}
a_buf->start = calloc(1, buf_size_min);
a_buf->size_min = buf_size_min;
a_buf->size_max = buf_size_max;
a_buf->size_real = a_buf->size_min;
a_buf->start = calloc(1, a_buf->size_real);
if (!a_buf->start) {
zc_error("calloc fail, errno[%d]", errno);
goto err;
}
a_buf->tail = a_buf->start;
a_buf->size_real = a_buf->size_min;
a_buf->end_plus_1 = a_buf->start + a_buf->size_real;
a_buf->end = a_buf->end_plus_1 - 1;
a_buf->size_min = buf_size_min;
a_buf->size_max = buf_size_max;
zlog_buf_profile(a_buf, ZC_DEBUG);
//zlog_buf_profile(a_buf, ZC_DEBUG);
return a_buf;
err:
......@@ -244,7 +245,7 @@ int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args)
nwrite = vsnprintf(a_buf->tail, size_left, format, ap);
if (nwrite >= 0 && nwrite < size_left) {
a_buf->tail += nwrite;
*(a_buf->tail) = '\0';
//*(a_buf->tail) = '\0';
return 0;
} else if (nwrite < 0) {
zc_error("vsnprintf fail, errno[%d]", errno);
......@@ -260,7 +261,7 @@ int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args)
size_left = a_buf->end_plus_1 - a_buf->start;
vsnprintf(a_buf->tail, size_left, format, ap);
a_buf->tail += size_left - 1;
*(a_buf->tail) = '\0';
//*(a_buf->tail) = '\0';
zlog_buf_truncate(a_buf);
return 1;
} else if (rc < 0) {
......@@ -278,7 +279,7 @@ int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args)
return -1;
} else {
a_buf->tail += nwrite;
*(a_buf->tail) = '\0';
//*(a_buf->tail) = '\0';
return 0;
}
}
......@@ -290,7 +291,7 @@ 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)
{
char *p;
#if 0
if (str_len <= 0 || str == NULL) {
return 0;
......@@ -302,10 +303,10 @@ int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len)
return -1;
}
if (str_len > a_buf->end - a_buf->tail) {
if ((p = a_buf->tail + str_len) > a_buf->end) {
int rc;
zc_debug("size_left not enough, resize");
rc = zlog_buf_resize(a_buf, a_buf->end - a_buf->tail);
rc = zlog_buf_resize(a_buf, str_len - (a_buf->end - a_buf->tail));
if (rc > 0) {
size_t len_left;
zc_error("conf limit to %ld, can't extend, so output",
......@@ -313,7 +314,7 @@ int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len)
len_left = a_buf->end - a_buf->tail;
memcpy(a_buf->tail, str, len_left);
a_buf->tail += len_left;
*(a_buf->tail) = '\0';
//*(a_buf->tail) = '\0';
zlog_buf_truncate(a_buf);
return 1;
} else if (rc < 0) {
......@@ -325,8 +326,8 @@ int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len)
}
memcpy(a_buf->tail, str, str_len);
a_buf->tail += str_len;
*(a_buf->tail) = '\0';
a_buf->tail = p;
// *(a_buf->tail) = '\0';
return 0;
}
......@@ -397,7 +398,7 @@ int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
memcpy(a_buf->tail + space_len, str, source_len);
}
a_buf->tail += append_len;
*(a_buf->tail) = '\0';
//*(a_buf->tail) = '\0';
zlog_buf_truncate(a_buf);
return 1;
} else if (rc < 0) {
......@@ -416,7 +417,7 @@ int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
memcpy(a_buf->tail + space_len, str, source_len);
}
a_buf->tail += append_len;
*(a_buf->tail) = '\0';
//*(a_buf->tail) = '\0';
return 0;
}
/*******************************************************************************/
......
......@@ -48,6 +48,7 @@ void zlog_buf_profile(zlog_buf_t * a_buf, int flag);
#define zlog_buf_len(a_buf) (a_buf->tail - a_buf->start)
#define zlog_buf_str(a_buf) (a_buf->start)
#define zlog_buf_seal(a_buf) do {*(a_buf)->tail = '\0';} while (0)
int zlog_buf_printf(zlog_buf_t * a_buf, const char *format, ...);
int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args);
......
......@@ -83,7 +83,7 @@ zlog_event_t *zlog_event_new(void)
a_event->tid_str_len = sprintf(a_event->tid_str, "%lu", (unsigned long)a_event->tid);
a_event->tid_hex_str_len = sprintf(a_event->tid_hex_str, "0x%x", (unsigned int)a_event->tid);
zlog_event_profile(a_event, ZC_DEBUG);
//zlog_event_profile(a_event, ZC_DEBUG);
return a_event;
err:
zlog_event_del(a_event);
......
......@@ -143,7 +143,7 @@ zlog_format_t *zlog_format_new(char *line, zc_arraylist_t *levels)
}
}
zlog_format_profile(a_format, ZC_DEBUG);
//zlog_format_profile(a_format, ZC_DEBUG);
return a_format;
err:
zlog_format_del(a_format);
......
......@@ -36,7 +36,6 @@ void zlog_format_del(zlog_format_t * a_format);
void zlog_format_profile(zlog_format_t * a_format, int flag);
int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread);
int zlog_format_has_name(zlog_format_t * a_format, char *name);
#define zlog_format_has_name(a_format, fname) \
STRCMP(a_format->name, ==, fname)
......
......@@ -79,7 +79,7 @@ zc_arraylist_t *zlog_level_list_new(void)
goto err;
}
zlog_level_list_profile(levels, ZC_DEBUG);
//zlog_level_list_profile(levels, ZC_DEBUG);
return levels;
err:
zc_arraylist_del(levels);
......
......@@ -93,7 +93,7 @@ zlog_mdc_t *zlog_mdc_new(void)
goto err;
}
zlog_mdc_profile(a_mdc, ZC_DEBUG);
//zlog_mdc_profile(a_mdc, ZC_DEBUG);
return a_mdc;
err:
zlog_mdc_del(a_mdc);
......
......@@ -103,7 +103,7 @@ zlog_rotater_t *zlog_rotater_new(char *lock_file)
a_rotater->lock_fd = fd;
a_rotater->lock_file = lock_file;
zlog_rotater_profile(a_rotater, ZC_DEBUG);
//zlog_rotater_profile(a_rotater, ZC_DEBUG);
return a_rotater;
err:
zlog_rotater_del(a_rotater);
......
......@@ -308,8 +308,9 @@ static int zlog_rule_output_syslog(zlog_rule_t * a_rule, zlog_thread_t * a_threa
*/
a_level = zlog_level_list_get(a_rule->levels, a_thread->event->level);
zlog_buf_seal(a_thread->msg_buf);
syslog(a_rule->syslog_facility | a_level->syslog_level,
"%s", zlog_buf_str(a_thread->msg_buf));
"%s", zlog_buf_str(a_thread->msg_buf));
return 0;
}
......@@ -321,6 +322,7 @@ static int zlog_rule_output_record(zlog_rule_t * a_rule, zlog_thread_t * a_threa
}
if (a_rule->record_output) {
zlog_buf_seal(a_thread->msg_buf);
if (a_rule->record_output(a_rule->record_param,
zlog_buf_str(a_thread->msg_buf),
zlog_buf_len(a_thread->msg_buf))) {
......@@ -694,7 +696,7 @@ zlog_rule_t *zlog_rule_new(char *line,
goto err;
}
zlog_rule_profile(a_rule, ZC_DEBUG);
//zlog_rule_profile(a_rule, ZC_DEBUG);
return a_rule;
err:
zlog_rule_del(a_rule);
......
......@@ -102,7 +102,8 @@ static int zlog_spec_write_time_D(zlog_spec_t * a_spec, zlog_thread_t * a_thread
/* do fetch time every event once */
zlog_spec_fetch_time;
return zlog_buf_append(a_buf, a_thread->event->D_time_str, 19);
return zlog_buf_append(a_buf, a_thread->event->D_time_str,
sizeof(a_thread->event->D_time_str) - 1);
}
static int zlog_spec_write_ms(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
......
......@@ -116,7 +116,7 @@ zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max)
}
zlog_thread_profile(a_thread, ZC_DEBUG);
//zlog_thread_profile(a_thread, ZC_DEBUG);
return a_thread;
err:
zlog_thread_del(a_thread);
......
......@@ -47,6 +47,6 @@ int zc_arraylist_sortadd(zc_arraylist_t * a_list, zc_arraylist_cmp_fn cmp,
((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++)
for(i = 0, a_unit = a_list->array[0]; (i < a_list->len) && (a_unit = a_list->array[i], 1) ; i++)
#endif
......@@ -361,7 +361,7 @@ zlog_category_t *zlog_get_category(char *cname)
goto err;
}
zc_debug("------zlog_get_category[%s] fail, end------ ", cname);
zc_debug("------zlog_get_category[%s] success, end------ ", cname);
rc = pthread_rwlock_unlock(&zlog_env_lock);
if (rc) {
zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
......
......@@ -28,7 +28,7 @@ $(exe) : %:%.o
gcc -O2 -g -o $@ $^ -L../libzlog/.libs -lzlog -lpthread -Wl,-rpath ../libzlog/.libs
.c.o :
gcc -O2 -g -pg -Wall -D_GNU_SOURCE -o $@ -c $< -I. -I../libzlog
gcc -O2 -g -Wall -D_GNU_SOURCE -o $@ -c $< -I. -I../libzlog
clean :
rm -f press.log* *.o $(exe)
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