1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <time.h>
10 #include "ulog/ulog.h"
11 #include "ulog_api.h"
12 #include "ulog_ring_fifo.h"
13 #include "aos/kernel.h"
14 #include "k_config.h"
15 #if ULOG_POP_CLOUD_ENABLE
16 #include "uagent.h"
17 #endif
18
19 #define SUB_FUNC_MAX_NUM 2
20
21 static aos_task_t ulog_routine;
22
23 typedef void(*on_ulog_man_service)(const uint32_t, const uint32_t);
24
25 typedef struct {
26 char *func_mark;
27 char *sub_func_mark[SUB_FUNC_MAX_NUM];
28 on_ulog_man_service handler;
29 } ulog_man_handler_service_t;
30
31 static void ulog_man_handler(const char* raw_str);
32
33 const ulog_man_handler_service_t ulog_man_handler_service[] = {
34 #if ULOG_POP_UDP_ENABLE
35 { "listen", {"ip","port"}, on_update_syslog_watcher_addr },
36 { "tcpip", {"on","off" }, on_tcpip_service_on },
37 #endif
38 #if ULOG_POP_FS_ENABLE
39 { "fspause", {"on","off"}, on_fs_record_pause },
40 #if ULOG_UPLOAD_LOG_FILE
41 { "up", {"idx","none"}, on_fs_upload },
42 #endif
43 #endif
44 };
45
ulog_handler(void * para,void * log_text,const uint16_t log_len)46 static void ulog_handler(void* para, void* log_text, const uint16_t log_len)
47 {
48 if ((log_text != NULL) && log_len > 0) {
49 char *str = (char*)log_text;
50 char *text_info = NULL;
51 if ((str[0] == '<') && ((text_info = strchr(str, '>')) != NULL)) {
52 /* pri = facility*8+level */
53 const uint32_t pri = strtoul(&str[1], NULL, 10);
54 const uint8_t severity = (pri & 0x7);
55 #if LOG_DIR_ASYNC
56 if (check_pass_pop_out(ulog_session_std, severity)) {
57 if (log_get_mutex()) {
58 puts(&((char*)log_text)[LOG_PREFIX_LEN]);
59 log_release_mutex();
60 }
61 }
62 #endif
63
64 #if ULOG_POP_UDP_ENABLE
65 if (check_pass_pop_out(ulog_session_udp, severity)) {
66 (void)pop_out_on_udp(&((char *)log_text)[LOG_PREFIX_LEN], log_len);
67 }
68 #endif
69
70 #if ULOG_POP_FS_ENABLE
71 if (check_pass_pop_out(ulog_session_file, severity)) {
72 (void)pop_out_on_fs(&((char *)log_text)[LOG_PREFIX_LEN], log_len);
73 }
74 #endif
75
76 #if ULOG_POP_CLOUD_ENABLE
77 if ((FACILITY_NORMAL_LOG_NO_POP_CLOUD != (pri & 0xF8))
78 && check_pass_pop_out(ulog_session_cloud, severity)) {
79 (void)uagent_send(UAGENT_MOD_ULOG, ULOG_SHOW, strlen(text_info + 1), text_info + 1, 0);
80 }
81 #endif
82
83 } else if (0 == strncmp(str, ULOG_CMD_PREFIX, strlen(ULOG_CMD_PREFIX))) {
84 /* syslog management */
85 ulog_man_handler(&str[strlen(ULOG_CMD_PREFIX)]);
86 } else { /* unkown format */
87 puts(log_text);
88 }
89 }
90 }
91
log_routine(void * para)92 static void log_routine(void* para)
93 {
94 while (1) {
95 /* PRI HEADER MSG */
96 /* <130>Oct 9 22:33:20.111 soc kernel.c[111]: The audit daemon is exiting. */
97 uring_fifo_pop_cb(ulog_handler, NULL);
98 }
99 }
100
ulog_async_init()101 void ulog_async_init()
102 {
103 /* start log on fs feature if both vfs && spiffs support */
104 #if ULOG_POP_FS_ENABLE
105 ulog_fs_init();
106 #endif
107 uring_fifo_init();
108
109 if (0 == aos_task_new_ext(&ulog_routine,
110 "ulog",
111 log_routine,
112 NULL,
113 LOG_ROUTINE_TASK_STACK_DEPTH,
114 RHINO_CONFIG_USER_PRI_MAX)) {
115
116 }
117 }
118
ulog_man_handler(const char * raw_str)119 void ulog_man_handler(const char* raw_str)
120 {
121 const int8_t man_handler_service_size = sizeof(ulog_man_handler_service) / sizeof(ulog_man_handler_service_t);
122 int8_t i = 0;
123 uint8_t j = 0;
124 int func_len = 0;
125 int subfunc_len = 0;
126 unsigned short data_len = 0;
127 uint32_t param[SUB_FUNC_MAX_NUM] = { -1, -1 };
128 char *p = NULL;
129 char *val_str = NULL;
130
131 if (raw_str == NULL) {
132 return ;
133 }
134
135 data_len = strlen(raw_str);
136
137 for (; i < man_handler_service_size; i++) {
138 func_len = strlen(ulog_man_handler_service[i].func_mark);
139 if (0 == strncmp(ulog_man_handler_service[i].func_mark, raw_str, func_len)) {
140 /*
141 printf("%s %d find ulog func mark %s \r\n", __FILE__, __LINE__, ulog_man_handler_service[i].func_mark);
142 */
143 p = &raw_str[func_len];
144 if (p[0] == ' ') {
145 for (; j < SUB_FUNC_MAX_NUM; j++) {
146 subfunc_len = strlen(ulog_man_handler_service[i].sub_func_mark[j]);
147 if (strncmp(&p[1], ulog_man_handler_service[i].sub_func_mark[j], subfunc_len) == 0) {
148 /*
149 printf("%s %d find ulog sub func mark %s \r\n", __FILE__, __LINE__, ulog_man_handler_service[i].sub_func_mark[j]);
150 */
151 if (data_len > strlen(ulog_man_handler_service[i].func_mark) + 1 + subfunc_len + 1) {
152 /*jump off the space and the '='*/
153 val_str = &p[subfunc_len + 1 + 1];
154 param[j] = strtoul(val_str, NULL, 10);
155 /*
156 printf("%s %d find ulog sub func mark %s param value %s param %d \r\n", __FILE__, __LINE__,
157 ulog_man_handler_service[i].sub_func_mark[j], val_str, param[j]);
158 */
159 } else {
160 /*there is no param for this cmd ,set true */
161 param[j] = 1;
162 /*
163 printf("%s %d find ulog sub func mark %s param %d \r\n", __FILE__, __LINE__,
164 ulog_man_handler_service[i].sub_func_mark[j], param[j]);
165 */
166 }
167 }
168 }
169 if ((param[0] != -1 || param[1] != -1) && (ulog_man_handler_service[i].handler != NULL)) {
170 /*
171 printf("%s %d find ulog man handler %d param0 %d param1 %d\r\n", __FILE__, __LINE__, i, param[0], param[1]);
172 */
173 ulog_man_handler_service[i].handler(param[0], param[1]);
174 }
175 }
176 break;
177 }
178 }
179
180 }
181
182