From 1a44bfb09ab404ba93c85f6f8b606565b1f369a0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 19 Jun 2014 22:12:29 +0800 Subject: [PATCH] launch logger in C --- service/bootstrap.lua | 2 -- skynet-src/skynet_imp.h | 1 + skynet-src/skynet_main.c | 1 + skynet-src/skynet_server.c | 10 ++++++++++ skynet-src/skynet_server.h | 1 + skynet-src/skynet_start.c | 11 +++++++++-- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 3d66115b..4e2377e0 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -2,8 +2,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" skynet.start(function() - assert(skynet.launch("logger", skynet.getenv "logger")) - local standalone = skynet.getenv "standalone" local harbor_id = tonumber(skynet.getenv "harbor") if harbor_id == 0 then diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index 11b12ee1..c5a144a4 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -7,6 +7,7 @@ struct skynet_config { const char * daemon; const char * module_path; const char * bootstrap; + const char * logger; }; #define THREAD_WORKER 0 diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 8596c778..9c38fd25 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -115,6 +115,7 @@ main(int argc, char *argv[]) { config.harbor = optint("harbor", 1); config.bootstrap = optstring("bootstrap","snlua bootstrap"); config.daemon = optstring("daemon", NULL); + config.logger = optstring("logger", NULL); lua_close(L); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index e20f8102..b7b809ef 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -234,6 +234,16 @@ _dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { CHECKCALLING_END(ctx) } +void +skynet_context_dispatchall(struct skynet_context * ctx) { + // for skynet_error + struct skynet_message msg; + struct message_queue *q = ctx->queue; + while (!skynet_mq_pop(q,&msg)) { + _dispatch_message(ctx, &msg); + } +} + struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q) { if (q == NULL) { diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 51b8d1b2..be4e301f 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -18,6 +18,7 @@ void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, int skynet_context_newsession(struct skynet_context *); struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *); // return next queue int skynet_context_total(); +void skynet_context_dispatchall(struct skynet_context * context); // for skynet_error output before exit void skynet_context_endless(uint32_t handle); // for monitor diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index d45ded49..c101da30 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -184,7 +184,7 @@ _start(int thread) { } static void -bootstrap(const char * cmdline) { +bootstrap(struct skynet_context * logger, const char * cmdline) { int sz = strlen(cmdline); char name[sz+1]; char args[sz+1]; @@ -192,6 +192,7 @@ bootstrap(const char * cmdline) { struct skynet_context *ctx = skynet_context_new(name, args); if (ctx == NULL) { skynet_error(NULL, "Bootstrap error : %s\n", cmdline); + skynet_context_dispatchall(logger); exit(1); } } @@ -210,7 +211,13 @@ skynet_start(struct skynet_config * config) { skynet_timer_init(); skynet_socket_init(); - bootstrap(config->bootstrap); + struct skynet_context *ctx = skynet_context_new("logger", config->logger); + if (ctx == NULL) { + fprintf(stderr, "Can't launch logger service\n"); + exit(1); + } + + bootstrap(ctx, config->bootstrap); _start(config->thread); skynet_socket_free();