| author | |
| committer | |
| log | 220d3264c929187cb72111fcf75a223f9e4a7399 |
| tree | 9eb71fb86133145d0b962074e13a00d043260bf1 |
| parent | 776cd673f206099012d789fd5d05d49dd72b9faa |
10 files changed, 52 insertions(+), 113 deletions(-)
lib/std/log.zig+3-3| ... | @@ -18,12 +18,12 @@ | ... | @@ -18,12 +18,12 @@ |
| 18 | //! ``` | 18 | //! ``` |
| 19 | //! const std = @import("std"); | 19 | //! const std = @import("std"); |
| 20 | //! | 20 | //! |
| 21 | //! pub const std_options = struct { | 21 | //! pub const std_options = .{ |
| 22 | //! // Set the log level to info | 22 | //! // Set the log level to info |
| 23 | //! pub const log_level = .info; | 23 | //! .log_level = .info, |
| 24 | //! | 24 | //! |
| 25 | //! // Define logFn to override the std implementation | 25 | //! // Define logFn to override the std implementation |
| 26 | //! pub const logFn = myLogFn; | 26 | //! .logFn = myLogFn, |
| 27 | //! }; | 27 | //! }; |
| 28 | //! | 28 | //! |
| 29 | //! pub fn myLogFn( | 29 | //! pub fn myLogFn( |
lib/std/std.zig+21-78| ... | @@ -195,79 +195,35 @@ pub const zig = @import("zig.zig"); | ... | @@ -195,79 +195,35 @@ pub const zig = @import("zig.zig"); |
| 195 | pub const start = @import("start.zig"); | 195 | pub const start = @import("start.zig"); |
| 196 | 196 | ||
| 197 | const root = @import("root"); | 197 | const root = @import("root"); |
| 198 | const options_override = if (@hasDecl(root, "std_options")) root.std_options else struct {}; | ||
| 199 | 198 | ||
| 200 | /// Stdlib-wide options that can be overridden by the root file. | 199 | /// Stdlib-wide options that can be overridden by the root file. |
| 201 | pub const options = struct { | 200 | pub const options: Options = if (@hasDecl(root, "std_options")) root.std_options else .{}; |
| 202 | pub const enable_segfault_handler: bool = if (@hasDecl(options_override, "enable_segfault_handler")) | 201 | |
| 203 | options_override.enable_segfault_handler | 202 | pub const Options = struct { |
| 204 | else | 203 | enable_segfault_handler: bool = debug.default_enable_segfault_handler, |
| 205 | debug.default_enable_segfault_handler; | ||
| 206 | 204 | ||
| 207 | /// Function used to implement `std.fs.cwd` for WASI. | 205 | /// Function used to implement `std.fs.cwd` for WASI. |
| 208 | pub const wasiCwd: fn () fs.Dir = if (@hasDecl(options_override, "wasiCwd")) | 206 | wasiCwd: fn () fs.Dir = fs.defaultWasiCwd, |
| 209 | options_override.wasiCwd | ||
| 210 | else | ||
| 211 | fs.defaultWasiCwd; | ||
| 212 | |||
| 213 | /// The application's chosen I/O mode. | ||
| 214 | pub const io_mode: io.Mode = if (@hasDecl(options_override, "io_mode")) | ||
| 215 | options_override.io_mode | ||
| 216 | else if (@hasDecl(options_override, "event_loop")) | ||
| 217 | .evented | ||
| 218 | else | ||
| 219 | .blocking; | ||
| 220 | |||
| 221 | pub const event_loop: event.Loop.Instance = if (@hasDecl(options_override, "event_loop")) | ||
| 222 | options_override.event_loop | ||
| 223 | else | ||
| 224 | event.Loop.default_instance; | ||
| 225 | |||
| 226 | pub const event_loop_mode: event.Loop.Mode = if (@hasDecl(options_override, "event_loop_mode")) | ||
| 227 | options_override.event_loop_mode | ||
| 228 | else | ||
| 229 | event.Loop.default_mode; | ||
| 230 | 207 | ||
| 231 | /// The current log level. | 208 | /// The current log level. |
| 232 | pub const log_level: log.Level = if (@hasDecl(options_override, "log_level")) | 209 | log_level: log.Level = log.default_level, |
| 233 | options_override.log_level | ||
| 234 | else | ||
| 235 | log.default_level; | ||
| 236 | 210 | ||
| 237 | pub const log_scope_levels: []const log.ScopeLevel = if (@hasDecl(options_override, "log_scope_levels")) | 211 | log_scope_levels: []const log.ScopeLevel = &.{}, |
| 238 | options_override.log_scope_levels | ||
| 239 | else | ||
| 240 | &.{}; | ||
| 241 | 212 | ||
| 242 | pub const logFn: fn ( | 213 | logFn: fn ( |
| 243 | comptime message_level: log.Level, | 214 | comptime message_level: log.Level, |
| 244 | comptime scope: @TypeOf(.enum_literal), | 215 | comptime scope: @TypeOf(.enum_literal), |
| 245 | comptime format: []const u8, | 216 | comptime format: []const u8, |
| 246 | args: anytype, | 217 | args: anytype, |
| 247 | ) void = if (@hasDecl(options_override, "logFn")) | 218 | ) void = log.defaultLog, |
| 248 | options_override.logFn | 219 | |
| 249 | else | 220 | fmt_max_depth: usize = fmt.default_max_depth, |
| 250 | log.defaultLog; | 221 | |
| 251 | 222 | cryptoRandomSeed: fn (buffer: []u8) void = @import("crypto/tlcsprng.zig").defaultRandomSeed, | |
| 252 | pub const fmt_max_depth = if (@hasDecl(options_override, "fmt_max_depth")) | 223 | |
| 253 | options_override.fmt_max_depth | 224 | crypto_always_getrandom: bool = false, |
| 254 | else | 225 | |
| 255 | fmt.default_max_depth; | 226 | crypto_fork_safety: bool = true, |
| 256 | |||
| 257 | pub const cryptoRandomSeed: fn (buffer: []u8) void = if (@hasDecl(options_override, "cryptoRandomSeed")) | ||
| 258 | options_override.cryptoRandomSeed | ||
| 259 | else | ||
| 260 | @import("crypto/tlcsprng.zig").defaultRandomSeed; | ||
| 261 | |||
| 262 | pub const crypto_always_getrandom: bool = if (@hasDecl(options_override, "crypto_always_getrandom")) | ||
| 263 | options_override.crypto_always_getrandom | ||
| 264 | else | ||
| 265 | false; | ||
| 266 | |||
| 267 | pub const crypto_fork_safety: bool = if (@hasDecl(options_override, "crypto_fork_safety")) | ||
| 268 | options_override.crypto_fork_safety | ||
| 269 | else | ||
| 270 | true; | ||
| 271 | 227 | ||
| 272 | /// By default Zig disables SIGPIPE by setting a "no-op" handler for it. Set this option | 228 | /// By default Zig disables SIGPIPE by setting a "no-op" handler for it. Set this option |
| 273 | /// to `true` to prevent that. | 229 | /// to `true` to prevent that. |
| ... | @@ -280,35 +236,22 @@ pub const options = struct { | ... | @@ -280,35 +236,22 @@ pub const options = struct { |
| 280 | /// cases it's unclear why the process was terminated. By capturing SIGPIPE instead, functions that | 236 | /// cases it's unclear why the process was terminated. By capturing SIGPIPE instead, functions that |
| 281 | /// write to broken pipes will return the EPIPE error (error.BrokenPipe) and the program can handle | 237 | /// write to broken pipes will return the EPIPE error (error.BrokenPipe) and the program can handle |
| 282 | /// it like any other error. | 238 | /// it like any other error. |
| 283 | pub const keep_sigpipe: bool = if (@hasDecl(options_override, "keep_sigpipe")) | 239 | keep_sigpipe: bool = false, |
| 284 | options_override.keep_sigpipe | ||
| 285 | else | ||
| 286 | false; | ||
| 287 | 240 | ||
| 288 | /// By default, std.http.Client will support HTTPS connections. Set this option to `true` to | 241 | /// By default, std.http.Client will support HTTPS connections. Set this option to `true` to |
| 289 | /// disable TLS support. | 242 | /// disable TLS support. |
| 290 | /// | 243 | /// |
| 291 | /// This will likely reduce the size of the binary, but it will also make it impossible to | 244 | /// This will likely reduce the size of the binary, but it will also make it impossible to |
| 292 | /// make a HTTPS connection. | 245 | /// make a HTTPS connection. |
| 293 | pub const http_disable_tls = if (@hasDecl(options_override, "http_disable_tls")) | 246 | http_disable_tls: bool = false, |
| 294 | options_override.http_disable_tls | 247 | |
| 295 | else | 248 | side_channels_mitigations: crypto.SideChannelsMitigations = crypto.default_side_channels_mitigations, |
| 296 | false; | ||
| 297 | |||
| 298 | pub const side_channels_mitigations: crypto.SideChannelsMitigations = if (@hasDecl(options_override, "side_channels_mitigations")) | ||
| 299 | options_override.side_channels_mitigations | ||
| 300 | else | ||
| 301 | crypto.default_side_channels_mitigations; | ||
| 302 | }; | 249 | }; |
| 303 | 250 | ||
| 304 | // This forces the start.zig file to be imported, and the comptime logic inside that | 251 | // This forces the start.zig file to be imported, and the comptime logic inside that |
| 305 | // file decides whether to export any appropriate start symbols, and call main. | 252 | // file decides whether to export any appropriate start symbols, and call main. |
| 306 | comptime { | 253 | comptime { |
| 307 | _ = start; | 254 | _ = start; |
| 308 | |||
| 309 | for (@typeInfo(options_override).Struct.decls) |decl| { | ||
| 310 | if (!@hasDecl(options, decl.name)) @compileError("no option named " ++ decl.name); | ||
| 311 | } | ||
| 312 | } | 255 | } |
| 313 | 256 | ||
| 314 | test { | 257 | test { |
lib/test_runner.zig+3-3| ... | @@ -3,9 +3,9 @@ const std = @import("std"); | ... | @@ -3,9 +3,9 @@ const std = @import("std"); |
| 3 | const io = std.io; | 3 | const io = std.io; |
| 4 | const builtin = @import("builtin"); | 4 | const builtin = @import("builtin"); |
| 5 | 5 | ||
| 6 | pub const std_options = struct { | 6 | pub const std_options = .{ |
| 7 | pub const io_mode: io.Mode = builtin.test_io_mode; | 7 | .io_mode = builtin.test_io_mode, |
| 8 | pub const logFn = log; | 8 | .logFn = log, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | var log_err_count: usize = 0; | 11 | var log_err_count: usize = 0; |
src/main.zig+6-6| ... | @@ -29,16 +29,16 @@ const AstGen = @import("AstGen.zig"); | ... | @@ -29,16 +29,16 @@ const AstGen = @import("AstGen.zig"); |
| 29 | const mingw = @import("mingw.zig"); | 29 | const mingw = @import("mingw.zig"); |
| 30 | const Server = std.zig.Server; | 30 | const Server = std.zig.Server; |
| 31 | 31 | ||
| 32 | pub const std_options = struct { | 32 | pub const std_options = .{ |
| 33 | pub const wasiCwd = wasi_cwd; | 33 | .wasiCwd = wasi_cwd, |
| 34 | pub const logFn = log; | 34 | .logFn = log, |
| 35 | pub const enable_segfault_handler = false; | 35 | .enable_segfault_handler = false, |
| 36 | 36 | ||
| 37 | pub const log_level: std.log.Level = switch (builtin.mode) { | 37 | .log_level = switch (builtin.mode) { |
| 38 | .Debug => .debug, | 38 | .Debug => .debug, |
| 39 | .ReleaseSafe, .ReleaseFast => .info, | 39 | .ReleaseSafe, .ReleaseFast => .info, |
| 40 | .ReleaseSmall => .err, | 40 | .ReleaseSmall => .err, |
| 41 | }; | 41 | }, |
| 42 | }; | 42 | }; |
| 43 | 43 | ||
| 44 | // Crash report needs to override the panic handler | 44 | // Crash report needs to override the panic handler |
test/compare_output.zig+8-8| ... | @@ -440,14 +440,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { | ... | @@ -440,14 +440,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 440 | cases.add("std.log per scope log level override", | 440 | cases.add("std.log per scope log level override", |
| 441 | \\const std = @import("std"); | 441 | \\const std = @import("std"); |
| 442 | \\ | 442 | \\ |
| 443 | \\pub const std_options = struct { | 443 | \\pub const std_options = .{ |
| 444 | \\ pub const log_level: std.log.Level = .debug; | 444 | \\ .log_level = .debug, |
| 445 | \\ | 445 | \\ |
| 446 | \\ pub const log_scope_levels = &[_]std.log.ScopeLevel{ | 446 | \\ .log_scope_levels = &.{ |
| 447 | \\ .{ .scope = .a, .level = .warn }, | 447 | \\ .{ .scope = .a, .level = .warn }, |
| 448 | \\ .{ .scope = .c, .level = .err }, | 448 | \\ .{ .scope = .c, .level = .err }, |
| 449 | \\ }; | 449 | \\ }, |
| 450 | \\ pub const logFn = log; | 450 | \\ .logFn = log, |
| 451 | \\}; | 451 | \\}; |
| 452 | \\ | 452 | \\ |
| 453 | \\const loga = std.log.scoped(.a); | 453 | \\const loga = std.log.scoped(.a); |
| ... | @@ -497,9 +497,9 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { | ... | @@ -497,9 +497,9 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 497 | cases.add("std.heap.LoggingAllocator logs to std.log", | 497 | cases.add("std.heap.LoggingAllocator logs to std.log", |
| 498 | \\const std = @import("std"); | 498 | \\const std = @import("std"); |
| 499 | \\ | 499 | \\ |
| 500 | \\pub const std_options = struct { | 500 | \\pub const std_options = .{ |
| 501 | \\ pub const log_level: std.log.Level = .debug; | 501 | \\ .log_level = .debug, |
| 502 | \\ pub const logFn = log; | 502 | \\ .logFn = log, |
| 503 | \\}; | 503 | \\}; |
| 504 | \\ | 504 | \\ |
| 505 | \\pub fn main() !void { | 505 | \\pub fn main() !void { |
test/src/Cases.zig+2-2| ... | @@ -1207,8 +1207,8 @@ const WaitGroup = std.Thread.WaitGroup; | ... | @@ -1207,8 +1207,8 @@ const WaitGroup = std.Thread.WaitGroup; |
| 1207 | const build_options = @import("build_options"); | 1207 | const build_options = @import("build_options"); |
| 1208 | const Package = @import("../../src/Package.zig"); | 1208 | const Package = @import("../../src/Package.zig"); |
| 1209 | 1209 | ||
| 1210 | pub const std_options = struct { | 1210 | pub const std_options = .{ |
| 1211 | pub const log_level: std.log.Level = .err; | 1211 | .log_level = .err, |
| 1212 | }; | 1212 | }; |
| 1213 | 1213 | ||
| 1214 | var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{ | 1214 | var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{ |
test/standalone/http.zig+2-2| ... | @@ -7,8 +7,8 @@ const Client = http.Client; | ... | @@ -7,8 +7,8 @@ const Client = http.Client; |
| 7 | const mem = std.mem; | 7 | const mem = std.mem; |
| 8 | const testing = std.testing; | 8 | const testing = std.testing; |
| 9 | 9 | ||
| 10 | pub const std_options = struct { | 10 | pub const std_options = .{ |
| 11 | pub const http_disable_tls = true; | 11 | .http_disable_tls = true, |
| 12 | }; | 12 | }; |
| 13 | 13 | ||
| 14 | const max_header_size = 8192; | 14 | const max_header_size = 8192; |
test/standalone/issue_7030.zig+2-2| ... | @@ -1,7 +1,7 @@ | ... | @@ -1,7 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | 2 | ||
| 3 | pub const std_options = struct { | 3 | pub const std_options = .{ |
| 4 | pub const logFn = log; | 4 | .logFn = log, |
| 5 | }; | 5 | }; |
| 6 | 6 | ||
| 7 | pub fn log( | 7 | pub fn log( |
test/standalone/issue_9693/main.zig deleted-4| ... | @@ -1,4 +0,0 @@ | ||
| 1 | pub const std_options = struct { | ||
| 2 | pub const io_mode = .evented; | ||
| 3 | }; | ||
| 4 | pub fn main() void {} | ||
test/standalone/sigpipe/breakpipe.zig+5-5| ... | @@ -1,11 +1,11 @@ | ... | @@ -1,11 +1,11 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const build_options = @import("build_options"); | 2 | const build_options = @import("build_options"); |
| 3 | 3 | ||
| 4 | pub const std_options = if (build_options.keep_sigpipe) struct { | 4 | pub usingnamespace if (build_options.keep_sigpipe) struct { |
| 5 | pub const keep_sigpipe = true; | 5 | pub const std_options = .{ |
| 6 | } else struct { | 6 | .keep_sigpipe = true, |
| 7 | // intentionally not setting keep_sigpipe to ensure the default behavior is equivalent to false | 7 | }; |
| 8 | }; | 8 | } else struct {}; |
| 9 | 9 | ||
| 10 | pub fn main() !void { | 10 | pub fn main() !void { |
| 11 | const pipe = try std.os.pipe(); | 11 | const pipe = try std.os.pipe(); |