forked from sanikoyes/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
209 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.