Compare commits

...

3 commits

Author SHA1 Message Date
8e9faefc0e rm debug print 2019-08-11 11:17:47 -03:00
f3b8196ac5 journal: catch err, add sliceifyKnown 2019-08-11 11:17:13 -03:00
6b78bb16d8 convert c pointers to optional unknown length ptrs 2019-08-11 11:12:21 -03:00
2 changed files with 25 additions and 22 deletions

View file

@ -9,7 +9,7 @@ pub fn build(b: *Builder) void {
const source_files = [_][]const u8{"src/journal/main.c"};
for (source_files) |source| {
exe.addCSourceFile(source, [_][]const u8{"-W -O"});
exe.addCSourceFile(source, [_][]const u8{"-Wall"});
}
const zig_sources = [_][]const u8{"src/journal/journal.zig"};

View file

@ -12,17 +12,16 @@ const c = @cImport({
const warn = std.debug.warn;
const allocator = std.heap.c_allocator;
fn alloc_str(size: c_ulong) [*c]u8 {
return @ptrCast([*c]u8, @alignCast(@alignOf(u8), c.malloc(c_ulong(size))));
fn sliceify(ptr: [*]u8) []u8 {
return ptr[0..c.strlen(ptr)];
}
fn free_str(ptr: [*c]u8) void {
c.free(@ptrCast(?*c_void, ptr));
fn sliceifyKnown(slice: []u8) []u8 {
return slice[0..c.strlen(slice.ptr)];
}
export fn journal_open(topic_opt: [*c]u8) os.fd_t {
const topic_len = c.strlen(topic_opt);
const topic: []u8 = topic_opt[0..topic_len];
export fn journal_open(topic_cptr: ?[*]u8) os.fd_t {
const topic = sliceify(topic_cptr.?);
const journal_dir = std.fs.path.resolve(allocator, [_][]const u8{
std.os.getenvC(c"HOME").?,
@ -61,10 +60,11 @@ export fn journal_open(topic_opt: [*c]u8) os.fd_t {
return fd;
}
export fn journal_write(journal: os.fd_t, message: [*c]u8) void {
const msglen = c.strlen(message);
std.os.write(journal, message[0..msglen]) catch |err| {
export fn journal_write(journal: os.fd_t, message_cptr: ?[*]u8) void {
std.os.write(
journal,
sliceify(message_cptr.?),
) catch |err| {
std.debug.warn("Error while writing to file: {}\n", err);
return;
};
@ -76,24 +76,28 @@ export fn journal_close(journal: os.fd_t) void {
export fn journal_write_topic(
journal: os.fd_t,
topic_opt: [*c]u8,
message_opt: [*c]u8,
topic_cptr: ?[*]u8,
message_cptr: ?[*]u8,
) void {
var topic = topic_opt[0..c.strlen(topic_opt)];
var message = message_opt[0..c.strlen(message_opt)];
var topic = sliceify(topic_cptr.?);
var message = sliceify(message_cptr.?);
var tstamp_total = allocator.alloc(u8, 128) catch |err| {
warn("failed to allocate string for timestamp\n");
return;
};
defer allocator.free(tstamp_total);
var tstamp_cstr: [*c]u8 = alloc_str(128);
var rawtime: c.time_t = undefined;
_ = c.time(&rawtime);
var cur_time: [*c]const c.struct_tm = c.gmtime(&rawtime);
_ = c.strftime(tstamp_cstr, usize(128), c"%c", cur_time);
_ = c.strftime(tstamp_total.ptr, usize(128), c"%c", cur_time);
var tstamp = tstamp_cstr[0..c.strlen(tstamp_cstr)];
var tstamp = sliceifyKnown(tstamp_total);
std.debug.warn("tstamp: {}\ntopic: {}\nmsg: {}\n", tstamp, topic, message);
// catch unreachable is intended.
var fmt_msg = std.fmt.allocPrint(
allocator,
"[{}] [{}]: {}\n",
@ -105,12 +109,11 @@ export fn journal_write_topic(
return;
};
// catch unreachable is intended.
var fmt_msg_cstr = std.cstr.addNullByte(
allocator,
fmt_msg,
) catch unreachable;
free_str(tstamp_cstr);
journal_write(journal, fmt_msg_cstr.ptr);
}