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
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)
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'
43 (options) on targets are not yet implemented
58 ":WARNING", LOG_WARNING
,
60 ":NOTICE", LOG_NOTICE
,
66 const char* nobug_targets
[] =
67 {"@ringbuffer", "@console", "@file", "@syslog", "@application"};
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
)
79 if (!strncmp(env
, flag
->name
, flaglen
))
81 /* flagname matches */
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
))
96 for (target
= 0; target
<5; ++target
)
98 size_t targetlen
= strlen (nobug_targets
[target
]);
99 if (!strncmp(env
, nobug_targets
[target
], targetlen
))
103 /* TODO target options are unimplemented
106 case NOBUG_TARGET_RINGBUFFER:
107 // filename flags size
108 case NOBUG_TARGET_CONSOLE:
110 case NOBUG_TARGET_FILE:
112 case NOBUG_TARGET_SYSLOG:
113 // facility options prefix
114 case NOBUG_TARGET_APPLICATION:
117 flag
->limits
[target
] = nobug_limits
[limit
].value
;
123 /* flag:LIMIT with no @target */
124 flag
->limits
[default_target
] = nobug_limits
[limit
].value
;
130 /* flag with no :LIMIT */
131 flag
->limits
[default_target
] = default_limit
;
134 env
= strchr(env
, ',');
143 nobug_env_init_flag (struct nobug_flag
* flag
, int default_target
, int default_limit
)
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 */
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
];