Skip to content

Commit 16f63df

Browse files
committed
add message log
1 parent 04688e9 commit 16f63df

File tree

8 files changed

+209
-37
lines changed

8 files changed

+209
-37
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \
4848
SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \
4949
skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \
5050
skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \
51-
malloc_hook.c skynet_daemon.c
51+
malloc_hook.c skynet_daemon.c skynet_log.c
5252

5353
all : \
5454
$(SKYNET_BUILD_PATH)/skynet \

examples/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
root = "./"
22
thread = 8
33
logger = nil
4+
logpath = "."
45
harbor = 1
56
address = "127.0.0.1:2526"
67
master = "127.0.0.1:2013"

lualib/skynet.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,7 @@ local function dispatch_message(...)
523523
end
524524

525525
function skynet.newservice(name, ...)
526-
local handle = skynet.tostring(skynet.rawcall(".launcher", "lua" , skynet.pack("LAUNCH", "snlua", name, ...)))
527-
if handle == "" then
528-
return nil
529-
else
530-
return string_to_handle(handle)
531-
end
526+
return skynet.call(".launcher", "lua" , "LAUNCH", "snlua", name, ...)
532527
end
533528

534529
function skynet.uniqueservice(global, ...)

service/debug_console.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local skynet = require "skynet"
22
local codecache = require "skynet.codecache"
3+
local core = require "skynet.core"
34
local socket = require "socket"
45
local snax = require "snax"
56

@@ -120,6 +121,9 @@ function COMMAND.help()
120121
service = "List unique service",
121122
task = "task address : show service task detail",
122123
inject = "inject address luascript.lua",
124+
logon = "logon address",
125+
logoff = "logoff address",
126+
log = "launch a new lua service with log",
123127
}
124128
end
125129

@@ -136,6 +140,15 @@ function COMMAND.start(...)
136140
end
137141
end
138142

143+
function COMMAND.log(...)
144+
local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...)
145+
if ok then
146+
return { [skynet.address(addr)] = ... }
147+
else
148+
return "Failed"
149+
end
150+
end
151+
139152
function COMMAND.snax(...)
140153
local ok, s = pcall(snax.newservice, ...)
141154
if ok then
@@ -181,3 +194,13 @@ function COMMAND.info(address)
181194
address = adjust_address(address)
182195
return skynet.call(address,"debug","INFO")
183196
end
197+
198+
function COMMAND.logon(address)
199+
address = adjust_address(address)
200+
core.command("LOGON", skynet.address(address))
201+
end
202+
203+
function COMMAND.logoff(address)
204+
address = adjust_address(address)
205+
core.command("LOGOFF", skynet.address(address))
206+
end

service/launcher.lua

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local skynet = require "skynet"
2+
local core = require "skynet.core"
23
local string = string
34

45
local services = {}
@@ -65,18 +66,29 @@ function command.REMOVE(_, handle)
6566
return NORET
6667
end
6768

68-
local function return_string(str)
69-
return str
70-
end
71-
72-
function command.LAUNCH(_, service, ...)
69+
local function launch_service(service, ...)
7370
local param = table.concat({...}, " ")
7471
local inst = skynet.launch(service, param)
72+
local response = skynet.response()
7573
if inst then
7674
services[inst] = service .. " " .. param
77-
instance[inst] = skynet.response(return_string)
75+
instance[inst] = response
7876
else
79-
skynet.ret("") -- launch failed
77+
response(false)
78+
return
79+
end
80+
return inst
81+
end
82+
83+
function command.LAUNCH(_, service, ...)
84+
launch_service(service, ...)
85+
return NORET
86+
end
87+
88+
function command.LOGLAUNCH(_, service, ...)
89+
local inst = launch_service(service, ...)
90+
if inst then
91+
core.command("LOGON", skynet.address(inst))
8092
end
8193
return NORET
8294
end
@@ -97,7 +109,7 @@ function command.LAUNCHOK(address)
97109
-- init notice
98110
local response = instance[address]
99111
if response then
100-
response(true, skynet.address(address))
112+
response(true, address)
101113
instance[address] = nil
102114
end
103115

@@ -116,9 +128,7 @@ skynet.register_protocol {
116128
elseif cmd == "ERROR" then
117129
command.ERROR(address)
118130
else
119-
-- launch request
120-
local service, param = string.match(cmd,"([^ ]+) (.*)")
121-
command.LAUNCH(_, service, param)
131+
error ("Invalid text command " .. cmd)
122132
end
123133
end,
124134
}

skynet-src/skynet_log.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "skynet_log.h"
2+
#include "skynet_timer.h"
3+
#include "skynet.h"
4+
#include "skynet_socket.h"
5+
#include <string.h>
6+
#include <time.h>
7+
8+
FILE *
9+
skynet_log_open(struct skynet_context * ctx, uint32_t handle) {
10+
const char * logpath = skynet_getenv("logpath");
11+
if (logpath == NULL)
12+
return NULL;
13+
size_t sz = strlen(logpath);
14+
char tmp[sz + 16];
15+
sprintf(tmp, "%s/%08x.log", logpath, handle);
16+
FILE *f = fopen(tmp, "ab");
17+
if (f) {
18+
uint32_t starttime = skynet_gettime_fixsec();
19+
uint32_t currenttime = skynet_gettime();
20+
time_t ti = starttime + currenttime/100;
21+
skynet_error(ctx, "Open log file %s", tmp);
22+
fprintf(f, "open time: %u %s", currenttime, ctime(&ti));
23+
fflush(f);
24+
} else {
25+
skynet_error(ctx, "Open log file %s fail", tmp);
26+
}
27+
return f;
28+
}
29+
30+
void
31+
skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) {
32+
skynet_error(ctx, "Close log file :%08x", handle);
33+
fprintf(f, "close time: %u\n", skynet_gettime());
34+
fclose(f);
35+
}
36+
37+
static void
38+
log_blob(FILE *f, void * buffer, size_t sz) {
39+
size_t i;
40+
uint8_t * buf = buffer;
41+
for (i=0;i!=sz;i++) {
42+
fprintf(f, "%02x", buf[i]);
43+
}
44+
}
45+
46+
static void
47+
log_socket(FILE * f, struct skynet_socket_message * message, size_t sz) {
48+
fprintf(f, "[socket] %d %d %d ", message->type, message->id, message->ud);
49+
50+
if (message->buffer == NULL) {
51+
const char *buffer = (const char *)(message + 1);
52+
sz -= sizeof(*message);
53+
const char * eol = memchr(buffer, '\0', sz);
54+
if (eol) {
55+
sz = eol - buffer;
56+
}
57+
fprintf(f, "[%*s]", (int)sz, (const char *)buffer);
58+
} else {
59+
sz = message->ud;
60+
log_blob(f, message->buffer, sz);
61+
}
62+
fprintf(f, "\n");
63+
fflush(f);
64+
}
65+
66+
void
67+
skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz) {
68+
if (type == PTYPE_SOCKET) {
69+
log_socket(f, buffer, sz);
70+
} else {
71+
uint32_t ti = skynet_gettime();
72+
fprintf(f, ":%08x %d %d %u ", source, type, session, ti);
73+
log_blob(f, buffer, sz);
74+
fprintf(f,"\n");
75+
fflush(f);
76+
}
77+
}

skynet-src/skynet_log.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef skynet_log_h
2+
#define skynet_log_h
3+
4+
#include "skynet_env.h"
5+
#include "skynet.h"
6+
7+
#include <stdio.h>
8+
#include <stdint.h>
9+
10+
FILE * skynet_log_open(struct skynet_context * ctx, uint32_t handle);
11+
void skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle);
12+
void skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz);
13+
14+
#endif

0 commit comments

Comments
 (0)