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 {...@@ -134,7 +134,7 @@ pub fn build(b: *Builder) !void {
134 test_stage2.linkLibC();134 test_stage2.linkLibC();
135 }135 }
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
139 const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git.");139 const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git.");
140 const version = if (opt_version_string) |version| version else v: {140 const version = if (opt_version_string) |version| version else v: {
...@@ -190,7 +190,7 @@ pub fn build(b: *Builder) !void {...@@ -190,7 +190,7 @@ pub fn build(b: *Builder) !void {
190 const semver = try std.SemanticVersion.parse(version);190 const semver = try std.SemanticVersion.parse(version);
191 exe.addBuildOption(std.SemanticVersion, "semver", semver);191 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);
194 exe.addBuildOption(bool, "enable_tracy", tracy != null);194 exe.addBuildOption(bool, "enable_tracy", tracy != null);
195 exe.addBuildOption(bool, "is_stage1", is_stage1);195 exe.addBuildOption(bool, "is_stage1", is_stage1);
196 exe.addBuildOption(bool, "omit_stage2", false);196 exe.addBuildOption(bool, "omit_stage2", false);
src/Compilation.zig+3
...@@ -1560,6 +1560,9 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor...@@ -1560,6 +1560,9 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
1560 }1560 }
1561 }1561 }
15621562
1563 log.debug("calling updateDecl on '{s}', type={}", .{
1564 decl.name, decl.typed_value.most_recent.typed_value.ty,
1565 });
1563 assert(decl.typed_value.most_recent.typed_value.ty.hasCodeGenBits());1566 assert(decl.typed_value.most_recent.typed_value.ty.hasCodeGenBits());
15641567
1565 self.bin_file.updateDecl(module, decl) catch |err| switch (err) {1568 self.bin_file.updateDecl(module, decl) catch |err| switch (err) {
src/config.zig.in+1-1
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1pub const have_llvm = true;1pub const have_llvm = true;
2pub const version: [:0]const u8 = "@ZIG_VERSION@";2pub const version: [:0]const u8 = "@ZIG_VERSION@";
3pub const semver = try @import("std").SemanticVersion.parse(version);3pub const semver = try @import("std").SemanticVersion.parse(version);
4pub const log_scopes: []const []const u8 = &[_][]const u8{};4pub const enable_logging: bool = false;
5pub const enable_tracy = false;5pub const enable_tracy = false;
6pub const is_stage1 = true;6pub const is_stage1 = true;
7pub const skip_non_native = false;7pub 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) {...@@ -70,20 +70,26 @@ pub const log_level: std.log.Level = switch (std.builtin.mode) {
70 .ReleaseSmall => .crit,70 .ReleaseSmall => .crit,
71};71};
7272
73var log_scopes: std.ArrayListUnmanaged([]const u8) = .{};
74
73pub fn log(75pub fn log(
74 comptime level: std.log.Level,76 comptime level: std.log.Level,
75 comptime scope: @TypeOf(.EnumLiteral),77 comptime scope: @TypeOf(.EnumLiteral),
76 comptime format: []const u8,78 comptime format: []const u8,
77 args: anytype,79 args: anytype,
78) void {80) 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
80 if (@enumToInt(level) > @enumToInt(std.log.level) or84 if (@enumToInt(level) > @enumToInt(std.log.level) or
81 @enumToInt(level) > @enumToInt(std.log.Level.info))85 @enumToInt(level) > @enumToInt(std.log.Level.info))
82 {86 {
87 if (!build_options.enable_logging) return;
88
83 const scope_name = @tagName(scope);89 const scope_name = @tagName(scope);
84 const ok = comptime for (build_options.log_scopes) |log_scope| {90 for (log_scopes.items) |log_scope| {
85 if (mem.eql(u8, log_scope, scope_name))91 if (mem.eql(u8, log_scope, scope_name))
86 break true;92 break;
87 } else return;93 } else return;
88 }94 }
8995
...@@ -156,6 +162,8 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v...@@ -156,6 +162,8 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
156 }162 }
157 }163 }
158164
165 defer log_scopes.deinit(gpa);
166
159 const cmd = args[1];167 const cmd = args[1];
160 const cmd_args = args[2..];168 const cmd_args = args[2..];
161 if (mem.eql(u8, cmd, "build-exe")) {169 if (mem.eql(u8, cmd, "build-exe")) {
...@@ -358,6 +366,7 @@ const usage_build_generic =...@@ -358,6 +366,7 @@ const usage_build_generic =
358 \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR366 \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR
359 \\ --verbose-cimport Enable compiler debug output for C imports367 \\ --verbose-cimport Enable compiler debug output for C imports
360 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features368 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
369 \\ --debug-log [scope] Enable printing debug/info log messages for scope
361 \\370 \\
362;371;
363372
...@@ -811,6 +820,10 @@ fn buildOutputType(...@@ -811,6 +820,10 @@ fn buildOutputType(
811 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});820 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
812 i += 1;821 i += 1;
813 override_lib_dir = args[i];822 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]);
814 } else if (mem.eql(u8, arg, "-fcompiler-rt")) {827 } else if (mem.eql(u8, arg, "-fcompiler-rt")) {
815 want_compiler_rt = true;828 want_compiler_rt = true;
816 } else if (mem.eql(u8, arg, "-fno-compiler-rt")) {829 } else if (mem.eql(u8, arg, "-fno-compiler-rt")) {