forked from cloudwu/skynet
/
skynet_log.c
77 lines (71 loc) · 1.92 KB
/
skynet_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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_starttime();
uint64_t currenttime = skynet_now();
time_t ti = starttime + currenttime/100;
skynet_error(ctx, "Open log file %s", tmp);
fprintf(f, "open time: %u %s", (uint32_t)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", (uint32_t)skynet_now());
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 = (uint32_t)skynet_now();
fprintf(f, ":%08x %d %d %u ", source, type, session, ti);
log_blob(f, buffer, sz);
fprintf(f,"\n");
fflush(f);
}
}