diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-01-19 15:49:08 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-01-19 15:49:08 -0700 |
| commit | 1af31baf0ba447b6ea5a1456df5ba2d82dc26e56 (patch) | |
| tree | a03fbbd5250b28d63188bbeae7e83e0b0487d81d /src/main.zig | |
| parent | 287f640cc94d7f1cddb30e9ef57a8c921621a5b9 (diff) | |
| download | zig-1af31baf0ba447b6ea5a1456df5ba2d82dc26e56.tar.gz zig-1af31baf0ba447b6ea5a1456df5ba2d82dc26e56.zip | |
stage2: -Dlog enables all logging, log scopes can be set at runtime
Previously you had to recompile if you wanted to change the log scopes
that get printed. Now, log scopes can be set at runtime, and -Dlog
controls whether all logging is available at runtime.
Purpose here is a nicer development experience. Most likely stage2
developers will always want -Dlog enabled and then pass --debug-log
scopes when debugging particular issues.
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/main.zig b/src/main.zig index 13bea13a5e..8a40c1441c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -70,20 +70,26 @@ pub const log_level: std.log.Level = switch (std.builtin.mode) { .ReleaseSmall => .crit, }; +var log_scopes: std.ArrayListUnmanaged([]const u8) = .{}; + pub fn log( comptime level: std.log.Level, comptime scope: @TypeOf(.EnumLiteral), comptime format: []const u8, args: anytype, ) void { - // Hide debug messages unless added with `-Dlog=foo`. + // Hide debug messages unless: + // * logging enabled with `-Dlog`. + // * the --debug-log arg for the scope has been provided if (@enumToInt(level) > @enumToInt(std.log.level) or @enumToInt(level) > @enumToInt(std.log.Level.info)) { + if (!build_options.enable_logging) return; + const scope_name = @tagName(scope); - const ok = comptime for (build_options.log_scopes) |log_scope| { + for (log_scopes.items) |log_scope| { if (mem.eql(u8, log_scope, scope_name)) - break true; + break; } else return; } @@ -156,6 +162,8 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v } } + defer log_scopes.deinit(gpa); + const cmd = args[1]; const cmd_args = args[2..]; if (mem.eql(u8, cmd, "build-exe")) { @@ -358,6 +366,7 @@ const usage_build_generic = \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR \\ --verbose-cimport Enable compiler debug output for C imports \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features + \\ --debug-log [scope] Enable printing debug/info log messages for scope \\ ; @@ -811,6 +820,10 @@ fn buildOutputType( if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); i += 1; override_lib_dir = args[i]; + } else if (mem.eql(u8, arg, "--debug-log")) { + if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); + i += 1; + try log_scopes.append(gpa, args[i]); } else if (mem.eql(u8, arg, "-fcompiler-rt")) { want_compiler_rt = true; } else if (mem.eql(u8, arg, "-fno-compiler-rt")) { |
