mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
rewrite makefile, and remove luacode cache.
This commit is contained in:
56
service-src/service_logger.c
Normal file
56
service-src/service_logger.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "skynet.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct logger {
|
||||
FILE * handle;
|
||||
int close;
|
||||
};
|
||||
|
||||
struct logger *
|
||||
logger_create(void) {
|
||||
struct logger * inst = malloc(sizeof(*inst));
|
||||
inst->handle = NULL;
|
||||
inst->close = 0;
|
||||
return inst;
|
||||
}
|
||||
|
||||
void
|
||||
logger_release(struct logger * inst) {
|
||||
if (inst->close) {
|
||||
fclose(inst->handle);
|
||||
}
|
||||
free(inst);
|
||||
}
|
||||
|
||||
static int
|
||||
_logger(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
||||
struct logger * inst = ud;
|
||||
fprintf(inst->handle, "[:%x] ",source);
|
||||
fwrite(msg, sz , 1, inst->handle);
|
||||
fprintf(inst->handle, "\n");
|
||||
fflush(inst->handle);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) {
|
||||
if (parm) {
|
||||
inst->handle = fopen(parm,"w");
|
||||
if (inst->handle == NULL) {
|
||||
return 1;
|
||||
}
|
||||
inst->close = 1;
|
||||
} else {
|
||||
inst->handle = stdout;
|
||||
}
|
||||
if (inst->handle) {
|
||||
skynet_callback(ctx, inst, _logger);
|
||||
skynet_command(ctx, "REG", ".logger");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user