1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
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
)
40 unsigned int GP_GetDebugLevel(void)
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
,
58 char *level
= getenv("GP_DEBUG");
63 int new_level
= atoi(level
);
66 debug_level
= new_level
;
68 GP_DEBUG(1, "Using debug level GP_DEBUG=%i "
69 "from enviroment variable",
74 GP_DEBUG(1, "GFXprim library version " GP_VER_STR
);
77 if (level
> (int)debug_level
)
80 /* If handler is set, fill struct msg and call it */
86 vsnprintf(buf
, sizeof(buf
), fmt
, va
);
89 struct GP_DebugMsg msg
= {
102 for (i
= 1; i
< level
; i
++)
107 GP_DebugPrintCStack();
108 fprintf(stderr
, "*** FATAL: %s:%s():%u: ", file
, function
, line
);
111 GP_DebugPrintCStack();
112 fprintf(stderr
, "*** BUG: %s:%s():%u: ", file
, function
, line
);
115 fprintf(stderr
, "*** WARNING: %s:%s():%u: ", file
, function
, line
);
118 fprintf(stderr
, "*** TODO: %s:%s():%u: ", file
, function
, line
);
121 fprintf(stderr
, "%u: %s:%s():%u: ",
122 level
, file
, function
, line
);
128 vfprintf(stderr
, fmt
, va
);