-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
76 lines (65 loc) · 1.79 KB
/
util.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
/*
* util.c
*
* Created on: Sep 5, 2015
* Author: lieven
*/
#include <osapi.h>
#include "Sys.h"
const char* space = " ";
void IROM strAlign(char *dst, int dstLength, char* src, int srcLength) {
int copyLength = (srcLength < dstLength) ? srcLength : dstLength;
strncat(dst, src, copyLength);
strncat(dst, space, dstLength - copyLength);
}
#include <stdarg.h>
char lastLog[512];
char buffer[512];
uint32_t conflicts = 0;
const char* SysLogLevelStr[] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR",
"FATAL" };
#include "mutex.h"
mutex_t logMutex = 1;
void IROM SysLogInit() {
logMutex = 1;
}
void IROM SysLog(SysLogLevel level, const char* file, const char* function,
const char * format, ...) {
char *p;
buffer[0] = 0;
va_list args;
va_start(args, format);
if (GetMutex(&logMutex) == 0) {
for (p = "$$$:"; *p != 0; p++)
uart0WriteWait(*p);
for (p = file; *p != 0; p++)
uart0WriteWait(*p);
for (p = "$$$"; *p != 0; p++)
uart0WriteWait(*p);
for (p = function; *p != 0; p++)
uart0WriteWait(*p);
// uart0WriteBytes(":",1);
// uart0WriteBytes(function,strlen(function));
// os_printf_plus(" Syslog called by %s ",function);
conflicts += 1000;
va_end(args);
return;
}
uint32_t time = SysMillis();
ets_vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
char dst[40];
dst[0] = '\0';
strAlign(dst, 18, file, strlen(file));
strAlign(&dst[18], 18, function, strlen(function));
// if (level > LOG_INFO) { // put log in mqtt buffer
// ets_sprintf(lastLog, "%s:%s:%s", SysLogLevelStr[level], dst, buffer);
// }
ets_snprintf(lastLog, sizeof(lastLog), "%10u | %s | %s\n", time, dst,
buffer);
uart0WriteBytes(lastLog, strlen(lastLog));
// os_delay_us(1000);
ets_delay_us(10000);
// os_printf_plus();
ReleaseMutex(&logMutex);
}