authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-19 15:49:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-19 15:49:08-07:00
log1af31baf0ba447b6ea5a1456df5ba2d82dc26e56
treea03fbbd5250b28d63188bbeae7e83e0b0487d81d
parent287f640cc94d7f1cddb30e9ef57a8c921621a5b9

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.

4 files changed, 22 insertions(+), 6 deletions(-)

build.zig+2-2
......@@ -134,7 +134,7 @@ pub fn build(b: *Builder) !void {
134134 test_stage2.linkLibC();
135135 }
136136
137 const log_scopes = b.option([]const []const u8, "log", "Which log scopes to enable") orelse &[0][]const u8{};
137 const enable_logging = b.option(bool, "log", "Whether to enable logging") orelse false;
138138
139139 const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git.");
140140 const version = if (opt_version_string) |version| version else v: {
......@@ -190,7 +190,7 @@ pub fn build(b: *Builder) !void {
190190 const semver = try std.SemanticVersion.parse(version);
191191 exe.addBuildOption(std.SemanticVersion, "semver", semver);
192192
193 exe.addBuildOption([]const []const u8, "log_scopes", log_scopes);
193 exe.addBuildOption(bool, "enable_logging", enable_logging);
194194 exe.addBuildOption(bool, "enable_tracy", tracy != null);
195195 exe.addBuildOption(bool, "is_stage1", is_stage1);
196196 exe.addBuildOption(bool, "omit_stage2", false);
src/Compilation.zig+3
......@@ -1560,6 +1560,9 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
15601560 }
15611561 }
15621562
1563 log.debug("calling updateDecl on '{s}', type={}", .{
1564 decl.name, decl.typed_value.most_recent.typed_value.ty,
1565 });
15631566 assert(decl.typed_value.most_recent.typed_value.ty.hasCodeGenBits());
15641567
15651568 self.bin_file.updateDecl(module, decl) catch |err| switch (err) {
src/config.zig.in+1-1
......@@ -1,7 +1,7 @@
11pub const have_llvm = true;
22pub const version: [:0]const u8 = "@ZIG_VERSION@";
33pub const semver = try @import("std").SemanticVersion.parse(version);
4pub const log_scopes: []const []const u8 = &[_][]const u8{};
4pub const enable_logging: bool = false;
55pub const enable_tracy = false;
66pub const is_stage1 = true;
77pub const skip_non_native = false;
src/main.zig+16-3
......@@ -70,20 +70,26 @@ pub const log_level: std.log.Level = switch (std.builtin.mode) {
7070 .ReleaseSmall => .crit,
7171};
7272
73var log_scopes: std.ArrayListUnmanaged([]const u8) = .{};
74
7375pub fn log(
7476 comptime level: std.log.Level,
7577 comptime scope: @TypeOf(.EnumLiteral),
7678 comptime format: []const u8,
7779 args: anytype,
7880) void {
79 // Hide debug messages unless added with `-Dlog=foo`.
81 // Hide debug messages unless:
82 // * logging enabled with `-Dlog`.
83 // * the --debug-log arg for the scope has been provided
8084 if (@enumToInt(level) > @enumToInt(std.log.level) or
8185 @enumToInt(level) > @enumToInt(std.log.Level.info))
8286 {
87 if (!build_options.enable_logging) return;
88
8389 const scope_name = @tagName(scope);
84 const ok = comptime for (build_options.log_scopes) |log_scope| {
90 for (log_scopes.items) |log_scope| {
8591 if (mem.eql(u8, log_scope, scope_name))
86 break true;
92 break;
8793 } else return;
8894 }
8995
......@@ -156,6 +162,8 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
156162 }
157163 }
158164
165 defer log_scopes.deinit(gpa);
166
159167 const cmd = args[1];
160168 const cmd_args = args[2..];
161169 if (mem.eql(u8, cmd, "build-exe")) {
......@@ -358,6 +366,7 @@ const usage_build_generic =
358366 \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR
359367 \\ --verbose-cimport Enable compiler debug output for C imports
360368 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
369 \\ --debug-log [scope] Enable printing debug/info log messages for scope
361370 \\
362371;
363372
......@@ -811,6 +820,10 @@ fn buildOutputType(
811820 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
812821 i += 1;
813822 override_lib_dir = args[i];
823 } else if (mem.eql(u8, arg, "--debug-log")) {
824 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
825 i += 1;
826 try log_scopes.append(gpa, args[i]);
814827 } else if (mem.eql(u8, arg, "-fcompiler-rt")) {
815828 want_compiler_rt = true;
816829 } else if (mem.eql(u8, arg, "-fno-compiler-rt")) {