Log Hex Prolin

mail@pastecode.io avatar
unknown
c_cpp
17 days ago
2.7 kB
2
Indexable
Never

#define MAX_LOG_BUFFER			(4200)
#define logHexData(title,dataIn,lendata){logHexDataA((__FUNCTION__),(__LINE__),(title),(dataIn),(lendata) );}
#define LOGGER(type, fmt, ... ) {  logger((type),(fmt),( __DATE__), (__TIME__), (__FUNCTION__),(__LINE__),##__VA_ARGS__ ); }
#define LOGGERL(function,line,type, fmt, ... ) {  logger((type),(fmt),( __DATE__), (__TIME__), (function),(line),##__VA_ARGS__ ); }

void PubBcd2Asc0(const uchar *in, uint length, uchar *out);
void PubBcd2Asc(const uchar* in, uint length, uchar* out) 

void logger(LOG_T logType, const char* fmt, ...) {
	va_list args;
	char buffer[MAX_LOG_BUFFER];
	const char str[] = "[%s] [%s] [%s] [%d] ";
	memset(buffer, 0, sizeof(buffer));
	char* formatStr = (char*)malloc(strlen((char*)str) + strlen((char*)fmt) + 1);
	if (formatStr == NULL) { LOGGER(LOG_DEBUG, "No hay memoria ");return ; }
	strcpy(formatStr, str);
	strcat(formatStr, fmt);

	va_start(args, fmt);
	
	int RValSn= vsnprintf(buffer, MAX_LOG_BUFFER, formatStr, args);
	if(RValSn < 0 || RValSn > MAX_LOG_BUFFER)
	{
		/*ERROR_snprintf*/OsLog(LOG_ERROR, "[%d][%s][%s][%s] vsnprintf overflow [%ld][%ld]", __LINE__, __DATE__, __TIME__, __FUNCTION__, RValSn, MAX_LOG_BUFFER);
	}	

	va_end(args);
	OsLog(logType, buffer);


	free(formatStr);
}



 //Convert BIN string to readable HEX string, which have double length of BIN string. 0x12AB-->"12AB"
void PubBcd2Asc(const uchar* in, uint length, uchar* out) 
{
	static uchar ucHexToChar[16 + 2] = { 0 };
	uint i;
	snprintf((char*)ucHexToChar, sizeof(ucHexToChar), "0123456789ABCDEF");

	if ((in == NULL) || (out == NULL)) {
		return;
	}

	for (i = 0; i < length; i++) {
		out[2 * i] = ucHexToChar[(in[i] >> 4)];
		out[2 * i + 1] = ucHexToChar[(in[i] & 0x0F)];
	}
}

void PubBcd2Asc0(const uchar* in, uint length, uchar* out) {
	if ((in == NULL) || (out == NULL)) {
		return;
	}

	PubBcd2Asc(in, length, out);
	out[2 * length] = 0;
}

int logHexDataA(const char* function, int line, char* msg, uchar* data, int len) {
	uchar ascii[MAX_LOG_BUFFER] = { 0 };

	if ((len * 2) > sizeof(ascii)) {
		LOGGER(LOG_ERROR, "buffer too long [%d]", len);
		return -1;
	}

	PubBcd2Asc0(data, len, ascii);

	LOGGERL(function,line, LOG_DEBUG, "%s", msg);

	if (strlen((char*)ascii) > 900)//because Elipse IDE right limit  (~980bytes)
	{
		int maxlen = strlen((char*)ascii);
		int curr = 0;
		int bytesRemain = 0;
		char temp[900 + 2];
		do {
			memset(temp, 0, sizeof(temp));
			bytesRemain = ((maxlen - curr) > 900 ? 900 : (maxlen - curr));
			memcpy(temp, ascii + curr, bytesRemain);
			LOGGERL(function, line, LOG_DEBUG, "%s", temp);
			curr += bytesRemain;
		} while (curr < maxlen);
	}
	else {
		LOGGERL(function, line, LOG_DEBUG, "%s", ascii);
	}
	return RET_OK;
}
Leave a Comment