blob: 510b815772af21b0980fbe55893a2de0284cf418 [file] [log] [blame]
hj.shaod6593de2025-07-01 05:06:07 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <dirent.h>
5#include <regex.h>
6#include <sys/stat.h>
7#include <sys/time.h>
8#include <time.h>
9#include <limits.h>
10#include <fcntl.h>
11#include <unistd.h>
12#include <ctype.h>
13#include <dlfcn.h>
14
15
16typedef void (*mbtk_log)(int level, const char *format,...);
17static void *handle = NULL;
18static mbtk_log fun_ptr_log = NULL;
19#define LIB_PATH "/lib/libmbtk_lib.so"
20#define LOG_LEVLE_CONFIG_FILE "/etc/telinit"
21#define MAX_FILES 500
22#define MAX_PATH 100
23#define CP_LOG_PATH "/media/var/log"
24#define DUMP_LOG_PATH "/media/var/log/modem_dump"
25#define EMMC_HEALTH "/sys/kernel/debug/mmc0/mmc0:0001/health"
26#define CP_LOG_MAX_SIZE (600 * 1024 * 1024)
27#define DUMP_LOG_MAX_SIZE (100 * 1024 * 1024)
28#define SEVEN_DAYS_SECONDS (7 * 24 * 60 * 60)
29
30#ifndef LOG_ERR_LEVEL
31#define LOG_ERR_LEVEL 3 /* error conditions */
32#endif
33#ifndef LOG_WARN_LEVEL
34#define LOG_WARN_LEVEL 4 /* warning conditions */
35#endif
36#ifndef LOG_INFO_LEVEL
37#define LOG_INFO_LEVEL 6 /* informational */
38#endif
39#ifndef LOG_DEBUG_LEVEL
40#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
41#endif
42#ifndef LOG_VERBOSE_LEVEL
43#define LOG_VERBOSE_LEVEL 8
44#endif
45
46#define LOGV(fmt, args ...) \
47 do{ \
48 char *file_ptr_1001 = __FILE__; \
49 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
50 char line_1001[10] = {0}; \
51 sprintf(line_1001, "%d", __LINE__); \
52 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
53 if(*ptr_1001 == '/') \
54 break; \
55 ptr_1001--; \
56 } \
57 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: [log_check]" fmt, ptr_1001 + 1, line_1001, ##args); \
58 } while(0)
59
60#define LOGI(fmt, args...) \
61 do{ \
62 char *file_ptr_1001 = __FILE__; \
63 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
64 char line_1001[10] = {0}; \
65 sprintf(line_1001, "%d", __LINE__); \
66 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
67 if(*ptr_1001 == '/') \
68 break; \
69 ptr_1001--; \
70 } \
71 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: [log_check]" fmt, ptr_1001 + 1, line_1001, ##args); \
72 } while(0)
73
74#define LOGD(fmt, args...) \
75 do{ \
76 char *file_ptr_1001 = __FILE__; \
77 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
78 char line_1001[10] = {0}; \
79 sprintf(line_1001, "%d", __LINE__); \
80 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
81 if(*ptr_1001 == '/') \
82 break; \
83 ptr_1001--; \
84 } \
85 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: [log_check]" fmt, ptr_1001 + 1, line_1001, ##args); \
86 } while(0)
87
88#define LOGW(fmt, args...) \
89 do{ \
90 char *file_ptr_1001 = __FILE__; \
91 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
92 char line_1001[10] = {0}; \
93 sprintf(line_1001, "%d", __LINE__); \
94 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
95 if(*ptr_1001 == '/') \
96 break; \
97 ptr_1001--; \
98 } \
99 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: [log_check]" fmt, ptr_1001 + 1, line_1001, ##args); \
100 } while(0)
101
102#define LOGE(fmt, args...) \
103 do{ \
104 char *file_ptr_1001 = __FILE__; \
105 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
106 char line_1001[10] = {0}; \
107 sprintf(line_1001, "%d", __LINE__); \
108 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
109 if(*ptr_1001 == '/') \
110 break; \
111 ptr_1001--; \
112 } \
113 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: [log_check]" fmt, ptr_1001 + 1, line_1001, ##args); \
114 } while(0)
115
116int32_t init_handle()
117{
118 handle = dlopen(LIB_PATH, RTLD_NOW);
119 if(handle == NULL)
120 {
121 return -1;
122 }
123 if(fun_ptr_log == NULL)
124 {
125 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
126 if(fun_ptr_log == NULL)
127 {
128 return -1;
129 }
130 }
131 return 0;
132}
133
134typedef struct {
135 char name[128];
136 time_t mtime;
137 off_t size;
138} LogDir;
139
140int compare_mtime(const void *a, const void *b) {
141 LogDir *dirA = (LogDir *)a;
142 LogDir *dirB = (LogDir *)b;
143 return (dirA->mtime - dirB->mtime); // 升序排序
144}
145
146static int get_cp_log_folder(char *path, LogDir *dirs, int *dirs_count)
147{
148 DIR *dp = NULL;
149 DIR *subdp = NULL;
150 struct dirent *entry;
151 struct dirent *subentry;
152 struct stat st;
153 regex_t regex;
154 int count = 0;
155 int folder_size = 0;
156 int total_size = 0;
157 char full_path[1024];
158 char temp_path[1024];
159
160 // 编译正则表达式:^Log[0-9]{3}.*
161 if (regcomp(&regex, "^Log[0-9]{3}.*", REG_EXTENDED) != 0)
162 {
163 fprintf(stderr, "Failed to compile regex.\n");
164 return -1;
165 }
166
167 dp = opendir(path);
168 if (dp == NULL)
169 {
170 perror("opendir");
171 return -1;
172 }
173
174 while ((entry = readdir(dp)) != NULL)
175 {
176 if (regexec(&regex, entry->d_name, 0, NULL, 0) == 0) //entr:Log00x
177 {
178 snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
179 strncpy(dirs[count].name, full_path, sizeof(dirs[count].name));
180 //printf("flod:%s \n", full_path);
181
182 if (strstr(full_path, "tar.gz") != NULL) //tar file
183 {
184 if (lstat(full_path, &st) < 0)
185 {
186 perror("lstat");
187 }
188 folder_size += st.st_size;
189 dirs[count].mtime = st.st_mtime;
190
191 }
192 else
193 {
194 subdp = opendir(full_path); //Log00x Folder
195 if (NULL == subdp)
196 {
197 LOGE("cant open:%s\n", full_path);
198 continue;
199 }
200
201 while ((subentry = readdir(subdp)) != NULL)
202 {
203 if (strcmp(subentry->d_name, ".") == 0 || strcmp(subentry->d_name, "..") == 0)
204 continue;
205
206 //snprintf(temp_path, sizeof(temp_path), "%s/%s", full_path, subentry->d_name);
207 temp_path[0] = '\0';
208 strncat(temp_path, full_path, sizeof(temp_path) - 1);
209 strncat(temp_path, "/", sizeof(temp_path) - strlen(temp_path) - 1);
210 strncat(temp_path, subentry->d_name, sizeof(temp_path) - strlen(temp_path) - 1);
211
212 //printf("file:%s \n", temp_path);
213
214 if (lstat(temp_path, &st) < 0)
215 {
216 perror("lstat");
217 continue;
218 }
219 if (S_ISREG(st.st_mode))
220 {
221 folder_size += st.st_size;
222 dirs[count].mtime = st.st_mtime;
223 }
224
225 }
226 if(NULL != subdp)
227 closedir(subdp);
228
229 }
230 dirs[count].size = folder_size;
231 total_size += folder_size;
232 folder_size = 0;
233 count++;
234
235 if (count >= MAX_FILES)
236 {
237 fprintf(stderr, "Too many matching directories, increase MAX_DIRS.\n");
238 break;
239 }
240 }
241 }
242
243 closedir(dp);
244 regfree(&regex);
245 qsort(dirs, count, sizeof(LogDir), compare_mtime);
246
247 *dirs_count = count;
248 return total_size;
249
250}
251
252static int get_dump_log_folder(char *path, LogDir *files, int *dirs_count)
253{
254
255 DIR *dp = NULL;
256 struct dirent *entry;
257 char full_path[4096];
258 int total_size = 0;
259 int count = 0;
260 struct stat st;
261
262 dp = opendir(path);
263 if (NULL == dp)
264 {
265 perror("opendir");
266 return -1;
267 }
268
269 while ((entry = readdir(dp)) != NULL)
270 {
271 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
272 continue;
273 snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
274
275
276
277
278 //printf("file:%s \n", full_path);
279
280 if (lstat(full_path, &st) < 0) {
281 perror("lstat");
282 continue;
283 }
284 if (S_ISREG(st.st_mode)) {
285 total_size += st.st_size;
286 strncpy(files[count].name, full_path, sizeof(files[count].name));
287 files[count].size = st.st_size;
288 files[count++].mtime = st.st_mtime;
289 }
290 }
291 if(NULL != dp)
292 closedir(dp);
293 *dirs_count = count;
294
295 qsort(files, count, sizeof(LogDir), compare_mtime);
296 return total_size;
297
298}
299
300
301int get_emmc_health(char *path)
302{
303 char buffer[1024];
304 int result = -1;
305 int i = 0;
306 int fd = open(path, O_RDONLY);
307 if (fd == -1)
308 {
309 LOGE("can not open health node");
310 return -1;
311 }
312
313 ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
314 if (bytes_read > 0)
315 {
316 buffer[bytes_read] = '\0';
317 LOGI("emmc health node string:\n%s len:%d\n", buffer, bytes_read);
318 }
319 close(fd);
320
321 for(i = 0; i < bytes_read; i++)
322 {
323 if('-' == buffer[i])
324 {
325 char num_str[4] = {buffer[i+1], buffer[i+2], buffer[i+3], '\0'};
326 result = atoi(num_str);
327 LOGI("emmc health %d\n", result);
328 break;
329 }
330 }
331
332 return result;
333
334}
335
336int set_log_level(const char *log_level)
337{
338 char command[256] = {0};
339 char *log_levl_param[6] = {"default", "cp_load", "atcmdsrv", "ciClientStubTas", "nvmproxy", "rild"};
340 int i = 0;
341
342 for(i = 0; i < 6; i++)
343 {
344 snprintf(command, sizeof(command),
345 "sed -i 's/\\(setprop sys.%s.loglevel \\)\\S*/\\1%s/' %s",
346 log_levl_param[i], log_level, LOG_LEVLE_CONFIG_FILE);
347
348 int result = system(command);
349 if (result != 0)
350 {
351 LOGE("command execution failed.\n", command);
352 return -1;
353 }
354 }
355
356 return 0;
357}
358
359
360
361int is_modified_within_7_days(LogDir dir)
362{
363 time_t now = time(NULL);
364 time_t mtime = dir.mtime;
365
366 // 计算时间差
367 double diff = difftime(now, mtime);
368
369 if (diff <= SEVEN_DAYS_SECONDS)
370 {
371 return 1;
372 } else {
373 return 0;
374 }
375}
376
377int tar_log_file(const char *path_name, time_t mtime)
378{
379 char command[256] = {0};
380 struct timeval times[2];
381 char tar_file[128];
382 struct stat st;
383 time_t now;
384 struct tm *tm_info;
385 char time_str[64];
386 int ret;
387
388 LOGI("input log file: %s\n", path_name);
389 time(&now);
390 tm_info = localtime(&now);
391 strftime(time_str, sizeof(time_str), "%Y-%m-%d-%H:%M:%S", tm_info);
392 snprintf(tar_file, sizeof(tar_file)," %s-%s.tar.gz", path_name, time_str);
393
394 LOGI("input log file: %s\n", tar_file);
395
396 snprintf(command, sizeof(command), "tar -czvf %s %s", tar_file, path_name);
397 ret = system(command);
398 if (ret != 0)
399 {
400 LOGE("command execution failed.\n", command);
401 return -1;
402 }
403
404
405 if (lstat(tar_file, &st) < 0)
406 {
407 return -1;
408 }
409 times[0].tv_sec = st.st_atime;
410 times[0].tv_usec = 0;
411 times[1].tv_sec = mtime;
412 times[1].tv_usec = 0;
413
414 if (utimes(tar_file, times) < 0)
415 {
416 return -1;
417 } //update tar file mtime
418
419 return 0;
420
421}
422
423
424int main(int argc, char* argv[])
425{
426 int health;
427 LogDir cp_dirs[MAX_FILES];
428 LogDir dump_files[MAX_FILES];
429 int cp_dirs_count = 0;
430 int dump_files_count = 0;
431 int i = 0;
432 int ret = 0;
433 char command[128] = {0};
434 int total_size = 0;
435 int modify_7day;
436 init_handle();
437
438 health = get_emmc_health(EMMC_HEALTH);
439 if(health != -1)
440 {
441 if(health >= 50 && health < 70)
442 {
443 ret = set_log_level("5"); //warn
444 if(ret != 0)
445 LOGE("set log level error\n");
446 }
447
448 if(health >= 70)
449 {
450 ret = set_log_level("0");
451 if(ret != 0)
452 LOGE("set log level error\n");
453 }
454 }
455 else
456 {
457 LOGE("get emmc health error\n");
458 }
459
460 while(1)
461 {
462 i = 0;
463 total_size = get_cp_log_folder(CP_LOG_PATH, cp_dirs, &cp_dirs_count);
464 LOGI("module %s files size:%d MB\n",CP_LOG_PATH, (total_size/1024)/1024);
465 while(total_size >= CP_LOG_MAX_SIZE)
466 {
467 LOGI("Exceed capacity limit:%d MB now:%d MB\n", (CP_LOG_MAX_SIZE/1024)/1024, (total_size/1024)/1024);
468 if(i >= cp_dirs_count)
469 {
470 LOGE("No more files that can be compressed\n");
471 break;
472 }
473 modify_7day = is_modified_within_7_days(cp_dirs[i]);
474 if(modify_7day == 0)
475 {
476 //Delete files created for more than 7 days
477 //snprintf(command, sizeof(command), "rm -rf %s", cp_dirs[i].name);
478 command[0] = '\0';
479 strncat(command, "rm -rf ", sizeof(command) - 1);
480 strncat(command, cp_dirs[i].name, sizeof(command) - strlen(command) - 1);
481
482 LOGI("Delete files:%s created for more than 7 days\n", cp_dirs[i].name);
483 ret = system(command);
484 if (ret != 0)
485 {
486 LOGE("command execution failed.\n", command);
487 return -1;
488 }
489 LOGI("Execute the command:%s\n", command);
490 }
491
492 if(modify_7day == 1)
493 {
494 if(strstr(cp_dirs[i].name, "tar.gz") != NULL)
495 {
496 LOGI("Tar file:%s already exists, No more compression at once\n", cp_dirs[i].name);
497 i++;
498 continue;
499 }
500
501 tar_log_file(cp_dirs[i].name, cp_dirs[i].mtime);
502
503 //snprintf(command, sizeof(command), "rm -rf %s", cp_dirs[i].name);
504 command[0] = '\0';
505 strncat(command, "rm -rf ", sizeof(command) - 1);
506 strncat(command, cp_dirs[i].name, sizeof(command) - strlen(command) - 1);
507
508 LOGI("del folder command:%s\n", command);
509 ret = system(command);
510 if (ret != 0)
511 {
512 LOGE("command execution failed.\n", command);
513 return -1;
514 }
515 }
516 i++;
517 total_size = get_cp_log_folder(CP_LOG_PATH, cp_dirs, &cp_dirs_count);
518 sleep(2);
519
520 }
521
522 i = 0;
523 total_size = get_dump_log_folder(DUMP_LOG_PATH, dump_files, &dump_files_count);
524 LOGI("module %s files size:%d MB\n",DUMP_LOG_PATH, (total_size/1024)/1024);
525 while(total_size >= DUMP_LOG_MAX_SIZE)
526 {
527 LOGI("Exceed capacity limit:%d MB now:%d MB\n", (DUMP_LOG_MAX_SIZE/1024)/1024, (total_size/1024)/1024);
528 if(i >= dump_files_count)
529 {
530 LOGE("No more files that can be compressed\n");
531 break;
532 }
533 modify_7day = is_modified_within_7_days(dump_files[i]);
534 if(modify_7day == 0)
535 {
536 //del timeout file
537 //snprintf(command, sizeof(command), "rm -rf %s", dump_files[i].name);
538 command[0] = '\0';
539 strncat(command, "rm -rf ", sizeof(command) - 1);
540 strncat(command, cp_dirs[i].name, sizeof(command) - strlen(command) - 1);
541
542 LOGI("command:%s, total_size:%d \n", command, total_size);
543 ret = system(command);
544 if (ret != 0)
545 {
546 LOGE("command execution failed.\n", command);
547 return -1;
548 }
549 }
550 if(modify_7day == 1)
551 {
552 if(strstr(dump_files[i].name, "tar.gz") != NULL)
553 {
554 LOGI("Tar file:%s already exists, No more compression at once\n", dump_files[i].name);
555 i++;
556 continue;
557 }
558 tar_log_file(dump_files[i].name, dump_files[i].mtime);
559
560 //del source file
561 //snprintf(command, sizeof(command), "rm -rf %s", dump_files[i].name);
562 command[0] = '\0';
563 strncat(command, "rm -rf ", sizeof(command) - 1);
564 strncat(command, dump_files[i].name, sizeof(command) - strlen(command) - 1);
565
566 LOGI("del folder command:%s\n", command);
567 ret = system(command);
568 if (ret != 0)
569 {
570 LOGE("command execution failed.\n", command);
571 return -1;
572 }
573 }
574 i++;
575 total_size = get_dump_log_folder(DUMP_LOG_PATH, dump_files, &dump_files_count);
576 sleep(2);
577 }
578
579 sleep(60);
580
581#if 0
582 for(i = 0; i < cp_dirs_count && cp_dirs_count < MAX_FILES; i++) {
583 struct tm *tm_info = localtime(&cp_dirs[i].mtime);
584 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_info);
585 printf("folder:%s, files:%ld, time:%s\n", cp_dirs[i].name, cp_dirs[i].size, timebuf);
586 }
587
588
589
590 for(i = 0; i < dump_files_count && dump_files_count < MAX_FILES; i++) {
591 struct tm *tm_info = localtime(&dump_files[i].mtime);
592 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_info);
593 printf("folder:%s, files:%ld, time:%s\n", dump_files[i].name, dump_files[i].size, timebuf);
594 }
595#endif
596
597 }
598
599 return 0;
600}
601
602
603
604