mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 13:08:13 +08:00
web_log
This commit is contained in:
34
TC1/http_server/web_log.c
Normal file
34
TC1/http_server/web_log.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include"web_log.h"
|
||||
|
||||
LogRecord log_record = { 1,{ 0 } };
|
||||
char log_record_str[LOG_NUM*LOG_LEN] = { 0 };
|
||||
|
||||
void SetLogRecord(LogRecord* lr, char* log)
|
||||
{
|
||||
if (strlen(log) > LOG_LEN)
|
||||
{
|
||||
log[LOG_LEN-1] = 0;
|
||||
}
|
||||
char** log = &lr->logs[(++lr->idx)% LOG_NUM];
|
||||
if (*log)
|
||||
{
|
||||
free(*log);
|
||||
}
|
||||
*log = lr;
|
||||
}
|
||||
|
||||
char* GetLogRecord(int idx)
|
||||
{
|
||||
if (idx > log_record.idx) return "";
|
||||
|
||||
int i = idx > 0 ? idx : (log_record.idx - LOG_NUM + 1);
|
||||
i = i < 0 ? 0 : i;
|
||||
char* tmp = log_record_str;
|
||||
for (; i <= log_record.idx; i++)
|
||||
{
|
||||
sprintf(tmp, "%s\n", log_record.logs[i%LOG_NUM]);
|
||||
tmp += strlen(tmp);
|
||||
}
|
||||
return log_record_str;
|
||||
}
|
||||
|
||||
18
TC1/http_server/web_log.h
Normal file
18
TC1/http_server/web_log.h
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
#define LOG_NUM 100
|
||||
#define LOG_LEN 128
|
||||
typedef struct
|
||||
{
|
||||
int idx;
|
||||
char* logs[LOG_NUM];
|
||||
} LogRecord;
|
||||
|
||||
void SetLogRecord(LogRecord* lr, char* log);
|
||||
char* GetLogRecord(int idx);
|
||||
|
||||
#define os_log(format, ...) \
|
||||
char* log = malloc(sizeof(char)*LOG_LEN); \
|
||||
snprintf(log, LOG_LEN, format, ##__VA_ARGS__); \
|
||||
SetLogRecord(log_record, log); \
|
||||
custom_log("WIFI", format, ##__VA_ARGS__) \
|
||||
|
||||
Reference in New Issue
Block a user