lots of comment updates, preparing slugger for oss
This commit is contained in:
parent
743150514a
commit
65b660f349
|
|
@ -1,3 +1,5 @@
|
|||
# P2 - Peterino's stdlib
|
||||
# P2 - Peterino's lib
|
||||
|
||||
Algorithms, experiments, definitions and stuff I use for gamedev.
|
||||
Includes: old version of zmath
|
||||
|
||||
Contains a bunch of stuff i think is missing from the zig stdlib that is needed for gamedev
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
pub const std = @import("std");
|
||||
|
||||
pub const CountingAllocatorContext = struct {
|
||||
// wrap any allocator and get statistics
|
||||
pub const Context = struct {
|
||||
wrappedAllocator: std.mem.Allocator = undefined,
|
||||
alloc_count: usize = 0,
|
||||
free_count: usize = 0,
|
||||
alloc_bytes: usize = 0,
|
||||
free_bytes: usize = 0,
|
||||
memory_usage: usize = 0,
|
||||
nameBuf: [32]u8 = blk: {
|
||||
var x: [u32]u8 = @splat(0);
|
||||
@memcpy(&x, "AllocatorName");
|
||||
break :blk x;
|
||||
},
|
||||
nameLen: u32 = 0,
|
||||
|
||||
const vtable: std.mem.Allocator.VTable = .{
|
||||
.alloc = alloc,
|
||||
|
|
@ -15,6 +22,18 @@ pub const CountingAllocatorContext = struct {
|
|||
.free = free,
|
||||
};
|
||||
|
||||
pub fn init(gpa: std.mem.Allocator, statName: []const u8) @This() {
|
||||
var new = @This(){
|
||||
.wrappedAllocator = gpa,
|
||||
};
|
||||
new.nameLen = @intCast(std.fmt.bufPrint(&new.nameBuf, "{s}", .{statName}).len);
|
||||
return new;
|
||||
}
|
||||
|
||||
pub fn name(self: @This()) []const u8 {
|
||||
return self.nameBuf[0..self.nameLen];
|
||||
}
|
||||
|
||||
pub fn allocator(self: *@This(), wrappedAllocator: std.mem.Allocator) std.mem.Allocator {
|
||||
self.wrappedAllocator = wrappedAllocator;
|
||||
return .{
|
||||
|
|
@ -68,4 +87,15 @@ pub const CountingAllocatorContext = struct {
|
|||
self.memory_usage -= @min(self.memory_usage, delta);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getStatistics(self: *const @This()) stats.PoolStatistics {
|
||||
return .{
|
||||
.instanceCount = self.alloc_count,
|
||||
.memoryReserved = 0,
|
||||
.memoryUsed = self.alloc_bytes,
|
||||
.statName = "wrappedAllocator",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const stats = @import("stats.zig");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
const Engine = struct {};
|
||||
|
||||
pub fn println(comptime fmt: []const u8, args: anytype) void {
|
||||
std.debug.print(fmt ++ "\n", .{args});
|
||||
}
|
||||
|
||||
pub fn printlns(comptime fmt: []const u8) void {
|
||||
std.debug.print(fmt ++ "\n", .{});
|
||||
}
|
||||
|
||||
const MyEngineSystem = struct {
|
||||
pub fn init(self: *@This(), engineContext: *Engine) !void {
|
||||
_ = self;
|
||||
_ = engineContext;
|
||||
println("initialized", .{});
|
||||
}
|
||||
|
||||
pub fn tick(self: *@This(), dt: f32) void {
|
||||
_ = self;
|
||||
printlns("tick!");
|
||||
_ = dt;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *@This()) void {
|
||||
_ = self;
|
||||
|
||||
printlns("deinitialized");
|
||||
}
|
||||
};
|
||||
|
||||
const Vec2 = struct {
|
||||
x: f32 = 0,
|
||||
y: f32 = 0,
|
||||
};
|
||||
|
||||
const Vec2d = struct {
|
||||
x: f32 = 0,
|
||||
y: f32 = 0,
|
||||
};
|
||||
|
||||
test "testing" {}
|
||||
|
||||
const std = @import("std");
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
# slugger
|
||||
|
||||
Peter's structured logging library. Goal here is to add a general purpose logging library that is good enough for all possible usecases from within the game engine.
|
||||
My structured logging library. Goal here is to add a general purpose logging library that is good enough for all possible usecases from within the game engine.
|
||||
|
||||
Basic premise of operation: you log from any arbitrary thread and it goes to an in-memory log file format (currently it's just an arraylist, soon to be an in-memory db called LogMem)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,17 +3,17 @@ const std = @import("std");
|
|||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
const p2 = b.dependency("p2", .{ .target = target, .optimize = optimize });
|
||||
|
||||
const lz4 = b.dependency("lz4", .{ .target = target, .optimize = optimize });
|
||||
const xxd = b.dependency("xxd", .{ .target = target, .optimize = optimize });
|
||||
|
||||
const mod = b.addModule("slugger", .{
|
||||
.root_source_file = b.path("src/slugger.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.imports = &.{.{ .name = "p2", .module = p2.module("p2") }},
|
||||
});
|
||||
|
||||
mod.addImport("lz4", lz4.module("lz4_zig"));
|
||||
mod.addImport("xxd", xxd.module("xxd"));
|
||||
|
||||
const tests = b.addTest(.{
|
||||
.root_module = mod,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
.version = "0.0.0",
|
||||
.minimum_zig_version = "0.17.0",
|
||||
.dependencies = .{
|
||||
.p2 = .{ .path = "../p2" },
|
||||
.xxd = .{ .path = "../xxd" },
|
||||
.lz4 = .{ .path = "../lz4" },
|
||||
},
|
||||
.paths = .{ "build.zig", "build.zig.zon", "src" },
|
||||
|
|
|
|||
|
|
@ -385,7 +385,7 @@ const RenderOp = slugger.RenderOp;
|
|||
const StructuredLog = slugger.StructuredLog;
|
||||
const RegistryCommand = sluggerformat.RegistryCommand;
|
||||
|
||||
const xxd = @import("p2").xxd;
|
||||
const xxd = @import("xxd");
|
||||
const lz4 = @import("lz4");
|
||||
const Lz4Writer = lz4.Lz4Writer;
|
||||
const Lz4Reader = lz4.Lz4Reader;
|
||||
|
|
|
|||
|
|
@ -1141,7 +1141,7 @@ pub fn DeclareLogger(comptime LoggerName: []const u8, comptime opts: LoggerOptio
|
|||
}
|
||||
|
||||
test "testing" {
|
||||
const xxd = @import("p2").xxd;
|
||||
const xxd = @import("xxd");
|
||||
|
||||
const TestEnum = enum(u8) {
|
||||
hello = 0,
|
||||
|
|
@ -1339,7 +1339,6 @@ pub const LoadedLogs = struct {
|
|||
|
||||
pub const reader = @import("reader.zig");
|
||||
pub const sluggerformat = @import("sluggerformat.zig");
|
||||
const p2 = @import("p2");
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const lz4 = @import("lz4");
|
||||
|
|
|
|||
|
|
@ -470,6 +470,6 @@ const ArgsDecoderOpKind = slugger.ArgsDecoderOpKind;
|
|||
const RenderOp = slugger.RenderOp;
|
||||
const StructuredLog = slugger.StructuredLog;
|
||||
|
||||
const xxd = @import("p2").xxd;
|
||||
const xxd = @import("xxd");
|
||||
const lz4 = @import("lz4");
|
||||
const Lz4Writer = lz4.Lz4Writer;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
.zig-cache/
|
||||
zig-out/
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
# xxd utility
|
||||
|
||||
This has been a really nice helper function when helping with doing any kind of serialization work.
|
||||
|
||||
I've been finding that generally I've been using debug print statements in tests with --watch more than i've actually been using a real debugger.
|
||||
|
||||
And having a tool to just inspect the bytes at any moment has been absolutely amazing.
|
||||
|
||||
these 300 or so lines of code have helped me fix a lot of bugs.
|
||||
|
||||
```
|
||||
zig fetch --save git+ssh://git@codeberg.org/peterino2/xxd.git
|
||||
```
|
||||
|
||||
## Usage - debug printing:
|
||||
|
||||
```zig
|
||||
const xxd = @import("xxd");
|
||||
|
||||
test "hello xxd" {
|
||||
const teststring: []const u8 = "this is a test string";
|
||||
// second argument is the offset address, so you
|
||||
// can view multiple chunks toegether if you were to loop this.
|
||||
xxd.print(teststring, 0);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
output:
|
||||
|
||||
this is the top 0xa0 as a result of running xxd on the build.zig file
|
||||
|
||||
xxd.print is a debug function like std.debug.print and outputs to stderr.
|
||||
|
||||
```
|
||||
0001 0203 0405 0607 0809 0a0b 0c0d 0e0f
|
||||
00000000: 636F 6E73 7420 7374 6420 3D20 4069 6D70 |const std = @imp|
|
||||
00000010: 6F72 7428 2273 7464 2229 3B0A 0A70 7562 |ort("std");..pub|
|
||||
00000020: 2066 6E20 6275 696C 6428 623A 202A 7374 | fn build(b: *st|
|
||||
00000030: 642E 4275 696C 6429 2076 6F69 6420 7B0A |d.Build) void {.|
|
||||
00000040: 2020 2020 636F 6E73 7420 7461 7267 6574 | const target|
|
||||
00000050: 203D 2062 2E73 7461 6E64 6172 6454 6172 | = b.standardTar|
|
||||
00000060: 6765 744F 7074 696F 6E73 282E 7B7D 293B |getOptions(.{});|
|
||||
00000070: 0A20 2020 2063 6F6E 7374 206F 7074 696D |. const optim|
|
||||
00000080: 697A 6520 3D20 622E 7374 616E 6461 7264 |ize = b.standard|
|
||||
00000090: 4F70 7469 6D69 7A65 4F70 7469 6F6E 282E |OptimizeOption(.|
|
||||
000000a0: 7B7D 293B 0A0A 2020 2020 636F 6E73 7420 |{});.. const |
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Usage - writers interface
|
||||
|
||||
Supports colors and a multitude of options if you use the writer
|
||||
|
||||
|
||||
```zig
|
||||
pub const XxdOptions = struct {
|
||||
wordSize: u32 = 8, // value is in bits... but maybe its better if it's in bytes?
|
||||
endian: Endian = .little, // .big will reverse each word
|
||||
wordsPerGroup: u32 = 2,
|
||||
showHeader: bool = true,
|
||||
groupsPerLine: u32 = 8,
|
||||
groupSplit: u32 = 4, // extra space every N groups; 0 disables it
|
||||
showAddress: bool = true,
|
||||
baseAddress: u64 = 0,
|
||||
showAscii: bool = false,
|
||||
color: bool = true, // if true uses an ascii color code
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
```zig
|
||||
try xxd.xxdWrite(&myWriter, bytes, options);
|
||||
```
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const mod = b.addModule("xxd", .{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.root_source_file = b.path("xxd.zig"),
|
||||
});
|
||||
|
||||
_ = mod;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
.{
|
||||
.name = .xxd,
|
||||
.version = "1.0.0",
|
||||
.dependencies = .{},
|
||||
.paths = .{
|
||||
"",
|
||||
},
|
||||
.fingerprint = 0x89ab645843e0a1e,
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const xxd = b.dependency("xxd", .{ .target = target, .optimize = optimize });
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "xxd",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.imports = &.{
|
||||
.{ .name = "xxd", .module = xxd.module("xxd") },
|
||||
},
|
||||
}),
|
||||
});
|
||||
b.installArtifact(exe);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
run_cmd.addPassthruArgs();
|
||||
|
||||
b.step("run", "Hex dump the file given as an argument").dependOn(&run_cmd.step);
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
.{
|
||||
// This is the default name used by packages depending on this one. For
|
||||
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||
// as the key in the `dependencies` table. Although the user can choose a
|
||||
// different name, most users will stick with this provided value.
|
||||
//
|
||||
// It is redundant to include "zig" in this name because it is already
|
||||
// within the Zig package namespace.
|
||||
.name = .xxdcli,
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.0.0",
|
||||
// Together with name, this represents a globally unique package
|
||||
// identifier. This field is generated by the Zig toolchain when the
|
||||
// package is first created, and then *never changes*. This allows
|
||||
// unambiguous detection of one package being an updated version of
|
||||
// another.
|
||||
//
|
||||
// When forking a Zig project, this id should be regenerated (delete the
|
||||
// field and run `zig build`) if the upstream project is still maintained.
|
||||
// Otherwise, the fork is *hostile*, attempting to take control over the
|
||||
// original project's identity. Thus it is recommended to leave the comment
|
||||
// on the following line intact, so that it shows up in code reviews that
|
||||
// modify the field.
|
||||
.fingerprint = 0x9ce26d7b57bb76f0, // Changing this has security and trust implications.
|
||||
// Tracks the earliest Zig version that the package considers to be a
|
||||
// supported use case.
|
||||
.minimum_zig_version = "0.17.0",
|
||||
// This field is optional.
|
||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||
// Once all dependencies are fetched, `zig build` no longer requires
|
||||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
.xxd = .{ .path = "../" },
|
||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||
//.example = .{
|
||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||
// // the new URL. If the contents of a URL change this will result in a hash mismatch
|
||||
// // which will prevent zig from using it.
|
||||
// .url = "https://example.com/foo.tar.gz",
|
||||
//
|
||||
// // This is computed from the file contents of the directory of files that is
|
||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||
// // `paths`.
|
||||
// //
|
||||
// // This field is the source of truth; packages do not come from a `url`; they
|
||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||
// // obtain a package matching this `hash`.
|
||||
// .hash = "...",
|
||||
//
|
||||
// // When this is provided, the package is found in a directory relative to the
|
||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||
// // computed. This field and `url` are mutually exclusive.
|
||||
// .path = "foo",
|
||||
//
|
||||
// // When this is set to `true`, a package is declared to be lazily
|
||||
// // fetched. This makes the dependency only get fetched if it is
|
||||
// // actually used.
|
||||
// .lazy = false,
|
||||
//},
|
||||
},
|
||||
// Specifies the set of files and directories that are included in this package.
|
||||
// Only files and directories listed here are included in the `hash` that
|
||||
// is computed for this package. Only files listed here will remain on disk
|
||||
// when using the zig package manager. As a rule of thumb, one should list
|
||||
// files required for compilation plus any license(s).
|
||||
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||
// the build root itself.
|
||||
// A directory listed here means that all files within, recursively, are included.
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
// For example...
|
||||
//"LICENSE",
|
||||
//"README.md",
|
||||
},
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
const std = @import("std");
|
||||
const xxd = @import("xxd");
|
||||
const Io = std.Io;
|
||||
|
||||
/// Hex dumps the file named by argv[1] to stdout.
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const io = init.io;
|
||||
const args = try init.minimal.args.toSlice(init.arena.allocator());
|
||||
if (args.len < 2) {
|
||||
std.debug.print("usage: {s} <file>\n", .{args[0]});
|
||||
return error.MissingArgument;
|
||||
}
|
||||
|
||||
var nocolor: bool = false;
|
||||
|
||||
const eql = std.mem.eql;
|
||||
|
||||
for (args) |a| {
|
||||
if (eql(u8, "--plain", a) or eql(u8, "-p", a)) {
|
||||
nocolor = true;
|
||||
}
|
||||
}
|
||||
|
||||
const file = try Io.Dir.cwd().openFile(io, args[1], .{});
|
||||
defer file.close(io);
|
||||
|
||||
var file_buffer: [4096]u8 = undefined;
|
||||
var file_reader = file.reader(io, &file_buffer);
|
||||
const reader = &file_reader.interface;
|
||||
|
||||
var stdout_buffer: [4096]u8 = undefined;
|
||||
var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
|
||||
const stdout = &stdout_file_writer.interface;
|
||||
|
||||
var chunk: [4096]u8 = undefined;
|
||||
var address: u64 = 0;
|
||||
while (true) {
|
||||
const n = try reader.readSliceShort(&chunk);
|
||||
if (n == 0) break;
|
||||
try xxd.xxdWriteAscii(stdout, chunk[0..n], .{
|
||||
.baseAddress = address,
|
||||
.showHeader = address == 0,
|
||||
.color = !nocolor,
|
||||
});
|
||||
address += n;
|
||||
if (n < chunk.len) break;
|
||||
}
|
||||
|
||||
try stdout.flush();
|
||||
}
|
||||
|
|
@ -6,10 +6,8 @@ pub const Endian = enum {
|
|||
};
|
||||
|
||||
pub const XxdOptions = struct {
|
||||
wordSize: u32 = 8, // bits; must be a nonzero multiple of 8 , deprecated
|
||||
// Preserve the existing convention: little prints bytes in input order;
|
||||
// big reverses the bytes within each word. ASCII always uses input order.
|
||||
endian: Endian = .little,
|
||||
wordSize: u32 = 8, // value is in bits... but maybe its better if it's in bytes?
|
||||
endian: Endian = .little, // .big will reverse each word
|
||||
wordsPerGroup: u32 = 2,
|
||||
showHeader: bool = true,
|
||||
groupsPerLine: u32 = 8,
|
||||
|
|
@ -117,10 +115,12 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions)
|
|||
if (i % bytesPerWord == 0) word += 1;
|
||||
if (word % options.wordsPerGroup == 0) group += 1;
|
||||
const op = if (i % 2 == 0) ColorOp.reset else ColorOp.green;
|
||||
try writer.writeAll(op.colorAscii());
|
||||
if (options.color)
|
||||
try writer.writeAll(op.colorAscii());
|
||||
try writeByteAsHex2(@intCast(i), writer, true);
|
||||
}
|
||||
try writer.writeAll(ColorOp.reset.colorAscii());
|
||||
if (options.color)
|
||||
try writer.writeAll(ColorOp.reset.colorAscii());
|
||||
}
|
||||
try writer.writeByte('\n');
|
||||
|
||||
|
|
@ -179,7 +179,10 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions)
|
|||
|
||||
// render byteline
|
||||
var opCursor: usize = 0;
|
||||
try writer.writeAll(ops.items[opCursor].colorAscii());
|
||||
|
||||
if (options.color) {
|
||||
try writer.writeAll(ops.items[opCursor].colorAscii());
|
||||
}
|
||||
|
||||
{
|
||||
var word: usize = 0;
|
||||
|
|
@ -199,7 +202,9 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions)
|
|||
if (op.count == 0) {
|
||||
opCursor += 1;
|
||||
op = ops.items[opCursor];
|
||||
try writer.writeAll(ops.items[opCursor].colorAscii());
|
||||
|
||||
if (options.color)
|
||||
try writer.writeAll(ops.items[opCursor].colorAscii());
|
||||
}
|
||||
if (b) |x| {
|
||||
try writeByteAsHex2(x, writer, false);
|
||||
|
|
@ -230,7 +235,9 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions)
|
|||
{
|
||||
opCursor = 0;
|
||||
var op = ops.items[opCursor];
|
||||
try writer.writeAll(ColorOp.reset.colorAscii());
|
||||
if (options.color)
|
||||
try writer.writeAll(ColorOp.reset.colorAscii());
|
||||
|
||||
try writer.writeAll(" |");
|
||||
for (lineBytes.items, 0..) |mb, i| {
|
||||
const b = mb orelse ' ';
|
||||
|
|
@ -238,12 +245,14 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions)
|
|||
if (op.count == 0) {
|
||||
opCursor += 1;
|
||||
op = ops.items[opCursor];
|
||||
try writer.writeAll(ops.items[opCursor].colorAscii());
|
||||
if (options.color)
|
||||
try writer.writeAll(ops.items[opCursor].colorAscii());
|
||||
}
|
||||
try writer.writeByte(if (b >= 0x20 and b <= 0x7e) b else getUnprintableByte(b));
|
||||
op.count -= 1;
|
||||
}
|
||||
try writer.writeAll(ColorOp.reset.colorAscii());
|
||||
if (options.color)
|
||||
try writer.writeAll(ColorOp.reset.colorAscii());
|
||||
|
||||
if (lineBytes.items.len < bytesPerLine) {
|
||||
_ = try writer.splatByte(' ', bytesPerLine - lineBytes.items.len);
|
||||
Loading…
Reference in New Issue