Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
add backup to log_reopen, suppress compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kevyang committed Mar 7, 2016
1 parent bc1b56f commit b676f75
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
7 changes: 6 additions & 1 deletion include/cc_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ struct logger *log_create(char *filename, uint32_t buf_cap);

void log_destroy(struct logger **logger);

rstatus_i log_reopen(struct logger *logger);
/**
* Reopen the log file. Optional argument backup - if left NULL, log_reopen
* will simply reopen the log file. If specified, log_reopen will rename the
* original log file to the provided backup filename, and reopen the log file.
*/
rstatus_i log_reopen(struct logger *logger, char *backup);

/* _log_write returns true if msg written, false if skipped or failed */
bool log_write(struct logger *logger, char *buf, uint32_t len);
Expand Down
8 changes: 4 additions & 4 deletions include/cc_rbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ struct rbuf {

#define RBUF_HDR_SIZE offsetof(struct rbuf, data)

inline uint32_t
static inline uint32_t
get_rpos(struct rbuf *buf)
{
return __atomic_load_n(&(buf->rpos), __ATOMIC_RELAXED);
}

inline uint32_t
static inline uint32_t
get_wpos(struct rbuf *buf)
{
return __atomic_load_n(&(buf->wpos), __ATOMIC_RELAXED);
}

inline void
static inline void
set_rpos(struct rbuf *buf, uint32_t rpos)
{
__atomic_store_n(&(buf->rpos), rpos, __ATOMIC_RELAXED);
}

inline void
static inline void
set_wpos(struct rbuf *buf, uint32_t wpos)
{
__atomic_store_n(&(buf->wpos), wpos, __ATOMIC_RELAXED);
Expand Down
2 changes: 1 addition & 1 deletion src/cc_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static void
_logrotate(int signo)
{
log_info("received signal %d, reopen log file", signo);
log_reopen(dlog->logger);
log_reopen(dlog->logger, NULL);
}

static void
Expand Down
17 changes: 14 additions & 3 deletions src/cc_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,21 @@ log_destroy(struct logger **l)
}

rstatus_i
log_reopen(struct logger *logger)
log_reopen(struct logger *logger, char *backup)
{
int ret;

if (logger->fd != STDERR_FILENO && logger->fd != STDOUT_FILENO) {
close(logger->fd);

if (backup != NULL) {
ret = rename(logger->name, backup);
if (ret < 0) {
log_stderr("rename old klog file '%s' to '%s' failed, ignored: "
"%s", logger->name, backup, strerror(errno));
}
}

logger->fd = open(logger->name, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (logger->fd < 0) {
log_stderr("reopening log file '%s' failed, ignored: %s", logger->name,
Expand Down Expand Up @@ -282,13 +293,13 @@ log_flush(struct logger *logger)
size_t buf_len;

if (logger->buf == NULL) {
return;
return 0;
}

if (logger->fd < 0) {
log_stderr("Cannot flush logger %p; invalid file descriptor", logger);
INCR(log_metrics, log_flush_ex);
return;
return 0;
}

buf_len = rbuf_rcap(logger->buf);
Expand Down

0 comments on commit b676f75

Please sign in to comment.