Work on logging.
This commit is contained in:
parent
5ceeda56f0
commit
ab2c3824a3
|
@ -3,6 +3,7 @@ const Widget = @import("../types/widget.zig").Widget;
|
||||||
const Info = @import("../types/info.zig");
|
const Info = @import("../types/info.zig");
|
||||||
const MouseEvent = @import("../types/mouseevent.zig");
|
const MouseEvent = @import("../types/mouseevent.zig");
|
||||||
const os = std.os;
|
const os = std.os;
|
||||||
|
const log = std.log;
|
||||||
const terminal_version = @import("build_options").terminal_version;
|
const terminal_version = @import("build_options").terminal_version;
|
||||||
const debug_allocator = @import("build_options").debug_allocator;
|
const debug_allocator = @import("build_options").debug_allocator;
|
||||||
const disable_terminal_mouse = @import("build_options").disable_terminal_mouse;
|
const disable_terminal_mouse = @import("build_options").disable_terminal_mouse;
|
||||||
|
@ -42,21 +43,20 @@ pub const Bar = struct {
|
||||||
_ = os.linux.sigprocmask(std.os.SIG_BLOCK, &mask, null);
|
_ = os.linux.sigprocmask(std.os.SIG_BLOCK, &mask, null);
|
||||||
const signal_fd = try os.signalfd(-1, &mask, 0);
|
const signal_fd = try os.signalfd(-1, &mask, 0);
|
||||||
defer os.close(signal_fd);
|
defer os.close(signal_fd);
|
||||||
std.debug.print("signalfd: {}\n", .{signal_fd});
|
log.debug(.bar, "Spawning threads.\n", .{});
|
||||||
|
|
||||||
for (self.widgets) |w| {
|
for (self.widgets) |w| {
|
||||||
|
log.debug(.bar, "Spawning thread=\"{}\"\n", .{w.name()});
|
||||||
var thread = try std.Thread.spawn(w, Widget.start);
|
var thread = try std.Thread.spawn(w, Widget.start);
|
||||||
}
|
}
|
||||||
_ = try std.Thread.spawn(self, Bar.process);
|
_ = try std.Thread.spawn(self, Bar.process);
|
||||||
// TODO: wait for kill signal to kill bar instead of waiting for thread.
|
log.info(.bar, "Waiting for kill signal.\n", .{});
|
||||||
//thread.wait();
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
readFromSignalFd(signal_fd) catch |err| {
|
readFromSignalFd(signal_fd) catch |err| {
|
||||||
if (err == error.Shutdown) break else std.debug.print("failed to read from signal fd: {}\n", .{err});
|
if (err == error.Shutdown) break else log.err(.bar, "failed to read from signal fd: {}\n", .{err});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
std.debug.print("Shutting Down.\n", .{});
|
log.info(.bar, "Shutting Down.\n", .{});
|
||||||
|
|
||||||
self.running = false;
|
self.running = false;
|
||||||
const lock = self.items_mutex.acquire();
|
const lock = self.items_mutex.acquire();
|
||||||
|
@ -68,7 +68,7 @@ pub const Bar = struct {
|
||||||
try self.free_info(info);
|
try self.free_info(info);
|
||||||
}
|
}
|
||||||
self.infos.deinit();
|
self.infos.deinit();
|
||||||
std.debug.print("Shut Down.\n", .{});
|
log.info(.bar, "Shut Down.\n", .{});
|
||||||
if (terminal_version and !disable_terminal_mouse) {
|
if (terminal_version and !disable_terminal_mouse) {
|
||||||
try self.out_file.writer().writeAll("\u{001b}[?1000;1006;1015l");
|
try self.out_file.writer().writeAll("\u{001b}[?1000;1006;1015l");
|
||||||
}
|
}
|
||||||
|
@ -205,17 +205,17 @@ pub const Bar = struct {
|
||||||
if (line[0] == ',') line = line[1..line.len];
|
if (line[0] == ',') line = line[1..line.len];
|
||||||
const parseOptions = std.json.ParseOptions{ .allocator = allocator };
|
const parseOptions = std.json.ParseOptions{ .allocator = allocator };
|
||||||
const data = try std.json.parse(MouseEvent, &std.json.TokenStream.init(line), parseOptions);
|
const data = try std.json.parse(MouseEvent, &std.json.TokenStream.init(line), parseOptions);
|
||||||
|
defer std.json.parseFree(MouseEvent, data, parseOptions);
|
||||||
// TODO: maybe make a function for getting the widget or widget index by name?
|
// TODO: maybe make a function for getting the widget or widget index by name?
|
||||||
// We do use this patern a lot in this code.
|
// We do use this patern a lot in this code.
|
||||||
for (self.widgets) |w| {
|
for (self.widgets) |w| {
|
||||||
if (std.mem.eql(u8, w.name(), data.name)) {
|
if (std.mem.eql(u8, w.name(), data.name)) {
|
||||||
w.mouse_event(data) catch {};
|
try w.mouse_event(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If mouse_event needs to store the event for after the call is finished,
|
// If mouse_event needs to store the event for after the call is finished,
|
||||||
// it should do it by itself, this just keeps the lifetime of the event to bare minimum.
|
// it should do it by itself, this just keeps the lifetime of the event to bare minimum.
|
||||||
// Free the memory allocated by the MouseEvent struct.
|
// Free the memory allocated by the MouseEvent struct.
|
||||||
std.json.parseFree(MouseEvent, data, parseOptions);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,10 +228,10 @@ pub const Bar = struct {
|
||||||
while (self.running) {
|
while (self.running) {
|
||||||
if (terminal_version) {
|
if (terminal_version) {
|
||||||
if (!disable_terminal_mouse) {
|
if (!disable_terminal_mouse) {
|
||||||
self.terminal_input_process() catch {};
|
self.terminal_input_process() catch |err| { log.err(.bar, "failed to process terminal input: {}\n", .{err}); };
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try self.i3bar_input_process();
|
self.i3bar_input_process() catch |err| { log.err(.bar, "failed to process i3bar input: {}\n", .{err}); };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,6 +242,10 @@ pub const Bar = struct {
|
||||||
|
|
||||||
/// This frees the name and text fields of a Info struct.
|
/// This frees the name and text fields of a Info struct.
|
||||||
fn free_info(self: *Bar, info: Info) !void {
|
fn free_info(self: *Bar, info: Info) !void {
|
||||||
|
const name_size = info.name.len * @sizeOf(u8);
|
||||||
|
const text_size = info.full_text.len * @sizeOf(u8);
|
||||||
|
const total_size = name_size + text_size;
|
||||||
|
log.debug(.bar, "Freeing info for \"{}\", name_size={} text_size={} total={}\n", .{info.name, name_size, text_size, total_size});
|
||||||
self.allocator.free(info.name);
|
self.allocator.free(info.name);
|
||||||
self.allocator.free(info.full_text);
|
self.allocator.free(info.full_text);
|
||||||
}
|
}
|
||||||
|
@ -250,6 +254,10 @@ pub const Bar = struct {
|
||||||
/// memory lifetime, we duplicate the info fields.
|
/// memory lifetime, we duplicate the info fields.
|
||||||
fn dupe_info(self: *Bar, info: Info) !Info {
|
fn dupe_info(self: *Bar, info: Info) !Info {
|
||||||
// TODO: name should be comptime known, rework.
|
// TODO: name should be comptime known, rework.
|
||||||
|
const name_size = info.name.len * @sizeOf(u8);
|
||||||
|
const text_size = info.full_text.len * @sizeOf(u8);
|
||||||
|
const total_size = name_size + text_size;
|
||||||
|
log.debug(.bar, "Duping info for \"{}\", name_size={} text_size={} total={}\n", .{info.name, name_size, text_size, total_size});
|
||||||
const new_name = try self.allocator.alloc(u8, info.name.len);
|
const new_name = try self.allocator.alloc(u8, info.name.len);
|
||||||
std.mem.copy(u8, new_name, info.name);
|
std.mem.copy(u8, new_name, info.name);
|
||||||
const new_text = try self.allocator.alloc(u8, info.full_text.len);
|
const new_text = try self.allocator.alloc(u8, info.full_text.len);
|
||||||
|
|
35
src/main.zig
35
src/main.zig
|
@ -17,7 +17,38 @@ const Info = @import("types/info.zig");
|
||||||
|
|
||||||
const debug_allocator = @import("build_options").debug_allocator;
|
const debug_allocator = @import("build_options").debug_allocator;
|
||||||
|
|
||||||
|
// Set the log level to warning
|
||||||
|
//pub const log_level: std.log.Level = .warn;
|
||||||
|
// Define root.log to override the std implementation
|
||||||
|
pub fn log(
|
||||||
|
comptime level: std.log.Level,
|
||||||
|
comptime scope: @TypeOf(.EnumLiteral),
|
||||||
|
comptime format: []const u8,
|
||||||
|
args: anytype,
|
||||||
|
) void {
|
||||||
|
// Ignore all non-critical logging from sources other than
|
||||||
|
// .my_project and .nice_library
|
||||||
|
const prefix_text = switch (level) {
|
||||||
|
.emerg => "EMER",
|
||||||
|
.alert => "ALER",
|
||||||
|
.crit => "CRIT",
|
||||||
|
.err => "ERROR",
|
||||||
|
.warn => "WARN",
|
||||||
|
.notice => "NOTI",
|
||||||
|
.info => "INFO",
|
||||||
|
.debug => "DBUG",
|
||||||
|
else => "WAT",
|
||||||
|
};
|
||||||
|
var format_text: []const u8 = "";
|
||||||
|
const prefix = prefix_text ++ "[" ++ @tagName(scope) ++ "] ";
|
||||||
|
const held = std.debug.getStderrMutex().acquire();
|
||||||
|
defer held.release();
|
||||||
|
const stderr = std.io.getStdErr().writer();
|
||||||
|
nosuspend stderr.print(prefix ++ format, args) catch return;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
|
std.log.info(.main, "Starting Bar.", .{});
|
||||||
var allocator: *std.mem.Allocator = undefined;
|
var allocator: *std.mem.Allocator = undefined;
|
||||||
var dbgAlloc: *DebugAllocator = undefined;
|
var dbgAlloc: *DebugAllocator = undefined;
|
||||||
if (debug_allocator) {
|
if (debug_allocator) {
|
||||||
|
@ -42,8 +73,8 @@ pub fn main() !void {
|
||||||
bar.widgets = widgets[0..];
|
bar.widgets = widgets[0..];
|
||||||
try br.start();
|
try br.start();
|
||||||
if (debug_allocator) {
|
if (debug_allocator) {
|
||||||
std.debug.print("Finished cleanup, last allocation info.\n", .{});
|
std.log.debug(.main, "Finished cleanup, last allocation info.\n", .{});
|
||||||
std.debug.print("\n{}\n", .{dbgAlloc.info});
|
std.log.debug(.main, "\n{}\n", .{dbgAlloc.info});
|
||||||
dbgAlloc.printRemainingStackTraces();
|
dbgAlloc.printRemainingStackTraces();
|
||||||
dbgAlloc.deinit();
|
dbgAlloc.deinit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,4 +17,4 @@ relative_x: u16 = 0,
|
||||||
relative_y: u16 = 0,
|
relative_y: u16 = 0,
|
||||||
height: u16 = 0,
|
height: u16 = 0,
|
||||||
width: u16 = 0,
|
width: u16 = 0,
|
||||||
scale: u16 = 0,
|
scale: u16 = 0,
|
||||||
|
|
Loading…
Reference in a new issue