1
0
Эх сурвалжийг харах

log macro to print file name and line number

wangyu 8 жил өмнө
parent
commit
966ec3c925
4 өөрчлөгдсөн 30 нэмэгдсэн , 14 устгасан
  1. 16 8
      encrypt.cpp
  2. 4 0
      encrypt.h
  3. 8 4
      log.cpp
  4. 2 2
      log.h

+ 16 - 8
encrypt.cpp

@@ -6,6 +6,8 @@
 #include <stdio.h>
 #include <encrypt.h>
 
+#include "log.h"
+
 //static uint64_t seq=1;
 
 static int8_t zero_iv[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   0,0,0,0};//this prog use zero iv,you should make sure first block of data contains a random/nonce data
@@ -18,6 +20,10 @@ static const int disable_aes=0;
 int auth_mode=auth_md5;
 int cipher_mode=cipher_aes128cbc;
 
+
+
+
+
 //int auth(uint8_t *data,)
 /*
 int my_encrypt(uint8_t *data,uint8_t *output,int &len,uint8_t * key)
@@ -48,6 +54,7 @@ int auth_md5_verify(const char *data,int &len)
 {
 	if(len<16)
 	{
+		log(log_trace,"auth_md5_verify len<16\n");
 		return -1;
 	}
 	char md5_res[16];
@@ -56,6 +63,7 @@ int auth_md5_verify(const char *data,int &len)
 
 	if(memcmp(md5_res,data+len-16,16)!=0)
 	{
+		log(log_trace,"auth_md5_verify md5 check failed\n");
 		return -2;
 	}
 	len-=16;
@@ -91,8 +99,8 @@ int cipher_none_encrypt(const char *data,char *output,int &len,char * key)
 }
 int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
 {
-	if(len%16 !=0) return -1;
-	if(len<2) return -1;
+	if(len%16 !=0) {log(log_trace,"len%16!=0");return -1;}
+	if(len<2) {log(log_trace,"len <2 ");return -1;}return -1;
 	AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv);
 	len=((unsigned char)output[len-2])*256u+((unsigned char)output[len-1]);
 	return 0;
@@ -135,14 +143,14 @@ int cipher_decrypt(const char *data,char *output,int &len,char * key)
 
 int my_encrypt(const char *data,char *output,int &len,char * key)
 {
-	if(len<0) return -1;
-	if(len>65535) return -1;
+	if(len<0) {log(log_trace,"len<0");return -1;}
+	if(len>65535) {log(log_trace,"len>65535");return -1;}
 
 	char buf[65535+100];
 	char buf2[65535+100];
 	memcpy(buf,data,len);
-	if(auth_cal(buf,buf2,len)!=0) {return -1;}
-	if(cipher_encrypt(buf2,output,len,key) !=0) {return -1;}
+	if(auth_cal(buf,buf2,len)!=0) {log(log_trace,"auth_cal failed ");return -1;}
+	if(cipher_encrypt(buf2,output,len,key) !=0) {log(log_trace,"auth_cal failed ");return -1;}
 	return 0;
 
 }
@@ -151,8 +159,8 @@ int my_decrypt(const char *data,char *output,int &len,char * key)
 	if(len<0) return -1;
 	if(len>65535) return -1;
 
-	if(cipher_decrypt(data,output,len,key) !=0) {return -1;}
-	if(auth_verify(output,len)!=0) {return -1;}
+	if(cipher_decrypt(data,output,len,key) !=0) {log(log_trace,"cipher_decrypt failed "); return -1;}
+	if(auth_verify(output,len)!=0) {log(log_trace,"auth_verify failed ");return -1;}
 
 	return 0;
 }

+ 4 - 0
encrypt.h

@@ -12,6 +12,10 @@ int my_decrypt(const char *data,char *output,int &len,char * key);
 int my_encrypt_pesudo_header(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen);
 int my_decrypt_pesudo_header(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen);
 
+
+unsigned short csum(const unsigned short *ptr,int nbytes) ;
+
+
 const int auth_none=0;
 const int auth_md5=1;
 

+ 8 - 4
log.cpp

@@ -1,10 +1,11 @@
 #include <log.h>
 
 const int log_level=log_debug;
-
+int enable_log_position=1;
+int enable_log_color=1;
 char log_text[][10]={"FATAL","ERROR","WARN","INFO","DEBUG","TRACE"};
 char log_color[][10]={RED,RED,YEL,GRN,BLU,""};
-void log(int level,const char* str, ...) {
+void log0(const char * file,const char * function,int line,int level,const char* str, ...) {
 
 	if(level>log_level) return ;
 	if(level>log_trace||level<0) return ;
@@ -17,17 +18,20 @@ void log(int level,const char* str, ...) {
 	time(&timer);
 	tm_info = localtime(&timer);
 
+	if(enable_log_color)
 	printf(log_color[level]);
 
 	strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
-	printf("[%s][%s]",buffer,log_text[level]);
+	printf("[%s][%s]",buffer,log_text[level],file,line);
 
+	if(enable_log_position)printf("[%s,func:%s,line:%d]",file,function,line);
 
 	va_list vlist;
 	va_start(vlist, str);
 	vfprintf(stdout, str, vlist);
 	va_end(vlist);
-	printf(RESET);
+	if(enable_log_color)
+		printf(RESET);
 	//printf("\n");
 	fflush(stdout);
 }

+ 2 - 2
log.h

@@ -73,9 +73,9 @@ const int log_debug=4;
 const int log_trace=5;
 
 
+#define log(...) log0(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__)
 
-
-void log(int level,const char* str, ...);
+void log0(const char * file,const char * function,int line,int level,const char* str, ...);
 
 
 #endif