Skip to content

Commit

Permalink
Add set file permission function.
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Jul 26, 2021
1 parent 1b80601 commit 4e1c76a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
14 changes: 13 additions & 1 deletion tlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
80 changes: 49 additions & 31 deletions tlog.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* tinylog
* Copyright (C) 2018-2020 Ruilin Peng (Nick) <[email protected]>
* Copyright (C) 2018-2021 Ruilin Peng (Nick) <[email protected]>
* https://github.com/pymumu/tinylog
*/

#ifndef TLOG_H
#define TLOG_H
#include <stdarg.h>
#include <sys/stat.h>

#ifdef __cplusplus
#include <functional>
Expand Down Expand Up @@ -196,66 +197,83 @@ 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<Stream, std::function<void(Stream *)>>;

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<Stream, std::function<void(Stream *)>>;

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)
#define tlog_warn Tlog_stream(TLOG_WARN)
#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
Expand Down

0 comments on commit 4e1c76a

Please sign in to comment.