Debug Code

Do you need a registry key to do a specific task? Would you like to customize the way your PocketPC/Smartphone looks and works? This is the right place.

Moderators: Teksoft, admin

Debug Code

Postby radhoo » Thu Sep 07, 2006 10:23 pm

A simple debug code that would trace the time and the memory:

Code: Select all
DWORD _getfreememory()
{
   MEMORYSTATUS Buffer;
   GlobalMemoryStatus(&Buffer);
   return Buffer.dwAvailPhys;
}

DWORD nProgramStartMemoryFree = 0;

void _debug(TCHAR *szFormat, ...)
{
   TCHAR szBuffer[10240]; //in this buffer we form the message
   const size_t NUMCHARS = sizeof(szBuffer) / sizeof(szBuffer[0]);
   const int LASTCHAR = NUMCHARS - 1;
   //format the input string
   va_list pArgs;
   va_start(pArgs, szFormat);
   // use a bounded buffer size to prevent buffer overruns.  Limit count to
   // character size minus one to allow for a NULL terminating character.
   _vsntprintf(szBuffer, NUMCHARS - 1, szFormat, pArgs);
   va_end(pArgs);
   //ensure that the formatted string is NULL-terminated
   SYSTEMTIME pSystemTime;
   GetLocalTime( &pSystemTime );
   //get mem
   if (nProgramStartMemoryFree == 0) nProgramStartMemoryFree = _getfreememory();
   //write the file
   FILE *f = _tfopen(TEXT("\\dbg.txt"),TEXT("a"));
   _ftprintf(f, TEXT("\n[%2d:%2d:%2d] [mem: free=%3.3fMB, used=%3.3fMB] >>>\n%s\n"),
      pSystemTime.wHour,pSystemTime.wMinute,pSystemTime.wSecond,
      _getfreememory()/1048576.0,
      (nProgramStartMemoryFree - _getfreememory())/1048576.0,
      szBuffer);
   fclose(f);
}


use it similar to printf:
Code: Select all
int i=2;
TCHAR szTmp[255]=TEXT("RES_OK");
_debug(_T("Test %d: res=%s"), i , szTmp);


Radu
radhoo
TEK-Insider
TEK-Insider
 
Posts: 640
Joined: Fri Mar 17, 2006 2:47 am

Return to Developers

Who is online

Users browsing this forum: No registered users and 1 guest


cron