diff --git a/tlog.c b/tlog.c index 6a3cfce..354a130 100644 --- a/tlog.c +++ b/tlog.c @@ -78,6 +78,8 @@ struct tlog_log { time_t last_try; time_t last_waitpid; + mode_t file_perm; + mode_t archive_perm; int waiters; int is_exit; @@ -304,6 +306,12 @@ void tlog_set_maxline_size(struct tlog_log *log, int size) log->max_line_size = size; } +void tlog_set_permission(struct tlog_log *log, unsigned int file, unsigned int archive) +{ + log->file_perm = file; + log->archive_perm = archive; +} + int tlog_localtime(struct tlog_time *tm) { return _tlog_gettime(tm); @@ -684,6 +692,8 @@ static int _tlog_rename_logfile(struct tlog_log *log, const char *log_file) return -1; } + chmod(archive_file, log->archive_perm); + return 0; } @@ -1123,7 +1133,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen) } snprintf(logfile, sizeof(logfile), "%s/%s", log->logdir, log->logname); log->filesize = 0; - log->fd = open(logfile, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, 0640); + log->fd = open(logfile, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, log->file_perm); if (log->fd < 0) { if (print_errmsg == 0) { return -1; @@ -1608,6 +1618,8 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu log->segment_log = ((flag & TLOG_SEGMENT) == 0) ? 0 : 1; log->max_line_size = TLOG_MAX_LINE_LEN; log->output_func = _tlog_write; + log->file_perm = S_IRUSR | S_IWUSR | S_IRGRP; + log->archive_perm = S_IRUSR | S_IRGRP; tlog_rename_logfile(log, logfile); if (log->nocompress) { diff --git a/tlog.h b/tlog.h index c58530b..e07fd8c 100644 --- a/tlog.h +++ b/tlog.h @@ -1,12 +1,13 @@ /* * tinylog - * Copyright (C) 2018-2020 Ruilin Peng (Nick) + * Copyright (C) 2018-2021 Ruilin Peng (Nick) * https://github.com/pymumu/tinylog */ #ifndef TLOG_H #define TLOG_H #include +#include #ifdef __cplusplus #include @@ -196,57 +197,75 @@ extern int tlog_localtime(struct tlog_time *tm); /* set max line size */ extern void tlog_set_maxline_size(struct tlog_log *log, int size); +/* +Function: set log file and archive permission +log: log stream +file: log file permission, default is 640 +archive: archive file permission, default is 440 +*/ + +extern void tlog_set_permission(struct tlog_log *log, mode_t file, mode_t archive); + #ifdef __cplusplus class Tlog { - using Stream = std::ostringstream; - using Buffer = std::unique_ptr>; - public: - Tlog() { } - ~Tlog() { } + Tlog(tlog_level level, const char *file, int line, const char *func, void *userptr) + { + level_ = level; + file_ = file; + line_ = line; + func_ = func; + userptr_ = userptr; + } - static Tlog &Instance() + ~Tlog() { - static Tlog logger; - return logger; + tlog_ext(level_, file_, line_, func_, userptr_, "%s", msg_.str().c_str()); } - Buffer LogStream(tlog_level level, const char *file, int line, const char *func, void *userptr) + std::ostream &Stream() { - return Buffer(new Stream, [=](Stream *st) { - tlog_ext(level, file, line, func, userptr, "%s", st->str().c_str()); - delete st; - }); + return msg_; } + +private: + tlog_level level_; + const char *file_; + int line_; + const char *func_; + void *userptr_; + std::ostringstream msg_; }; class TlogOut { - using Stream = std::ostringstream; - using Buffer = std::unique_ptr>; - public: - TlogOut() { } - ~TlogOut() { } + TlogOut(tlog_log *log) + { + log_ = log; + } - static TlogOut &Instance() + ~TlogOut() { - static TlogOut logger; - return logger; + if (log_ == nullptr) { + return; + } + + tlog_printf(log_, "%s", msg_.str().c_str()); } - Buffer Out(tlog_log *log) + std::ostream &Stream() { - return Buffer(new Stream, [=](Stream *st) { - tlog_printf(log, "%s", st->str().c_str()); - delete st; - }); + return msg_; } + +private: + tlog_log *log_; + std::ostringstream msg_; }; -#define Tlog_logger (Tlog::Instance()) #define Tlog_stream(level) \ if (tlog_getlevel() <= level) \ - *Tlog_logger.LogStream(level, BASE_FILE_NAME, __LINE__, __func__, NULL) + Tlog(level, BASE_FILE_NAME, __LINE__, __func__, NULL).Stream() #define tlog_debug Tlog_stream(TLOG_DEBUG) #define tlog_info Tlog_stream(TLOG_INFO) #define tlog_notice Tlog_stream(TLOG_NOTICE) @@ -254,8 +273,7 @@ class TlogOut { #define tlog_error Tlog_stream(TLOG_ERROR) #define tlog_fatal Tlog_stream(TLOG_FATAL) -#define Tlog_out_logger (TlogOut::Instance()) -#define tlog_out(stream) (*Tlog_out_logger.Out(stream)) +#define tlog_out(stream) TlogOut(stream).Stream() } /*__cplusplus */ #else