標籤:rect sel rem date dff inf inpu 示範 orm
來源:http://blog.csdn.net/lezhiyong
1. 簡單介紹
將指定記憶體段轉為16進位與asci碼的輸出到記錄檔的類。
2. 功能介紹
1) 支援window與linus雙系統。
2) 可指定輸出檔案夾。
3) 日誌能輸出的時間精確到毫秒,日誌能輸出線程號。
4) 提供字串輸出。
5) 提供指定記憶體轉換為16進位輸出。
6) 提供指定記憶體轉換為16進位和ascii碼同一時候輸出。
7) 可調整16進位輸出間距。
3. 原理和演算法
1) 模組是基於C++語言編寫。
2) 通過條件定義同一時候支援window與linus雙系統
4. 編譯和連結
將來源程式增加到project中
5. 執行環境
windows、linux作業系統驗證同一時候支援。
6. 限制條件
無
7. 介面
1.建構函式: CViTrace(char*szPathName,//路徑名
char* szAppName,//檔案名稱
_APP_INFO_OUT_CALLBACK pInfoOutCallback=NULL,//額外的輸出回調
void *pInfoOutCallbackParam=NULL);//回呼函數參數
/*************************************************************************************
功能:設定日誌輸出路徑和額外的輸出回調:
參數:
char*szPathName://路徑名
char* szAppName,//檔案名稱
_APP_INFO_OUT_CALLBACK pInfoOutCallback:額外的輸出回調
**************************************************************************************/
3. UINT trace(char* szFormat,...);
/*************************************************************************************
功能:字串輸出:
參數:
char* szFormat:可變參數。類似printf的使用
**************************************************************************************/
4. trace_bin(char*pBuffer,UINT nLength,bool bPrintAsci=false)
/******************************************************************************************
功能:轉換為16進位和ascii碼輸出:
參數:
void *Buf:記憶體片地址。
UINT nLength:記憶體片長度
bool bPrintAsci: false:左側不輸出ascii碼資訊,true:左側輸出ascii碼資訊
******************************************************************************************/
8. 示範範例
8.1. Linux Demo
#include "vitrace.h"
CViTrace vilog("/mds6800","1234");
vilog.trace("log out put demo,%s","string inf");
char* szName= "1234567asdfghddffgeerrtyuiopasdfgghhjjzxcvbbbnnnmn";
vilog.trace_bin(szName, 50);
vilog.trace_bin(szName, 100,true);
日誌輸出:
<2015-06-09,15:18:48-124, tId=134597956>log out put demo,string inf
<2015-06-09,15:18:48-124, tId=134597956>
num address :bufferContent
0 0x807370c:31323334 35363761 73646667 68646466 66676565 72727479 75696F70 61736466
32 0x807372c:67676868 6A6A7A78 63766262 626E6E6E 6D6E
<2015-06-09,15:18:48-124, tId=134597956>
num address :bufferContent
0 0x807370c:31323334 35363761 73646667 68646466 66676565 72727479 75696F70 61736466 1234567asdfghddffgeerrtyuiopasdf
32 0x807372c:67676868 6A6A7A78 63766262 626E6E6E 6D6E000D 0A20696E 70757420 77726F6E gghhjjzxcvbbbnnnmn... input wron
64 0x807374c:67212075 73616765 3A257320 25730000 0D0A2069 6E707574 2077726F 6E672120 g! usage:%s %s.... input wrong!
96 0x807376c:75736167 usag
8.2. Window Demo
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdarg.h>
#include <direct.h>
#include "vitrace.h"
#include <windows.h>
#include <tchar.h>
#include "targetver.h"
char* szName= "1234567asdfghjklqwertyuiopasdfghjklzxcvbnm1234567asdfghjklqwertyuiopasdfghjklzxcvbnm";
int _tmain(int argc, _TCHAR* argv[])
{
char buffer[MAX_PATH];
getcwd(buffer, MAX_PATH);
CViTrace viLog(buffer,"buffer_print_test");
viLog.trace("字串資訊輸出:");
viLog.trace("that‘s why with other flash applications you just don‘t get the expected results. the best option for each photo.");
viLog.trace("按進位編碼輸出指定記憶體的前位元組內容:");
viLog.trace_bin(szName,50);
Sleep(100);
viLog.trace("輸出指定記憶體的前位元組內容,左邊為進位值。右邊為assci碼:");
viLog.trace_bin(szName,100,true);
getchar();
return 0;
}
<2015-06-09,15:23:18-526, tId=20451738>字串資訊輸出:
<2015-06-09,15:23:18-526, tId=20451738>that‘s why with other flash applications you just don‘t get the expected results. the best option for each photo.
<2015-06-09,15:23:18-526, tId=20451738>按16進位編碼輸出指定記憶體的前300位元組內容:
<2015-06-09,15:23:18-526, tId=20451738>
num address :bufferContent
0 01387740:31323334 35363761 73646667 686A6B6C 71776572 74797569 6F706173 64666768
32 01387760:6A6B6C7A 78637662 6E6D3132 33343536 3761
<2015-06-09,15:23:18-635, tId=20451738>輸出指定記憶體的前1000位元組內容,左邊為16進位值,右邊為assci碼:
<2015-06-09,15:23:18-635, tId=20451738>
num address :bufferContent
0 01387740:31323334 35363761 73646667 686A6B6C 71776572 74797569 6F706173 64666768 1234567asdfghjklqwertyuiopasdfgh
32 01387760:6A6B6C7A 78637662 6E6D3132 33343536 37617364 6667686A 6B6C7177 65727479 jklzxcvbnm1234567asdfghjklqwerty
64 01387780:75696F70 61736466 67686A6B 6C7A7863 76626E6D 00000000 00000000 00000000 uiopasdfghjklzxcvbnm............
96 013877A0:00000000
/** * @file Vitrace.h * @brief 支援windows linux下將指定記憶體段轉為16進位與ascii碼的日誌輸出類 * * @writer longf * @version 1.00 * @date 2015-01-27 * @history*///#include "XAutoLock.h"//#include "rcs_public.h"#ifdef WIN32#include <windows.h>#else#endif#ifndef UINTtypedef unsigned int UINT; #endif // UINT#ifndef NULL#define NULL 0 #endif // NULL#define MAX_PATH 512UINT vi_bin2ascii(char* pDstBuf,UINT nDstBufLen,char*pSrcBuf,UINT nSrcBufLen);UINT vi_bin2hex(char* pDstBuf,UINT nDstBufLen,char*pSrcBuf,UINT nSrcBufLen);typedef void (*_APP_INFO_OUT_CALLBACK)(char*szInfo,void*pCallParam);class CViTrace{public:CViTrace(char*szPathName,//路徑名char* szAppName,//檔案名稱_APP_INFO_OUT_CALLBACK pInfoOutCallback=NULL,//額外的輸出回調void *pInfoOutCallbackParam=NULL);//回呼函數參數~CViTrace(void);public:UINT trace(char* szFormat,...);void trace_bin(char*pBuffer,UINT nLength,bool bPrintAsci=false);public: //方便應用程式層取出輸出資訊。在不同的輸出裝置打入自己的輸出隊列_APP_INFO_OUT_CALLBACK m_pInfoOutCallback;void* m_pInfoOutCallbackParam;private:charm_szFileName[MAX_PATH];//拼接好的路徑名和檔案名稱//XCritSecm_Lock;};
/** * @file Vitrace.cpp * @brief 支援windows linux下將指定記憶體段轉為16進位與ascii碼的日誌輸出類 * * @writer longf * @version 1.00 * @date 2015-01-27 * @history*/#include "vitrace.h"#include <time.h>#include <stdio.h>#include <string.h>#include <stdarg.h>#ifdef WIN32#include <windows.h>#else#include <pthread.h> #include <sys/time.h>#endif#ifdef CON_DEBUG#define CON_PRINTF printf #else#define CON_PRINTF//printf#endif#ifdef WIN32 #define PATH_CHAR "\\"#else #define PATH_CHAR "/"#endifinline unsigned int PthreadSelf(){#ifdef WIN32return ::GetCurrentThreadId();#elsereturn pthread_self();#endif}#define VITRACE_BUFFER_LENGTH 10000 //TRACE緩衝區大小#define VITRACE_HEX_PRINTBYTES_PERLINE 32#define VITRACE_HEX_SPACE 4static UINT safe_printf(char*pDst,UINT nDstSize,char*szFormat,...){UINT nListCount=0;va_list pArgList;if (!pDst) goto _EXIT_FUN;va_start(pArgList,szFormat);nListCount+=vsnprintf(pDst+nListCount,nDstSize-nListCount,szFormat,pArgList);va_end(pArgList);if (nListCount>(nDstSize-1)) nListCount = nDstSize-1;*(pDst+nListCount)=‘\0‘;_EXIT_FUN:return nListCount;}void GetFullFileName(char* pBufFullname,UINT nBufSize,char* path,char* name,char* ext_name){if (strlen(path)){if (strlen(ext_name))safe_printf(pBufFullname,nBufSize,"%s%s%s.%s",path,PATH_CHAR,name,ext_name);else safe_printf(pBufFullname,nBufSize,"%s%s%s",path,PATH_CHAR,name,name);} else{if (strlen(ext_name))safe_printf(pBufFullname,nBufSize,"%s.%s",name,ext_name);else safe_printf(pBufFullname,nBufSize,"%s",name);}}UINT get_timestamp(char* szBuf,UINT nMaxLength){UINT nLength=0; #ifdef WIN32 SYSTEMTIME tm; GetLocalTime(&tm); nLength= safe_printf(szBuf,nMaxLength,"%02d-%02d-%02d,%02d:%02d:%02d-%03d",tm.wYear,tm.wMonth,tm.wDay,tm.wHour,tm.wMinute,tm.wSecond,tm.wMilliseconds ); #else/*time_t t;struct tm*pTM=NULL;time(&t);pTM= localtime(&t);nLength= safe_printf(szBuf,nMaxLength,"%s",asctime(pTM)); */struct timeval tv; struct timezone tz; struct tm *p; gettimeofday(&tv, &tz); p = localtime(&tv.tv_sec); nLength= safe_printf(szBuf,nMaxLength,"%02d-%02d-%02d,%02d:%02d:%02d-%03d", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec/1000); #endif return nLength;}////以ascii顯示指定資料區內容UINT vi_bin2ascii(char* pDstBuf,UINT nDstBufLen,char*pSrcBuf,UINT nSrcBufLen){if (nSrcBufLen>nDstBufLen){ nSrcBufLen = nDstBufLen; }UINT i;UINT nCount=0;for (i=0;i<nSrcBufLen;i++){//ascii字元表中可顯示字元代碼>32if (32<=*(pSrcBuf+i)){nCount+= safe_printf(pDstBuf+nCount,nDstBufLen-nCount,"%c",pSrcBuf[i]);}else{nCount+= safe_printf(pDstBuf+nCount,nDstBufLen-nCount,".");}}return nCount;}//以16進位顯示指定資料區內容UINT vi_bin2hex(char* pDstBuf,UINT nDstBufLen,char*pSrcBuf,UINT nSrcBufLen){if (nSrcBufLen>nDstBufLen){nSrcBufLen = nDstBufLen; }UINT i=0;UINT j=0;UINT nCount=0;for (i=0;i<nSrcBufLen;i++){nCount+= safe_printf(pDstBuf+nCount,nDstBufLen-nCount,"%02X",(unsigned char)pSrcBuf[i]);j++;if (VITRACE_HEX_SPACE==j){j=0;nCount+= safe_printf(pDstBuf+nCount,nDstBufLen-nCount," ");}}if (VITRACE_HEX_PRINTBYTES_PERLINE>nSrcBufLen)//每行列印VITRACE_PRINTBYTES_PER_LINE位元組,不足補充空格{for (;i<VITRACE_HEX_PRINTBYTES_PERLINE;i++){nCount+= safe_printf(pDstBuf+nCount,nDstBufLen-nCount," ");//一個字元佔用兩個位元組j++;if (VITRACE_HEX_SPACE==j){j=0;nCount+= safe_printf(pDstBuf+nCount,nDstBufLen-nCount," ");}}} return nCount;}//使用者指定的輸出檔案,檔案開啟檔案,一般“a+”。返回輸出字元總數UINT vi_trace(char* szFileName,char*szMode,char*strBuf){UINT nListCount =0;char szTime[256];get_timestamp(szTime,256);char tmp[VITRACE_BUFFER_LENGTH+256];sprintf(tmp, "\r\n<%s, tId=%d>",szTime,PthreadSelf);FILE* fp;fp=fopen(szFileName,szMode);if (fp){nListCount += fwrite(tmp, strlen(tmp), 1, fp);} nListCount += fwrite(strBuf, strlen(strBuf), 1, fp); fclose(fp);return nListCount;}/*Vint32 vi_trace(char* szFileName,char*szMode,char*szFormat,...){char szBuf[LODEBUG_BUFFER_LENGTH];char szTime[256];Vint32 nListCount=0;va_list pArgList;va_start(pArgList,szFormat);nListCount+=_vsnprintf(szBuf+nListCount,LODEBUG_BUFFER_LENGTH-nListCount,szFormat,pArgList);va_end(pArgList);if (nListCount>(LODEBUG_BUFFER_LENGTH-1)){nListCount=LODEBUG_BUFFER_LENGTH-1;}*(szBuf+nListCount)=‘\0‘; nListCount = vi_trace(szFileName,szMode,szBuf); return nListCount;}*///調用函數。VITRACE_HEX_PRINTBYTES_PERLINE位元組為一行,格式化輸出二進位內容void vi_trace_bin(char* szFileName,char*szMode,char*pBuffer,UINT nLength,bool bPrintAsci=false){if (nLength>VITRACE_BUFFER_LENGTH){ nLength =VITRACE_BUFFER_LENGTH;}int nAddr=0;int nLineValidCount=0;int nLineSpaceCount=0;int nBufferCount =nLength;int n=0;char szLine[VITRACE_BUFFER_LENGTH+1]={0};int iLineNum=0;if(0<nLength){n+=safe_printf(szLine+n,VITRACE_BUFFER_LENGTH-n,"\r\n%4s %8s:%s","num","address ","bufferContent ");if (bPrintAsci){n+=vi_bin2ascii(szLine+n,VITRACE_BUFFER_LENGTH-n,pBuffer+nAddr,nLineValidCount);}while (1){if (nBufferCount>=VITRACE_HEX_PRINTBYTES_PERLINE){ nLineValidCount = VITRACE_HEX_PRINTBYTES_PERLINE;nLineSpaceCount = 0;}else{nLineValidCount = nBufferCount;nLineSpaceCount = VITRACE_HEX_PRINTBYTES_PERLINE- nLineValidCount; }n+=safe_printf(szLine+n,VITRACE_BUFFER_LENGTH-n,"\r\n%4d %p:",iLineNum*VITRACE_HEX_PRINTBYTES_PERLINE,pBuffer+nAddr);n+=vi_bin2hex(szLine+n,VITRACE_BUFFER_LENGTH-n,pBuffer+nAddr,nLineValidCount);if (bPrintAsci){n+=vi_bin2ascii(szLine+n,VITRACE_BUFFER_LENGTH-n,pBuffer+nAddr,nLineValidCount);}nAddr+=VITRACE_HEX_PRINTBYTES_PERLINE;nBufferCount-=VITRACE_HEX_PRINTBYTES_PERLINE;iLineNum++;if (0>=nBufferCount) break;}}vi_trace(szFileName,szMode,szLine);}/*************************************************************************************功能:設定日誌輸出路徑和額外的輸出回調:參數:char*szPathName://路徑名char* szAppName,//檔案名稱_APP_INFO_OUT_CALLBACK pInfoOutCallback:額外的輸出回調**************************************************************************************/CViTrace::CViTrace(char*szPathName,char* szAppName,_APP_INFO_OUT_CALLBACK pInfoOutCallback, void *pInfoOutCallbackParam){m_pInfoOutCallback=pInfoOutCallback;m_pInfoOutCallbackParam=pInfoOutCallbackParam;if (szAppName){GetFullFileName(m_szFileName,MAX_PATH,szPathName,szAppName,"log");}elsem_szFileName[0]=‘\0‘;remove(m_szFileName);}CViTrace::~CViTrace(void){}/*************************************************************************************功能:字串輸出:參數: char* szFormat:可變參數,相似printf的使用**************************************************************************************/UINT CViTrace::trace(char* szFormat,...){ //XAutoLock xLock(m_Lock);char szBuff[VITRACE_BUFFER_LENGTH]={0};FILE *fp=NULL;UINT nListCount=0;va_list pArgList; va_start(pArgList,szFormat);nListCount+= vsnprintf(szBuff+nListCount,VITRACE_BUFFER_LENGTH-nListCount,szFormat,pArgList);va_end(pArgList);if (nListCount>(VITRACE_BUFFER_LENGTH-1)){nListCount = VITRACE_BUFFER_LENGTH-1;}*(szBuff+nListCount)=‘\0‘;nListCount = vi_trace(m_szFileName,"a+",szBuff);//回調輸出;if(m_pInfoOutCallback){char szTime[256];get_timestamp(szTime,256);char tmp[256];sprintf(tmp, "\r\n<%s, tId=%d>\t",szTime,PthreadSelf);char szInfoOut[VITRACE_BUFFER_LENGTH];safe_printf(szInfoOut,VITRACE_BUFFER_LENGTH,"%s%s",tmp,szBuff);m_pInfoOutCallback(szInfoOut,m_pInfoOutCallbackParam);//CON_PRINTF("%s",szInfoOut);}return nListCount;}/******************************************************************************************功能:轉換為16進位和ascii碼輸出:參數:void *Buf:記憶體片地址。UINT nLength:記憶體片長度bool bPrintAsci: false:左側不輸出ascii碼資訊,true:左側輸出ascii碼資訊******************************************************************************************/void CViTrace::trace_bin(char*pBuffer,UINT nLength,bool bPrintAsci){if (nLength>1000){ nLength = 1000;}//XAutoLock xLock(m_Lock);vi_trace_bin(m_szFileName,"a+",pBuffer,nLength,bPrintAsci);}
支援windows linux下將指定記憶體段轉為16進位與ascii碼的日誌輸出類