Fix:Core:Correct attribute alloc and free
[navit-package] / navit / profile.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <sys/time.h>
25 #include "profile.h"
26 #include "debug.h"
27
28 void
29 profile_timer(int level, const char *module, const char *function, const char *fmt, ...)
30 {
31         va_list ap;
32         static struct timeval last[10];
33         struct timeval curr;
34         int msec,usec;
35         char buffer[strlen(module)+20];
36
37         va_start(ap, fmt);
38         if (level < 0)
39                 level=0;
40         if (level > 9)
41                 level=9;
42         if (fmt) {
43                 gettimeofday(&curr, NULL);
44                 msec=(curr.tv_usec-last[level].tv_usec)/1000+
45                      (curr.tv_sec-last[level].tv_sec)*1000;
46         
47                 sprintf(buffer, "profile:%s", module);
48                 debug_vprintf(1, buffer, strlen(buffer), function, strlen(function), 1, fmt, ap); 
49                 if (msec >= 100) 
50                         debug_printf(1, buffer, strlen(buffer), function, strlen(function), 0, " %d msec\n", msec);
51                 else {
52                         usec=(curr.tv_usec-last[level].tv_usec)+(curr.tv_sec-last[level].tv_sec)*1000*1000;
53                         debug_printf(1, buffer, strlen(buffer), function, strlen(function), 0, " %d.%d msec\n", usec/1000, usec%1000);
54                 }
55                 gettimeofday(&last[level], NULL);
56         } else {
57                 gettimeofday(&curr, NULL);
58                 while (level < 10) 
59                         last[level++]=curr;
60         }
61         va_end(ap);
62 }