spiv: Fix image list counter
[gfxprim.git] / libs / core / GP_Debug.c
blob4fbecdc6d0e2418203d68f11cf713b0ad6d2138f
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim 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 GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <stdarg.h>
24 #include <errno.h>
26 #include "core/GP_Version.h"
27 #include "core/GP_Debug.h"
29 static unsigned int debug_level = GP_DEFAULT_DEBUG_LEVEL;
31 static int env_used = 0;
33 static void (*debug_handler)(const struct GP_DebugMsg *msg) = NULL;
35 void GP_SetDebugLevel(unsigned int level)
37 debug_level = level;
40 unsigned int GP_GetDebugLevel(void)
42 return debug_level;
45 void GP_SetDebugHandler(void (*handler)(const struct GP_DebugMsg *msg))
47 debug_handler = handler;
50 void GP_DebugPrint(int level, const char *file, const char *function, int line,
51 const char *fmt, ...)
53 int i, err;
55 err = errno;
57 if (!env_used) {
58 char *level = getenv("GP_DEBUG");
60 env_used = 1;
62 if (level != NULL) {
63 int new_level = atoi(level);
65 if (new_level >= 0) {
66 debug_level = new_level;
68 GP_DEBUG(1, "Using debug level GP_DEBUG=%i "
69 "from enviroment variable",
70 debug_level);
74 GP_DEBUG(1, "GFXprim library version " GP_VER_STR);
77 if (level > (int)debug_level)
78 goto end;
80 /* If handler is set, fill struct msg and call it */
81 if (debug_handler) {
82 char buf[256];
84 va_list va;
85 va_start(va, fmt);
86 vsnprintf(buf, sizeof(buf), fmt, va);
87 va_end(va);
89 struct GP_DebugMsg msg = {
90 .level = level,
91 .file = file,
92 .fn = function,
93 .line = line,
94 .msg = buf,
97 debug_handler(&msg);
99 goto end;
102 for (i = 1; i < level; i++)
103 fputc(' ', stderr);
105 switch (level) {
106 case GP_DEBUG_FATAL:
107 GP_DebugPrintCStack();
108 fprintf(stderr, "*** FATAL: %s:%s():%u: ", file, function, line);
109 break;
110 case GP_DEBUG_BUG:
111 GP_DebugPrintCStack();
112 fprintf(stderr, "*** BUG: %s:%s():%u: ", file, function, line);
113 break;
114 case GP_DEBUG_WARN:
115 fprintf(stderr, "*** WARNING: %s:%s():%u: ", file, function, line);
116 break;
117 case GP_DEBUG_TODO:
118 fprintf(stderr, "*** TODO: %s:%s():%u: ", file, function, line);
119 break;
120 default:
121 fprintf(stderr, "%u: %s:%s():%u: ",
122 level, file, function, line);
123 break;
126 va_list va;
127 va_start(va, fmt);
128 vfprintf(stderr, fmt, va);
129 va_end(va);
131 fputc('\n', stderr);
132 end:
133 errno = err;