Skip to content

Commit 07f3a97

Browse files
committed
logging: add more logging
currently there is an issue where `--test` along with a filename will usually cause seamstress to exit prematurely.
1 parent 8d35518 commit 07f3a97

File tree

7 files changed

+61
-20
lines changed

7 files changed

+61
-20
lines changed

lua/core/test.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,10 @@ end
171171

172172
local ok, busted = pcall(require, 'busted')
173173
if ok then
174-
local ret = seamstress.async(runner)
175-
event.addSubscriber({ 'init' }, function()
176-
seamstress.async.Promise(function()
177-
ret():await()
174+
return seamstress.async(function()
175+
seamstress.async.Promise(runner):await()
178176
seamstress:stop()
179-
end)
180-
return false
181177
end)
182-
return ret
183178
end
184179

185180
return function()

src/args.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ const Args = @This();
33
logging: logging.Args,
44
run: Seamstress.RunArgs,
55

6+
pub fn format(args: Args, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
7+
try writer.print("log file: {s}, log level: {s}\n", .{ args.logging.path orelse "default", switch (args.logging.level) {
8+
.err => "error",
9+
.warn => "warning",
10+
.info => "info",
11+
.debug => "debug",
12+
} });
13+
if (args.run.file) |name| {
14+
try writer.print("root lua file: {s}\n", .{name});
15+
}
16+
if (!args.run.tests.run_tests) return;
17+
try writer.writeAll("running tests\n");
18+
const dir = args.run.tests.dir orelse return;
19+
try writer.print("additional tests directory: {s}\n", .{dir});
20+
}
21+
622
pub fn process(cli_args: []const []const u8) Args {
723
var file: ?[]const u8 = null;
824
var logging_arg: logging.Args = .{};

src/async.zig

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,24 +411,44 @@ fn anonCallback(ptr: ?*anyopaque, loop: *xev.Loop, _: *xev.Completion, r: xev.As
411411
return .disarm;
412412
}
413413

414-
pub fn waitForLua(
415-
l: *Lua,
416-
num_args: i32,
417-
) !void {
414+
pub fn waitForLua(l: *Lua, waiting_on: union(enum) {
415+
function_with_num_args: i32,
416+
async_function_with_num_args: i32,
417+
promise: void,
418+
}) !void {
418419
const builtin = @import("builtin");
419420
const a = try xev.Async.init();
420421
const top = if (builtin.mode == .Debug) blk: {
421422
const top = l.getTop();
423+
const num_args: i32 = switch (waiting_on) {
424+
.promise => 0,
425+
.function_with_num_args, .async_function_with_num_args => |n| n,
426+
};
422427
std.debug.assert(top >= num_args + 2 + 1);
423428
break :blk top;
424429
};
425-
lu.load(l, "seamstress.async.Promise");
426-
l.rotate(-num_args - 4, 3); // stack is now: closure closure Promise function ...
427-
try lu.doCall(l, num_args + 1, 1); // stack is now closure closure returned_promise
430+
switch (waiting_on) {
431+
.promise => l.rotate(-3, 2), // stack is now: closure closure promise
432+
.function_with_num_args => |num_args| {
433+
lu.load(l, "seamstress.async.Promise");
434+
l.rotate(-num_args - 4, 3); // stack is now: closure closure Promise function ...
435+
try lu.doCall(l, num_args + 1, 1); // stack is now closure closure returned_promise
436+
},
437+
.async_function_with_num_args => |num_args| {
438+
l.rotate(-num_args - 3, 3); // stack is now: closure closure function ...
439+
try lu.doCall(l, num_args, 1); // stack is now closure closure returned_promise
440+
},
441+
}
428442
const promise: *Promise = l.newUserdata(Promise, 3);
429443
l.pushValue(-1); // copy it
430444
const handle = try l.ref(ziglua.registry_index); // ref pops
431-
defer if (builtin.mode == .Debug) std.debug.assert(l.getTop() + num_args + 2 == top);
445+
defer if (builtin.mode == .Debug) {
446+
const num_args: i32 = switch (waiting_on) {
447+
.promise => 0,
448+
.function_with_num_args, .async_function_with_num_args => |n| n,
449+
};
450+
std.debug.assert(l.getTop() + num_args + 2 == top);
451+
};
432452
const thread = l.newThread();
433453
l.rotate(-5, -2); // stack is now: returned_promise new_promise thread closure closure
434454
l.xMove(thread, 2); // pops closures
@@ -438,8 +458,11 @@ pub fn waitForLua(
438458
.data = .{ .@"async" = a },
439459
};
440460
_ = l.getUserValue(-2, 2) catch unreachable; // get the table of dependent promises
461+
l.len(-1); // get its length
462+
const len = l.toInteger(-1) catch unreachable;
463+
l.pop(1);
441464
l.pushValue(-2); // copy new_promise
442-
l.setIndex(-2, 1); // assign as dependent promise
465+
l.setIndex(-2, len + 1); // assign as dependent promise
443466
l.pop(1); // pop the table
444467
l.rotate(-2, 1); // stack is now new_promise returned_promise
445468
l.setUserValue(-2, 3) catch unreachable;

src/cli.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn stdinCallback2(cli: *Cli, l: *Lua, loop: *xev.Loop, c: *xev.Completion, ptr:
158158
l.pushValue(-2);
159159
l.pushClosure(ziglua.wrap(resumeRepl(.failure)), 1);
160160
l.remove(-3);
161-
try lu.waitForLuaCall(l, 0);
161+
try lu.waitForLuaCall(l, .{ .function_with_num_args = 0 });
162162
l.pop(1); // pop the promise
163163
}
164164

src/env.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ old: switch (builtin.os.tag) {
1212
},
1313

1414
pub fn set(gpa: std.mem.Allocator) ?Env {
15-
var env = Env.init(gpa) catch return null;
15+
var env = Env.init(gpa) catch |err| {
16+
std.log.scoped(.env).warn("environment not set; encountered error: {s}", .{@errorName(err)});
17+
return null;
18+
};
1619
env.old = switch (builtin.os.tag) {
1720
.macos, .linux => std.c.environ,
1821
.windows => std.os.windows.peb().ProcessParameters.Environment,
@@ -29,6 +32,7 @@ pub fn init(gpa: std.mem.Allocator) !Env {
2932
.allocator = gpa,
3033
.argv = &.{ "luarocks", "path" },
3134
}) catch |err| {
35+
std.log.scoped(.env).warn("error running luarocks path: {s}", .{@errorName(err)});
3236
if (err == error.FileNotFound) break :luarocks;
3337
return err;
3438
};
@@ -42,6 +46,7 @@ pub fn init(gpa: std.mem.Allocator) !Env {
4246
const equals = std.mem.indexOfScalar(u8, inner, '=') orelse continue;
4347
const key = inner[0..equals];
4448
const value = inner[equals + 2 .. inner.len - 1];
49+
std.log.scoped(.env).debug("setting {s} to {s}", .{ key, value });
4550
try map.put(key, value);
4651
}
4752
}

src/main.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub fn main() !void {
1111

1212
logging.init(allocator, args.logging);
1313

14+
std.log.scoped(.args).debug("{}", .{args});
15+
1416
defer {
1517
std.process.argsFree(allocator, cli_args);
1618
if (gpa.deinit() == .leak) {
@@ -24,7 +26,7 @@ pub fn main() !void {
2426
const environ = Env.set(allocator);
2527
defer if (environ) |env| env.deinit(allocator);
2628

27-
// handle SIGABRT (called by lua in Debug mode)
29+
// handle SIGABRT (raised by lua in Debug mode)
2830
const using_sigaction = builtin.mode == .Debug and builtin.os.tag != .windows;
2931
if (using_sigaction) setAbrtHandler() catch {};
3032

src/seamstress.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ pub fn main(l: *Lua, run_args: RunArgs) !void {
121121
lua.traceback(lua, error_msg, 1); // local with_stack_trace = debug.traceback(error_msg, 1)
122122
const with_stack_trace = lua.toString(-1) catch unreachable;
123123
@call(.always_inline, std.debug.panic, .{ "lua crashed: {s}", .{with_stack_trace} });
124+
// no need to return anything since std.debug.panic is of type noreturn
124125
}
125-
// no need to return anything since std.debug.panic is of type noreturn
126126
}.panic));
127127
lu.load(l, "seamstress");
128128
const seamstress = l.toUserdata(Seamstress, -1) catch unreachable;

0 commit comments

Comments
 (0)