Commit 13685346 authored by Hardy Simpson's avatar Hardy Simpson

feature: add %D, default time support

parent 5386645c
......@@ -108,7 +108,7 @@ zlog使用攻略
status collapsed
\begin_layout Plain Layout
适用于 zlog v1.0.x
适用于 zlog v1.1.x
\end_layout
\end_inset
......@@ -313,7 +313,7 @@ type "mailto:"
\end_layout
\begin_layout Section
zlog 1.0 发布说明
zlog 1.1 发布说明
\end_layout
\begin_layout Enumerate
......@@ -346,10 +346,23 @@ target "http://www.jhweiss.de/software/snprintf.html"
\end_layout
\begin_layout Enumerate
zlog1.0和0.9不是兼容的。因为我增加了__func__,这样在C99情况下能打印函数名。我还改了配置文件的格式,对配置文件分节,更加清晰易懂。如果你是zlo
g0.9的用户,想要升级到1.0,那就需要重新编译你的程序链接新的库(你的源代码倒不用改,因为几个输出日志的宏和0.9还是一样)。
zlog 1.1在库方面是和zlog 1.0二进制兼容的,在配置文件方面有两个的改动,
\end_layout
\begin_deeper
\begin_layout Enumerate
%d(%ms,%us)不再是合法的,%d.%ms是合法的。%ms毫秒和%us微秒提出来作为单独的转换字符串。
\end_layout
\begin_layout Enumerate
增加了默认时间串%D,输出"2012-02-14 17:03:12"这样的时间。
\end_layout
\begin_layout Standard
之所以改变这些,是为了简化代码,以及能提升30%的性能。所以如果你是从1.0.x升级,如果使用动态库的话,无须重新编译程序,只要稍微改动一下配置文件即可。
\end_layout
\end_deeper
\begin_layout Chapter
zlog不是什么?
\end_layout
......@@ -1093,7 +1106,7 @@ rotate lock file = /tmp/zlog.lock
\end_layout
\begin_layout LyX-Code
default format = "%d(%F %T.%l) %-6P (%c:%F:%L) - %m%n"
default format = "%D.%us %-6P (%c:%F:%L) - %m%n"
\end_layout
\begin_layout LyX-Code
......@@ -1129,7 +1142,7 @@ simple = "%m%n"
\end_layout
\begin_layout LyX-Code
normal = "%d(%F %T.%l) %m%n"
normal = "%D %m%n"
\end_layout
\begin_layout LyX-Code
......@@ -1313,7 +1326,7 @@ default format
\end_layout
\begin_layout LyX-Code
"%d(%F %T) %V [%p:%F:%L] %m%n"
"%D %V [%p:%F:%L] %m%n"
\end_layout
\begin_layout Standard
......@@ -1537,7 +1550,7 @@ ZLOG_INFO(c, "hello, zlog");
\begin_layout Standard
\begin_inset Tabular
<lyxtabular version="3" rows="19" columns="3">
<lyxtabular version="3" rows="21" columns="3">
<features islongtable="true" longtabularalignment="center">
<column alignment="center" valignment="top" width="10text%">
<column alignment="center" valignment="top" width="50text%">
......@@ -1615,11 +1628,7 @@ aa_bb
\begin_layout Plain Layout
打日志的时间。这个后面要跟一对小括号()内含说明具体的日期格式。就像%d(%F)或者%d(%m-%d %T)。如果不跟小括号,默认是%d(%F
%T)。括号内的格式和 strftime(3)的格式一致,就是增加了%us表示微秒,还有
\end_layout
\begin_layout Plain Layout
%ms表示毫秒。详见
%T)。括号内的格式和 strftime(3)的格式一致。详见
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:时间字符"
......@@ -1643,7 +1652,7 @@ reference "sub:时间字符"
\end_layout
\begin_layout Plain Layout
%d(%T.ms) 17:17:42.035
%d(%T) 17:17:42.035
\end_layout
\begin_layout Plain Layout
......@@ -1658,7 +1667,69 @@ reference "sub:时间字符"
\begin_inset Text
\begin_layout Plain Layout
%E
%D
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
相当于%d(%F %T),zlog默认时间串,在性能上比%d有一定的提升
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
2012-02-14 17:03:12
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
%ms
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
毫秒,3位数字字符串
\end_layout
\begin_layout Plain Layout
取自gettimeofday(2)
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
013
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
%us
\end_layout
\end_inset
......@@ -1667,8 +1738,11 @@ reference "sub:时间字符"
\begin_inset Text
\begin_layout Plain Layout
环境变量,后面也要跟小括号,内涵环境变量的键。变量的值在zlog_init()的时候就求出来了。所以只含有%E的文件名被认为是静态路径文件,相比动态路径文件写日
志会有一定的性能提升。
微秒,6位数字字符串
\end_layout
\begin_layout Plain Layout
取自gettimeofday(2)
\end_layout
\end_inset
......@@ -1677,7 +1751,7 @@ reference "sub:时间字符"
\begin_inset Text
\begin_layout Plain Layout
%E(HOME) /home/harry
002323
\end_layout
\end_inset
......@@ -2492,121 +2566,7 @@ name "sub:时间字符"
\end_layout
\begin_layout Standard
其中2个是zlog特有的,取自gettimeofday(2)
\end_layout
\begin_layout Standard
\begin_inset Tabular
<lyxtabular version="3" rows="3" columns="3">
<features tabularvalignment="middle">
<column alignment="center" valignment="top" width="10text%">
<column alignment="center" valignment="top" width="30text%">
<column alignment="center" valignment="top" width="30text%">
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
效果
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\noun off
\color none
例子
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
%ms
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
毫秒,3位数字字符串
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
013
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
%us
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
微秒,6位数字字符串
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
002323
\end_layout
\end_inset
</cell>
</row>
</lyxtabular>
\end_inset
\end_layout
\begin_layout Standard
其他的字符都是由strftime(2)生成的,在我的linux操作系统上支持的是:
所有字符都是由strftime(2)生成的,在我的linux操作系统上支持的是:
\end_layout
\begin_layout Standard
......
This diff is collapsed.
......@@ -34,7 +34,7 @@
#include "zc_defs.h"
/*******************************************************************************/
#define ZLOG_CONF_DEFAULT_FORMAT "default = \"%d(%F %T) %V [%p:%F:%L] %m%n\""
#define ZLOG_CONF_DEFAULT_FORMAT "default = \"%D %V [%p:%F:%L] %m%n\""
#define ZLOG_CONF_DEFAULT_RULE "*.* >stdout"
#define ZLOG_CONF_DEFAULT_BUF_SIZE_MIN 1024
#define ZLOG_CONF_DEFAULT_BUF_SIZE_MAX (2 * 1024 * 1024)
......
......@@ -52,10 +52,10 @@ typedef struct {
struct timeval time_stamp;
time_t last_sec;
int sec_changed;
struct tm local_time;
char us[6 + 1];
char D_time_str[10 + 1 + 8 + 1]; /* YYYY-mm-dd HH:mm:ss */
char time_str[MAXLEN_CFG_LINE + 1];
size_t time_str_len;
char *last_time_fmt;
......
......@@ -51,6 +51,7 @@ struct zlog_spec_s {
char time_fmt[MAXLEN_CFG_LINE + 1];
char mdc_key[MAXLEN_PATH + 1];
char print_fmt[MAXLEN_CFG_LINE + 1];
int left_adjust;
size_t max_width;
......@@ -77,30 +78,43 @@ void zlog_spec_profile(zlog_spec_t * a_spec, int flag)
}
/*******************************************************************************/
#define zlog_spec_fetch_time do {\
if (!a_thread->event->time_stamp.tv_sec) { \
gettimeofday(&(a_thread->event->time_stamp), NULL); \
sprintf(a_thread->event->us, "%6.6ld", \
(long)a_thread->event->time_stamp.tv_usec); \
\
if (a_thread->event->time_stamp.tv_sec != a_thread->event->last_sec) { \
/* localtime_r is slow on linux, do it once per second */ \
/* thanks for nikuailema@gmail.com */ \
a_thread->event->last_sec = a_thread->event->time_stamp.tv_sec; \
localtime_r(&(a_thread->event->time_stamp.tv_sec), \
&(a_thread->event->local_time)); \
\
/* strftime %D time fmt per second*/ \
strftime(a_thread->event->D_time_str, \
sizeof(a_thread->event->D_time_str), \
"%F %T", &(a_thread->event->local_time) ); \
\
/* strftime %d() per second */ \
if (a_thread->event->last_time_fmt) { \
a_thread->event->time_str_len = strftime(a_thread->event->time_str, \
sizeof(a_thread->event->time_str), \
a_thread->event->last_time_fmt, &(a_thread->event->local_time)); \
} \
} \
} \
} while(0)
/* implementation of write function */
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 */
if (!a_thread->event->time_stamp.tv_sec) {
gettimeofday(&(a_thread->event->time_stamp), NULL);
sprintf(a_thread->event->us, "%6.6ld",
(long)a_thread->event->time_stamp.tv_usec);
if (a_thread->event->time_stamp.tv_sec != a_thread->event->last_sec) {
/* localtime_r is slow on linux, do it once per second */
/* thanks for nikuailema@gmail.com */
a_thread->event->sec_changed = 1;
a_thread->event->last_sec = a_thread->event->time_stamp.tv_sec;
localtime_r(&(a_thread->event->time_stamp.tv_sec),
&(a_thread->event->local_time));
} else {
a_thread->event->sec_changed = 0;
}
}
zlog_spec_fetch_time;
/* strftime is slow too, do it once per second
* or time_fmt changed(event go through another spec) */
if (a_thread->event->last_time_fmt != a_spec->time_fmt || a_thread->event->sec_changed) {
/* strftime %d() is slow too, do it when
* time_fmt changed(event go through another spec) */
if (a_thread->event->last_time_fmt != a_spec->time_fmt) {
a_thread->event->last_time_fmt = a_spec->time_fmt;
a_thread->event->time_str_len = strftime(a_thread->event->time_str,
sizeof(a_thread->event->time_str),
......@@ -110,47 +124,25 @@ static int zlog_spec_write_time(zlog_spec_t * a_spec, zlog_thread_t * a_thread,
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, 19);
}
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 */
if (!a_thread->event->time_stamp.tv_sec) {
gettimeofday(&(a_thread->event->time_stamp), NULL);
sprintf(a_thread->event->us, "%6.6ld",
(long)a_thread->event->time_stamp.tv_usec);
if (a_thread->event->time_stamp.tv_sec != a_thread->event->last_sec) {
/* localtime_r is slow on linux, do it once per second */
/* thanks for nikuailema@gmail.com */
a_thread->event->sec_changed = 1;
a_thread->event->last_sec = a_thread->event->time_stamp.tv_sec;
localtime_r(&(a_thread->event->time_stamp.tv_sec),
&(a_thread->event->local_time));
} else {
a_thread->event->sec_changed = 0;
}
}
zlog_spec_fetch_time;
return zlog_buf_append(a_buf, a_thread->event->us, 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 */
if (!a_thread->event->time_stamp.tv_sec) {
gettimeofday(&(a_thread->event->time_stamp), NULL);
sprintf(a_thread->event->us, "%6.6ld",
(long)a_thread->event->time_stamp.tv_usec);
if (a_thread->event->time_stamp.tv_sec != a_thread->event->last_sec) {
/* localtime_r is slow on linux, do it once per second */
/* thanks for nikuailema@gmail.com */
a_thread->event->sec_changed = 1;
a_thread->event->last_sec = a_thread->event->time_stamp.tv_sec;
localtime_r(&(a_thread->event->time_stamp.tv_sec),
&(a_thread->event->local_time));
} else {
a_thread->event->sec_changed = 0;
}
}
zlog_spec_fetch_time;
return zlog_buf_append(a_buf, a_thread->event->us, 6);
}
......@@ -620,6 +612,9 @@ zlog_spec_t *zlog_spec_new(char *pattern_start, char **pattern_next, zc_arraylis
case 'c':
a_spec->write_buf = zlog_spec_write_category;
break;
case 'D':
a_spec->write_buf = zlog_spec_write_time_D;
break;
case 'F':
a_spec->write_buf = zlog_spec_write_srcfile;
break;
......
[global]
default format = "%d(%F %T).%us %-6V %p:%T:%F:%L %m%n"
#default format = "%d(%F %T).%us %-6V %p:%T:%F:%L %m%n"
default format = "%D.%us %-6V %p:%T:%F:%L %m%n"
[rules]
*.* "press.log"
[global]
default format = "%d(%F %T).%us %-6V %p:%T:%F:%L %m%n"
default format = "%D.%us %-6V %p:%T:%F:%L %m%n"
[rules]
cat0.* "press.0.log"
......
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