mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
reopen log file when recv signal HUP
This commit is contained in:
@@ -3,9 +3,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
struct logger {
|
struct logger {
|
||||||
FILE * handle;
|
FILE * handle;
|
||||||
|
char * filename;
|
||||||
int close;
|
int close;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -14,6 +16,8 @@ logger_create(void) {
|
|||||||
struct logger * inst = skynet_malloc(sizeof(*inst));
|
struct logger * inst = skynet_malloc(sizeof(*inst));
|
||||||
inst->handle = NULL;
|
inst->handle = NULL;
|
||||||
inst->close = 0;
|
inst->close = 0;
|
||||||
|
inst->filename = NULL;
|
||||||
|
|
||||||
return inst;
|
return inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,16 +26,26 @@ logger_release(struct logger * inst) {
|
|||||||
if (inst->close) {
|
if (inst->close) {
|
||||||
fclose(inst->handle);
|
fclose(inst->handle);
|
||||||
}
|
}
|
||||||
|
skynet_free(inst->filename);
|
||||||
skynet_free(inst);
|
skynet_free(inst);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_logger(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
logger_cb(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
||||||
struct logger * inst = ud;
|
struct logger * inst = ud;
|
||||||
fprintf(inst->handle, "[:%08x] ",source);
|
switch (type) {
|
||||||
fwrite(msg, sz , 1, inst->handle);
|
case PTYPE_SYSTEM:
|
||||||
fprintf(inst->handle, "\n");
|
if (inst->filename) {
|
||||||
fflush(inst->handle);
|
inst->handle = freopen(inst->filename, "a", inst->handle);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PTYPE_TEXT:
|
||||||
|
fprintf(inst->handle, "[:%08x] ",source);
|
||||||
|
fwrite(msg, sz , 1, inst->handle);
|
||||||
|
fprintf(inst->handle, "\n");
|
||||||
|
fflush(inst->handle);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -43,12 +57,14 @@ logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm)
|
|||||||
if (inst->handle == NULL) {
|
if (inst->handle == NULL) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
inst->filename = skynet_malloc(strlen(parm)+1);
|
||||||
|
strcpy(inst->filename, parm);
|
||||||
inst->close = 1;
|
inst->close = 1;
|
||||||
} else {
|
} else {
|
||||||
inst->handle = stdout;
|
inst->handle = stdout;
|
||||||
}
|
}
|
||||||
if (inst->handle) {
|
if (inst->handle) {
|
||||||
skynet_callback(ctx, inst, _logger);
|
skynet_callback(ctx, inst, logger_cb);
|
||||||
skynet_command(ctx, "REG", ".logger");
|
skynet_command(ctx, "REG", ".logger");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
struct monitor {
|
struct monitor {
|
||||||
int count;
|
int count;
|
||||||
@@ -32,6 +33,15 @@ struct worker_parm {
|
|||||||
int weight;
|
int weight;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int SIG = 0;
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_hup(int signal) {
|
||||||
|
if (signal == SIGHUP) {
|
||||||
|
SIG = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#define CHECK_ABORT if (skynet_context_total()==0) break;
|
#define CHECK_ABORT if (skynet_context_total()==0) break;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -100,6 +110,21 @@ thread_monitor(void *p) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
signal_hup() {
|
||||||
|
// make log file reopen
|
||||||
|
|
||||||
|
struct skynet_message smsg;
|
||||||
|
smsg.source = 0;
|
||||||
|
smsg.session = 0;
|
||||||
|
smsg.data = NULL;
|
||||||
|
smsg.sz = (size_t)PTYPE_SYSTEM << MESSAGE_TYPE_SHIFT;
|
||||||
|
uint32_t logger = skynet_handle_findname("logger");
|
||||||
|
if (logger) {
|
||||||
|
skynet_context_push(logger, &smsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void *
|
static void *
|
||||||
thread_timer(void *p) {
|
thread_timer(void *p) {
|
||||||
struct monitor * m = p;
|
struct monitor * m = p;
|
||||||
@@ -109,6 +134,10 @@ thread_timer(void *p) {
|
|||||||
CHECK_ABORT
|
CHECK_ABORT
|
||||||
wakeup(m,m->count-1);
|
wakeup(m,m->count-1);
|
||||||
usleep(2500);
|
usleep(2500);
|
||||||
|
if (SIG) {
|
||||||
|
signal_hup();
|
||||||
|
SIG = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// wakeup socket thread
|
// wakeup socket thread
|
||||||
skynet_socket_exit();
|
skynet_socket_exit();
|
||||||
@@ -216,6 +245,13 @@ bootstrap(struct skynet_context * logger, const char * cmdline) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
skynet_start(struct skynet_config * config) {
|
skynet_start(struct skynet_config * config) {
|
||||||
|
// register SIGHUP for log file reopen
|
||||||
|
struct sigaction sa;
|
||||||
|
sa.sa_handler = &handle_hup;
|
||||||
|
sa.sa_flags = SA_RESTART;
|
||||||
|
sigfillset(&sa.sa_mask);
|
||||||
|
sigaction(SIGHUP, &sa, NULL);
|
||||||
|
|
||||||
if (config->daemon) {
|
if (config->daemon) {
|
||||||
if (daemon_init(config->daemon)) {
|
if (daemon_init(config->daemon)) {
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user