blob: e19383621e684d221c9dbe4677a229ed9182ef9d [file] [log] [blame]
w.deng59bac422025-07-25 15:32:57 +08001#define _POSIX_C_SOURCE 199309
2#include <stdio.h>
3#include <string.h>
4#include <signal.h>
5#include <sys/types.h>
6#include <sys/syscall.h>
7#include <unistd.h>
8#include <stdlib.h>
9#include <stdarg.h>
10#include <time.h>
11#include <syslog.h>
12#include <stdarg.h>
13#include <pthread.h>
14#include "softap_log.h"
15#include "softap_api.h"
16
17#define ON 1
18#define OFF -1
19
20int slog_printlevel = SLOG_ERR;
21int slog_sysloglevel = SLOG_OFF;
22int soctime_sw = SLOG_SOCTIME_OFF;
w.deng5f640e02025-07-28 09:07:02 +080023int g_open_lynq_log = 0;
w.dengeb7983e2025-08-12 10:55:55 +080024int g_lynq_log_size = 102400; // 100KB
w.deng59bac422025-07-25 15:32:57 +080025
26int log_switch = LOG_ON;
27long long time_us;
28
29void log_sig_hdl(int sig, siginfo_t *siginfo, void *ptr)
30{
31 int level = 0;
32 int printlog = 0, syslog = 0, soctime = 0;
33
34 if (sig == SIGUSR2) {
35 slog(NET_PRINT, SLOG_ERR, "prelevels(printlog:%d,syslog:%d,soctime:%d), setlevel:%d \n",
36 slog_printlevel, slog_sysloglevel, soctime_sw, siginfo->si_value.sival_int);
37
38 level = siginfo->si_value.sival_int;
39
40 soctime = (level % 1000) / 100; //È¡°Ùλ£¬Ç§Î»ÒÔÉϲ»Ö§³Ö
41 syslog = (level - soctime * 100) / 10; //ȡʮλ
42 printlog = level - soctime * 100 - syslog * 10; //È¡¸öλ
43
44 slog(NET_PRINT, SLOG_ERR, "printlog:%d, syslog:%d, soctime:%d \n", printlog, syslog, soctime);
45
46 if (printlog > 0 && printlog <= SLOG_OFF)
47 slog_printlevel = printlog;
48 else
49 slog(NET_PRINT, SLOG_ERR, "slog_printlevel only support %d~%d \n", SLOG_DEBUG, SLOG_OFF);
50
51 if (syslog > 0 && syslog <= SLOG_OFF)
52 slog_sysloglevel = syslog;
53 else
54 slog(NET_PRINT, SLOG_ERR, "slog_sysloglevel only support %d~%d \n", SLOG_DEBUG, SLOG_OFF);
55
56 if (soctime == 0 || soctime == 1)
57 soctime_sw = soctime;
58 else
59 slog(NET_PRINT, SLOG_ERR, "soctime_sw only support 0~1 \n");
60 }
61}
62
63//×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
64void register_sig_logswitch(void)
65{
66 struct sigaction st;
67
68 memset(&st, 0, sizeof(st));
69 st.sa_flags = SA_SIGINFO;
70 st.sa_sigaction = log_sig_hdl;
71 sigaction(SIGUSR2, &st, NULL);
72}
73
74//¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð
75void loglevel_init(void)
76{
77 char nv_print_level[32] = {0};
78 char nv_syslog_level[32] = {0};
79 char nv_soctime_switch[32] = {0};
w.deng5f640e02025-07-28 09:07:02 +080080 char open_lynq_log[8] = {0};
w.dengeb7983e2025-08-12 10:55:55 +080081 char lynq_log_size[8] = {0};
w.deng59bac422025-07-25 15:32:57 +080082
83 cfg_get_item("print_level", nv_print_level, sizeof(nv_print_level));
84 cfg_get_item("syslog_level", nv_syslog_level, sizeof(nv_syslog_level));
85 cfg_get_item("soctime_switch", nv_soctime_switch, sizeof(nv_soctime_switch));
w.deng5f640e02025-07-28 09:07:02 +080086 cfg_get_item("open_lynq_log", open_lynq_log, sizeof(open_lynq_log));
w.dengeb7983e2025-08-12 10:55:55 +080087 cfg_get_item("lynq_log_size", lynq_log_size, sizeof(lynq_log_size));
w.deng5f640e02025-07-28 09:07:02 +080088
89 g_open_lynq_log = atoi(open_lynq_log);
w.dengeb7983e2025-08-12 10:55:55 +080090 g_lynq_log_size = atoi(lynq_log_size);
w.deng59bac422025-07-25 15:32:57 +080091
92 //loglevel·ÇÓÐЧֵʱ£¬½«Ä¬ÈÏ´òÓ¡¼¶±ðÉèÖÃΪoff£¬¼´¹Ø±Õ´òÓ¡
93 slog_printlevel = atoi(nv_print_level);
94 if((slog_printlevel < SLOG_DEBUG) || (slog_printlevel > SLOG_OFF)) {
95 slog_printlevel = SLOG_OFF;
96 }
97
98 slog_sysloglevel = atoi(nv_syslog_level);
99 if((slog_sysloglevel < SLOG_DEBUG) || (slog_sysloglevel > SLOG_OFF)) {
100 slog_sysloglevel = SLOG_OFF;
101 }
102
103 soctime_sw = atoi(nv_soctime_switch);
104 if((soctime_sw > SLOG_SOCTIME_ON) || (soctime_sw < SLOG_SOCTIME_OFF)) {
105 soctime_sw = SLOG_SOCTIME_OFF;
106 }
107
108 register_sig_logswitch();
109
110 slog(NET_PRINT, SLOG_DEBUG, "loglevel_init, print:%d, syslog:%d, soctime:%d \n",
111 slog_printlevel, slog_sysloglevel, soctime_sw);
112}
113
114int sys_log(char *ident, int prio, const char * fmt, va_list arg)
115{
116 vsyslog(prio, fmt, arg);
117 return 0;
118}
119
120inline void output_syslog_time(char *mod, int prio)
121{
122 if (SLOG_SOCTIME_ON == soctime_sw)
123 syslog(prio, "[%lld.%llds]: ", time_us / 1000000, time_us % 1000000);
124}
125
w.deng5f640e02025-07-28 09:07:02 +0800126#define LYNQ_LOG_FILE_PATH "/cache/lynqlog"
127#define LYNQ_LOG_FILE_BACKUP "/cache/lynqlog.0"
w.deng5f640e02025-07-28 09:07:02 +0800128
w.deng59bac422025-07-25 15:32:57 +0800129#define put_to_console(mod, fmt, arg) do {\
130 char buf[1024] = {0}; \
131 if(SLOG_SOCTIME_ON == soctime_sw) \
132 snprintf(buf, 1023, "%s[%d][%lld.%llds]: ", mod, syscall(SYS_gettid), time_us/1000000, time_us%1000000); \
133 else \
134 snprintf(buf, 1023, "%s[%d]: ", mod, syscall(SYS_gettid)); \
135 int n = strlen(buf); \
136 va_start(arg, fmt); \
137 vsnprintf(buf+n, 1023-n, fmt, arg); \
138 va_end(arg); \
139 printf("%s",buf); \
w.deng5f640e02025-07-28 09:07:02 +0800140 if (1 == g_open_lynq_log) \
141 {\
142 struct stat st; \
w.dengeb7983e2025-08-12 10:55:55 +0800143 if (stat(LYNQ_LOG_FILE_PATH, &st) == 0 && st.st_size >= g_lynq_log_size) \
w.deng5f640e02025-07-28 09:07:02 +0800144 { \
145 rename(LYNQ_LOG_FILE_PATH, LYNQ_LOG_FILE_BACKUP); \
146 } \
147 time_t now = time(NULL); \
148 struct tm *tm_info = localtime(&now); \
149 char time_buf[64]; \
150 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", tm_info); \
151 FILE *fp = fopen(LYNQ_LOG_FILE_PATH, "a"); \
152 if (fp != NULL) \
153 { \
154 fprintf(fp, "%s %s\n", time_buf, buf); \
155 fclose(fp); \
156 } \
157 } \
w.deng59bac422025-07-25 15:32:57 +0800158}while(0)
159
160
161//output_syslog_time(mod, LOG_##LEVEL, &t);
162#define put_to_syslog(LEVEL) do { \
163 openlog(mod, LOG_PID, LOG_MAIL); \
164 output_syslog_time(mod, LOG_##LEVEL); \
165 va_start(arg, fmt); \
166 ret = sys_log(mod, LOG_##LEVEL, fmt, arg); \
167 va_end(arg); \
168 closelog(); \
169}while(0)
170
171#define SYSLOG_LVL_PATH "/proc/sys/slog/syslog_level"
172#define PRINT_LVL_PATH "/proc/sys/slog/print_level"
173#define LOG_SOC_TIME_SWITCH "/proc/sys/slog/soctime_sw"
174static void set_log_level(int* loglevel, char* filepath)
175{
176 int fd;
177 char buf[4] = {0};
178 int len = 0;
179 fd = open(filepath, O_RDWR);
180 if (fd < 0) {
181 printf("fail to open\n");
182 return;
183 }
184
185 len = read(fd, buf, 3);
186
187 if (len > 0 )
188 *loglevel = atoi(buf);
189 else {
190 close(fd);
191 return;
192 }
193 close(fd);
194}
195
196int slog(char *mod, int prio, const char *fmt, ...)
197{
198 va_list arg = {0};
199 int ret = 0;
200
201 if (SLOG_SOCTIME_ON == soctime_sw)
202 time_us = get_time_us();
203
204 if (slog_sysloglevel > 0 && slog_sysloglevel <= prio) {
205 switch (prio) {
206 case SLOG_NORMAL: {
207 put_to_syslog(NOTICE);
208 }
209 break;
210
211 case SLOG_DEBUG: {
212 put_to_syslog(DEBUG);
213 }
214 break;
215
216 case SLOG_ERR: {
217 put_to_syslog(ERR);
218 }
219 break;
220
221 default:
222 break;
223 }
224 }
225
226 if (slog_printlevel > 0 && slog_printlevel <= prio) {
227 put_to_console(mod, fmt, arg);
228 }
229
230 return ret;
231}
232
233void security_log(int mod,const char *fmt, ...)
234{
235 char buf[512] = {0};
236 va_list arg = {0};
237 int n = 0;
238
239 snprintf(buf, sizeof(buf), "[%X][%d]", mod, syscall(SYS_gettid));
240 n = strlen(buf);
241 va_start(arg, fmt);
242 vsnprintf(buf+n, sizeof(buf)-n, fmt, arg);
243 va_end(arg);
244 ipc_send_message(mod, MODULE_ID_SECURITY_LOG, MSG_CMD_SECURITY_LOG_SAVE, sizeof(buf), (unsigned char *)buf, IPC_NOWAIT);
245}
246