-
Notifications
You must be signed in to change notification settings - Fork 1
/
lagan.c
157 lines (130 loc) · 4.18 KB
/
lagan.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2020-2021 The jdh99 Authors. All rights reserved.
// 实时流日志库.可以用于串口等流日志打印
// Authors: jdh99 <jdh821@163.com>
// lagan取名来自于宜家的水龙头"拉根"
#include "lagan.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
static bool gIsPause = false;
static LaganLevel gFilterLevel = LAGAN_FILTER_LEVEL_DEAFULT;
static char gLevelCh[] = {'O', 'D', 'I', 'W', 'E'};
static LaganPrintFunc gOutput = NULL;
static LaganGetTimeFunc gGetTime = NULL;
static LaganGetLocalTimeFunc gGetLocalTime = NULL;
static bool gIsLoad = false;
// LaganLoad 模块载入
// getTime是读取北京时间,getLocalTime是读取本地时间,进度是us
// 如果不需要使用哪个时间,就将其设置为NULL.如果两个都有效,则使用的是北京时间
void LaganLoad(LaganPrintFunc print, LaganGetTimeFunc getTime, LaganGetLocalTimeFunc getLocalTime) {
if (gIsLoad) {
return;
}
gOutput = print;
gGetTime = getTime;
if (gGetTime == NULL) {
gGetLocalTime = getLocalTime;
}
gIsLoad = true;
}
// LaganSetFilterLevel 设置过滤日志等级
void LaganSetFilterLevel(LaganLevel level) {
gFilterLevel = level;
}
// PaxGetFilterLevel 显示过滤日志等级
LaganLevel LaganGetFilterLevel(void) {
return gFilterLevel;
}
// LaganPrint 日志打印
void LaganPrint(char* tag, LaganLevel level, char *format, ...) {
if (gIsLoad == false || gIsPause) {
return;
}
if (gFilterLevel == LAGAN_LEVEL_OFF || level < gFilterLevel || tag == NULL ||
strlen(tag) > LAGAN_MODULE_NAME_LEN_MAX - 1) {
return;
}
char buf[LAGAN_RECORD_MAX_SIZE_DEFAULT] = {0};
// 前缀
if (gGetTime != NULL) {
LaganTime time = gGetTime();
sprintf(buf, "%02d/%02d/%02d %02d:%02d:%02d.%06d %c/%s ", time.Year, time.Month, time.Day, time.Hour, time.Minute,
time.Second, time.Us, gLevelCh[level], tag);
} else {
uint64_t us = gGetLocalTime();
int second = (int)(us / 1000000);
us = us % 1000000;
int ms = (int)(us / 1000);
us = us % 1000;
sprintf(buf, "%06d/%03d/%03d %c/%s ", second, ms, (int)us, gLevelCh[level], tag);
}
gOutput((uint8_t*)buf, (int)strlen(buf));
// 正文
va_list args;
va_start(args, format);
int len = vsnprintf(buf, LAGAN_RECORD_MAX_SIZE_DEFAULT - 1, format, args);
if (len > LAGAN_RECORD_MAX_SIZE_DEFAULT || len < 0) {
len = LAGAN_RECORD_MAX_SIZE_DEFAULT;
}
gOutput((uint8_t*)buf, (int)strlen(buf));
va_end(args);
// 后缀
gOutput((uint8_t*)"\n", 1);
}
// LaganPrintHex 打印字节流
void LaganPrintHex(char* tag, LaganLevel level, uint8_t* bytes, int size) {
if (gIsLoad == false || gIsPause) {
return;
}
if (gFilterLevel == LAGAN_LEVEL_OFF || level < gFilterLevel) {
return;
}
LaganPrint(tag, level, "");
char buf[LAGAN_RECORD_MAX_SIZE_DEFAULT] = {0};
char temp[16] = {0};
int len = 0;
for (int i = 0; i < size; i++) {
if (len + 3 > LAGAN_RECORD_MAX_SIZE_DEFAULT - 1) {
gOutput((uint8_t*)buf, len);
buf[0] = '\0';
len = 0;
}
if (i >= size - 1) {
sprintf(temp, "%02x\n", bytes[i]);
} else if (i != 0 && (i + 1) % 16 == 0) {
sprintf(temp, "%02x\n", bytes[i]);
} else {
sprintf(temp, "%02x ", bytes[i]);
}
strcat(buf, temp);
len += 3;
}
if (len > 0) {
gOutput((uint8_t*)buf, len);
}
}
// PaxPause 暂停日志打印
void LaganPause(void) {
gIsPause = true;
}
// PaxResume 恢复日志打印
void LaganResume(void) {
gIsPause = false;
}
// PaxIsPause 是否暂停
bool LaganIsPause(void) {
return gIsPause;
}
// LaganRaw 原始打印.无时间戳和模块信息
void LaganRaw(char *format, ...) {
char buf[LAGAN_RECORD_MAX_SIZE_DEFAULT] = {0};
// 正文
va_list args;
va_start(args, format);
int len = vsnprintf(buf, LAGAN_RECORD_MAX_SIZE_DEFAULT - 1, format, args);
if (len > LAGAN_RECORD_MAX_SIZE_DEFAULT || len < 0) {
len = LAGAN_RECORD_MAX_SIZE_DEFAULT;
}
gOutput((uint8_t*)buf, (int)strlen(buf));
va_end(args);
}