| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====* | 
 | 2 |   LINUX ZTE LOG MODULE | 
 | 3 |  | 
 | 4 |   GENERAL DESCRIPTION | 
 | 5 |   print the log to the log file | 
 | 6 |  *====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*/ | 
 | 7 |  | 
 | 8 |  /*=========================================================================== | 
 | 9 |   INCLUDES | 
 | 10 |   ===========================================================================*/ | 
 | 11 | #include <unistd.h> | 
 | 12 | #include <string.h> | 
 | 13 | #include <sys/types.h> | 
 | 14 | #include <sys/stat.h> | 
 | 15 | #include <fcntl.h> | 
 | 16 | #include <time.h> | 
 | 17 | #include <pthread.h> | 
 | 18 | #include <sys/stat.h> | 
 | 19 | #include <unistd.h>  | 
 | 20 |  | 
 | 21 | #include "libzte_log.h" | 
 | 22 |  | 
 | 23 | #include "softap_api.h" | 
 | 24 | /*=========================================================================== | 
 | 25 |   DATA | 
 | 26 |   ===========================================================================*/ | 
 | 27 | char para_buf[MAX_PARA_BUF]= {0}; | 
 | 28 | char log_buf[MAX_LOG_BUF]= {0}; | 
 | 29 | static int log_switch = 0; | 
 | 30 | const off_t log_max_size = 2 * 1024 * 1024; | 
 | 31 |  | 
 | 32 | pthread_mutex_t A_SmsMutexForLog; | 
 | 33 | static int iSymbol = -1; | 
 | 34 | /*=========================================================================== | 
 | 35 |   FUNCTIONS | 
 | 36 |   ===========================================================================*/ | 
 | 37 | void zte_msg_print(const char *source_file,const unsigned int line,const char *fmt,...) | 
 | 38 | { | 
 | 39 |     va_list arg = {0}; | 
 | 40 |  | 
 | 41 |     if(NULL == source_file || NULL == fmt) | 
 | 42 |     { | 
 | 43 |         return ; | 
 | 44 |     } | 
 | 45 |     va_start(arg,fmt); | 
 | 46 |     vsprintf(para_buf,fmt,arg); | 
 | 47 |     va_end(arg); | 
 | 48 |  | 
 | 49 |     printf("%s in FILE :%s,LINE :%d\n",para_buf,source_file,line); | 
 | 50 | } | 
 | 51 |  | 
 | 52 | extern int slog(char *mod, int prio, const char *fmt, ...); | 
 | 53 |  | 
 | 54 | extern char *get_local_date_time(void); | 
 | 55 | void zte_log_append(const char *source_file,const unsigned int line, | 
 | 56 |                     const char *log_file,const char *fmt,...) | 
 | 57 | { | 
 | 58 |     //printf("[ccapp]:"); | 
 | 59 |     //printf(fmt); | 
 | 60 |    // printf("\n"); | 
 | 61 |     //slog("[ccapp]", 4, fmt); | 
 | 62 |  | 
 | 63 | //if(access("/usr/ccapp_log_printf", F_OK)>=0) | 
 | 64 | { | 
 | 65 | 	va_list arg = {0}; | 
 | 66 | 	char log_buf[1024]= {0}; | 
 | 67 |  | 
 | 68 | 	if(NULL == source_file || NULL == fmt) | 
 | 69 | 	{ | 
 | 70 | 		return ; | 
 | 71 | 	} | 
 | 72 | 	va_start(arg,fmt); | 
 | 73 | 	vsprintf(log_buf,fmt,arg); | 
 | 74 | 	va_end(arg); | 
 | 75 |  | 
 | 76 | 	printf("%s %s %d %s\n",get_local_date_time(),source_file,line,log_buf); | 
 | 77 | 	return; | 
 | 78 | } | 
 | 79 | #if 0 | 
 | 80 | else if(access("/usr/ccapp_log_file", F_OK)>=0) | 
 | 81 | { | 
 | 82 |     char nv_buf[NV_LOG_BUF_LEN]= {0}; | 
 | 83 |     char log_path[256]={0}; | 
 | 84 |     va_list arg; | 
 | 85 |     int nv_status ; | 
 | 86 |     int fd; | 
 | 87 |     time_t clock; | 
 | 88 |     char time_buf[64]={0}; | 
 | 89 |    | 
 | 90 | 	char log_path_old[300]={0}; | 
 | 91 |     struct stat statbuf; | 
 | 92 | 	 | 
 | 93 | 	if(-1 == iSymbol) | 
 | 94 | 	{ | 
 | 95 | 		pthread_mutex_init(&A_SmsMutexForLog, NULL); | 
 | 96 | 		iSymbol = 0; | 
 | 97 | 	} | 
 | 98 |     | 
 | 99 | 	pthread_mutex_lock(&A_SmsMutexForLog); | 
 | 100 |      | 
 | 101 | #if 0 | 
 | 102 |     if(0 == access("/data/zte_apps_data/log_enable.flg",F_OK)) | 
 | 103 |     { | 
 | 104 |         return; | 
 | 105 |     } | 
 | 106 | #endif | 
 | 107 |     if(NULL == source_file || NULL == fmt || NULL == log_file) | 
 | 108 |     { | 
 | 109 |     	pthread_mutex_unlock(&A_SmsMutexForLog); | 
 | 110 |         return ; | 
 | 111 |     } | 
 | 112 |     va_start(arg,fmt); | 
 | 113 |     vsprintf(para_buf,fmt,arg); | 
 | 114 |     va_end(arg); | 
 | 115 | #if 0 | 
 | 116 |     sprintf(log_buf,"%s[` basename %s` %d H]   '%s' %s%s%s","echo ",\ | 
 | 117 |             source_file,line,para_buf,OP_APPEND,LOG_DIR_PATH,log_file); | 
 | 118 |     soft_system(log_buf); | 
 | 119 | #endif | 
 | 120 |  | 
 | 121 | #if 1 | 
 | 122 |     time(&clock); | 
 | 123 |     sprintf(time_buf, "%s", ctime(&clock)); | 
 | 124 |     //set the final \n to \0 | 
 | 125 |     time_buf[strlen(time_buf)-1] = 0; | 
 | 126 |     sprintf(log_path, "%s%s", LOG_DIR_PATH, log_file); | 
 | 127 |     sprintf(log_buf,"%s %s %d %s\n", time_buf, source_file, line, para_buf); | 
 | 128 | 	 | 
 | 129 | 	if(0 == access(log_path, 0)) | 
 | 130 | 	{ | 
 | 131 | 		if (0 == stat(log_path, &statbuf)) {//cov m | 
 | 132 | 			if(statbuf.st_size > log_max_size) | 
 | 133 | 			{ | 
 | 134 | 				sprintf(log_path_old, "%s%s_old", LOG_DIR_PATH, log_file);        | 
 | 135 | 				if (0 == remove(log_path_old)) { | 
 | 136 | 					if (rename(log_path, log_path_old) != 0) {//cov m | 
 | 137 | 						printf("rename zte_log_append err\n"); | 
 | 138 | 					} | 
 | 139 | 				} | 
 | 140 | 			} | 
 | 141 | 		} | 
 | 142 | 	} | 
 | 143 | 	 | 
 | 144 |     if((fd=open(log_path, O_CREAT|O_WRONLY|O_APPEND, 0666))<0)//cov m | 
 | 145 |     { | 
 | 146 |         printf("can not open log: %s!\n", log_path); | 
 | 147 | 		pthread_mutex_unlock(&A_SmsMutexForLog); | 
 | 148 |         return ; | 
 | 149 |     } | 
 | 150 |     write(fd, log_buf, strlen(log_buf)); | 
 | 151 |     close(fd); | 
 | 152 |  | 
 | 153 |  | 
 | 154 |      | 
 | 155 | #endif | 
 | 156 |     //printf("%s\n",para_buf); | 
 | 157 | 	pthread_mutex_unlock(&A_SmsMutexForLog); | 
 | 158 |     return ; | 
 | 159 | } | 
 | 160 | #endif | 
 | 161 | } | 
 | 162 | void zte_system_call(const char *fmt,...) | 
 | 163 | { | 
 | 164 |     va_list arg = {0}; | 
 | 165 |  | 
 | 166 |     va_start(arg,fmt); | 
 | 167 |     vsprintf(para_buf,fmt,arg); | 
 | 168 |     va_end(arg); | 
 | 169 |     soft_system(para_buf); | 
 | 170 | } |