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
)
74 size_t flaglen
= strlen (flag
->name
);
75 if (!env
|| !flag
->name
)
81 if (!strncmp(env
, flag
->name
, flaglen
))
83 /* flagname matches */
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
))
98 for (target
= 0; target
<5; ++target
)
100 size_t targetlen
= strlen (nobug_targets
[target
]);
101 if (!strncmp(env
, nobug_targets
[target
], targetlen
))
105 /* TODO target options are unimplemented
108 case NOBUG_TARGET_RINGBUFFER:
109 // filename flags size
110 case NOBUG_TARGET_CONSOLE:
112 case NOBUG_TARGET_FILE:
114 case NOBUG_TARGET_SYSLOG:
115 // facility options prefix
116 case NOBUG_TARGET_APPLICATION:
119 ret
= flag
->limits
[target
] = nobug_limits
[limit
].value
;
125 /* flag:LIMIT with no @target */
126 ret
= flag
->limits
[default_target
] = nobug_limits
[limit
].value
;
132 /* flag with no :LIMIT */
133 ret
= flag
->limits
[default_target
] = default_limit
;
136 env
= strchr(env
, ',');
146 nobug_env_init_flag (struct nobug_flag
* flag
, int default_target
, int default_limit
)
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 */
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
];