Skip to content

Commit 4e1c76a

Browse files
committed
Add set file permission function.
1 parent 1b80601 commit 4e1c76a

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed

tlog.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ struct tlog_log {
7878

7979
time_t last_try;
8080
time_t last_waitpid;
81+
mode_t file_perm;
82+
mode_t archive_perm;
8183

8284
int waiters;
8385
int is_exit;
@@ -304,6 +306,12 @@ void tlog_set_maxline_size(struct tlog_log *log, int size)
304306
log->max_line_size = size;
305307
}
306308

309+
void tlog_set_permission(struct tlog_log *log, unsigned int file, unsigned int archive)
310+
{
311+
log->file_perm = file;
312+
log->archive_perm = archive;
313+
}
314+
307315
int tlog_localtime(struct tlog_time *tm)
308316
{
309317
return _tlog_gettime(tm);
@@ -684,6 +692,8 @@ static int _tlog_rename_logfile(struct tlog_log *log, const char *log_file)
684692
return -1;
685693
}
686694

695+
chmod(archive_file, log->archive_perm);
696+
687697
return 0;
688698
}
689699

@@ -1123,7 +1133,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
11231133
}
11241134
snprintf(logfile, sizeof(logfile), "%s/%s", log->logdir, log->logname);
11251135
log->filesize = 0;
1126-
log->fd = open(logfile, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, 0640);
1136+
log->fd = open(logfile, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, log->file_perm);
11271137
if (log->fd < 0) {
11281138
if (print_errmsg == 0) {
11291139
return -1;
@@ -1608,6 +1618,8 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
16081618
log->segment_log = ((flag & TLOG_SEGMENT) == 0) ? 0 : 1;
16091619
log->max_line_size = TLOG_MAX_LINE_LEN;
16101620
log->output_func = _tlog_write;
1621+
log->file_perm = S_IRUSR | S_IWUSR | S_IRGRP;
1622+
log->archive_perm = S_IRUSR | S_IRGRP;
16111623

16121624
tlog_rename_logfile(log, logfile);
16131625
if (log->nocompress) {

tlog.h

+49-31
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
22
* tinylog
3-
* Copyright (C) 2018-2020 Ruilin Peng (Nick) <[email protected]>
3+
* Copyright (C) 2018-2021 Ruilin Peng (Nick) <[email protected]>
44
* https://github.com/pymumu/tinylog
55
*/
66

77
#ifndef TLOG_H
88
#define TLOG_H
99
#include <stdarg.h>
10+
#include <sys/stat.h>
1011

1112
#ifdef __cplusplus
1213
#include <functional>
@@ -196,66 +197,83 @@ extern int tlog_localtime(struct tlog_time *tm);
196197
/* set max line size */
197198
extern void tlog_set_maxline_size(struct tlog_log *log, int size);
198199

200+
/*
201+
Function: set log file and archive permission
202+
log: log stream
203+
file: log file permission, default is 640
204+
archive: archive file permission, default is 440
205+
*/
206+
207+
extern void tlog_set_permission(struct tlog_log *log, mode_t file, mode_t archive);
208+
199209
#ifdef __cplusplus
200210
class Tlog {
201-
using Stream = std::ostringstream;
202-
using Buffer = std::unique_ptr<Stream, std::function<void(Stream *)>>;
203-
204211
public:
205-
Tlog() { }
206-
~Tlog() { }
212+
Tlog(tlog_level level, const char *file, int line, const char *func, void *userptr)
213+
{
214+
level_ = level;
215+
file_ = file;
216+
line_ = line;
217+
func_ = func;
218+
userptr_ = userptr;
219+
}
207220

208-
static Tlog &Instance()
221+
~Tlog()
209222
{
210-
static Tlog logger;
211-
return logger;
223+
tlog_ext(level_, file_, line_, func_, userptr_, "%s", msg_.str().c_str());
212224
}
213225

214-
Buffer LogStream(tlog_level level, const char *file, int line, const char *func, void *userptr)
226+
std::ostream &Stream()
215227
{
216-
return Buffer(new Stream, [=](Stream *st) {
217-
tlog_ext(level, file, line, func, userptr, "%s", st->str().c_str());
218-
delete st;
219-
});
228+
return msg_;
220229
}
230+
231+
private:
232+
tlog_level level_;
233+
const char *file_;
234+
int line_;
235+
const char *func_;
236+
void *userptr_;
237+
std::ostringstream msg_;
221238
};
222239

223240
class TlogOut {
224-
using Stream = std::ostringstream;
225-
using Buffer = std::unique_ptr<Stream, std::function<void(Stream *)>>;
226-
227241
public:
228-
TlogOut() { }
229-
~TlogOut() { }
242+
TlogOut(tlog_log *log)
243+
{
244+
log_ = log;
245+
}
230246

231-
static TlogOut &Instance()
247+
~TlogOut()
232248
{
233-
static TlogOut logger;
234-
return logger;
249+
if (log_ == nullptr) {
250+
return;
251+
}
252+
253+
tlog_printf(log_, "%s", msg_.str().c_str());
235254
}
236255

237-
Buffer Out(tlog_log *log)
256+
std::ostream &Stream()
238257
{
239-
return Buffer(new Stream, [=](Stream *st) {
240-
tlog_printf(log, "%s", st->str().c_str());
241-
delete st;
242-
});
258+
return msg_;
243259
}
260+
261+
private:
262+
tlog_log *log_;
263+
std::ostringstream msg_;
244264
};
245265

246-
#define Tlog_logger (Tlog::Instance())
247266
#define Tlog_stream(level) \
248267
if (tlog_getlevel() <= level) \
249-
*Tlog_logger.LogStream(level, BASE_FILE_NAME, __LINE__, __func__, NULL)
268+
Tlog(level, BASE_FILE_NAME, __LINE__, __func__, NULL).Stream()
250269
#define tlog_debug Tlog_stream(TLOG_DEBUG)
251270
#define tlog_info Tlog_stream(TLOG_INFO)
252271
#define tlog_notice Tlog_stream(TLOG_NOTICE)
253272
#define tlog_warn Tlog_stream(TLOG_WARN)
254273
#define tlog_error Tlog_stream(TLOG_ERROR)
255274
#define tlog_fatal Tlog_stream(TLOG_FATAL)
256275

257-
#define Tlog_out_logger (TlogOut::Instance())
258-
#define tlog_out(stream) (*Tlog_out_logger.Out(stream))
276+
#define tlog_out(stream) TlogOut(stream).Stream()
259277

260278
} /*__cplusplus */
261279
#else

0 commit comments

Comments
 (0)