blob: 442770bea18c5d4e41cdcd058cbc055e27453236 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#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;
23
24int log_switch = LOG_ON;
25long long time_us;
26
27void log_sig_hdl(int sig, siginfo_t *siginfo, void *ptr)
28{
29 int level = 0;
30 int printlog = 0, syslog = 0, soctime = 0;
31
32 if (sig == SIGUSR2) {
33 slog(NET_PRINT, SLOG_ERR, "prelevels(printlog:%d,syslog:%d,soctime:%d), setlevel:%d \n",
34 slog_printlevel, slog_sysloglevel, soctime_sw, siginfo->si_value.sival_int);
35
36 level = siginfo->si_value.sival_int;
37
38 soctime = (level % 1000) / 100; //È¡°Ùλ£¬Ç§Î»ÒÔÉϲ»Ö§³Ö
39 syslog = (level - soctime * 100) / 10; //ȡʮλ
40 printlog = level - soctime * 100 - syslog * 10; //È¡¸öλ
41
42 slog(NET_PRINT, SLOG_ERR, "printlog:%d, syslog:%d, soctime:%d \n", printlog, syslog, soctime);
43
44 if (printlog > 0 && printlog <= SLOG_OFF)
45 slog_printlevel = printlog;
46 else
47 slog(NET_PRINT, SLOG_ERR, "slog_printlevel only support %d~%d \n", SLOG_DEBUG, SLOG_OFF);
48
49 if (syslog > 0 && syslog <= SLOG_OFF)
50 slog_sysloglevel = syslog;
51 else
52 slog(NET_PRINT, SLOG_ERR, "slog_sysloglevel only support %d~%d \n", SLOG_DEBUG, SLOG_OFF);
53
54 if (soctime == 0 || soctime == 1)
55 soctime_sw = soctime;
56 else
57 slog(NET_PRINT, SLOG_ERR, "soctime_sw only support 0~1 \n");
58 }
59}
60
61//×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
62void register_sig_logswitch(void)
63{
64 struct sigaction st;
65
66 memset(&st, 0, sizeof(st));
67 st.sa_flags = SA_SIGINFO;
68 st.sa_sigaction = log_sig_hdl;
69 sigaction(SIGUSR2, &st, NULL);
70}
71
72//¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð
73void loglevel_init(void)
74{
75 char nv_print_level[32] = {0};
76 char nv_syslog_level[32] = {0};
77 char nv_soctime_switch[32] = {0};
78
79 sc_cfg_get("print_level", nv_print_level, sizeof(nv_print_level));
80 sc_cfg_get("syslog_level", nv_syslog_level, sizeof(nv_syslog_level));
81 sc_cfg_get("soctime_switch", nv_soctime_switch, sizeof(nv_soctime_switch));
82
83 //loglevel·ÇÓÐЧֵʱ£¬½«Ä¬ÈÏ´òÓ¡¼¶±ðÉèÖÃΪoff£¬¼´¹Ø±Õ´òÓ¡
84 slog_printlevel = atoi(nv_print_level);
85 if((slog_printlevel < SLOG_DEBUG) || (slog_printlevel > SLOG_OFF)) {
86 slog_printlevel = SLOG_OFF;
87 }
88
89 slog_sysloglevel = atoi(nv_syslog_level);
90 if((slog_sysloglevel < SLOG_DEBUG) || (slog_sysloglevel > SLOG_OFF)) {
91 slog_sysloglevel = SLOG_OFF;
92 }
93
94 soctime_sw = atoi(nv_soctime_switch);
95 if((soctime_sw > SLOG_SOCTIME_ON) || (soctime_sw < SLOG_SOCTIME_OFF)) {
96 soctime_sw = SLOG_SOCTIME_OFF;
97 }
98
99 register_sig_logswitch();
100
101 slog(NET_PRINT, SLOG_DEBUG, "loglevel_init, print:%d, syslog:%d, soctime:%d \n",
102 slog_printlevel, slog_sysloglevel, soctime_sw);
103}
104
105int sys_log(char *ident, int prio, const char * fmt, va_list arg)
106{
107 vsyslog(prio, fmt, arg);
108 return 0;
109}
110
111inline void output_syslog_time(char *mod, int prio)
112{
113 if (SLOG_SOCTIME_ON == soctime_sw)
114 syslog(prio, "[%lld.%llds]: ", time_us / 1000000, time_us % 1000000);
115}
116
117#define put_to_console(mod, fmt, arg) do {\
118 char buf[1024] = {0}; \
119 if(SLOG_SOCTIME_ON == soctime_sw) \
120 snprintf(buf, 1023, "%s[%d][%lld.%llds]: ", mod, syscall(SYS_gettid), time_us/1000000, time_us%1000000); \
121 else \
122 snprintf(buf, 1023, "%s[%d]: ", mod, syscall(SYS_gettid)); \
123 int n = strlen(buf); \
124 va_start(arg, fmt); \
125 vsnprintf(buf+n, 1023-n, fmt, arg); \
126 va_end(arg); \
127 printf("%s",buf); \
128}while(0)
129
130
131//output_syslog_time(mod, LOG_##LEVEL, &t);
132#define put_to_syslog(LEVEL) do { \
133 openlog(mod, LOG_PID, LOG_MAIL); \
134 output_syslog_time(mod, LOG_##LEVEL); \
135 va_start(arg, fmt); \
136 ret = sys_log(mod, LOG_##LEVEL, fmt, arg); \
137 va_end(arg); \
138 closelog(); \
139}while(0)
140
141#define SYSLOG_LVL_PATH "/proc/sys/slog/syslog_level"
142#define PRINT_LVL_PATH "/proc/sys/slog/print_level"
143#define LOG_SOC_TIME_SWITCH "/proc/sys/slog/soctime_sw"
144static void set_log_level(int* loglevel, char* filepath)
145{
146 int fd;
147 char buf[4] = {0};
148 int len = 0;
149 fd = open(filepath, O_RDWR);
150 if (fd < 0) {
151 printf("fail to open\n");
152 return;
153 }
154
155 len = read(fd, buf, 3);
156
157 if (len > 0 )
158 *loglevel = atoi(buf);
159 else {
160 close(fd);
161 return;
162 }
163 close(fd);
164}
165
166int slog(char *mod, int prio, const char *fmt, ...)
167{
168 va_list arg = {0};
169 int ret = 0;
170
171 if (SLOG_SOCTIME_ON == soctime_sw)
172 time_us = get_time_us();
173
174 if (slog_sysloglevel > 0 && slog_sysloglevel <= prio) {
175 switch (prio) {
176 case SLOG_NORMAL: {
177 put_to_syslog(NOTICE);
178 }
179 break;
180
181 case SLOG_DEBUG: {
182 put_to_syslog(DEBUG);
183 }
184 break;
185
186 case SLOG_ERR: {
187 put_to_syslog(ERR);
188 }
189 break;
190
191 default:
192 break;
193 }
194 }
195
196 if (slog_printlevel > 0 && slog_printlevel <= prio) {
197 put_to_console(mod, fmt, arg);
198 }
199
200 return ret;
201}
202
203void security_log(int mod,const char *fmt, ...)
204{
205 char buf[512] = {0};
206 va_list arg = {0};
207 int n = 0;
208
209 snprintf(buf, sizeof(buf), "[%X][%d]", mod, syscall(SYS_gettid));
210 n = strlen(buf);
211 va_start(arg, fmt);
212 vsnprintf(buf+n, sizeof(buf)-n, fmt, arg);
213 va_end(arg);
214 ipc_send_message(mod, MODULE_ID_SECURITY_LOG, MSG_CMD_SECURITY_LOG_SAVE, sizeof(buf), (unsigned char *)buf, IPC_NOWAIT);
215}
216