Skip to content

Commit a7eb229

Browse files
authored
Deep log optimize (#4)
* deep log optimize * log: log system optimize
1 parent bdb5371 commit a7eb229

File tree

6 files changed

+81
-86
lines changed

6 files changed

+81
-86
lines changed

include/deep_log.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@ extern "C" {
3434
#endif
3535

3636

37-
void log_printf(const char *pFileName, unsigned int uiLine, const char *pFuncName, char *LogFmtBuf, ...);
38-
39-
void
40-
log_data(const char *pFileName, unsigned int uiLine, const char *pFuncName, const char *pcStr, unsigned char *pucBuf,
41-
unsigned int usLen);
42-
43-
#define error(...) log_printf(__FILE__, __LINE__,__FUNCTION__,__VA_ARGS__)
44-
#define debug(...) log_printf(__FILE__, __LINE__,__FUNCTION__,__VA_ARGS__)
45-
#define dump(pcStr, pucBuf, usLen) log_data(__FILE__, __LINE__,__FUNCTION__,pcStr,pucBuf,usLen)
37+
void log_printf (const char* pFileName, unsigned int uiLine, const char* pFuncName, const char *pFlag, char *LogFmtBuf, ...);
38+
void log_data(const char *pFileName, unsigned int uiLine, const char* pFuncName, const char *pcStr,unsigned char *pucBuf,unsigned int usLen);
39+
#define deep_error(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<error>",__VA_ARGS__)
40+
#define deep_warn(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<warn>",__VA_ARGS__)
41+
#define deep_debug(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<debug>",__VA_ARGS__)
42+
#define deep_info(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<info>",__VA_ARGS__)
43+
#define deep_dump(pcStr,pucBuf,usLen) log_data(__FILE__, __LINE__,__FUNCTION__,pcStr,pucBuf,usLen)
44+
45+
// #define DBG
46+
#ifdef DBG
47+
#define PRINT_ARG(FSTRING, ARG) do {printf(FSTRING, ARG); fflush(stdout);} while (0)
48+
#else
49+
#define PRINT_ARG(FSTRING, ARG) ((void *)0)
50+
#endif
4651

4752

4853
#ifdef __cplusplus

src/deep_interp.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "deep_loader.h"
1111
#include "deep_mem.h"
1212
#include "deep_opcode.h"
13+
#include "deep_log.h"
1314

1415
#define popS32() (int32_t)*(--sp)
1516
#define popF32() (float)*(--sp)
@@ -29,13 +30,13 @@
2930
DEEPStack *stack_cons(void) {
3031
DEEPStack *stack = (DEEPStack *) deep_malloc(sizeof(DEEPStack));
3132
if (stack == NULL) {
32-
printf("Operand stack creation failed!\r\n");
33+
deep_error("Operand stack creation failed!");
3334
return NULL;
3435
}
3536
stack->capacity = STACK_CAPACITY;
3637
stack->sp = (uint32_t *) deep_malloc(sizeof(uint32_t) * STACK_CAPACITY);
3738
if (stack->sp == NULL) {
38-
printf("Malloc area for stack error!\r\n");
39+
deep_error("Malloc area for stack error!");
3940
}
4041
stack->sp_end = stack->sp + stack->capacity;
4142
return stack;
@@ -303,7 +304,7 @@ void call_function(DEEPExecEnv *current_env, DEEPModule *module, int func_index)
303304
//为func函数创建帧
304305
DEEPInterpFrame *frame = (DEEPInterpFrame *) deep_malloc(sizeof(DEEPInterpFrame));
305306
if (frame == NULL) {
306-
printf("Malloc area for normal_frame error!\r\n");
307+
deep_error("Malloc area for normal_frame error!");
307308
}
308309
//初始化
309310
frame->sp = current_env->sp;
@@ -338,7 +339,7 @@ int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module) {
338339
}
339340
}
340341
if (main_index < 0) {
341-
printf("the main function index failed!\r\n");
342+
deep_error("the main function index failed!");
342343
return -1;
343344
}
344345

@@ -348,7 +349,7 @@ int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module) {
348349
//为main函数创建帧
349350
DEEPInterpFrame *main_frame = (DEEPInterpFrame *) deep_malloc(sizeof(struct DEEPInterpFrame));
350351
if (main_frame == NULL) {
351-
printf("Malloc area for main_frame error!\r\n");
352+
deep_error("Malloc area for main_frame error!");
352353
}
353354
//初始化
354355
main_frame->sp = current_env->sp;

src/deep_loader.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,18 @@ static void decode_each_sections(DEEPModule* module, section_listnode* section_l
255255

256256
DEEPModule* deep_load(uint8_t** p, int size) {
257257
if (!check_magic_number_and_version(p)) {
258-
error("magic number error");
258+
deep_error("magic number error");
259259
return NULL;
260260
}
261261
section_listnode* section_list = create_section_list((const uint8_t**)p, size);
262262
if(section_list == NULL) {
263-
error("create section list fail");
263+
deep_error("create section list fail");
264264
return NULL;
265265
}
266266
size -= 8;
267267
DEEPModule* module = (DEEPModule*)deep_malloc(sizeof(DEEPModule));
268268
if(module == NULL) {
269-
error("module malloc fail");
269+
deep_error("module malloc fail");
270270
return NULL;
271271
}
272272
decode_each_sections(module, section_list);
@@ -364,22 +364,6 @@ void write_mem64(uint8_t* mem, uint64_t val, uint32_t offset) {
364364
write_memory(mem, buf, offset, 8);
365365
}
366366

367-
// DEEPModule* module = deep_load(&p, size);
368-
//DOING:开发dump指令,方便后续调试程序
369-
370-
// void print_type(uint8_t t) {
371-
// switch (t)
372-
// {
373-
// case :
374-
375-
// break;
376-
377-
// default:
378-
// error("not correct parameter type");
379-
// break;
380-
// }
381-
// }
382-
383367
void type_section_dump(DEEPModule* module) {
384368
uint32_t i, j = 0;
385369
printf("%s\n", "========================================================");

src/deep_log.c

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,73 +29,87 @@ Description: dump/debug funtions for logs.
2929
#include <stdarg.h>
3030
#include "deep_log.h"
3131

32-
void log_printf(const char *pFileName, unsigned int uiLine, const char *pFnucName, char *LogFmtBuf, ...) {
33-
va_list args;
34-
if (pFileName == NULL || uiLine == 0 || LogFmtBuf == NULL) {
35-
return;
36-
}
32+
void log_printf (const char* pFileName, unsigned int uiLine, const char* pFnucName, const char *pFlag, char *LogFmtBuf, ...)
33+
{
34+
va_list args;
35+
if(pFileName == NULL || uiLine == 0 || LogFmtBuf == NULL)
36+
{
37+
return ;
38+
}
3739
char logbuf[256];
38-
memset(logbuf, '\0', 256);
39-
sprintf(logbuf, "%s:%d, %s(), ", pFileName, uiLine, pFnucName);
40-
printf("%s", logbuf);
41-
memset(logbuf, '\0', 256);
42-
va_start (args, LogFmtBuf);
43-
vsnprintf(logbuf, 256, LogFmtBuf, args);
44-
va_end (args);
45-
printf("%s\r\n", logbuf);
40+
memset (logbuf,'\0',256);
41+
sprintf (logbuf,"%s:%d, %s(), %s, ", pFileName, uiLine, pFnucName, pFlag);
42+
printf ("%s",logbuf);
43+
memset (logbuf,'\0',256);
44+
va_start (args, LogFmtBuf);
45+
vsnprintf (logbuf, 256, LogFmtBuf, args);
46+
va_end (args);
47+
printf ("%s\r\n",logbuf);
4648
}
4749

48-
void
49-
log_data(const char *pFileName, unsigned int uiLine, const char *pFnucName, const char *pcStr, unsigned char *pucBuf,
50-
unsigned int usLen) {
50+
void log_data(const char *pFileName, unsigned int uiLine, const char* pFnucName, const char *pcStr,unsigned char *pucBuf,unsigned int usLen)
51+
{
5152
unsigned int i;
5253
unsigned char acTmp[17];
5354
unsigned char *p;
5455
unsigned char *pucAddr = pucBuf;
5556

56-
if (pcStr) {
57-
log_printf(pFileName, uiLine, pFnucName, "[%s]: length = %d (0x%X)\r\n", pcStr, usLen, usLen);
57+
if(pcStr)
58+
{
59+
log_printf (pFileName, uiLine, pFnucName, "<dump>", "[%s]: length = %d (0x%X)\r\n",pcStr, usLen, usLen);
5860
}
59-
if (usLen == 0) {
61+
if(usLen == 0)
62+
{
6063
return;
6164
}
6265
p = acTmp;
63-
printf(" %p ", pucAddr);
64-
for (i = 0; i < usLen; i++) {
66+
printf (" %p ", pucAddr);
67+
for(i=0;i<usLen;i++)
68+
{
6569

66-
printf("%02X ", pucBuf[i]);
67-
if ((pucBuf[i] >= 0x20) && (pucBuf[i] < 0x7F)) {
70+
printf ("%02X ",pucBuf[i]);
71+
if((pucBuf[i] >= 0x20) && (pucBuf[i] < 0x7F))
72+
{
6873
*p++ = pucBuf[i];
69-
} else {
74+
}
75+
else
76+
{
7077
*p++ = '.';
7178
}
72-
if ((i + 1) % 16 == 0) {
79+
if((i+1)%16==0)
80+
{
7381
*p++ = 0;
74-
printf(" | %s", acTmp);
82+
printf (" | %s", acTmp);
7583
p = acTmp;
7684

77-
printf("\r\n");
85+
printf ("\r\n");
7886

79-
if ((i + 1) < usLen) {
87+
if((i+1) < usLen)
88+
{
8089
pucAddr += 16;
81-
printf(" %p ", pucAddr);
90+
printf (" %p ", pucAddr);
8291
}
83-
} else if ((i + 1) % 8 == 0) {
84-
printf("- ");
92+
}
93+
else if((i+1)%8==0)
94+
{
95+
printf ("- ");
8596
}
8697
}
87-
if (usLen % 16 != 0) {
88-
for (i = usLen % 16; i < 16; i++) {
89-
printf(" ");
90-
if (((i + 1) % 8 == 0) && ((i + 1) % 16 != 0)) {
91-
printf("- ");
98+
if(usLen%16!=0)
99+
{
100+
for(i=usLen%16;i<16;i++)
101+
{
102+
printf (" ");
103+
if(((i+1)%8==0) && ((i+1)%16!=0))
104+
{
105+
printf ("- ");
92106
}
93107
}
94108
*p++ = 0;
95-
printf(" | %s", acTmp);
96-
printf("\r\n");
109+
printf (" | %s", acTmp);
110+
printf ("\r\n");
97111
}
98-
printf("\r\n");
112+
printf ("\r\n");
99113
}
100114

101115

src/deep_main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,30 @@ int32_t main(int argv, char **args) {
2222

2323
char *path;
2424
if(argv==1){
25-
error("no file input!");
25+
deep_error("no file input!");
2626
}else{
2727
path = args[1];
2828
}
2929
uint8_t *q = (uint8_t *) deep_malloc(WASM_FILE_SIZE);
3030
uint8_t *p = q;
3131
if (p == NULL) {
32-
error("malloc fail.");
32+
deep_error("malloc fail.");
3333
return -1;
3434
}
3535

3636
FILE *fp = fopen(path, "rb"); /* read wasm file with binary mode */
3737
if (fp == NULL) {
38-
error("file open fail.");
38+
deep_error("file open fail.");
3939
return -1;
4040
}
4141
int32_t size = fread(p, 1, WASM_FILE_SIZE, fp);
4242
if (size == 0) {
43-
error ("fread faill.");
43+
deep_error ("fread faill.");
4444
return -1;
4545
}
4646
DEEPModule *module = deep_load(&p, size);
4747
if (module == NULL) {
48-
error("load fail.");
48+
deep_error("load fail.");
4949
return -1;
5050
}
5151
//创建操作数栈

src/deep_mem.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
#include "deep_mem.h"
88
#include "deep_log.h"
99

10-
// #define DBG
11-
12-
#ifdef DBG
13-
#define PRINT_ARG(FSTRING, ARG) do {printf(FSTRING, ARG); fflush(stdout);} while (0)
14-
#else
15-
#define PRINT_ARG(FSTRING, ARG) ((void *)0)
16-
#endif
17-
1810
mem_pool_t *pool;
1911

2012
/*
@@ -163,7 +155,6 @@ bool deep_mem_init (void *mem, uint32_t size)
163155
// initialise remainder block's head
164156
block_set_A_flag (pool->remainder_block_head, false);
165157
block_set_P_flag (pool->remainder_block_head, true);
166-
167158
return true;
168159
}
169160

0 commit comments

Comments
 (0)