mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
add message log
This commit is contained in:
77
skynet-src/skynet_log.c
Normal file
77
skynet-src/skynet_log.c
Normal file
@@ -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
skynet-src/skynet_log.h
Normal file
14
skynet-src/skynet_log.h
Normal file
@@ -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
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "skynet_env.h"
|
||||
#include "skynet_monitor.h"
|
||||
#include "skynet_imp.h"
|
||||
#include "skynet_log.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
@@ -37,13 +38,14 @@
|
||||
struct skynet_context {
|
||||
void * instance;
|
||||
struct skynet_module * mod;
|
||||
uint32_t handle;
|
||||
int ref;
|
||||
char result[32];
|
||||
void * cb_ud;
|
||||
skynet_cb cb;
|
||||
int session_id;
|
||||
struct message_queue *queue;
|
||||
FILE * logfile;
|
||||
char result[32];
|
||||
uint32_t handle;
|
||||
int session_id;
|
||||
int ref;
|
||||
bool init;
|
||||
bool endless;
|
||||
|
||||
@@ -129,6 +131,7 @@ skynet_context_new(const char * name, const char *param) {
|
||||
ctx->cb = NULL;
|
||||
ctx->cb_ud = NULL;
|
||||
ctx->session_id = 0;
|
||||
ctx->logfile = NULL;
|
||||
|
||||
ctx->init = false;
|
||||
ctx->endless = false;
|
||||
@@ -177,6 +180,9 @@ skynet_context_grab(struct skynet_context *ctx) {
|
||||
|
||||
static void
|
||||
delete_context(struct skynet_context *ctx) {
|
||||
if (ctx->logfile) {
|
||||
fclose(ctx->logfile);
|
||||
}
|
||||
skynet_module_instance_release(ctx->mod, ctx->instance);
|
||||
skynet_mq_mark_release(ctx->queue);
|
||||
skynet_free(ctx);
|
||||
@@ -224,12 +230,15 @@ skynet_isremote(struct skynet_context * ctx, uint32_t handle, int * harbor) {
|
||||
}
|
||||
|
||||
static void
|
||||
_dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
|
||||
dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
|
||||
assert(ctx->init);
|
||||
CHECKCALLING_BEGIN(ctx)
|
||||
pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle));
|
||||
int type = msg->sz >> HANDLE_REMOTE_SHIFT;
|
||||
size_t sz = msg->sz & HANDLE_MASK;
|
||||
if (ctx->logfile) {
|
||||
skynet_log_output(ctx->logfile, msg->source, type, msg->session, msg->data, sz);
|
||||
}
|
||||
if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) {
|
||||
skynet_free(msg->data);
|
||||
}
|
||||
@@ -242,7 +251,7 @@ skynet_context_dispatchall(struct skynet_context * ctx) {
|
||||
struct skynet_message msg;
|
||||
struct message_queue *q = ctx->queue;
|
||||
while (!skynet_mq_pop(q,&msg)) {
|
||||
_dispatch_message(ctx, &msg);
|
||||
dispatch_message(ctx, &msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,7 +289,7 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue
|
||||
if (ctx->cb == NULL) {
|
||||
skynet_free(msg.data);
|
||||
} else {
|
||||
_dispatch_message(ctx, &msg);
|
||||
dispatch_message(ctx, &msg);
|
||||
}
|
||||
|
||||
skynet_monitor_trigger(sm, 0,0);
|
||||
@@ -412,17 +421,23 @@ cmd_exit(struct skynet_context * context, const char * param) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_kill(struct skynet_context * context, const char * param) {
|
||||
static uint32_t
|
||||
tohandle(struct skynet_context * context, const char * param) {
|
||||
uint32_t handle = 0;
|
||||
if (param[0] == ':') {
|
||||
handle = strtoul(param+1, NULL, 16);
|
||||
} else if (param[0] == '.') {
|
||||
handle = skynet_handle_findname(param+1);
|
||||
} else {
|
||||
skynet_error(context, "Can't kill %s",param);
|
||||
// todo : kill global service
|
||||
skynet_error(context, "Can't convert %s to handle",param);
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_kill(struct skynet_context * context, const char * param) {
|
||||
uint32_t handle = tohandle(context, param);
|
||||
if (handle) {
|
||||
handle_exit(context, handle);
|
||||
}
|
||||
@@ -503,14 +518,7 @@ cmd_monitor(struct skynet_context * context, const char * param) {
|
||||
}
|
||||
return NULL;
|
||||
} else {
|
||||
if (param[0] == ':') {
|
||||
handle = strtoul(param+1, NULL, 16);
|
||||
} else if (param[0] == '.') {
|
||||
handle = skynet_handle_findname(param+1);
|
||||
} else {
|
||||
skynet_error(context, "Can't monitor %s",param);
|
||||
// todo : monitor global service
|
||||
}
|
||||
handle = tohandle(context, param);
|
||||
}
|
||||
G_NODE.monitor_exit = handle;
|
||||
return NULL;
|
||||
@@ -523,6 +531,48 @@ cmd_mqlen(struct skynet_context * context, const char * param) {
|
||||
return context->result;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_logon(struct skynet_context * context, const char * param) {
|
||||
uint32_t handle = tohandle(context, param);
|
||||
if (handle == 0)
|
||||
return NULL;
|
||||
struct skynet_context * ctx = skynet_handle_grab(handle);
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
FILE *f = NULL;
|
||||
FILE * lastf = ctx->logfile;
|
||||
if (lastf == NULL) {
|
||||
f = skynet_log_open(context, handle);
|
||||
if (f) {
|
||||
if (!__sync_bool_compare_and_swap(&ctx->logfile, NULL, f)) {
|
||||
// logfile opens in other thread, close this one.
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
skynet_context_release(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_logoff(struct skynet_context * context, const char * param) {
|
||||
uint32_t handle = tohandle(context, param);
|
||||
if (handle == 0)
|
||||
return NULL;
|
||||
struct skynet_context * ctx = skynet_handle_grab(handle);
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
FILE * f = ctx->logfile;
|
||||
if (f) {
|
||||
// logfile may close in other thread
|
||||
if (__sync_bool_compare_and_swap(&ctx->logfile, f, NULL)) {
|
||||
skynet_log_close(context, f, handle);
|
||||
}
|
||||
}
|
||||
skynet_context_release(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct command_func cmd_funcs[] = {
|
||||
{ "TIMEOUT", cmd_timeout },
|
||||
{ "REG", cmd_reg },
|
||||
@@ -539,6 +589,8 @@ static struct command_func cmd_funcs[] = {
|
||||
{ "ABORT", cmd_abort },
|
||||
{ "MONITOR", cmd_monitor },
|
||||
{ "MQLEN", cmd_mqlen },
|
||||
{ "LOGON", cmd_logon },
|
||||
{ "LOGOFF", cmd_logoff },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user