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