2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
32 #ifdef DEBUG_WIN32_CE_MESSAGEBOX
39 int timestamp_prefix=0;
42 static GHashTable *debug_hash;
43 static const char *gdb_program;
45 static FILE *debug_fp;
47 #if defined(_WIN32) || defined(__CEGCC__)
49 static void sigsegv(int sig)
55 static void sigsegv(int sig)
59 sprintf(buffer, "gdb -ex bt %s %d", gdb_program, getpid());
61 sprintf(buffer, "gdb -ex bt -ex detach -ex quit %s %d", gdb_program, getpid());
68 debug_init(const char *program_name)
70 gdb_program=program_name;
71 signal(SIGSEGV, sigsegv);
72 debug_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
78 debug_update_level(gpointer key, gpointer value, gpointer user_data)
80 if (debug_level < (int) value)
81 debug_level=(int) value;
85 debug_level_set(const char *name, int level)
87 if (!strcmp(name, "segv")) {
90 signal(SIGSEGV, sigsegv);
92 signal(SIGSEGV, NULL);
93 } else if (!strcmp(name, "timestamps")) {
94 timestamp_prefix=level;
97 g_hash_table_insert(debug_hash, g_strdup(name), (gpointer) level);
98 g_hash_table_foreach(debug_hash, debug_update_level, NULL);
103 debug_new(struct attr *parent, struct attr **attrs)
105 struct attr *name,*level;
106 name=attr_search(attrs, NULL, attr_name);
107 level=attr_search(attrs, NULL, attr_level);
110 debug_level_set(name->u.str, level->u.num);
111 return (struct debug *)&dummy;
116 debug_level_get(const char *name)
120 return (int)(g_hash_table_lookup(debug_hash, name));
123 static void debug_timestamp(FILE *fp)
127 if (gettimeofday(&tv, NULL) == -1)
129 /* Timestamps are UTC */
131 "%02d:%02d:%02d.%03d|",
132 (int)(tv.tv_sec/3600)%24,
133 (int)(tv.tv_sec/60)%60,
135 (int)tv.tv_usec/1000);
139 debug_vprintf(int level, const char *module, const int mlen, const char *function, const int flen, int prefix, const char *fmt, va_list ap)
141 #ifdef HAVE_API_WIN32_CE
144 char buffer[mlen+flen+3];
148 sprintf(buffer, "%s:%s", module, function);
149 if (debug_level_get(module) >= level || debug_level_get(buffer) >= level) {
150 #ifndef DEBUG_WIN32_CE_MESSAGEBOX
153 if (timestamp_prefix)
156 fprintf(fp,"%s:",buffer);
157 vfprintf(fp,fmt, ap);
164 strcpy(xbuffer,buffer);
168 vsprintf(xbuffer+len,fmt,ap);
169 mbstowcs(muni, xbuffer, strlen(xbuffer)+1);
170 MessageBoxW(NULL, muni, TEXT("Navit - Error"), MB_APPLMODAL|MB_OK|MB_ICONERROR);
176 debug_printf(int level, const char *module, const int mlen,const char *function, const int flen, int prefix, const char *fmt, ...)
180 debug_vprintf(level, module, mlen, function, flen, prefix, fmt, ap);
185 debug_assert_fail(char *module, const int mlen,const char *function, const int flen, char *file, int line, char *expr)
187 debug_printf(0,module,mlen,function,flen,1,"%s:%d assertion failed:%s\n", file, line, expr);
196 if (debug_fp == stderr || debug_fp == stdout)
202 void debug_set_logfile(const char *path)
205 fp = fopen(path, "a");
209 fprintf(debug_fp, "Navit log started\n");