summaryrefslogtreecommitdiff
path: root/msg.c
blob: 85e8382fde63cb86bef362382c8245d6f519e9f6 (plain)
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
/*   grep-dctrl - grep Debian control files
     Copyright (C) 1999, 2004  Antti-Juhani Kaijanaho
  
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
  
     This program 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; see the file COPYING.  If not, write to
     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
  
     The author can be reached via mail at (ISO 8859-1 charset for the city)
        Antti-Juhani Kaijanaho
        Helokantie 1 A 16
        FIN-40640 JYVÄSKYLÄ
        FINLAND
        EUROPE
     and via electronic mail from
        gaia@iki.fi
     If you have a choice, use the email address; it is more likely to
     stay current.

*/


#define MSG_C__

#include <stdbool.h>

#include <assert.h>
#include <string.h>
#include "msg.h"

#define PROGNAME_MAXLEN 64

bool errors = false;

int loglevel = L_IMPORTANT;
char progname [PROGNAME_MAXLEN];

struct str2int_avec_t {
  const char * str;
  int integer;
};

int
within_interval (int n, int a, int b)
{
  if (a > b)
    {
      int tmp;
      tmp = b;
      b = a;
      a = tmp;
    }

  assert (a <= b);

  return (a <= n && n <= b);
}

void
msg_set_progname (const char * pn)
{
  strncpy (progname, pn, PROGNAME_MAXLEN);
  progname [PROGNAME_MAXLEN - 1] = 0;
}

void
set_loglevel (int ll)
{
  assert (within_interval (loglevel, L_FATAL, L_DEBUG));

  loglevel = ll;
}

int
str2loglevel (const char * s)
{
  static struct str2int_avec_t avec [] = {
    { "fatal", L_FATAL },
    { "important", L_IMPORTANT },
    { "informational", L_INFORMATIONAL },
#if !defined(NDEBUG) && defined(ENABLE_L_DEBUG)
    { "debug", L_DEBUG },
#endif
    { 0, 0 } };

  int i;

  for (i = 0; avec [i].str != 0; i++)
    if (strcasecmp (avec [i].str, s) == 0)
      return avec[i].integer;
  return -1;
}