add RESOURCE_LEAVE_LOOKUP which does not need a handle to the holder
[nobug.git] / nobug_env.c
bloba45414dfff6e141aed0eb6e6a62398670b8f53c1
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 void
71 nobug_env_parse_flag (const char* env, struct nobug_flag* flag, int default_target, int default_limit)
73 size_t flaglen = strlen (flag->name);
74 if (!env || !flag->name)
75 return;
79 if (!strncmp(env, flag->name, flaglen))
81 /* flagname matches */
82 int limit;
83 env += flaglen;
84 if (*env == ':')
85 for (limit = 0; limit<11; ++limit)
87 size_t limitlen = strlen (nobug_limits[limit].name);
88 if (!strncmp(env, nobug_limits[limit].name, limitlen))
90 /* found :LIMIT */
91 int target;
92 env += limitlen;
93 if (*env == '@')
94 while (*env == '@')
96 for (target = 0; target<5; ++target)
98 size_t targetlen = strlen (nobug_targets[target]);
99 if (!strncmp(env, nobug_targets[target], targetlen))
101 /* @target found */
102 env += targetlen;
103 /* TODO target options are unimplemented
104 switch (target)
106 case NOBUG_TARGET_RINGBUFFER:
107 // filename flags size
108 case NOBUG_TARGET_CONSOLE:
109 // FILE*
110 case NOBUG_TARGET_FILE:
111 // filename flags
112 case NOBUG_TARGET_SYSLOG:
113 // facility options prefix
114 case NOBUG_TARGET_APPLICATION:
117 flag->limits[target] = nobug_limits[limit].value;
121 else
123 /* flag:LIMIT with no @target */
124 flag->limits[default_target] = nobug_limits[limit].value;
128 else
130 /* flag with no :LIMIT */
131 flag->limits[default_target] = default_limit;
134 env = strchr(env, ',');
135 if (env)
136 ++env;
138 while (env);
142 void
143 nobug_env_init_flag (struct nobug_flag* flag, int default_target, int default_limit)
145 nobug_init();
147 /* set some defaults */
148 flag->ringbuffer_target = nobug_default_ringbuffer;
149 flag->console_target = stderr;
150 flag->file_target = nobug_default_file;
152 /* parse $NOBUG_LOG */
153 nobug_env_parse_flag (getenv("NOBUG_LOG"), flag, default_target, default_limit);
155 /* ensure that the ringbuffer is the most verbose */
156 int i;
157 int max;
159 for (i = NOBUG_TARGET_CONSOLE; i <= NOBUG_TARGET_APPLICATION; ++i)
161 max = max > flag->limits[i] ? max : flag->limits[i];
164 flag->limits[NOBUG_TARGET_RINGBUFFER] = max > flag->limits[NOBUG_TARGET_RINGBUFFER]
165 ? max : flag->limits[NOBUG_TARGET_RINGBUFFER];