Skip to content

Commit

Permalink
add message log
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Aug 22, 2014
1 parent 04688e9 commit 16f63df
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \
SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \
skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \
skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \
malloc_hook.c skynet_daemon.c
malloc_hook.c skynet_daemon.c skynet_log.c

all : \
$(SKYNET_BUILD_PATH)/skynet \
Expand Down
1 change: 1 addition & 0 deletions examples/config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
root = "./"
thread = 8
logger = nil
logpath = "."
harbor = 1
address = "127.0.0.1:2526"
master = "127.0.0.1:2013"
Expand Down
7 changes: 1 addition & 6 deletions lualib/skynet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,7 @@ local function dispatch_message(...)
end

function skynet.newservice(name, ...)
local handle = skynet.tostring(skynet.rawcall(".launcher", "lua" , skynet.pack("LAUNCH", "snlua", name, ...)))
if handle == "" then
return nil
else
return string_to_handle(handle)
end
return skynet.call(".launcher", "lua" , "LAUNCH", "snlua", name, ...)
end

function skynet.uniqueservice(global, ...)
Expand Down
23 changes: 23 additions & 0 deletions service/debug_console.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local skynet = require "skynet"
local codecache = require "skynet.codecache"
local core = require "skynet.core"
local socket = require "socket"
local snax = require "snax"

Expand Down Expand Up @@ -120,6 +121,9 @@ function COMMAND.help()
service = "List unique service",
task = "task address : show service task detail",
inject = "inject address luascript.lua",
logon = "logon address",
logoff = "logoff address",
log = "launch a new lua service with log",
}
end

Expand All @@ -136,6 +140,15 @@ function COMMAND.start(...)
end
end

function COMMAND.log(...)
local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...)
if ok then
return { [skynet.address(addr)] = ... }
else
return "Failed"
end
end

function COMMAND.snax(...)
local ok, s = pcall(snax.newservice, ...)
if ok then
Expand Down Expand Up @@ -181,3 +194,13 @@ function COMMAND.info(address)
address = adjust_address(address)
return skynet.call(address,"debug","INFO")
end

function COMMAND.logon(address)
address = adjust_address(address)
core.command("LOGON", skynet.address(address))
end

function COMMAND.logoff(address)
address = adjust_address(address)
core.command("LOGOFF", skynet.address(address))
end
32 changes: 21 additions & 11 deletions service/launcher.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local skynet = require "skynet"
local core = require "skynet.core"
local string = string

local services = {}
Expand Down Expand Up @@ -65,18 +66,29 @@ function command.REMOVE(_, handle)
return NORET
end

local function return_string(str)
return str
end

function command.LAUNCH(_, service, ...)
local function launch_service(service, ...)
local param = table.concat({...}, " ")
local inst = skynet.launch(service, param)
local response = skynet.response()
if inst then
services[inst] = service .. " " .. param
instance[inst] = skynet.response(return_string)
instance[inst] = response
else
skynet.ret("") -- launch failed
response(false)
return
end
return inst
end

function command.LAUNCH(_, service, ...)
launch_service(service, ...)
return NORET
end

function command.LOGLAUNCH(_, service, ...)
local inst = launch_service(service, ...)
if inst then
core.command("LOGON", skynet.address(inst))
end
return NORET
end
Expand All @@ -97,7 +109,7 @@ function command.LAUNCHOK(address)
-- init notice
local response = instance[address]
if response then
response(true, skynet.address(address))
response(true, address)
instance[address] = nil
end

Expand All @@ -116,9 +128,7 @@ skynet.register_protocol {
elseif cmd == "ERROR" then
command.ERROR(address)
else
-- launch request
local service, param = string.match(cmd,"([^ ]+) (.*)")
command.LAUNCH(_, service, param)
error ("Invalid text command " .. cmd)
end
end,
}
Expand Down
77 changes: 77 additions & 0 deletions skynet-src/skynet_log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "skynet_log.h"
#include "skynet_timer.h"
#include "skynet.h"
#include "skynet_socket.h"
#include <string.h>
#include <time.h>

FILE *
skynet_log_open(struct skynet_context * ctx, uint32_t handle) {
const char * logpath = skynet_getenv("logpath");
if (logpath == NULL)
return NULL;
size_t sz = strlen(logpath);
char tmp[sz + 16];
sprintf(tmp, "%s/%08x.log", logpath, handle);
FILE *f = fopen(tmp, "ab");
if (f) {
uint32_t starttime = skynet_gettime_fixsec();
uint32_t currenttime = skynet_gettime();
time_t ti = starttime + currenttime/100;
skynet_error(ctx, "Open log file %s", tmp);
fprintf(f, "open time: %u %s", currenttime, ctime(&ti));
fflush(f);
} else {
skynet_error(ctx, "Open log file %s fail", tmp);
}
return f;
}

void
skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) {
skynet_error(ctx, "Close log file :%08x", handle);
fprintf(f, "close time: %u\n", skynet_gettime());
fclose(f);
}

static void
log_blob(FILE *f, void * buffer, size_t sz) {
size_t i;
uint8_t * buf = buffer;
for (i=0;i!=sz;i++) {
fprintf(f, "%02x", buf[i]);
}
}

static void
log_socket(FILE * f, struct skynet_socket_message * message, size_t sz) {
fprintf(f, "[socket] %d %d %d ", message->type, message->id, message->ud);

if (message->buffer == NULL) {
const char *buffer = (const char *)(message + 1);
sz -= sizeof(*message);
const char * eol = memchr(buffer, '\0', sz);
if (eol) {
sz = eol - buffer;
}
fprintf(f, "[%*s]", (int)sz, (const char *)buffer);
} else {
sz = message->ud;
log_blob(f, message->buffer, sz);
}
fprintf(f, "\n");
fflush(f);
}

void
skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz) {
if (type == PTYPE_SOCKET) {
log_socket(f, buffer, sz);
} else {
uint32_t ti = skynet_gettime();
fprintf(f, ":%08x %d %d %u ", source, type, session, ti);
log_blob(f, buffer, sz);
fprintf(f,"\n");
fflush(f);
}
}
14 changes: 14 additions & 0 deletions skynet-src/skynet_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef skynet_log_h
#define skynet_log_h

#include "skynet_env.h"
#include "skynet.h"

#include <stdio.h>
#include <stdint.h>

FILE * skynet_log_open(struct skynet_context * ctx, uint32_t handle);
void skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle);
void skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz);

#endif
Loading

0 comments on commit 16f63df

Please sign in to comment.