1 #include "linkkit/infra/infra_config.h"
2
3 #ifdef INFRA_LOG_NETWORK_PAYLOAD
4 #include <string.h>
5 #include <stdarg.h>
6 #include "linkkit/infra/infra_log.h"
7 #if defined(INFRA_CJSON)
8 #include "linkkit/infra/infra_cjson.h"
9 #endif
10
11 #define JSON_NEWLINE "\r\n"
12 #define JSON_INDENT " "
13
14 #define JSON_PRINT_NEWSTR HAL_Printf("%s", newstr);
15 #define JSON_PRINT_NEWLINE \
16 do { \
17 HAL_Printf("%s", JSON_NEWLINE); \
18 if (mark == '>' || mark == '<' || mark == ':') { \
19 HAL_Printf("%c ", mark); \
20 } \
21 } while (0)
22
23 /* 31, red. 32, green. 33, yellow. 34, blue. 35, magenta. 36, cyan. 37, white.
24 */
25 static char *_color[] = { "[0m", "[1;31m", "[1;31m", "[1;35m",
26 "[1;33m", "[1;36m", "[1;37m" };
27
iotx_facility_json_print(const char * str,int level,...)28 int iotx_facility_json_print(const char *str, int level, ...)
29 {
30 int length = 0;
31 char newstr[2];
32 int quoted = 0;
33 int escaped = 0;
34 int indent = 0;
35 int i = 0, j = 0;
36 #if defined(INFRA_CJSON)
37 int res = -1;
38 lite_cjson_t lite;
39 #endif
40 va_list ap;
41 int mark = ' ';
42
43 newstr[0] = 0x00;
44 newstr[1] = 0x00;
45
46 if (str == NULL || strlen(str) == 0) {
47 return -1;
48 }
49
50 #if defined(INFRA_CJSON)
51 res = lite_cjson_parse(str, strlen(str), &lite);
52 if (res != SUCCESS_RETURN || !lite_cjson_is_object(&lite)) {
53 return -2;
54 }
55 #endif
56
57 length = strlen(str);
58 HAL_Printf("%s%s", "\033", _color[level]);
59 va_start(ap, level);
60 mark = va_arg(ap, int);
61 JSON_PRINT_NEWLINE;
62 va_end(ap);
63
64 for (i = 0; i < length; i++) {
65 char ch = str[i];
66 switch (ch) {
67 case '{':
68 case '[':
69 newstr[0] = ch;
70 JSON_PRINT_NEWSTR;
71
72 if (!quoted) {
73 JSON_PRINT_NEWLINE;
74
75 if (!(str[i + 1] == '}' || str[i + 1] == ']')) {
76 ++indent;
77
78 for (j = 0; j < indent; j++) {
79 HAL_Printf("%s", JSON_INDENT);
80 }
81 }
82 }
83
84 break;
85
86 case '}':
87 case ']':
88 if (!quoted) {
89 if ((i > 0) && (!(str[i - 1] == '{' || str[i - 1] == '['))) {
90 JSON_PRINT_NEWLINE;
91 --indent;
92
93 for (j = 0; j < indent; j++) {
94 HAL_Printf("%s", JSON_INDENT);
95 }
96 } else if ((i > 0) && ((str[i - 1] == '[' && ch == ']') ||
97 (str[i - 1] == '{' && ch == '}'))) {
98 for (j = 0; j < indent; j++) {
99 HAL_Printf("%s", JSON_INDENT);
100 }
101 }
102 }
103
104 newstr[0] = ch;
105 JSON_PRINT_NEWSTR;
106
107 break;
108
109 case '"':
110 newstr[0] = ch;
111 JSON_PRINT_NEWSTR;
112 escaped = 1;
113
114 if (i > 0 && str[i - 1] == '\\') {
115 escaped = !escaped;
116 }
117
118 if (!escaped) {
119 quoted = !quoted;
120 }
121
122 break;
123
124 case ',':
125 newstr[0] = ch;
126 JSON_PRINT_NEWSTR;
127 if (!quoted) {
128 JSON_PRINT_NEWLINE;
129
130 for (j = 0; j < indent; j++) {
131 HAL_Printf("%s", JSON_INDENT);
132 }
133 }
134
135 break;
136
137 case ':':
138 newstr[0] = ch;
139 JSON_PRINT_NEWSTR;
140 if (!quoted) {
141 HAL_Printf("%s", " ");
142 }
143
144 break;
145
146 default:
147 newstr[0] = ch;
148 JSON_PRINT_NEWSTR;
149
150 break;
151 }
152 }
153
154 HAL_Printf("%s", JSON_NEWLINE JSON_NEWLINE);
155 HAL_Printf("%s", "\033[0m");
156 return 0;
157 }
158
159 #endif /* #ifdef INFRA_LOG_NETWORK_PAYLOAD */
160