diff options
| author | Marijn Haverbeke <[email protected]> | 2011-04-17 16:29:18 +0200 |
|---|---|---|
| committer | Marijn Haverbeke <[email protected]> | 2011-04-18 15:28:47 +0200 |
| commit | 148e6f7b00b89850d46624170b82a932d0302575 (patch) | |
| tree | c0639f08db0f51742b368a492afc931cf18f7b3f /src/rt/rust_crate.cpp | |
| parent | collect crate meta info and ext crate names (diff) | |
| download | rust-148e6f7b00b89850d46624170b82a932d0302575.tar.xz rust-148e6f7b00b89850d46624170b82a932d0302575.zip | |
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
Diffstat (limited to 'src/rt/rust_crate.cpp')
| -rw-r--r-- | src/rt/rust_crate.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/rt/rust_crate.cpp b/src/rt/rust_crate.cpp index 23f01c07..e7ee0350 100644 --- a/src/rt/rust_crate.cpp +++ b/src/rt/rust_crate.cpp @@ -65,6 +65,92 @@ rust_crate::get_debug_abbrev(rust_dom *dom) const { return mem_area(dom, 0, 0); } +struct mod_entry { + const char* name; + int* state; +}; + +struct cratemap { + mod_entry* entries; + cratemap* children[1]; +}; + +struct log_directive { + char* name; + size_t level; +}; + +const size_t max_log_directives = 255; + +size_t parse_logging_spec(char* spec, log_directive* dirs) { + size_t dir = 0; + while (dir < max_log_directives && *spec) { + char* start = spec; + char cur; + while (true) { + cur = *spec; + if (cur == ',' || cur == '=' || cur == '\0') { + if (start == spec) {spec++; break;} + *spec = '\0'; + spec++; + size_t level = 3; + if (cur == '=') { + level = *spec - '0'; + if (level > 3) level = 1; + if (*spec) ++spec; + } + dirs[dir].name = start; + dirs[dir++].level = level; + break; + } + spec++; + } + } + return dir; +} + +void update_crate_map(cratemap* map, log_directive* dirs, size_t n_dirs) { + // First update log levels for this crate + for (mod_entry* cur = map->entries; cur->name; cur++) { + size_t level = 1, longest_match = 0; + for (size_t d = 0; d < n_dirs; d++) { + if (strstr(cur->name, dirs[d].name) == cur->name && + strlen(dirs[d].name) > longest_match) { + longest_match = strlen(dirs[d].name); + level = dirs[d].level; + } + } + *cur->state = level; + } + + // Then recurse on linked crates + for (size_t i = 0; map->children[i]; i++) { + update_crate_map(map->children[i], dirs, n_dirs); + } +} + +void rust_crate::update_log_settings(char* settings) const { + // Only try this if the crate was generated by Rustc, not rustboot + if (image_base_off) return; + + // This is a rather ugly parser for strings in the form + // "crate1,crate2.mod3,crate3.x=2". Log levels range 0=err, 1=warn, + // 2=info, 3=debug. Default is 1. Words without an '=X' part set the log + // level for that module (and submodules) to 3. + char* buffer = NULL; + log_directive dirs[256]; + size_t dir = 0; + if (settings) { + buffer = (char*)malloc(strlen(settings)); + strcpy(buffer, settings); + dir = parse_logging_spec(buffer, &dirs[0]); + } + + update_crate_map((cratemap*)crate_map, &dirs[0], dir); + + free(buffer); +} + // // Local Variables: // mode: C++ |