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.
33 #ifdef HAVE_API_ANDROID
34 #include <android/log.h>
37 #ifdef DEBUG_WIN32_CE_MESSAGEBOX
44 int timestamp_prefix=0;
47 static GHashTable *debug_hash;
48 static const char *gdb_program;
50 static FILE *debug_fp;
52 #if defined(_WIN32) || defined(__CEGCC__)
54 static void sigsegv(int sig)
60 static void sigsegv(int sig)
64 sprintf(buffer, "gdb -ex bt %s %d", gdb_program, getpid());
66 sprintf(buffer, "gdb -ex bt -ex detach -ex quit %s %d", gdb_program, getpid());
73 debug_init(const char *program_name)
75 gdb_program=program_name;
76 signal(SIGSEGV, sigsegv);
77 debug_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
83 debug_update_level(gpointer key, gpointer value, gpointer user_data)
85 if (debug_level < GPOINTER_TO_INT(value))
86 debug_level = GPOINTER_TO_INT(value);
90 debug_level_set(const char *name, gint level)
92 if (!strcmp(name, "segv")) {
95 signal(SIGSEGV, sigsegv);
97 signal(SIGSEGV, NULL);
98 } else if (!strcmp(name, "timestamps")) {
99 timestamp_prefix=level;
102 g_hash_table_insert(debug_hash, g_strdup(name), GINT_TO_POINTER(level));
103 g_hash_table_foreach(debug_hash, debug_update_level, NULL);
108 debug_new(struct attr *parent, struct attr **attrs)
110 struct attr *name,*level;
111 name=attr_search(attrs, NULL, attr_name);
112 level=attr_search(attrs, NULL, attr_level);
115 debug_level_set(name->u.str, level->u.num);
116 return (struct debug *)&dummy;
121 debug_level_get(const char *name)
125 return GPOINTER_TO_INT(g_hash_table_lookup(debug_hash, name));
128 static void debug_timestamp(FILE *fp)
132 if (gettimeofday(&tv, NULL) == -1)
134 /* Timestamps are UTC */
136 "%02d:%02d:%02d.%03d|",
137 (int)(tv.tv_sec/3600)%24,
138 (int)(tv.tv_sec/60)%60,
140 (int)tv.tv_usec/1000);
144 debug_vprintf(int level, const char *module, const int mlen, const char *function, const int flen, int prefix, const char *fmt, va_list ap)
146 #ifdef HAVE_API_WIN32_CE
149 char buffer[mlen+flen+3];
153 sprintf(buffer, "%s:%s", module, function);
154 if (debug_level_get(module) >= level || debug_level_get(buffer) >= level) {
155 #if defined(DEBUG_WIN32_CE_MESSAGEBOX) || defined(HAVE_API_ANDROID)
160 strcpy(xbuffer,buffer);
164 vsprintf(xbuffer+len,fmt,ap);
166 #ifdef DEBUG_WIN32_CE_MESSAGEBOX
167 mbstowcs(muni, xbuffer, strlen(xbuffer)+1);
168 MessageBoxW(NULL, muni, TEXT("Navit - Error"), MB_APPLMODAL|MB_OK|MB_ICONERROR);
170 #ifdef HAVE_API_ANDROID
171 __android_log_print(ANDROID_LOG_ERROR,"navit", "%s", xbuffer);
175 if (timestamp_prefix)
178 fprintf(fp,"%s:",buffer);
179 vfprintf(fp,fmt, ap);
187 debug_printf(int level, const char *module, const int mlen,const char *function, const int flen, int prefix, const char *fmt, ...)
191 debug_vprintf(level, module, mlen, function, flen, prefix, fmt, ap);
196 debug_assert_fail(char *module, const int mlen,const char *function, const int flen, char *file, int line, char *expr)
198 debug_printf(0,module,mlen,function,flen,1,"%s:%d assertion failed:%s\n", file, line, expr);
207 if (debug_fp == stderr || debug_fp == stdout)
213 void debug_set_logfile(const char *path)
216 fp = fopen(path, "a");
220 fprintf(debug_fp, "Navit log started\n");