Timeline: Add missing include of unistd.h.
[nondaw.git] / nonlib / debug.C
blobf5275be127efe2a604852af0053dd9e872be596f
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
5 /* This program is free software; you can redistribute it and/or modify it     */
6 /* under the terms of the GNU General Public License as published by the       */
7 /* Free Software Foundation; either version 2 of the License, or (at your      */
8 /* option) any later version.                                                  */
9 /*                                                                             */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       */
12 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   */
13 /* more details.                                                               */
14 /*                                                                             */
15 /* You should have received a copy of the GNU General Public License along     */
16 /* with This program; see the file COPYING.  If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18 /*******************************************************************************/
20 #include "debug.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdarg.h>
26 void
27 warnf ( warning_t level,
28            const char *module,
29            const char *file,
30            const char *function, int line, const char *fmt, ... )
32         va_list args;
33         static const char *level_tab[] = {
34                 "message", "\033[1;32m",
35                 "warning", "\033[1;33m",
36                 "assertion", "\033[1;31m"
37         };
39         if ( module )
40                 fprintf( stderr, "[%s] ", module );
41 #ifndef NDEBUG
42         if ( file )
43                 fprintf( stderr, "%s", file );
44         if ( line )
45                 fprintf( stderr, ":%i", line );
46         if ( function )
47                 fprintf( stderr, " %s()", function );
49         fprintf( stderr, ": " );
50 #endif
52         if ( unsigned( ( level << 1 ) + 1 ) <
53                  ( sizeof( level_tab ) / sizeof( level_tab[0] ) ) )
54                 fprintf( stderr, "%s", level_tab[( level << 1 ) + 1] );
56         if ( fmt )
57         {
58                 va_start( args, fmt );
59                 vfprintf( stderr, fmt, args );
60                 va_end( args );
61         }
63         fprintf( stderr, "\033[0m\n" );