#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "drm_korea_def.h"
#ifdef __KDRM_FSTRACE
#define LOG_BUFFER_SIZE 512
#define LOG_LINE_NUM 16
#ifdef WIN32
#define LOG_FILE FILE *
#define LOG_OPEN(s, m) fopen(s, m)
#define LOG_CLOSE(hLog) fclose(hLog)
#define LOG_READ (hLog, p, x) fread (p, x, 1, hLog)
#define LOG_WRITE(hLog, p, x) fwrite(p, x, 1, hLog)
#define LOG_PUTC(hLog, c) fputc(c, hLog)
#define LOG_PUTS(hLog, s) fputs(s, hLog)
#else
#define LOG_FILE int32_t
#define LOG_OPEN(s, m) Fopen((uint8_t *)s, (uint8_t *)m)
#define LOG_CLOSE(hLog) Fclose(hLog)
#define LOG_READ (hLog, p, x) Fread (hLog, p, x)
#define LOG_WRITE(hLog, p, x) Fwrite(hLog, p, x)
#define LOG_PUTC(hLog, c) Fputc(hLog, c)
#define LOG_PUTS(hLog, s) Fputs(hLog, (uint8_t *)s)
#endif
static LOG_FILE fpLogfile;
static char logBuffer[LOG_BUFFER_SIZE];
void log_Init(char *logName)
{
fpLogfile = LOG_OPEN(logName, "w+");
if(fpLogfile > 0)
{
log_Print("\r\n====> Starting ... :%s %s\r\n", __TIME__, __FILE__);
}
}
void log_Print(char *logFormat, ...)
{
if(fpLogfile > 0)
{
va_list va;
int len;
va_start(va, logFormat);
len = vsprintf(logBuffer, logFormat, va);
va_end(va);
logBuffer[len+1] = 0;
LOG_PUTS(fpLogfile, logBuffer);
}
}
char Hex2Dec(unsigned char bHex)
{
bHex &= 0x0F;
bHex += bHex<10? '0': 'A' - 10;
return bHex;
}
void log_Dump(const unsigned char *pCache, int u32Size, bool bBinary)
{
int i, stride;
unsigned char *p = (unsigned char *)pCache;
char cTmp;
if(fpLogfile < 0)
return;
if(bBinary)
{
for(; p<pCache+u32Size; p+=LOG_BUFFER_SIZE)
{
stride = (p+LOG_BUFFER_SIZE <= pCache+u32Size)? LOG_BUFFER_SIZE: u32Size%LOG_BUFFER_SIZE;
LOG_WRITE(fpLogfile, p, stride);
}
}
else
{
for(; p<pCache+u32Size; p+=LOG_LINE_NUM)
{
stride = (p+LOG_LINE_NUM <= pCache+u32Size)? LOG_LINE_NUM:u32Size%LOG_LINE_NUM;
for(i=0; i<stride; i++)
{
cTmp = Hex2Dec((p[i]&0xF0) >> 4);
LOG_PUTC(fpLogfile, cTmp);
cTmp = Hex2Dec(p[i]&0x0F);
LOG_PUTC(fpLogfile, cTmp);
LOG_PUTC(fpLogfile, ' ');
if(i== (LOG_LINE_NUM/2 - 1))
{
LOG_PUTC(fpLogfile, '-');
LOG_PUTC(fpLogfile, ' ');
}
}
LOG_PUTC(fpLogfile, '\n');
}
}
}
void log_Terminate(void)
{
if(fpLogfile > 0)
{
log_Print("<==== Stopping ... :%s\r\n", __TIME__);
LOG_CLOSE(fpLogfile);
}
}
#endif //__KDRM_FSTRACE