Skip to content

Commit

Permalink
code: smooth debug output, and bugfix buf.c
Browse files Browse the repository at this point in the history
  • Loading branch information
HardySimpson committed Jun 29, 2012
1 parent 838dc73 commit c6979d1
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 29 deletions.
33 changes: 17 additions & 16 deletions libzlog/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -302,18 +303,18 @@ 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",
a_buf->size_max);
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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
/*******************************************************************************/
Expand Down
1 change: 1 addition & 0 deletions libzlog/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libzlog/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libzlog/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion libzlog/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libzlog/level_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libzlog/mdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libzlog/rotater.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions libzlog/rule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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))) {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion libzlog/spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libzlog/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libzlog/zc_arraylist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion libzlog/zlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/makefile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c6979d1

Please sign in to comment.