Commit 1be371743429dae51e797a67ef1621e98cdcd88a
1 parent
e52c384f
error
Showing
1 changed file
with
145 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,145 @@ | @@ -0,0 +1,145 @@ | ||
| 1 | + | ||
| 2 | +local skynet = require "skynet" | ||
| 3 | +local socketdriver = require "skynet.socketdriver" | ||
| 4 | +local serverId = tonumber(skynet.getenv("servId")) | ||
| 5 | + | ||
| 6 | +require "shared.init" | ||
| 7 | +require "skynet.manager" | ||
| 8 | + | ||
| 9 | +local table_insert = table.insert | ||
| 10 | +local pairs = pairs | ||
| 11 | +local ipairs = ipairs | ||
| 12 | +local string_format = string.format | ||
| 13 | + | ||
| 14 | +local logHandle = { | ||
| 15 | + log = { | ||
| 16 | + host = "127.0.0.1", | ||
| 17 | + port = 13003, | ||
| 18 | + handle = function(doc, address, msg) | ||
| 19 | + -- 注入字段 | ||
| 20 | + local now = skynet.timex() | ||
| 21 | + doc["time"] = now | ||
| 22 | + doc["timestamp"] = now | ||
| 23 | + doc["instance_id"] = serverId | ||
| 24 | + doc["game_name"] = "wasteland" | ||
| 25 | + doc["env"] = "cb" | ||
| 26 | + doc["app_name"] = "gs" | ||
| 27 | + doc["app_id"] = "game.wasteland.gs" | ||
| 28 | + --FATAL、ERROR、WARN、INFO、DEBUG | ||
| 29 | + doc["level"] = "ERROR" | ||
| 30 | + doc["log"] = msg | ||
| 31 | + doc["address"] = string_format(":%08x", address) | ||
| 32 | + end | ||
| 33 | + }, | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +local connect_relation = {} | ||
| 37 | +local function getConInfo(fd) | ||
| 38 | + return logHandle[connect_relation[fd] or ""] | ||
| 39 | +end | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + | ||
| 43 | +local socket_message = {} | ||
| 44 | +-- read skynet_socket.h for these macro | ||
| 45 | +-- SKYNET_SOCKET_TYPE_DATA = 1 | ||
| 46 | +socket_message[1] = function(id, size, data) | ||
| 47 | + skynet.error(string.format("LOG SOCKET: data: ", skynet.tostring(data, size))) | ||
| 48 | + socketdriver.drop(data, size) | ||
| 49 | +end | ||
| 50 | + | ||
| 51 | +-- SKYNET_SOCKET_TYPE_CONNECT = 2 | ||
| 52 | +socket_message[2] = function(id, _ , addr) | ||
| 53 | + local cur = getConInfo(id) | ||
| 54 | + skynet.error("LOG SOCKET: connect: ", addr) | ||
| 55 | + cur.connected = true | ||
| 56 | + cur.connecting = false | ||
| 57 | +end | ||
| 58 | + | ||
| 59 | +-- SKYNET_SOCKET_TYPE_CLOSE = 3 | ||
| 60 | +socket_message[3] = function(id) | ||
| 61 | + skynet.error("LOG SOCKET: closed") | ||
| 62 | + local cur = getConInfo(id) | ||
| 63 | + if not cur then return end | ||
| 64 | + cur.connected = false | ||
| 65 | + cur.connecting = false | ||
| 66 | + connect_relation[id] = nil | ||
| 67 | +end | ||
| 68 | + | ||
| 69 | +-- SKYNET_SOCKET_TYPE_ERROR = 5 | ||
| 70 | +socket_message[5] = function(id, _, err) | ||
| 71 | + skynet.error("LOG SOCKET: error: ", err) | ||
| 72 | + local cur = getConInfo(id) | ||
| 73 | + if not cur then return end | ||
| 74 | + cur.connected = false | ||
| 75 | + cur.connecting = false | ||
| 76 | + connect_relation[id] = nil | ||
| 77 | +end | ||
| 78 | + | ||
| 79 | + | ||
| 80 | + | ||
| 81 | +local function open() | ||
| 82 | + for logTo, data in pairs(logHandle) do | ||
| 83 | + if not data.connecting and not data.connected then | ||
| 84 | + data.connecting = true | ||
| 85 | + data.fd = socketdriver.connect(data.host, data.port) | ||
| 86 | + connect_relation[data.fd] = logTo | ||
| 87 | + end | ||
| 88 | + end | ||
| 89 | +end | ||
| 90 | + | ||
| 91 | +local function log(address, msg, logTo) | ||
| 92 | + logTo = logTo or "log" | ||
| 93 | + -- print(string.format(":%08x(%.2f): %s", address, skynet.time(), msg)) | ||
| 94 | + local cur = logHandle[logTo] | ||
| 95 | + if not cur then | ||
| 96 | + print("error log to ", logTo) | ||
| 97 | + end | ||
| 98 | + local doc = {} | ||
| 99 | + if cur.handle then | ||
| 100 | + cur.handle(doc, address, msg) | ||
| 101 | + end | ||
| 102 | + if cur.connected and cur.fd then | ||
| 103 | + socketdriver.send(cur.fd, json.encode(doc) .. "\n") | ||
| 104 | + else | ||
| 105 | + -- 断线会丢失一部分日志 | ||
| 106 | + if not cur.connecting then | ||
| 107 | + open() -- 连一下 | ||
| 108 | + end | ||
| 109 | + end | ||
| 110 | +end | ||
| 111 | + | ||
| 112 | + | ||
| 113 | +skynet.register_protocol { | ||
| 114 | + name = "socket", | ||
| 115 | + id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 | ||
| 116 | + unpack = socketdriver.unpack, | ||
| 117 | + dispatch = function (_, _, t, ...) | ||
| 118 | + if socket_message[t] then | ||
| 119 | + socket_message[t](...) | ||
| 120 | + end | ||
| 121 | + end | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +-- register protocol text before skynet.start would be better. | ||
| 125 | +skynet.register_protocol { | ||
| 126 | + name = "text", | ||
| 127 | + id = skynet.PTYPE_TEXT, | ||
| 128 | + unpack = skynet.tostring, | ||
| 129 | + dispatch = function(_, address, msg) | ||
| 130 | + log(address, msg) | ||
| 131 | + end | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +skynet.register_protocol { | ||
| 135 | + name = "SYSTEM", | ||
| 136 | + id = skynet.PTYPE_SYSTEM, | ||
| 137 | + unpack = function(...) return ... end, | ||
| 138 | + dispatch = function() | ||
| 139 | + -- reopen signal | ||
| 140 | + print("SIGHUP") | ||
| 141 | + end | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +open() | ||
| 145 | +skynet.start(function()end) |