File: debugmsg.c

package info (click to toggle)
heroes 0.21-8.4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,740 kB
  • sloc: ansic: 27,964; sh: 4,188; makefile: 694; yacc: 318; sed: 51; lisp: 10; perl: 9
file content (128 lines) | stat: -rw-r--r-- 3,456 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*------------------------------------------------------------------------.
| Copyright 2000, 2001  Alexandre Duret-Lutz <duret_g@epita.fr>           |
|                                                                         |
| This file is part of Heroes.                                            |
|                                                                         |
| Heroes is free software; you can redistribute it and/or modify it under |
| the terms of the GNU General Public License version 2 as published by   |
| the Free Software Foundation.                                           |
|                                                                         |
| Heroes is distributed in the hope that it will be useful, but WITHOUT   |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   |
| FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   |
| for more details.                                                       |
|                                                                         |
| You should have received a copy of the GNU General Public License along |
| with this program; if not, write to the Free Software Foundation, Inc., |
| 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                   |
`------------------------------------------------------------------------*/

#include "system.h"
#include "debugmsg.h"
#include "debughash.h"
#include "errors.h"
#include "misc.h"
#include "basename.h"

enum debug_lvl debug_level = 0;
const char* program_name = 0;

#ifndef dmsg

#if defined VA_START
void
dmsg (enum debug_lvl dlvl, const char* msg, ...)
#else
void
dmsg (dlvl, msg, va_alist)
     enum debug_lvl dlvl;
     const char* msg;
     va_dcl;
#endif
{
  if (dlvl & debug_level) {
#ifdef VA_START
    va_list args;
#endif
    fprintf (stdout, "%s: ", program_name);
#ifdef VA_START
    VA_START (args, msg);
# if HAVE_VPRINTF
    vfprintf (stdout, msg, args);
# else
    _doprnt (msg, args, stdout);
# endif /* HAVE_VPRINTF */
    va_end (args);
#else
    fprintf (stdout, msg, va_alist);
#endif /* VA_START */
    putc ('\n', stdout);
    fflush (stdout);
  }
}

void
dperror (const char* s)
{
  if (debug_level) {
    fprintf (stderr, "%s: ", program_name);
    fflush (stderr);
    perror (s);
  }
}

#endif /* !dmsg */

void
dmsg_parse_string (const char* option)
{
  char* buf;

  if (option) {
#ifndef USE_HEROES_DEBUG
    wmsg ("\
Ignoring debug channel specifications: debug messages are not\n\
compiled in.  Install Heroes with 'configure --enable-heroes-debug' if\n\
you want that feature.");
#else
    if (option[0] == '-' || (option[0] >= '0' && option[0] <= '9')) {
      /* the option is a number */
      debug_level = atol (option);
    } else {
      /* the option is a string */
      buf = xstrdup (option);
      strlwr (buf);
      option = strtok (buf, " \t:,|&");
      while (option) {
	int neg = 0;
	int val;

	if (option[0] == '-' || option[0] == '!') {
	  neg = 1;
	  ++option;
	}

	/* lookup the channel number associated to the given name */
	val = get_channel_number (option);

	if (neg)
	  debug_level &= ~val;
	else
	  debug_level |= val;

	option = strtok (0, " \t:,|&");
      }
      free (buf);
    }
    dmsg (D_MISC, "set debug level to %x", debug_level);
#endif /* HEROES_DEBUG */
  }
}

void
dmsg_init (const char* prgname)
{
  program_name = base_name (prgname);

  dmsg_parse_string (getenv ("HEROES_DEBUG"));
}