add CPPINIT_FLAG macros to ease flag initialization from C++ programs
[nobug.git] / nobug_env.c
blob043ae3cb8c99dd5f9bd4ad8748c4b42f375016b7
1 /*
2 nobug_env.c - a small debugging library
4 Copyright (C) 2007, Christian Thaeter <ct@pipapo.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
19 #define NOBUG_LIBNOBUG_C
20 #include "nobug.h"
23 Env variable parsing
27 Syntax:
29 logdecl_list --> logdecl, any( ',' logdecl_list)
30 logdecl --> flag, opt(limitdecl, any(targetdecl))
31 flag --> "identifier of a flag"
32 limitdecl --> ':', "LIMITNAME"
33 targetdecl --> '@', "targetname", opt(targetopts)
34 targetopts --> '(', "options for target", ')', opt(targetopts)
36 examples:
37 NOBUG_LOG='flag,other' # set the limit of the 'default' target a default limit
38 NOBUG_LOG='flag:DEBUG' # set the limit of the 'default' target to DEBUG
39 NOBUG_LOG='flag:DEBUG@console@syslog' # set console and syslog limits for flag to DEBUG
40 NOBUG_LOG='flag:DEBUG,other:TRACE@ringbuffer'
42 TODO:
43 (options) on targets are not yet implemented
47 const struct
49 const char * name;
50 int value;
51 } nobug_limits[] =
53 ":EMERG", LOG_EMERG,
54 ":ALERT", LOG_ALERT,
55 ":CRIT", LOG_CRIT,
56 ":ERR", LOG_ERR,
57 ":ERROR", LOG_ERR,
58 ":WARNING", LOG_WARNING,
59 ":WARN", LOG_WARNING,
60 ":NOTICE", LOG_NOTICE,
61 ":INFO", LOG_INFO,
62 ":DEBUG", LOG_DEBUG,
63 ":TRACE", LOG_DEBUG,
66 const char* nobug_targets[] =
67 {"@ringbuffer", "@console", "@file", "@syslog", "@application"};
70 int
71 nobug_env_parse_flag (const char* env, struct nobug_flag* flag, int default_target, int default_limit)
73 int ret = -1;
74 size_t flaglen = strlen (flag->name);
75 if (!env || !flag->name)
76 return ret;
81 if (!strncmp(env, flag->name, flaglen))
83 /* flagname matches */
84 int limit;
85 env += flaglen;
86 if (*env == ':')
87 for (limit = 0; limit<11; ++limit)
89 size_t limitlen = strlen (nobug_limits[limit].name);
90 if (!strncmp(env, nobug_limits[limit].name, limitlen))
92 /* found :LIMIT */
93 int target;
94 env += limitlen;
95 if (*env == '@')
96 while (*env == '@')
98 for (target = 0; target<5; ++target)
100 size_t targetlen = strlen (nobug_targets[target]);
101 if (!strncmp(env, nobug_targets[target], targetlen))
103 /* @target found */
104 env += targetlen;
105 /* TODO target options are unimplemented
106 switch (target)
108 case NOBUG_TARGET_RINGBUFFER:
109 // filename flags size
110 case NOBUG_TARGET_CONSOLE:
111 // FILE*
112 case NOBUG_TARGET_FILE:
113 // filename flags
114 case NOBUG_TARGET_SYSLOG:
115 // facility options prefix
116 case NOBUG_TARGET_APPLICATION:
119 ret = flag->limits[target] = nobug_limits[limit].value;
123 else
125 /* flag:LIMIT with no @target */
126 ret = flag->limits[default_target] = nobug_limits[limit].value;
130 else
132 /* flag with no :LIMIT */
133 ret = flag->limits[default_target] = default_limit;
136 env = strchr(env, ',');
137 if (env)
138 ++env;
140 while (env);
141 return ret;
146 nobug_env_init_flag (struct nobug_flag* flag, int default_target, int default_limit)
148 nobug_init();
150 /* set some defaults */
151 flag->ringbuffer_target = &nobug_default_ringbuffer;
152 flag->console_target = stderr;
153 flag->file_target = nobug_default_file;
155 /* parse $NOBUG_LOG */
156 int ret = nobug_env_parse_flag (getenv("NOBUG_LOG"), flag, default_target, default_limit);
158 /* ensure that the ringbuffer is the most verbose */
159 int i;
160 int max;
162 for (i = NOBUG_TARGET_CONSOLE; i <= NOBUG_TARGET_APPLICATION; ++i)
164 max = max > flag->limits[i] ? max : flag->limits[i];
167 flag->limits[NOBUG_TARGET_RINGBUFFER] = max > flag->limits[NOBUG_TARGET_RINGBUFFER]
168 ? max : flag->limits[NOBUG_TARGET_RINGBUFFER];
169 return ret;