Skip to content

Commit

Permalink
bugfix: the last fix for zlog_spec_write_time is not right.
Browse files Browse the repository at this point in the history
  • Loading branch information
HardySimpson committed Sep 29, 2012
1 parent acbd8c4 commit b1a3049
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ typedef struct {
zlog_event_cmd generate_cmd;

struct timeval time_stamp;
time_t last_sec;
struct tm local_time;
struct tm time_local;

char D_time_str[10 + 1 + 8 + 1]; /* YYYY-mm-dd HH:mm:ss */
time_t time_last_D;
char time_str_D[10 + 1 + 8 + 1]; /* YYYY-mm-dd HH:mm:ss */

time_t time_last;
char *time_fmt_last;
char time_str[MAXLEN_CFG_LINE + 1];
size_t time_str_len;

Expand Down
4 changes: 2 additions & 2 deletions src/release.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#define ZLOG_GIT_SHA1 "20c44a22"
#define ZLOG_GIT_DIRTY "0"
#define ZLOG_GIT_SHA1 "acbd8c41"
#define ZLOG_GIT_DIRTY "8"
58 changes: 49 additions & 9 deletions src/spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,69 @@ void zlog_spec_profile(zlog_spec_t * a_spec, int flag)

static int zlog_spec_write_time(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
/* do fetch time every event once */
zlog_spec_fetch_time;
if (!a_thread->event->time_stamp.tv_sec) {
gettimeofday(&(a_thread->event->time_stamp), NULL);
}

/* a_event->time_str is a cache, as strftime is slow.
* It is modified when this event meets another time specifier, or time slips one second.
* So it is a weak cache, just work when a event goes through only one time specifier.
* If there is any better way, please tell me!
*/
if (
(a_thread->event->time_stamp.tv_sec != a_thread->event->time_last)
|| (a_spec->time_fmt != a_thread->event->time_fmt_last)
) {
a_thread->event->time_last = a_thread->event->time_stamp.tv_sec;
a_thread->event->time_fmt_last = a_spec->time_fmt;

localtime_r(&(a_thread->event->time_stamp.tv_sec),
&(a_thread->event->time_local));

a_thread->event->time_str_len = strftime(a_thread->event->time_str,
sizeof(a_thread->event->time_str),
a_spec->time_fmt, &(a_thread->event->time_local));
}
return zlog_buf_append(a_buf, a_thread->event->time_str, a_thread->event->time_str_len);
}

static int zlog_spec_write_time_D(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
/* do fetch time every event once */
zlog_spec_fetch_time;
return zlog_buf_append(a_buf, a_thread->event->D_time_str, sizeof(a_thread->event->D_time_str) - 1);
if (!a_thread->event->time_stamp.tv_sec) {
gettimeofday(&(a_thread->event->time_stamp), NULL);
}

/* a_event->time_str_D is also a cache.
* It is modified when time slips one second.
* So it is a strong cache, as Default time format is always %F %T.
* That's why I said %D is faster than %d()
*/
if (a_thread->event->time_stamp.tv_sec != a_thread->event->time_last_D) {

a_thread->event->time_last_D = a_thread->event->time_stamp.tv_sec;
localtime_r(&(a_thread->event->time_stamp.tv_sec),
&(a_thread->event->time_local));

strftime(a_thread->event->time_str_D,
sizeof(a_thread->event->time_str_D),
ZLOG_DEFAULT_TIME_FMT, &(a_thread->event->time_local) );
}
return zlog_buf_append(a_buf, a_thread->event->time_str_D, sizeof(a_thread->event->time_str_D) - 1);
}

static int zlog_spec_write_ms(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
/* do fetch time every event once */
zlog_spec_fetch_time;
if (!a_thread->event->time_stamp.tv_sec) {
gettimeofday(&(a_thread->event->time_stamp), NULL);
}
return zlog_buf_printf_dec32(a_buf, (a_thread->event->time_stamp.tv_usec / 1000), 3);
}

static int zlog_spec_write_us(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
/* do fetch time every event once */
zlog_spec_fetch_time;
if (!a_thread->event->time_stamp.tv_sec) {
gettimeofday(&(a_thread->event->time_stamp), NULL);
}
return zlog_buf_printf_dec32(a_buf, a_thread->event->time_stamp.tv_usec, 6);
}

Expand Down

0 comments on commit b1a3049

Please sign in to comment.