Commit 6691ac1e authored by Hardy Simpson's avatar Hardy Simpson

feature: file permission can be set!

parent edbc5e0f
......@@ -40,9 +40,9 @@
[o] 把reload改成原子性的,失败用旧的
[o] 优化spec_gen_msg部分, spec内采用自己写的buf_append对齐来代替buf_sprintf
[o] 引入__func__
[o] 文件的权限设置
[p] 使用valgrind测试性能
[ ] 文件的权限设置
[ ] 输出函数自定义
[ ] 增加man age, df, 案例, doxygen
[ ] zlog-chk-conf -c
......
......@@ -4,18 +4,21 @@ buffer min = 1024
buffer max = 2MB
rotate lock file = /tmp/zlog.lock
default format = "%d(%F %T.%l) %-6P (%c:%F:%L) - %m%n"
file mode = 600
[levels]
TRACE = 10, LOG_ERR
TRACE = 10
CRIT = 130, LOG_CRIT
[formats]
simple = "%m%n"
normal = "%d(%F %T.%l) %m%n"
[rules]
default.* >stdout; simple
*.* "%12.2E(HOME)/log/%c.log", 1MB*12; simple
my_.INFO >stderr; default
my_.INFO >stderr;
my_cat.!ERROR "/var/log/aa.log"
my_dog.=DEBUG >syslog, LOG_LOCAL0; simple
my_fish.warn |/var/pipe; normal
my_mice.* @user_define;
......@@ -39,6 +39,7 @@
#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
/*******************************************************************************/
struct zlog_conf_s {
......@@ -57,6 +58,8 @@ struct zlog_conf_s {
char default_format_line[MAXLEN_CFG_LINE + 1];
zlog_format_t *default_format;
unsigned int file_perms;
zc_arraylist_t *formats;
zc_arraylist_t *rules;
......@@ -75,12 +78,13 @@ void zlog_conf_profile(zlog_conf_t * a_conf, int flag)
zc_profile(flag, "---strict init[%d]---", a_conf->strict_init);
zc_profile(flag, "---buffer min[%ld]---", a_conf->buf_size_min);
zc_profile(flag, "---buffer max[%ld]---", a_conf->buf_size_max);
zc_profile(flag, "---rotate lock file[%s]---", a_conf->rotate_lock_file);
if (a_conf->default_format) {
zc_profile(flag, "---default_format---");
zlog_format_profile(a_conf->default_format, flag);
}
zc_profile(flag, "---file perms[0%o]---", a_conf->file_perms);
zc_profile(flag, "---rotate lock file[%s]---", a_conf->rotate_lock_file);
if (a_conf->rotater) zlog_rotater_profile(a_conf->rotater, flag);
if (a_conf->levels) zlog_level_list_profile(a_conf->levels, flag);
......@@ -153,6 +157,7 @@ zlog_conf_t *zlog_conf_new(char *conf_file)
a_conf->buf_size_max = ZLOG_CONF_DEFAULT_BUF_SIZE_MAX;
strcpy(a_conf->rotate_lock_file, ZLOG_CONF_DEFAULT_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 */
a_conf->levels = zlog_level_list_new();
......@@ -222,7 +227,8 @@ static int zlog_conf_build_without_file(zlog_conf_t * a_conf)
a_conf->rotater,
a_conf->levels,
a_conf->default_format,
a_conf->formats);
a_conf->formats,
a_conf->file_perms);
if (!default_rule) {
zc_error("zlog_rule_new fail");
return -1;
......@@ -432,13 +438,15 @@ static int zlog_conf_parse_line(zlog_conf_t * a_conf, char *line, int *section)
a_conf->buf_size_min = zc_parse_byte_size(value);
} else if (STRCMP(word_1, ==, "buffer") && STRCMP(word_2, ==, "max")) {
a_conf->buf_size_max = zc_parse_byte_size(value);
} else if (STRCMP(word_1, ==, "file") && STRCMP(word_2, ==, "perms")) {
sscanf(value, "%o", &(a_conf->file_perms));
} else if (STRCMP(word_1, ==, "rotate") &&
STRCMP(word_2, ==, "lock") && STRCMP(word_3, ==, "file")) {
/* may overwrite the inner default value, or last value */
strcpy(a_conf->rotate_lock_file, value);
} else if (STRCMP(word_1, ==, "default") && STRCMP(word_2, ==, "format")) {
strcpy(a_conf->default_format_line, line + nread);
/* so the input now is [format = "xxyy"], fit format's style */
strcpy(a_conf->default_format_line, line + nread);
} else {
zc_error("name[%s] is not any one of global options", name);
if (a_conf->strict_init) return -1;
......@@ -466,12 +474,12 @@ static int zlog_conf_parse_line(zlog_conf_t * a_conf, char *line, int *section)
}
break;
case 4:
zc_debug("");
a_rule = zlog_rule_new(line,
a_conf->rotater,
a_conf->levels,
a_conf->default_format,
a_conf->formats);
a_conf->formats,
a_conf->file_perms);
if (!a_rule) {
zc_error("zlog_rule_new fail [%s]", line);
if (a_conf->strict_init) return -1;
......
......@@ -52,6 +52,7 @@ struct zlog_rule_s {
*/
char file_path[MAXLEN_PATH + 1];
unsigned int file_perms;
zc_arraylist_t *dynamic_file_specs;
zlog_rotater_t *rotater;
......@@ -72,12 +73,13 @@ void zlog_rule_profile(zlog_rule_t * a_rule, int flag)
zlog_spec_t *a_spec;
zc_assert(a_rule,);
zc_profile(flag, "---rule:[%p][%s%c%d]-[%s|%p,%ld*%d|%d;%p]---",
zc_profile(flag, "---rule:[%p][%s%c%d]-[%s(0%o)|%p,%ld*%d|%d;%p]---",
a_rule,
a_rule->category,
a_rule->compare_char,
a_rule->level,
a_rule->file_path,
a_rule->file_perms,
a_rule->dynamic_file_specs,
a_rule->file_max_size,
a_rule->file_max_count,
......@@ -111,8 +113,7 @@ static int zlog_rule_output_static_file_single(zlog_rule_t * a_rule,
msg = a_thread->msg_buf->start;
msg_len = a_thread->msg_buf->end - a_thread->msg_buf->start;
fd = open(a_rule->file_path, O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fd = open(a_rule->file_path, O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
if (fd < 0) {
zc_error("open file[%s] fail, errno[%d]", a_rule->file_path,
errno);
......@@ -148,8 +149,7 @@ static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule,
msg = a_thread->msg_buf->start;
msg_len = a_thread->msg_buf->end - a_thread->msg_buf->start;
fd = open(a_rule->file_path, O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fd = open(a_rule->file_path, O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
if (fd < 0) {
zc_error("open file[%s] fail, errno[%d]", a_rule->file_path,
errno);
......@@ -226,11 +226,9 @@ static int zlog_rule_output_dynamic_file_single(zlog_rule_t * a_rule,
msg = a_thread->msg_buf->start;
msg_len = a_thread->msg_buf->end - a_thread->msg_buf->start;
fd = open(file_path, O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fd = open(file_path, O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
if (fd < 0) {
zc_error("open file[%s] fail, errno[%d]", a_rule->file_path,
errno);
zc_error("open file[%s] fail, errno[%d]", file_path, errno);
return -1;
}
......@@ -269,11 +267,9 @@ static int zlog_rule_output_dynamic_file_rotate(zlog_rule_t * a_rule,
msg = a_thread->msg_buf->start;
msg_len = a_thread->msg_buf->end - a_thread->msg_buf->start;
fd = open(file_path, O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fd = open(file_path, O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
if (fd < 0) {
zc_error("open file[%s] fail, errno[%d]", a_rule->file_path,
errno);
zc_error("open file[%s] fail, errno[%d]", file_path, errno);
return -1;
}
......@@ -410,7 +406,8 @@ zlog_rule_t *zlog_rule_new(char *line,
zlog_rotater_t * a_rotater,
zc_arraylist_t * levels,
zlog_format_t * default_format,
zc_arraylist_t * formats)
zc_arraylist_t * formats,
unsigned int file_perms)
{
int rc = 0;
int nscan = 0;
......@@ -445,6 +442,7 @@ zlog_rule_t *zlog_rule_new(char *line,
a_rule->rotater = a_rotater;
a_rule->levels = levels;
a_rule->file_perms = file_perms;
/* line [f.INFO "%H/log/aa.log", 20MB * 12; MyTemplate]
* selector [f.INFO]
......
......@@ -37,7 +37,8 @@ zlog_rule_t *zlog_rule_new(char *line,
zlog_rotater_t * a_rotater,
zc_arraylist_t * levels,
zlog_format_t * default_format,
zc_arraylist_t * formats);
zc_arraylist_t * formats,
unsigned int file_perms);
void zlog_rule_del(zlog_rule_t * a_rule);
void zlog_rule_profile(zlog_rule_t * a_rule, int flag);
......
......@@ -96,7 +96,7 @@ typedef enum {
zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
ZLOG_LEVEL_NOTICE, format, ##args)
#define ZLOG_INFO(cat, format, args...) \
zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
zlog(cat, __FILE__, sizeof(__FILE__), __func__, sizeof(__func__), __LINE__, \
ZLOG_LEVEL_INFO, format, ##args)
#define ZLOG_DEBUG(cat, format, args...) \
zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
......
[global]
default format = "%d(%F %T.%us) %-6V %p:%F:%L %m%n"
file perms = 0777
[rules]
*.* "press.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