Log output class that supports the conversion of the specified memory segment to 16 and ASCII in Windows Linux

Source: Internet
Author: User
Tags define null sprintf

Source: Http://blog.csdn.net/lezhiyong

1. Introduction

Converts the specified memory segment to a class of 16-in and ASCI-code output to a log file.
2. Function Introduction
1) Supports Windows and Linus dual systems.
2) The output directory can be specified.
3) The log can output the time accurate to milliseconds, the log can output the thread number.
4) Provides a string output.
5) Provides a conversion of the specified memory to 16 binary output.
6) Provides simultaneous output of the specified memory to 16 binary and ASCII codes.
7) 16 binary output spacing can be adjusted.
3. Principles and Algorithms
1) module is based on C + + language;
2) Supports both window and Linus dual systems with conditional definitions
4. Compiling and linking
Adding the source program to the project
5. Operating Environment
Windows, Linux operating system validation is supported at the same time.
6. Conditions of limitation
No
7. Interface
1. Constructors: Cvitrace (char*szpathname,//path name
char* szappname,//file name
_app_info_out_callback pinfooutcallback=null,//Additional output callbacks
void *pinfooutcallbackparam=null);//callback function parameter
/*************************************************************************************
Function: Set the log output path and additional output callbacks:
Parameters:
char*szpathname://path name
char* szappname,//file name
_app_info_out_callback pinfooutcallback: Additional output callback
**************************************************************************************/


3. UINT Trace (char* szformat,...);
/*************************************************************************************
Function: String output:
Parameters:
char* szformat: Variable parameters, similar to the use of printf
**************************************************************************************/


4. Trace_bin (Char*pbuffer,uint nlength,bool bprintasci=false)
/******************************************************************************************
Function: Convert to 16 and ASCII output:
Parameters:
void *buf: Memory slice address.
UINT nlength: Memory piece length
BOOL Bprintasci:false: The left side does not output ASCII code information, true: Left output ASCII code information
******************************************************************************************/


8. Example
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);


Log output:
<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
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 1234567asdfghddffgeerrtyuiopas Df
0x807372c:67676868 6a6a7a78 63766262 626e6e6e 6d6e000d 0a20696e 70757420 77726f6e gghhjjzxcvbbbnnnmn ... input w Ron
0x807374c:67212075 73616765 3a257320 25730000 0d0a2069 6E707574 2077726F 6E672120 g! usage:%s%s .... Input wrong!
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 ("String information output:");
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 ("Output the pre-byte content of the specified memory by the encoded encoding:");
Vilog.trace_bin (szname,50);
Sleep (100);
Vilog.trace ("The output specifies the pre-byte contents of the memory, the left is the binary value, the right is the Assci code:");
Vilog.trace_bin (szname,100,true);
GetChar ();
return 0;
}


<2015-06-09,15:23:18-526, tid=20451738> string information output:
<2015-06-09,15:23:18-526, Tid=20451738>that's why with other Flash applications you just don ' t get the expected ResU  Lts. The best option for each photo.
<2015-06-09,15:23:18-526, tid=20451738> outputs the first 300 bytes of the specified memory by 16 encoding:
<2015-06-09,15:23:18-526, tid=20451738>
Num address:buffercontent
0 01387740:31323334 35363761 73646667 686a6b6c 71776572 74797569 6f706173 64666768
01387760:6a6b6c7a 78637662 6e6d3132 33343536 3761
<2015-06-09,15:23:18-635, the tid=20451738> outputs the first 1000 bytes of the specified memory, the left is a 16 binary value, and the right is the Assci code:
<2015-06-09,15:23:18-635, tid=20451738>
Num address:buffercontent
0 01387740:31323334 35363761 73646667 686a6b6c 71776572 74797569 6f706173 64666768 1234567ASDFGHJKLQWERTYUIOPASDFG H
01387760:6a6b6c7a 78637662 6e6d3132 33343536 37617364 6667686A 6b6c7177 65727479 jklzxcvbnm1234567asdfghjklqwer Ty
01387780:75696f70 61736466 67686a6b 6c7a7863 76626e6d 00000000 00000000 00000000 uiopasdfghjklzxcvbnm ..... ..

013877a0:00000000


/** * @file Vitrace.h * @brief support the log output class of converting the specified memory segment to 16 and ASCII in Windows Linux * * @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 N Dstbuflen,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,//pathname char* szappname,//file name _app_info_ Out_callback pinfooutcallback=null,//Additional output callback void *pinfooutcallbackparam=null);//callback function parameter ~cvitrace (void);p ublic: UINT Trace (char* szformat,...); void Trace_bin (Char*pbuffer,uint nlength,bool bprintasci=false);p ublic://facilitates the application layer to remove output information and enter its own output queue in different output devices _app_info_ Out_callback m_pinfooutcallback;void* M_pinfooutcallbackparam;private:charm_szfilename[max_path];//stitching Good path name and file name//xcritsecm_lock;}; 


/** * @file Vitrace.cpp * @brief support the log output class of converting the specified memory segment to 16 and ASCII in Windows Linux * * @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 buffer size # 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); (Nlistcount> (nDstSize-1)) Nlistcount = ndstsize-1;* (pdst+nlistcount) = ' n '; _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 (&AMP;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);p tm= localtime (&t); nlength= safe_printf (SzBuf,nMaxLength, "%s", Asctime (PTM));  */STRUCT Timeval TV;  struct timezone tz; struct TM *P  Gettimeofday (&AMP;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;} Displays the specified data area contents in ASCII 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 character table can display character codes >32if (32<=* (psrcbuf+i)) {ncount+= safe_printf ( Pdstbuf+ncount,ndstbuflen-ncount, "%c", Psrcbuf[i]);} else{ncount+= safe_printf (Pdstbuf+ncount,ndstbuflen-ncount, ".");}} return ncount;} Displays the specified data area contents in 16 binary 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)//print Vitrace_printbytes_per_line bytes per line, insufficient supplemental space {for (; I<vitrace_ hex_printbytes_perline;i++) {ncount+= safe_printf (Pdstbuf+ncount,ndstbuflen-ncount, "");//A character occupies two bytes j++;if (    VITRACE_HEX_SPACE==J) {j=0;ncount+= safe_printf (Pdstbuf+ncount,ndstbuflen-ncount, "");}}} return ncount;} User-specified output file, file open mode, general "A +", return the total number of output characters 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) = ' + '; Nlistcount = Vi_trace (SZFILENAME,SZMODE,SZBUF); return nlistcount;} *///call function, Vitrace_hex_printbytes_perline byte to one line, format output binary content 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);} /************************************************************************************* function: Set the log output path and additional output callbacks: parameter: Char *szpathname://pathname char* szappname,//file name _app_info_out_callback pinfooutcallback: Additional output callback *************************** /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]= ' + '; remove (m_szfilename);} Cvitrace::~cvitrace (void) {}/********************************************************************************** Function: String output: Parameter: char* szformat: Variable parameter, similar to the use of 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) = ' + '; nlistcount = Vi_trace (M_szfilename, "A +", szbuff);//callback output; if (m_pinfooutcallback) {Char Sztime[256];get_timestamp (sztime,256); charTMP[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;}  /****************************************************************************************** function: Convert to 16 and ASCII output: parameter: void *BUF: Memory chip address. UINT nlength: Memory piece length bool Bprintasci:false: The left side does not output ASCII code information, true: Left output ASCII code information ***************************************** /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);}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Log output class that supports the conversion of the specified memory segment to 16 and ASCII in Windows Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.