Skip to content

Commit

Permalink
support logrotate
Browse files Browse the repository at this point in the history
when run as daemon, stdout,stderr will be redirected to argv[0].log file
when receive SIGUSR1 signal, it will reopen argv[0].log file for
logrotate
  • Loading branch information
findstr committed Sep 24, 2017
1 parent 0fabf9e commit 9f333e5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 33 deletions.
12 changes: 6 additions & 6 deletions lualib-src/lualib-silly.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,27 @@ llog(lua_State *L)
int type = lua_type(L, i);
switch (type) {
case LUA_TSTRING:
silly_lograw("%s ", lua_tostring(L, i));
silly_log_raw("%s ", lua_tostring(L, i));
break;
case LUA_TNUMBER:
silly_lograw("%d ", (int)lua_tointeger(L, i));
silly_log_raw("%d ", (int)lua_tointeger(L, i));
break;
case LUA_TBOOLEAN:
silly_lograw("%s ",
silly_log_raw("%s ",
lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TTABLE:
silly_lograw("table: %p ", lua_topointer(L, i));
silly_log_raw("table: %p ", lua_topointer(L, i));
break;
case LUA_TNIL:
silly_lograw("#%d.null ", i);
silly_log_raw("#%d.null ", i);
break;
default:
return luaL_error(L, "log unspport param#%d type:%s",
i, lua_typename(L, type));
}
}
silly_lograw("\n");
silly_log_raw("\n");
return 0;
}

Expand Down
57 changes: 35 additions & 22 deletions silly-src/silly_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ static int pidfile;
extern int daemon(int, int);

static void
pidfile_create(const struct silly_config *config)
pidfile_create(const struct silly_config *conf)
{
int err;
const char *path = config->pidfile;
const char *path = conf->pidfile;
pidfile = -1;
if (path[0] == '\0')
return ;
Expand Down Expand Up @@ -55,48 +55,61 @@ pidfile_write()
}

static inline void
pidfile_delete(const struct silly_config *config)
pidfile_delete(const struct silly_config *conf)
{
if (pidfile == -1)
return ;
close(pidfile);
unlink(config->pidfile);
unlink(conf->pidfile);
return ;
}

void
silly_daemon_start(const struct silly_config *config)
static inline void
logfileopen(const struct silly_config *conf)
{
int fd;
int err;
char path[128];
if (!config->daemon)
return ;
pidfile_create(config);
err = daemon(1, 0);
if (err < 0) {
pidfile_delete(config);
silly_log("[daemon] %s\n", strerror(errno));
exit(0);
}
pidfile_write();
snprintf(path, 128, "%s%s-%d.log", config->logpath, config->selfname, getpid());
fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 00666);
snprintf(path, 128, "%s%s.log", conf->logpath, conf->selfname);
fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 00666);
if (fd >= 0) {
dup2(fd, 1);
dup2(fd, 2);
close(fd);
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
}
}

void
silly_daemon_start(const struct silly_config *conf)
{
int err;
if (!conf->daemon)
return ;
pidfile_create(conf);
err = daemon(1, 0);
if (err < 0) {
pidfile_delete(conf);
silly_log("[daemon] %s\n", strerror(errno));
exit(0);
}
pidfile_write();
logfileopen(conf);
return ;
}

void
silly_daemon_sigusr1(const struct silly_config *conf)
{
logfileopen(conf);
return ;
}

void
silly_daemon_stop(const struct silly_config *config)
silly_daemon_stop(const struct silly_config *conf)
{
if (!config->daemon)
if (!conf->daemon)
return ;
pidfile_delete(config);
pidfile_delete(conf);
return ;
}
5 changes: 3 additions & 2 deletions silly-src/silly_daemon.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef _SILLY_DAEMON_H
#define _SILLY_DAEMON_H

void silly_daemon_start(const struct silly_config *config);
void silly_daemon_stop(const struct silly_config *config);
void silly_daemon_start(const struct silly_config *conf);
void silly_daemon_sigusr1(const struct silly_config *conf);
void silly_daemon_stop(const struct silly_config *conf);


#endif
Expand Down
14 changes: 12 additions & 2 deletions silly-src/silly_log.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include "silly.h"
#include "silly_log.h"

static pid_t pid;

void
silly_log_start()
{
pid = getpid();
return ;
}

void
silly_lograw(const char *fmt, ...)
silly_log_raw(const char *fmt, ...)
{
va_list ap;
char buffer[LOG_MAX_LEN];
Expand All @@ -32,7 +42,7 @@ silly_log(const char *fmt, ...)
nr = strftime(head, sizeof(head), "%b %d %H:%M:%S.",
localtime(&tv.tv_sec));
snprintf(head + nr, sizeof(head) - nr, "%03d", (int)tv.tv_usec/1000);
fprintf(stdout, "%s %s", head, buffer);
fprintf(stdout, "%d %s %s", pid, head, buffer);
return ;
}

3 changes: 2 additions & 1 deletion silly-src/silly_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#define LOG_MAX_LEN (1024)

void silly_lograw(const char *fmt, ...);
void silly_log_start();
void silly_log_raw(const char *fmt, ...);
void silly_log(const char *fmt, ...);

#endif
Expand Down
11 changes: 11 additions & 0 deletions silly-src/silly_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
struct {
int exit;
int run;
const struct silly_config *conf;
pthread_mutex_t mutex;
pthread_cond_t cond;
} R;
Expand Down Expand Up @@ -109,12 +110,20 @@ signal_term(int sig)
R.exit = 1;
}

static void
signal_usr1(int sig)
{
(void)sig;
silly_daemon_sigusr1(R.conf);
}

static int
signal_init()
{
signal(SIGPIPE, SIG_IGN);
signal(SIGTERM, signal_term);
signal(SIGINT, signal_term);
signal(SIGUSR1, signal_usr1);
return 0;
}

Expand All @@ -126,9 +135,11 @@ silly_run(const struct silly_config *config)
pthread_t pid[3];
R.run = 1;
R.exit = 0;
R.conf = config;
pthread_mutex_init(&R.mutex, NULL);
pthread_cond_init(&R.cond, NULL);
silly_daemon_start(config);
silly_log_start();
signal_init();
silly_timer_init();
err = silly_socket_init();
Expand Down

0 comments on commit 9f333e5

Please sign in to comment.