LAL  7.5.0.1-08ee4f4
LALDebugLevel.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2013, 2015 Karl Wette
3 * Copyright (C) 2013 Jolien Creighton, Kipp Cannon
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with with program; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 */
20 
21 /**
22  * \defgroup LALDebugLevel_h Header LALDebugLevel.h
23  * \author Jolien Creighton, Kipp Cannon, Karl Wette
24  * \ingroup lal_std
25  *
26  * \brief Control LALSuite debugging information verbosity and memory debugging features
27 */
28 
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <config.h>
33 #include <lal/LALString.h>
34 #include <lal/LALDebugLevel.h>
35 #include <lal/LALError.h>
36 #undef lalDebugLevel
37 
38 #ifdef LAL_PTHREAD_LOCK
39 #include <pthread.h>
40 static pthread_once_t lalOnce = PTHREAD_ONCE_INIT;
41 #define LAL_ONCE(init) pthread_once(&lalOnce, (init))
42 #else
43 static int lalOnce = 1;
44 #define LAL_ONCE(init) (lalOnce ? (init)(), lalOnce = 0 : 0)
45 #endif
46 
47 /* LAL_DEFAULT_DEBUG_LEVEL is defined in <config.h> by LAL_WITH_DEFAULT_DEBUG_LEVEL() in gnuscripts/lal.m4 */
48 static int lalDebugLevel = LAL_DEFAULT_DEBUG_LEVEL;
49 
50 /*
51  * Bad function, put here only for people who know what they are doing,
52  * (and understand that what they are doing is wrong).
53  */
54 void XLALClobberDebugLevel(int level)
55 {
56  lalDebugLevel = level;
57 }
58 
59 static void XLALSetDebugLevel(void)
60 {
61  int level;
62  char *end;
63  const char *env = getenv("LAL_DEBUG_LEVEL");
64  if (env == NULL || *env == '\0')
65  return;
66 
67  /* parse the environment string */
68 
69  /* see if it is an integer */
70  level = (int) strtol(env, &end, 0);
71  if (*end == '\0') { /* conversion successful */
72  lalDebugLevel = level;
73  return;
74  }
75 
76  /* try to parse as a comma-separated list of debug level names */
77  const char *const seps = ",";
78  const char *token = env;
79  do {
80  size_t toklen = strcspn(token, seps);
81  if (toklen > 0) {
82  if (XLALStringNCaseCompare("NDEBUG", token, toklen) == 0) {
83  level = LALNDEBUG; /* no debugging */
84  } else if (XLALStringNCaseCompare("ERROR", token, toklen) == 0) {
85  level |= LALERROR; /* enable error messages */
86  } else if (XLALStringNCaseCompare("WARNING", token, toklen) == 0) {
87  level |= LALWARNING; /* enable warning messages */
88  } else if (XLALStringNCaseCompare("INFO", token, toklen) == 0) {
89  level |= LALINFO; /* enable info messages */
90  } else if (XLALStringNCaseCompare("TRACE", token, toklen) == 0) {
91  level |= LALTRACE; /* enable tracing messages */
92  } else if (XLALStringNCaseCompare("MSGLVL1", token, toklen) == 0) {
93  level |= LALMSGLVL1; /* enable error messages */
94  } else if (XLALStringNCaseCompare("MSGLVL2", token, toklen) == 0) {
95  level |= LALMSGLVL2; /* enable error and warning messages */
96  } else if (XLALStringNCaseCompare("MSGLVL3", token, toklen) == 0) {
97  level |= LALMSGLVL3; /* enable error, warning, and info messages */
98  } else if (XLALStringNCaseCompare("MEMDBG", token, toklen) == 0) {
99  level |= LALMEMDBG; /* enable memory debugging tools */
100  } else if (XLALStringNCaseCompare("MEMTRACE", token, toklen) == 0) {
101  level |= LALMEMTRACE; /* enable memory tracing tools */
102  } else if (XLALStringNCaseCompare("ALLDBG", token, toklen) == 0) {
103  level |= LALALLDBG; /* enable all debugging */
104  } else {
105  /* not a valid debug level name */
106  lalAbortHook("%s: could not parse LAL_DEBUG_LEVEL='%s'. For help try 'man LAL_DEBUG_LEVEL'.\n", __func__, env);
107  return;
108  }
109  }
110  token += toklen;
111  } while (*(token++) != '\0');
112  lalDebugLevel = level;
113  return;
114 
115 }
116 
118 {
120  return lalDebugLevel;
121 }
static int lalDebugLevel
Definition: LALDebugLevel.c:48
#define LAL_ONCE(init)
Definition: LALDebugLevel.c:44
static int lalOnce
Definition: LALDebugLevel.c:43
static void XLALSetDebugLevel(void)
Definition: LALDebugLevel.c:59
void(* lalAbortHook)(const char *,...)
Definition: LALError.c:75
void XLALClobberDebugLevel(int level)
Definition: LALDebugLevel.c:54
int XLALGetDebugLevel(void)
@ LALTRACE
enable tracing messages
Definition: LALDebugLevel.h:49
@ LALINFO
enable info messages
Definition: LALDebugLevel.h:48
@ LALMEMTRACE
enable memory tracing tools
Definition: LALDebugLevel.h:54
@ LALERROR
enable error messages
Definition: LALDebugLevel.h:46
@ LALWARNING
enable warning messages
Definition: LALDebugLevel.h:47
@ LALNDEBUG
no debug
Definition: LALDebugLevel.h:45
@ LALMEMDBG
enable memory debugging tools
Definition: LALDebugLevel.h:53
@ LALMSGLVL2
enable error and warning messages
Definition: LALDebugLevel.h:51
@ LALMSGLVL1
enable error messages
Definition: LALDebugLevel.h:50
@ LALMSGLVL3
enable error, warning, and info messages
Definition: LALDebugLevel.h:52
@ LALALLDBG
enable all debugging
Definition: LALDebugLevel.h:55
int XLALStringNCaseCompare(const char *s1, const char *s2, size_t n)
Compare the first N characters of two strings, ignoring case and without using locale-dependent funct...
Definition: LALString.c:219