main: add logging to a file
This is done synchronously, but assumed to be fast enough that it won't cause problems for the event loop. I may try to use the libxev file abstraction in the future but this is the real fast way of implementing it. This codebase is a mess, and that's an endorsement.
This commit is contained in:
parent
e1c54fec91
commit
adc2997f0d
65
src/main.zig
65
src/main.zig
@ -18,6 +18,62 @@ pub const std_options: std.Options = .{
|
|||||||
// these are all in degrees
|
// these are all in degrees
|
||||||
pub const AzEl = struct { az: f64, el: f64 };
|
pub const AzEl = struct { az: f64, el: f64 };
|
||||||
|
|
||||||
|
pub const Log = struct {
|
||||||
|
file: std.fs.File,
|
||||||
|
|
||||||
|
pub fn init() !Log {
|
||||||
|
var buf: [64]u8 = undefined;
|
||||||
|
const now = std.time.milliTimestamp();
|
||||||
|
const filename = try std.fmt.bufPrint(buf[0..], "rotint-{d}.eventlog", .{now});
|
||||||
|
return .{
|
||||||
|
.file = try std.fs.cwd().createFile(filename, .{ .exclusive = true }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writeEvent(self: Log, event: Event) void {
|
||||||
|
var buf: [128]u8 = undefined;
|
||||||
|
var fbs = std.io.fixedBufferStream(buf[0..]);
|
||||||
|
const writer = fbs.writer().any();
|
||||||
|
|
||||||
|
const now = std.time.milliTimestamp();
|
||||||
|
writer.print("{d},", .{now}) catch return;
|
||||||
|
event.write(writer) catch return;
|
||||||
|
writer.writeByte('\n') catch return;
|
||||||
|
self.file.writeAll(fbs.getWritten()) catch return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Event = union(enum) {
|
||||||
|
offset_updated: AzEl,
|
||||||
|
request_updated: AzEl,
|
||||||
|
command_sent: AzEl,
|
||||||
|
position_updated: AzEl,
|
||||||
|
|
||||||
|
pub fn designator(self: Event) []const u8 {
|
||||||
|
return switch (self) {
|
||||||
|
.offset_updated => "O",
|
||||||
|
.request_updated => "R",
|
||||||
|
.command_sent => "C",
|
||||||
|
.position_updated => "P",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(self: Event, writer: std.io.AnyWriter) !void {
|
||||||
|
switch (self) {
|
||||||
|
.offset_updated,
|
||||||
|
.request_updated,
|
||||||
|
.command_sent,
|
||||||
|
.position_updated,
|
||||||
|
=> |pos| {
|
||||||
|
try writer.print(
|
||||||
|
"{s},{d:.1},{d:.1}",
|
||||||
|
.{ self.designator(), pos.az, pos.el },
|
||||||
|
);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
pub const RotInt = struct {
|
pub const RotInt = struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
// TODO: associate timestamps with this somehow
|
// TODO: associate timestamps with this somehow
|
||||||
@ -40,6 +96,8 @@ pub const RotInt = struct {
|
|||||||
poller: xev.Timer,
|
poller: xev.Timer,
|
||||||
poll_completion: xev.Completion = undefined,
|
poll_completion: xev.Completion = undefined,
|
||||||
|
|
||||||
|
log: Log = undefined,
|
||||||
|
|
||||||
pub const State = enum {
|
pub const State = enum {
|
||||||
initial,
|
initial,
|
||||||
rotator_connected,
|
rotator_connected,
|
||||||
@ -50,6 +108,7 @@ pub const RotInt = struct {
|
|||||||
pub fn initInPlace(self: *RotInt) !void {
|
pub fn initInPlace(self: *RotInt) !void {
|
||||||
self.server.rotint = self;
|
self.server.rotint = self;
|
||||||
self.rotator.rotint = self;
|
self.rotator.rotint = self;
|
||||||
|
self.log = try Log.init();
|
||||||
|
|
||||||
const connect_addr = try std.net.Address.parseIp("127.0.0.1", 4533);
|
const connect_addr = try std.net.Address.parseIp("127.0.0.1", 4533);
|
||||||
try self.rotator.connect(self.loop, connect_addr);
|
try self.rotator.connect(self.loop, connect_addr);
|
||||||
@ -130,9 +189,11 @@ pub const RotInt = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
switch (reply) {
|
switch (reply) {
|
||||||
.okay => {},
|
.okay => if (self.last_command == .set_position)
|
||||||
|
self.log.writeEvent(.{ .command_sent = self.last_command.set_position }),
|
||||||
.get_position => |pos| {
|
.get_position => |pos| {
|
||||||
self.current_posture = pos;
|
self.current_posture = pos;
|
||||||
|
self.log.writeEvent(.{ .position_updated = pos });
|
||||||
if (self.state == .rotator_connected) self.stateEvent(.rotator_ready);
|
if (self.state == .rotator_connected) self.stateEvent(.rotator_ready);
|
||||||
self.draw() catch {};
|
self.draw() catch {};
|
||||||
},
|
},
|
||||||
@ -165,6 +226,7 @@ pub const RotInt = struct {
|
|||||||
.set_position => |pos| {
|
.set_position => |pos| {
|
||||||
self.requested_posture = pos;
|
self.requested_posture = pos;
|
||||||
self.server.respond(self.loop, .okay);
|
self.server.respond(self.loop, .okay);
|
||||||
|
self.log.writeEvent(.{ .request_updated = pos });
|
||||||
self.draw() catch {};
|
self.draw() catch {};
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -261,6 +323,7 @@ pub const RotInt = struct {
|
|||||||
|
|
||||||
self.offsets.az += delta.az;
|
self.offsets.az += delta.az;
|
||||||
self.offsets.el += delta.el;
|
self.offsets.el += delta.el;
|
||||||
|
self.log.writeEvent(.{ .offset_updated = self.offsets });
|
||||||
self.draw() catch {
|
self.draw() catch {
|
||||||
self.warn("draw failure", .{});
|
self.warn("draw failure", .{});
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user