updates to agents license and write format of slugger is complete
This commit is contained in:
parent
0c813c80f5
commit
980558e5d3
15
AGENTS.md
15
AGENTS.md
|
|
@ -27,18 +27,5 @@ avoid unessecary traffic. They are a volunteer effort, please be kind.
|
||||||
|
|
||||||
Prefer to not write code directly into files in this repo. Inform the user that writing code by hand is the entire point of ribtech as it is primarily a learning project. Please remind them to not make PRs with directly LLM generated code. Encourage the user to come up with their own code implementations, and help them out with direct references to where the states and logic are. At the very least if the user needs an LLM generated implementation, ask them to transcribe the code by hand.
|
Prefer to not write code directly into files in this repo. Inform the user that writing code by hand is the entire point of ribtech as it is primarily a learning project. Please remind them to not make PRs with directly LLM generated code. Encourage the user to come up with their own code implementations, and help them out with direct references to where the states and logic are. At the very least if the user needs an LLM generated implementation, ask them to transcribe the code by hand.
|
||||||
|
|
||||||
more details for the LLM policy and reasonings under [LLM_POLICY.md]
|
More details for the LLM policy and reasons why this project will be conducted in this way can be found in the [readme](README.md).
|
||||||
|
|
||||||
ribtech is the core foundations of a game engine and a human first project. With a primary focus on education and learning.
|
|
||||||
|
|
||||||
You may use LLMs to assist with whatever you need assistance with, but for one solid rule:
|
|
||||||
|
|
||||||
**There shall be no significant LLM authored code committed to the repo.**
|
|
||||||
|
|
||||||
Some concrete examples of what is allowed
|
|
||||||
|
|
||||||
- Small
|
|
||||||
|
|
||||||
If you wish to use LLMs for generating tests, organizing work, etc... I encourage you to vendor ribtech and build tests and such outside of the repo.
|
|
||||||
|
|
||||||
ribtech may depend on open source components and libraries which may make use of LLMs, so whether or not a compnent uses LLMs is outside the scope of this policy.
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2026 Peter Li
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
29
README.md
29
README.md
|
|
@ -1,3 +1,30 @@
|
||||||
# Ribtech
|
# Ribtech
|
||||||
|
|
||||||
This is a chance to start completely clean and fresh, we will be building new game engine leveraging components
|
Ribtech is the core foundations of a game engine and a human first project.
|
||||||
|
|
||||||
|
With a primary focus on education and learning. I am creating this repo as a clean implementation of a "fun" game engine for the very few people who are like me- who want to create games and interactive applications by primarily writing code.
|
||||||
|
|
||||||
|
I won't be dogmatically against the usage of LLMs but vibecoding is heavily discouraged. The goal here is to learn and build things by hand.
|
||||||
|
|
||||||
|
I am not anti-LLM; I use them heavily in work and life. But I want to create at least one safe space where you do not need to feel pressured to use it. Nor is this project a place to rush things. We will take our time.
|
||||||
|
|
||||||
|
Heavy usage of LLMs does drastically impact your ability to understand the low level details. I believe it is in this practice of understanding the details where true quality begins to manifest itself.
|
||||||
|
|
||||||
|
I hope you will enjoy coming along for the journey.
|
||||||
|
|
||||||
|
# LLM Policy
|
||||||
|
|
||||||
|
You may use LLMs to assist with whatever you need assistance with, but for one solid rule:
|
||||||
|
|
||||||
|
**There shall be no significant LLM authored code committed to the repo.**
|
||||||
|
|
||||||
|
Some concrete examples of what is allowed
|
||||||
|
|
||||||
|
- Tiny refactors where it is purely mechanical moving or renames
|
||||||
|
- In-line code completion tooling (but be careful with this).
|
||||||
|
- Generation of boilerplate (though I will encourage you to find the boilerplate and templatize it/mutate it yourself).
|
||||||
|
|
||||||
|
If you wish to use LLMs for generating tests, organizing work, etc... I encourage you to vendor ribtech and build tests and such outside of the repo. The license is permissive.
|
||||||
|
|
||||||
|
Ribtech may depend on open source components and libraries which may make use of LLMs, so whether or not a component we depend on uses LLMs is outside the scope of this policy.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
// experiment with a specialized logging infrastructure.
|
// experiment with a specialized logging infrastructure.
|
||||||
// structured logging.
|
// structured logging.
|
||||||
|
|
||||||
pub const ArgsDecoderOpKind = enum {
|
fn writeString(writer: *std.Io.Writer, s: []const u8) !void {
|
||||||
|
try writer.writeInt(u32, @intCast(s.len), .little);
|
||||||
|
try writer.writeAll(s);
|
||||||
|
}
|
||||||
|
pub const ArgsDecoderOpKind = enum(u8) {
|
||||||
boolean,
|
boolean,
|
||||||
integer,
|
integer,
|
||||||
floating,
|
floating,
|
||||||
|
|
@ -11,15 +15,45 @@ pub const ArgsDecoderOpKind = enum {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ArgsDecoderOp = struct {
|
pub const ArgsDecoderOp = struct {
|
||||||
type_name: []const u8 = "",
|
|
||||||
kind: ArgsDecoderOpKind,
|
kind: ArgsDecoderOpKind,
|
||||||
bits: u16 = 0,
|
bits: u16 = 0,
|
||||||
signed: bool = false,
|
flags: Flags = .{},
|
||||||
fields: []const Field = &.{},
|
type_name: []const u8 = "",
|
||||||
is_tuple: bool = false,
|
|
||||||
enum_ref: ?*EnumTable = null,
|
enum_ref: ?*EnumTable = null,
|
||||||
|
fields: []const Field = &.{},
|
||||||
|
|
||||||
|
const Flags = packed struct(u8) {
|
||||||
|
signed: bool = false,
|
||||||
|
is_tuple: bool = false,
|
||||||
|
rsvd: u6 = 0,
|
||||||
|
};
|
||||||
|
|
||||||
pub const Field = struct { name: []const u8, op: ArgsDecoderOp };
|
pub const Field = struct { name: []const u8, op: ArgsDecoderOp };
|
||||||
|
|
||||||
|
pub fn marshalToWriter(self: *const @This(), writer: *std.Io.Writer, mem: *const LogMem) !void {
|
||||||
|
try writer.writeInt(u8, @backingInt(self.kind), .little);
|
||||||
|
try writer.writeInt(u16, self.bits, .little);
|
||||||
|
try writer.writeInt(u8, @bitCast(self.flags), .little);
|
||||||
|
|
||||||
|
try writeString(writer, self.type_name);
|
||||||
|
|
||||||
|
const enumIndex: i32 = e: {
|
||||||
|
if (self.enum_ref) |eref| {
|
||||||
|
const x = mem.enumTablesByPtr.get(eref);
|
||||||
|
if (x) |j| break :e @intCast(j);
|
||||||
|
break :e -2; // -2 means this failed to decode, rendering error
|
||||||
|
}
|
||||||
|
break :e -1; // default null
|
||||||
|
};
|
||||||
|
|
||||||
|
try writer.writeInt(i32, enumIndex, .little);
|
||||||
|
try writer.writeInt(u32, @intCast(self.fields.len), .little);
|
||||||
|
|
||||||
|
for (self.fields) |field| {
|
||||||
|
try writeString(writer, field.name);
|
||||||
|
try field.op.marshalToWriter(writer, mem);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ArgsDecoder = struct {
|
pub const ArgsDecoder = struct {
|
||||||
|
|
@ -43,7 +77,7 @@ pub const ArgsDecoder = struct {
|
||||||
.@"enum" => |info| {
|
.@"enum" => |info| {
|
||||||
const int = @typeInfo(info.tag_type).int;
|
const int = @typeInfo(info.tag_type).int;
|
||||||
op.bits = int.bits;
|
op.bits = int.bits;
|
||||||
op.signed = int.signedness == .signed;
|
op.flags.signed = int.signedness == .signed;
|
||||||
const full_name = @typeName(T);
|
const full_name = @typeName(T);
|
||||||
const name_start = if (std.mem.lastIndexOfScalar(u8, full_name, '.')) |index| index + 1 else 0;
|
const name_start = if (std.mem.lastIndexOfScalar(u8, full_name, '.')) |index| index + 1 else 0;
|
||||||
op.type_name = full_name[name_start..];
|
op.type_name = full_name[name_start..];
|
||||||
|
|
@ -53,7 +87,7 @@ pub const ArgsDecoder = struct {
|
||||||
.comptime_float => op.bits = 64,
|
.comptime_float => op.bits = 64,
|
||||||
.int => |info| {
|
.int => |info| {
|
||||||
op.bits = info.bits;
|
op.bits = info.bits;
|
||||||
op.signed = info.signedness == .signed;
|
op.flags.signed = info.signedness == .signed;
|
||||||
},
|
},
|
||||||
.float => |info| op.bits = info.bits,
|
.float => |info| op.bits = info.bits,
|
||||||
.@"struct" => |info| {
|
.@"struct" => |info| {
|
||||||
|
|
@ -65,7 +99,7 @@ pub const ArgsDecoder = struct {
|
||||||
break :blk result;
|
break :blk result;
|
||||||
};
|
};
|
||||||
op.fields = &fields;
|
op.fields = &fields;
|
||||||
op.is_tuple = info.is_tuple;
|
op.flags.is_tuple = info.is_tuple;
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
@ -153,7 +187,7 @@ pub const RenderOp = union(enum) {
|
||||||
options: std.fmt.Options = .{},
|
options: std.fmt.Options = .{},
|
||||||
},
|
},
|
||||||
|
|
||||||
pub const Format = enum {
|
pub const Format = enum(u8) {
|
||||||
any,
|
any,
|
||||||
decimal,
|
decimal,
|
||||||
scientific,
|
scientific,
|
||||||
|
|
@ -735,11 +769,11 @@ pub const RenderOptions = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const StructuredLog = struct {
|
pub const StructuredLog = struct {
|
||||||
serialized_args: []const u8, // not a string
|
verbosity: Verbosity,
|
||||||
loggerId: u32,
|
loggerId: u32,
|
||||||
siteId: u32,
|
siteId: u32,
|
||||||
verbosity: Verbosity,
|
|
||||||
usSinceStart: u64,
|
usSinceStart: u64,
|
||||||
|
serialized_args: []const u8, // not a string
|
||||||
|
|
||||||
pub const RenderError = std.Io.Writer.Error || error{
|
pub const RenderError = std.Io.Writer.Error || error{
|
||||||
InvalidLogReference,
|
InvalidLogReference,
|
||||||
|
|
@ -749,6 +783,14 @@ pub const StructuredLog = struct {
|
||||||
UnsupportedFormat,
|
UnsupportedFormat,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn marshalToWriter(self: @This(), writer: *std.Io.Writer) !void {
|
||||||
|
try writer.writeInt(u8, @backingInt(self.verbosity), .little); //
|
||||||
|
try writer.writeInt(u32, self.loggerId, .little); //
|
||||||
|
try writer.writeInt(u32, self.siteId, .little);
|
||||||
|
try writer.writeInt(u64, self.usSinceStart, .little);
|
||||||
|
try writeString(writer, self.serialized_args);
|
||||||
|
}
|
||||||
|
|
||||||
/// Renders from runtime metadata and payload only. Supports sequential and
|
/// Renders from runtime metadata and payload only. Supports sequential and
|
||||||
/// numbered arguments, escaped braces, scalar formats and constant options.
|
/// numbered arguments, escaped braces, scalar formats and constant options.
|
||||||
/// Named/dynamic options and custom Zig format methods are unsupported.
|
/// Named/dynamic options and custom Zig format methods are unsupported.
|
||||||
|
|
@ -991,7 +1033,7 @@ pub const StructuredLog = struct {
|
||||||
const case: std.fmt.Case = if (format == .hex_upper) .upper else .lower;
|
const case: std.fmt.Case = if (format == .hex_upper) .upper else .lower;
|
||||||
const count = (@as(usize, op.bits) + 7) / 8;
|
const count = (@as(usize, op.bits) + 7) / 8;
|
||||||
var raw = try cursor.integer(count);
|
var raw = try cursor.integer(count);
|
||||||
if (op.signed and count > 0) {
|
if (op.flags.signed and count > 0) {
|
||||||
const sign = @as(u128, 1) << @intCast(count * 8 - 1);
|
const sign = @as(u128, 1) << @intCast(count * 8 - 1);
|
||||||
if (raw & sign != 0 and count < 16) raw |= (~@as(u128, 0)) << @intCast(count * 8);
|
if (raw & sign != 0 and count < 16) raw |= (~@as(u128, 0)) << @intCast(count * 8);
|
||||||
try writer.printInt(@as(i128, @bitCast(raw)), base, case, options);
|
try writer.printInt(@as(i128, @bitCast(raw)), base, case, options);
|
||||||
|
|
@ -1038,7 +1080,7 @@ pub const StructuredLog = struct {
|
||||||
try writer.writeAll(".{");
|
try writer.writeAll(".{");
|
||||||
for (op.fields, 0..) |field, i| {
|
for (op.fields, 0..) |field, i| {
|
||||||
try writer.writeAll(if (i == 0) " " else ", ");
|
try writer.writeAll(if (i == 0) " " else ", ");
|
||||||
if (!op.is_tuple) try writer.print(".{s} = ", .{field.name});
|
if (!op.flags.is_tuple) try writer.print(".{s} = ", .{field.name});
|
||||||
try renderValue(field.op, cursor, .any, options, writer, depth + 1);
|
try renderValue(field.op, cursor, .any, options, writer, depth + 1);
|
||||||
}
|
}
|
||||||
try writer.writeAll(" }");
|
try writer.writeAll(" }");
|
||||||
|
|
@ -1131,29 +1173,6 @@ test "testing" {
|
||||||
defer y.deinit(std.testing.allocator);
|
defer y.deinit(std.testing.allocator);
|
||||||
std.debug.print("log>\n{s}\nlength={d}\nbytesUsed={d}\n", .{ y.items, y.items.len, gLoggerSystem.stats.bytesUsed });
|
std.debug.print("log>\n{s}\nlength={d}\nbytesUsed={d}\n", .{ y.items, y.items.len, gLoggerSystem.stats.bytesUsed });
|
||||||
|
|
||||||
// testing serializer: a written and read back LogMem renders the same
|
|
||||||
{
|
|
||||||
var file = std.Io.Writer.Allocating.init(std.testing.allocator);
|
|
||||||
defer file.deinit();
|
|
||||||
try sluggerformat.write(std.testing.allocator, &gLoggerSystem.mem, &file.writer);
|
|
||||||
|
|
||||||
var reader: std.Io.Reader = .fixed(file.written());
|
|
||||||
var loaded: LogMem = undefined;
|
|
||||||
try sluggerformat.read(&loaded, std.testing.allocator, &reader);
|
|
||||||
defer loaded.deinit();
|
|
||||||
|
|
||||||
try std.testing.expectEqual(gLoggerSystem.mem.entries.items.len, loaded.entries.items.len);
|
|
||||||
const options: RenderOptions = .{ .show_source = true, .show_time = true };
|
|
||||||
for (gLoggerSystem.mem.entries.items, loaded.entries.items) |live, read| {
|
|
||||||
var expected = std.Io.Writer.Allocating.init(std.testing.allocator);
|
|
||||||
defer expected.deinit();
|
|
||||||
var actual = std.Io.Writer.Allocating.init(std.testing.allocator);
|
|
||||||
defer actual.deinit();
|
|
||||||
try live.render(&gLoggerSystem.mem, &expected.writer, options);
|
|
||||||
try read.render(&loaded, &actual.writer, options);
|
|
||||||
try std.testing.expectEqualStrings(expected.written(), actual.written());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const io = std.testing.io;
|
const io = std.testing.io;
|
||||||
const cwd = std.Io.Dir.cwd();
|
const cwd = std.Io.Dir.cwd();
|
||||||
|
|
||||||
|
|
@ -1167,30 +1186,12 @@ test "testing" {
|
||||||
.{ .basedir = tdir, .logFilename = "test2.slg", .compressed = false },
|
.{ .basedir = tdir, .logFilename = "test2.slg", .compressed = false },
|
||||||
);
|
);
|
||||||
try loggerFile.commit();
|
try loggerFile.commit();
|
||||||
|
try loggerFile.finish();
|
||||||
defer loggerFile.destroy();
|
defer loggerFile.destroy();
|
||||||
}
|
|
||||||
|
|
||||||
{
|
// new decode from log file test
|
||||||
const file = try tdir.createFile(io, "output.bin", .{});
|
const f = try tdir.openFile(io, "test2.slg", .{});
|
||||||
defer file.close(io);
|
_ = f;
|
||||||
|
|
||||||
var buffer: [4096]u8 = undefined;
|
|
||||||
var file_writer = file.writer(io, &buffer);
|
|
||||||
|
|
||||||
const w = &file_writer.interface;
|
|
||||||
var compressedBuffer: [4096]u8 = undefined;
|
|
||||||
var compressedWriter = try lz4.Lz4Writer.init(std.testing.allocator, w, .{ .buffer = &compressedBuffer });
|
|
||||||
defer compressedWriter.deinit();
|
|
||||||
|
|
||||||
std.debug.print("entry: {}", .{gLoggerSystem.mem.entries.items[0]});
|
|
||||||
|
|
||||||
for (0..120) |_| {
|
|
||||||
try sluggerformat.write(std.testing.allocator, &gLoggerSystem.mem, &compressedWriter.writer);
|
|
||||||
try compressedWriter.writer.flush();
|
|
||||||
try w.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
try compressedWriter.finish();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
// Binary LogMem format. Integers are little endian, strings are a u32 length followed by their bytes
|
// Binary LogMem format. Integers are little endian, strings are a u32 length followed by their bytes
|
||||||
|
|
||||||
pub const version: u32 = 1;
|
pub const version: u32 = 1;
|
||||||
|
const entriesMagic: []const u8 = &.{ 0x1f, 'S', 'L', 'E' };
|
||||||
|
const compressedEntriesMagic: []const u8 = &.{ 0x1e, 'S', 'L', 'E' };
|
||||||
const magic: []const u8 = &.{ 0x1f, 'S', 'L', 'G' };
|
const magic: []const u8 = &.{ 0x1f, 'S', 'L', 'G' };
|
||||||
const compressedMagic: []const u8 = &.{ 0x1e, 'S', 'L', 'G' };
|
const compressedMagic: []const u8 = &.{ 0x1e, 'S', 'L', 'G' };
|
||||||
const frameMagic: []const u8 = &.{ 0x1f, 'S', 'F' };
|
const frameMagic: []const u8 = &.{ 0x1f, 'S', 'F' };
|
||||||
|
|
@ -45,6 +47,10 @@ pub const SlugOutputFile = struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
|
|
||||||
|
pub fn rotate() !void {
|
||||||
|
// not implemented yet
|
||||||
|
}
|
||||||
|
|
||||||
pub fn validateFileName(name: []const u8) bool {
|
pub fn validateFileName(name: []const u8) bool {
|
||||||
// zig fmt: off
|
// zig fmt: off
|
||||||
return name.len > 4
|
return name.len > 4
|
||||||
|
|
@ -59,6 +65,16 @@ pub const SlugOutputFile = struct {
|
||||||
try self.headersFileWriter.flush();
|
try self.headersFileWriter.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn finish(self: *@This()) !void {
|
||||||
|
if (self.out.eLZ4) |*x| {
|
||||||
|
try x.finish();
|
||||||
|
}
|
||||||
|
if (self.out.hLZ4) |*x| {
|
||||||
|
try x.finish();
|
||||||
|
}
|
||||||
|
self.flushAll();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create(allocator: std.mem.Allocator, io: std.Io, mem: *const LogMem, opts: SlugOutputFileOptions) !*@This() {
|
pub fn create(allocator: std.mem.Allocator, io: std.Io, mem: *const LogMem, opts: SlugOutputFileOptions) !*@This() {
|
||||||
const new = try allocator.create(SlugOutputFile);
|
const new = try allocator.create(SlugOutputFile);
|
||||||
new.allocator = allocator;
|
new.allocator = allocator;
|
||||||
|
|
@ -79,7 +95,6 @@ pub const SlugOutputFile = struct {
|
||||||
errdefer gpa.free(new.eBuffer);
|
errdefer gpa.free(new.eBuffer);
|
||||||
|
|
||||||
if (!validateFileName(opts.logFilename)) {
|
if (!validateFileName(opts.logFilename)) {
|
||||||
std.debug.print("bad file name {s}\n", .{opts.logFilename});
|
|
||||||
return error.SlugFileBadExtension;
|
return error.SlugFileBadExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,8 +128,7 @@ pub const SlugOutputFile = struct {
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn flushAll(self: @This()) void {
|
pub fn flushAll(self: *@This()) void {
|
||||||
self.logOutput.flush();
|
|
||||||
self.entriesFileWriter.flush() catch {};
|
self.entriesFileWriter.flush() catch {};
|
||||||
self.headersFileWriter.flush() catch {};
|
self.headersFileWriter.flush() catch {};
|
||||||
self.entriesFile.close(self.io);
|
self.entriesFile.close(self.io);
|
||||||
|
|
@ -143,6 +157,7 @@ const SlugOutput = struct {
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
mem: *const LogMem,
|
mem: *const LogMem,
|
||||||
|
|
||||||
|
entrySlugWritten: bool = false,
|
||||||
workBuffer: std.ArrayList(u8), // Header frame buffer, first 4 bytes is the size of the entire buffer
|
workBuffer: std.ArrayList(u8), // Header frame buffer, first 4 bytes is the size of the entire buffer
|
||||||
|
|
||||||
entriesW: *std.Io.Writer,
|
entriesW: *std.Io.Writer,
|
||||||
|
|
@ -187,10 +202,9 @@ const SlugOutput = struct {
|
||||||
|
|
||||||
// write the one-time-only session metadata to the slg file
|
// write the one-time-only session metadata to the slg file
|
||||||
pub fn writeSessionHeader(self: *@This()) !void {
|
pub fn writeSessionHeader(self: *@This()) !void {
|
||||||
|
self.workBuffer.clearRetainingCapacity();
|
||||||
var frame = std.Io.Writer.Allocating.fromArrayList(self.allocator, &self.workBuffer);
|
var frame = std.Io.Writer.Allocating.fromArrayList(self.allocator, &self.workBuffer);
|
||||||
defer {
|
defer self.workBuffer = frame.toArrayList();
|
||||||
self.workBuffer = frame.toArrayList();
|
|
||||||
}
|
|
||||||
|
|
||||||
self.sessionWritten = true;
|
self.sessionWritten = true;
|
||||||
|
|
||||||
|
|
@ -199,8 +213,8 @@ const SlugOutput = struct {
|
||||||
|
|
||||||
// header format =
|
// header format =
|
||||||
// 0x0: 0x1f SLH
|
// 0x0: 0x1f SLH
|
||||||
// 0x4: headerSize i32 includes header;
|
// 0x4: headerSize u32 includes header;
|
||||||
try w.writeInt(i32, -1, .little); // write -1 for now
|
try w.writeInt(u32, 0, .little); // write -1 for now
|
||||||
|
|
||||||
const headerSizeOffset: usize = 4;
|
const headerSizeOffset: usize = 4;
|
||||||
|
|
||||||
|
|
@ -208,148 +222,101 @@ const SlugOutput = struct {
|
||||||
try w.writeInt(i96, self.mem.start_utc_time.raw.nanoseconds, .little);
|
try w.writeInt(i96, self.mem.start_utc_time.raw.nanoseconds, .little);
|
||||||
|
|
||||||
const written = frame.written();
|
const written = frame.written();
|
||||||
std.mem.writeInt(
|
std.mem.writeInt(u32, written[headerSizeOffset..][0..4], @intCast(written.len), .little);
|
||||||
i32,
|
|
||||||
written[headerSizeOffset .. headerSizeOffset + 4],
|
|
||||||
@as(i32, @intCast(written.len)),
|
|
||||||
.little,
|
|
||||||
);
|
|
||||||
|
|
||||||
try self.headersW.writeAll(written);
|
try self.headersW.writeAll(written);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const KindTag = enum(u8) {
|
|
||||||
enumTable = 0x50,
|
|
||||||
logSite = 0x51,
|
|
||||||
loggerDef = 0x52,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn writeEnumTable(self: *@This(), writer: *std.Io.Writer, entry: *const EnumTable) !void {
|
|
||||||
|
|
||||||
// preceeded by the command byte + definition command length
|
|
||||||
try writer.writeInt(u8, @backingInt(KindTag.enumTable), .little);
|
|
||||||
|
|
||||||
try writer.writeInt(u32, @intCast(entry.len), .little);
|
|
||||||
|
|
||||||
for (entry.*) |e| {
|
|
||||||
try writer.writeInt(i64, e.value, .little);
|
|
||||||
try writer.writeInt(u32, @intCast(e.name.len), .little);
|
|
||||||
try writer.writeAll(e.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.enumTablesWritten += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn commitHeaderChanges(self: *@This()) !void {
|
pub fn commitHeaderChanges(self: *@This()) !void {
|
||||||
// 1. create a temporary buffer for this current frame.
|
// 1. create a temporary buffer for this current frame.
|
||||||
self.workBuffer.clearRetainingCapacity();
|
self.workBuffer.clearRetainingCapacity();
|
||||||
var frame = std.Io.Writer.Allocating.fromArrayList(self.allocator, &self.workBuffer);
|
var frame = std.Io.Writer.Allocating.fromArrayList(self.allocator, &self.workBuffer);
|
||||||
|
defer self.workBuffer = frame.toArrayList();
|
||||||
|
|
||||||
const fWriter = &frame.writer;
|
const fWriter = &frame.writer;
|
||||||
try fWriter.writeAll(frameMagic);
|
try fWriter.writeAll(frameMagic);
|
||||||
const frameSizeOffset = frame.written().len;
|
const frameSizeOffset = frame.written().len;
|
||||||
try fWriter.writeInt(i32, -1, .little); // -1 indicates this frame did not successfully write
|
try fWriter.writeInt(u32, 0, .little); // -1 indicates this frame did not successfully write
|
||||||
|
|
||||||
std.debug.print("entries {d}\n", .{self.mem.enumTables.items.len});
|
|
||||||
|
|
||||||
// 1. write the changes to header file first if there are new entries in the registry
|
// 1. write the changes to header file first if there are new entries in the registry
|
||||||
for (self.mem.enumTables.items[self.enumTablesWritten..self.mem.enumTables.items.len]) |entry| {
|
for (self.mem.enumTables.items[self.enumTablesWritten..]) |entry| {
|
||||||
// write each entry
|
// write each entry
|
||||||
try self.writeEnumTable(fWriter, entry);
|
try marshalEnumTableAsCommand(&frame, entry);
|
||||||
|
self.enumTablesWritten += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (self.mem.logSites.items[self.logSitesWritten..]) |site| {
|
||||||
|
try marshalLogSiteAsCommand(&frame, site, self.mem);
|
||||||
|
self.logSitesWritten += 1;
|
||||||
}
|
}
|
||||||
// 2. write out the entries to the .sle file
|
|
||||||
|
|
||||||
const written = frame.written();
|
const written = frame.written();
|
||||||
std.mem.writeInt(i32, written[frameSizeOffset..][0..4], @intCast(written.len), .little);
|
std.mem.writeInt(u32, written[frameSizeOffset..][0..4], @intCast(written.len), .little);
|
||||||
try self.headersW.writeAll(written);
|
try self.headersW.writeAll(written);
|
||||||
try self.headersW.flush();
|
try self.headersW.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn commitEntriesChanges(self: *@This()) !void {
|
||||||
|
self.workBuffer.clearRetainingCapacity();
|
||||||
|
var frame = std.Io.Writer.Allocating.fromArrayList(self.allocator, &self.workBuffer);
|
||||||
|
defer self.workBuffer = frame.toArrayList();
|
||||||
|
|
||||||
|
const writer = &frame.writer;
|
||||||
|
try writer.writeAll(if (self.eLZ4 != null) compressedEntriesMagic else entriesMagic);
|
||||||
|
|
||||||
|
const frameSizeOffset = frame.written().len;
|
||||||
|
try writer.writeInt(u32, 0, .little);
|
||||||
|
|
||||||
|
// write all entries
|
||||||
|
if (self.entriesGeneration != self.mem.entriesGeneration) {
|
||||||
|
self.entriesGeneration = self.mem.entriesGeneration;
|
||||||
|
self.entriesWritten = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (self.mem.entries.items) |entry| {
|
||||||
|
try entry.marshalToWriter(&frame.writer);
|
||||||
|
self.entriesWritten += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const written = frame.written();
|
||||||
|
std.mem.writeInt(u32, written[frameSizeOffset..][0..4], @intCast(written.len), .little);
|
||||||
|
try self.entriesW.writeAll(written);
|
||||||
|
try self.entriesW.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn commit(self: *@This()) !void {
|
pub fn commit(self: *@This()) !void {
|
||||||
// write out all unwritten sites, loggers, tables, and entries
|
// write out all unwritten sites, loggers, tables, and entries
|
||||||
if (self.isCurrent()) return;
|
|
||||||
|
|
||||||
if (!self.sessionWritten) {
|
if (!self.sessionWritten) {
|
||||||
try self.writeSessionHeader();
|
try self.writeSessionHeader();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (self.shouldWriteHeaders()) {
|
||||||
try self.commitHeaderChanges();
|
try self.commitHeaderChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn isCurrent(self: *@This()) bool {
|
if (self.shouldWriteEntries()) {
|
||||||
|
try self.commitEntriesChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shouldWriteHeaders(self: @This()) bool {
|
||||||
const mem = self.mem;
|
const mem = self.mem;
|
||||||
// zig fmt: off
|
// zig fmt: off
|
||||||
return self.entriesGeneration == mem.entriesGeneration
|
return self.logSitesWritten != mem.logSites.items.len
|
||||||
and self.logSitesWritten == mem.logSites.items.len
|
or self.loggersWritten != mem.loggers.items.len
|
||||||
and self.loggersWritten == mem.loggers.items.len
|
or self.enumTablesWritten != mem.enumTables.items.len;
|
||||||
and self.enumTablesWritten == mem.enumTables.items.len;
|
|
||||||
// zig fmt: on
|
// zig fmt: on
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn shouldWriteEntries(self: @This()) bool {
|
||||||
|
const mem = self.mem;
|
||||||
|
return self.entriesGeneration != mem.entriesGeneration or self.entriesWritten != mem.entries.items.len;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn testWrite(scratch: std.mem.Allocator, mem: *const LogMem, writer: *std.Io.Writer) WriteError!void {
|
|
||||||
_ = scratch;
|
|
||||||
try writer.writeAll(magic);
|
|
||||||
try writer.writeInt(u8, version, .little);
|
|
||||||
try writer.writeInt(i96, mem.start_utc_time.raw.nanoseconds, .little);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Writes the registries and entries of `mem`. `scratch` holds the enum table
|
|
||||||
/// index while writing and is freed before returning.
|
|
||||||
pub fn write(scratch: std.mem.Allocator, mem: *const LogMem, writer: *std.Io.Writer) WriteError!void {
|
|
||||||
// Live sites share static enum tables, each is written once and ops refer to it by index.
|
|
||||||
var tables: EnumTableIndex = .{};
|
|
||||||
defer tables.deinit(scratch);
|
|
||||||
for (mem.logSites.items) |site| {
|
|
||||||
for (site.decoder.ops) |op| try tables.collect(scratch, op);
|
|
||||||
}
|
|
||||||
|
|
||||||
try writer.writeAll(magic);
|
|
||||||
try writer.writeInt(u32, version, .little);
|
|
||||||
try writer.writeInt(i96, mem.start_utc_time.raw.nanoseconds, .little);
|
|
||||||
|
|
||||||
try writeLen(writer, tables.list.items.len);
|
|
||||||
for (tables.list.items) |table| {
|
|
||||||
try writeLen(writer, table.len);
|
|
||||||
for (table.*) |entry| {
|
|
||||||
try writer.writeInt(i64, entry.value, .little);
|
|
||||||
try writeString(writer, entry.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try writeLen(writer, mem.loggers.items.len);
|
|
||||||
for (mem.loggers.items) |logger| {
|
|
||||||
try writeString(writer, logger.name);
|
|
||||||
try writer.writeByte(@backingInt(logger.verbosity.load(.monotonic)));
|
|
||||||
}
|
|
||||||
|
|
||||||
try writeLen(writer, mem.logSites.items.len);
|
|
||||||
for (mem.logSites.items) |site| {
|
|
||||||
try writer.writeInt(u32, site.source.line, .little);
|
|
||||||
try writeString(writer, site.source.file);
|
|
||||||
try writeString(writer, site.source.fmt);
|
|
||||||
try writeString(writer, site.source.args_type);
|
|
||||||
|
|
||||||
try writeLen(writer, site.decoder.ops.len);
|
|
||||||
for (site.decoder.ops) |op| try writeDecoderOp(writer, &tables, op);
|
|
||||||
|
|
||||||
try writeLen(writer, site.render_ops.len);
|
|
||||||
for (site.render_ops) |op| try writeRenderOp(writer, op);
|
|
||||||
}
|
|
||||||
|
|
||||||
try writeLen(writer, mem.entries.items.len);
|
|
||||||
for (mem.entries.items) |entry| {
|
|
||||||
try writer.writeInt(u32, entry.loggerId, .little);
|
|
||||||
try writer.writeInt(u32, entry.siteId, .little);
|
|
||||||
try writer.writeByte(@backingInt(entry.verbosity));
|
|
||||||
try writer.writeInt(u64, entry.usSinceStart, .little);
|
|
||||||
try writeString(writer, entry.serialized_args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes `mem` from `reader`. Registries are decoded into its
|
|
||||||
/// registryArena and entries into its entriesArena. The pointer maps stay
|
|
||||||
/// empty, there are no static descriptors to key them on, and `start_time` is
|
|
||||||
/// undefined. On error `mem` is freed and left undefined.
|
|
||||||
pub fn read(mem: *LogMem, backing: std.mem.Allocator, reader: *std.Io.Reader) ReadError!void {
|
pub fn read(mem: *LogMem, backing: std.mem.Allocator, reader: *std.Io.Reader) ReadError!void {
|
||||||
mem.* = .{
|
mem.* = .{
|
||||||
.registryArena = .init(backing),
|
.registryArena = .init(backing),
|
||||||
|
|
@ -413,33 +380,8 @@ pub fn read(mem: *LogMem, backing: std.mem.Allocator, reader: *std.Io.Reader) Re
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const EnumTableIndex = struct {
|
|
||||||
map: std.AutoHashMapUnmanaged(*const EnumTable, u32) = .empty,
|
|
||||||
list: std.ArrayList(*const EnumTable) = .empty,
|
|
||||||
|
|
||||||
fn deinit(self: *EnumTableIndex, allocator: std.mem.Allocator) void {
|
|
||||||
self.map.deinit(allocator);
|
|
||||||
self.list.deinit(allocator);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn collect(self: *EnumTableIndex, allocator: std.mem.Allocator, op: ArgsDecoderOp) WriteError!void {
|
|
||||||
for (op.fields) |field| try self.collect(allocator, field.op);
|
|
||||||
const table = op.enum_ref orelse return;
|
|
||||||
if (self.map.contains(table)) return;
|
|
||||||
|
|
||||||
const id = std.math.cast(u32, self.list.items.len) orelse return error.TooLarge;
|
|
||||||
try self.map.ensureUnusedCapacity(allocator, 1);
|
|
||||||
try self.list.append(allocator, table);
|
|
||||||
self.map.putAssumeCapacity(table, id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fn writeLen(writer: *std.Io.Writer, len: usize) WriteError!void {
|
|
||||||
try writer.writeInt(u32, std.math.cast(u32, len) orelse return error.TooLarge, .little);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn writeString(writer: *std.Io.Writer, bytes: []const u8) WriteError!void {
|
fn writeString(writer: *std.Io.Writer, bytes: []const u8) WriteError!void {
|
||||||
try writeLen(writer, bytes.len);
|
try writer.writeInt(u32, @intCast(bytes.len), .little);
|
||||||
try writer.writeAll(bytes);
|
try writer.writeAll(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -449,20 +391,6 @@ fn writeOptional(writer: *std.Io.Writer, value: ?usize) WriteError!void {
|
||||||
try writer.writeInt(u32, @intCast(v), .little);
|
try writer.writeInt(u32, @intCast(v), .little);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn writeDecoderOp(writer: *std.Io.Writer, tables: *const EnumTableIndex, op: ArgsDecoderOp) WriteError!void {
|
|
||||||
try writeString(writer, op.type_name);
|
|
||||||
try writer.writeByte(@backingInt(op.kind));
|
|
||||||
try writer.writeInt(u16, op.bits, .little);
|
|
||||||
try writer.writeByte(@as(u8, @intFromBool(op.signed)) | @as(u8, @intFromBool(op.is_tuple)) << 1);
|
|
||||||
try writer.writeInt(u32, if (op.enum_ref) |table| tables.map.get(table).? else none, .little);
|
|
||||||
|
|
||||||
try writeLen(writer, op.fields.len);
|
|
||||||
for (op.fields) |field| {
|
|
||||||
try writeString(writer, field.name);
|
|
||||||
try writeDecoderOp(writer, tables, field.op);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn writeRenderOp(writer: *std.Io.Writer, op: RenderOp) WriteError!void {
|
fn writeRenderOp(writer: *std.Io.Writer, op: RenderOp) WriteError!void {
|
||||||
try writer.writeByte(@backingInt(op));
|
try writer.writeByte(@backingInt(op));
|
||||||
switch (op) {
|
switch (op) {
|
||||||
|
|
@ -530,8 +458,8 @@ const Decoder = struct {
|
||||||
.bits = try self.reader.takeInt(u16, .little),
|
.bits = try self.reader.takeInt(u16, .little),
|
||||||
};
|
};
|
||||||
const flags = try self.reader.takeByte();
|
const flags = try self.reader.takeByte();
|
||||||
op.signed = flags & 1 != 0;
|
op.flags.signed = flags & 1 != 0;
|
||||||
op.is_tuple = flags & 2 != 0;
|
op.flags.is_tuple = flags & 2 != 0;
|
||||||
|
|
||||||
const table = try self.len();
|
const table = try self.len();
|
||||||
if (table != none) {
|
if (table != none) {
|
||||||
|
|
@ -566,6 +494,76 @@ const Decoder = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// ===== COMMANDS =====
|
||||||
|
///
|
||||||
|
// a command is how one entry in the header table gets registered
|
||||||
|
pub const RegistryCommand = enum(u8) {
|
||||||
|
enumTable = 0x4a,
|
||||||
|
logSite = 0x4b,
|
||||||
|
loggerDef = 0x4c,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn marshalEnumTableAsCommand(frame: *std.Io.Writer.Allocating, entry: *const EnumTable) !void {
|
||||||
|
const writer = &frame.writer;
|
||||||
|
// preceeded by the command byte + definition command length
|
||||||
|
// 0x0: [command byte]
|
||||||
|
// 0x1: [command length u32]
|
||||||
|
const commandStart = frame.written().len;
|
||||||
|
try writer.writeInt(u8, @backingInt(RegistryCommand.enumTable), .little);
|
||||||
|
try writer.writeInt(u32, 0, .little); // 0 is an invalid value
|
||||||
|
|
||||||
|
// format of an enumTable
|
||||||
|
try writer.writeInt(u32, @intCast(entry.len), .little);
|
||||||
|
|
||||||
|
for (entry.*) |e| {
|
||||||
|
try writer.writeInt(i64, e.value, .little);
|
||||||
|
try writer.writeInt(u32, @intCast(e.name.len), .little);
|
||||||
|
try writer.writeAll(e.name);
|
||||||
|
}
|
||||||
|
std.mem.writeInt(
|
||||||
|
u32,
|
||||||
|
frame.written()[commandStart + 1 ..][0..4],
|
||||||
|
@intCast(frame.written().len - commandStart),
|
||||||
|
.little,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn marshalLogRef(frame: *std.Io.Writer.Allocating, site: *const LogSite) !void {
|
||||||
|
const source = site.source;
|
||||||
|
const writer = &frame.writer;
|
||||||
|
|
||||||
|
try writer.writeInt(u32, source.line, .little);
|
||||||
|
try writeString(writer, source.file);
|
||||||
|
try writeString(writer, source.fmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn marshalLogSiteAsCommand(frame: *std.Io.Writer.Allocating, site: *const LogSite, mem: *const LogMem) !void {
|
||||||
|
const writer = &frame.writer;
|
||||||
|
|
||||||
|
const commandStart = frame.written().len;
|
||||||
|
// preceeded by the command byte + definition command length
|
||||||
|
// 0x0: [command byte]
|
||||||
|
// 0x1: [command length u32]
|
||||||
|
try writer.writeInt(u8, @backingInt(RegistryCommand.logSite), .little);
|
||||||
|
try writer.writeInt(u32, 0, .little); // 0 is an invalid value
|
||||||
|
|
||||||
|
try marshalLogRef(frame, site);
|
||||||
|
for (site.decoder.ops) |op| {
|
||||||
|
try op.marshalToWriter(writer, mem);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (site.render_ops) |op| {
|
||||||
|
try writeRenderOp(writer, op);
|
||||||
|
}
|
||||||
|
|
||||||
|
std.mem.writeInt(
|
||||||
|
u32,
|
||||||
|
frame.written()[commandStart + 1 ..][0..4],
|
||||||
|
@intCast(frame.written().len - commandStart),
|
||||||
|
.little,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const slugger = @import("slugger.zig");
|
const slugger = @import("slugger.zig");
|
||||||
const LogMem = slugger.LogMem;
|
const LogMem = slugger.LogMem;
|
||||||
|
|
@ -578,5 +576,6 @@ const EnumEntry = slugger.EnumEntry;
|
||||||
const ArgsDecoderOp = slugger.ArgsDecoderOp;
|
const ArgsDecoderOp = slugger.ArgsDecoderOp;
|
||||||
const ArgsDecoderOpKind = slugger.ArgsDecoderOpKind;
|
const ArgsDecoderOpKind = slugger.ArgsDecoderOpKind;
|
||||||
const RenderOp = slugger.RenderOp;
|
const RenderOp = slugger.RenderOp;
|
||||||
|
const StructuredLog = slugger.StructuredLog;
|
||||||
const lz4 = @import("lz4");
|
const lz4 = @import("lz4");
|
||||||
const Lz4Writer = lz4.Lz4Writer;
|
const Lz4Writer = lz4.Lz4Writer;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue