Knowledge Base Nr: 00015 profilefunktion.cpp - http://www.swe-kaiser.de
Downloads:
profiler-funktion für logfile schreiben
/****** Datum und Zeit in String schreiben ******/
void db_make_timestr(char *str, time_t *clk, int mode)
{
/* Abkürzungen der Monatsnamen für Funktion make_timestr() : */
static char monat[12][4]=
{ "JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC" };
static char mon[12][3]=
{ "01","02","03","04","05","06","07","08","09","10","11","12" };
struct tm *newtime;
char s[30];
newtime=localtime(clk);
strcpy(s,asctime(newtime));
if (mode==1) /* Form: DD-MON-YYYY HH24:MI */
sprintf(str,"%.2s-%s-%.4s %.5s",s+8,monat[newtime->tm_mon],s+20,s+11);
else if (mode==2) /* Form: DD.MN.YYYY HH24:MI:SS */
sprintf(str,"%.2s.%.2s.%.4s %.8s",s+8,mon[newtime->tm_mon],s+20,s+11);
else
printf("INTERNAL ERROR: Wrong mode parameter at db_make_timestr() !\n");
}
/****** Meldung ins Logfile schreiben ******/
void db_profilefunc(FILE *fp, char *szFuncname, int nAction, char *szHint) /* nAction: PROF_START,PROF_END else:unknown */
{
char szTime[20] = {0};
time_t tStart; /* Variable für Systemzeit */
struct timeval now;
static long s_nStart = 0;
long nEnd = 0;
if (0) /* disable/enable profiling */
return;
if (!fp)
return;
time(&tStart); /* Systemzeit holen */
db_make_timestr(szTime, &tStart, 2);
if (nAction == PROF_START) /*start*/
{
gettimeofday(&now, 0);
s_nStart = (now.tv_usec / 1000) + (now.tv_sec * 1000); //ms
fprintf(fp, "### profile start: %s() : %s %s\n", szFuncname, szTime, szHint);
}
else if (nAction == PROF_END) /*end*/
{
gettimeofday(&now, 0);
nEnd = (now.tv_usec / 1000) + (now.tv_sec * 1000); //ms
fprintf(fp, "### profile end: %s() takes %d ms : %s %s\n", szFuncname, nEnd-s_nStart, szTime, szHint);
}
else /*unknown*/
{
fprintf(fp, "profile unknown: %s() : %s %s\n", szFuncname, szTime, szHint);
}
fflush(fp);
}