Commit bb14f729 authored by Hardy Simpson's avatar Hardy Simpson

feature: using configure file as default lock file

parent 7b57d9d7
......@@ -45,6 +45,7 @@
[o] 引入__func__
[o] 文件的权限设置
[o] 输出函数自定义
[o] 采用配置文件作为锁文件
[p] 使用valgrind测试性能
[ ] 增加man age, df, 案例
......
......@@ -1334,8 +1334,57 @@ rotate lock file
\end_layout
\begin_layout Standard
这个选项指定了一个锁文件,用来保证多进程情况下日志安全转档。zlog会在zlog_init()时候创建这个文件。确认你执行程序的用户有权限创建和读写这个文件。如
果有多个用户需要转档同一个日志文件,确认这个锁文件对于多个用户都可读写。默认值是/tmp/zlog.lock。
这个选项指定了一个锁文件,用来保证多进程情况下日志安全转档。zlog会在zlog_init()时候以读写权限打开这个文件。确认你执行程序的用户有权限创建和读写这
个文件。转档日志的伪代码是:
\end_layout
\begin_layout LyX-Code
write(log_file, a_log);
\end_layout
\begin_layout LyX-Code
if (log_file > 1M)
\end_layout
\begin_deeper
\begin_layout LyX-Code
if (pthread_mutex_lock succ && fcntl_lock(lock_file) succ)
\end_layout
\begin_deeper
\begin_layout LyX-Code
if (log_file > 1M) //check again, prevent other process already rotate it
\end_layout
\begin_deeper
\begin_layout LyX-Code
rotate(log_file)
\end_layout
\end_deeper
\begin_layout LyX-Code
fcntl_unlock(lock_file)
\end_layout
\begin_layout LyX-Code
pthread_mutex_unlock
\end_layout
\end_deeper
\end_deeper
\begin_layout Standard
mutex_lock用于多线程, fcntl_lock用于多进程。fcntl_lock是POSIX建议锁。详见man 3 fcntl。这个锁是全系统有效的。在某
个进程意外死亡后,操作系统会释放此进程持有的锁。这就是我为什么用fcntl锁来保证安全转档。进程需要对锁文件有读写权限。
\end_layout
\begin_layout Standard
默认来说,rotate lock file = self。在这种情况下,zlog不会创建任何锁文件,用配置文件作为锁文件。fcntl是建议锁,所以用户可以自由的
修改存储他们的配置文件。一般来说,单个日志文件不会被不同操作系统用户的进程转档,所以用配置文件作为锁文件是安全的。
\end_layout
\begin_layout Standard
如果你设置其他路径作为锁文件,例如/tmp/zlog.lock,zlog会在zlog_init()的时候创建这个文件。如果有多个操作系统用户的进程需要转档同一个日
志文件,确认这个锁文件对于多个用户都可读写。默认值是/tmp/zlog.lock。
\end_layout
\begin_layout Itemize
......
......@@ -1421,12 +1421,72 @@ rotate lock file
\begin_layout Standard
This specifies a lock file for rotate a log safely between multi-process.
zlog will create the file at zlog_init().
zlog will open the file at zlog_init() with the premisson of read-write.
The pseudo-code of rotating a log file is:
\end_layout
\begin_layout LyX-Code
write(log_file, a_log);
\end_layout
\begin_layout LyX-Code
if (log_file > 1M)
\end_layout
\begin_deeper
\begin_layout LyX-Code
if (pthread_mutex_lock succ && fcntl_lock(lock_file) succ)
\end_layout
\begin_deeper
\begin_layout LyX-Code
if (log_file > 1M) //check again, prevent other process already rotate it
\end_layout
\begin_deeper
\begin_layout LyX-Code
rotate(log_file)
\end_layout
\end_deeper
\begin_layout LyX-Code
fcntl_unlock(lock_file)
\end_layout
\begin_layout LyX-Code
pthread_mutex_unlock
\end_layout
\end_deeper
\end_deeper
\begin_layout Standard
mutex_lock is for multi-thread and fcntl_lock is for multi-process.
fcntl_lock is the POSIX advisory record locking.
See man 3 fcntl for details.
The lock is system-wide, and when a process died unexpected unexpectedly,
operating system release all lock owned by the process.
That's why I chose fcntl lock for rotating log safely.
Process need read-write permisson of lock_file to lock it.
\end_layout
\begin_layout Standard
By default, rotate lock file = self.
In this way, zlog do not create any lock file and set the configure file
as the lock file.
As fcntl is advisory, it does not really forbid people to change and store
his configure file.
Generally speaking, one log file will not be rotated by processes run by
different operating system user, so using configure file as lock file is
safe.
\end_layout
\begin_layout Standard
If you chose other path as lock file, for example, /tmp/zlog.lock, zlog will
create it at zlog_init().
Make sure your program has permission to create and read-write the file.
If programs run by different users who need to write and rotater a same
log file, make sure that each program has permission to create and read-write
the same lock file.
The default is /tmp/zlog.lock
If processes run by different operating system users who need to write
and rotater the same log file, make sure that each program has permission
to create and read-write the same lock file.
\end_layout
\begin_layout Itemize
......
......@@ -38,8 +38,8 @@
#define ZLOG_CONF_DEFAULT_RULE "*.* >stdout"
#define ZLOG_CONF_DEFAULT_BUF_SIZE_MIN 1024
#define ZLOG_CONF_DEFAULT_BUF_SIZE_MAX (2 * 1024 * 1024)
#define ZLOG_CONF_DEFAULT_ROTATE_LOCK_FILE "/tmp/zlog.lock"
#define ZLOG_CONF_DEFAULT_FILE_PERMS 0600
#define ZLOG_CONF_BACKUP_ROTATE_LOCK_FILE "/tmp/zlog.lock"
/*******************************************************************************/
struct zlog_conf_s {
......@@ -155,7 +155,11 @@ zlog_conf_t *zlog_conf_new(char *conf_file)
a_conf->strict_init = 1;
a_conf->buf_size_min = ZLOG_CONF_DEFAULT_BUF_SIZE_MIN;
a_conf->buf_size_max = ZLOG_CONF_DEFAULT_BUF_SIZE_MAX;
strcpy(a_conf->rotate_lock_file, ZLOG_CONF_DEFAULT_ROTATE_LOCK_FILE);
if (has_conf_file) {
strcpy(a_conf->rotate_lock_file, a_conf->file);
} else {
strcpy(a_conf->rotate_lock_file, ZLOG_CONF_BACKUP_ROTATE_LOCK_FILE);
}
strcpy(a_conf->default_format_line, ZLOG_CONF_DEFAULT_FORMAT);
a_conf->file_perms = ZLOG_CONF_DEFAULT_FILE_PERMS;
/* set default configuration end */
......
......@@ -74,8 +74,6 @@ int main(int argc, char** argv)
return 2;
}
zlog_profile();
zc = zlog_get_category("my_cat");
if (!zc) {
printf("get cat failed\n");
......
......@@ -3,4 +3,4 @@ default format = "%d(%F %T.%us) %-6V %p:%F:%L %m%n"
file perms = 0777
[rules]
*.* "press.log";
*.* "press.log",1M;
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