-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.zig
165 lines (137 loc) · 5 KB
/
syslog.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//---------------------------------
const std = @import("std");
const Mutex = std.Thread.Mutex;
const Allocator = std.mem.Allocator;
pub const rfc5424 = @import("rfc5424.zig");
pub const application = @import("application.zig");
pub const transport = @import("transport.zig");
//---------------------------------
//---------------------------------
pub const TransportOpts = transport.TransportOpts;
pub const Protocol = transport.Protocol;
pub const ApplicationOpts = application.ApplicationOpts;
pub const Formatter = rfc5424.Formatter;
pub const Severity = rfc5424.Severity;
pub const Sender = transport.Sender;
//---------------------------------
pub const SyslogOpts = struct {
// application:
name: []const u8 = application.DefaultAppName,
fcl: rfc5424.Facility = application.DefaultFacility,
// transport:
proto: Protocol = transport.DefaultProto,
addr: []const u8 = transport.DefaultAddr,
port: u16 = transport.DafaultPort,
};
pub const Syslog = struct {
mutex: Mutex = .{},
frmtr: Formatter = undefined,
sndr: Sender = undefined,
ready: bool = false,
filter: ?Severity = null,
pub fn init(slog: *Syslog, allocator: Allocator, conf: SyslogOpts) !void {
slog.mutex.lock();
defer slog.mutex.unlock();
if (slog.ready) {
return error.WasInit;
}
_ = try slog.frmtr.init(allocator, .{ .name = conf.name, .fcl = conf.fcl });
slog.sndr = try Sender.connect(allocator, .{ .proto = conf.proto, .addr = conf.addr, .port = conf.port });
slog.ready = true;
}
pub fn deinit(slog: *Syslog) void {
slog.mutex.lock();
defer slog.mutex.unlock();
if (!slog.ready) {
return;
}
slog.frmtr.deinit();
slog.sndr.disconnect();
slog.ready = false;
return;
}
pub inline fn write_emerg(slog: *Syslog, msg: []const u8) !void {
return slog.write(.emerg, msg);
}
pub inline fn write_alert(slog: *Syslog, msg: []const u8) !void {
return slog.write(.alert, msg);
}
pub inline fn write_crit(slog: *Syslog, msg: []const u8) !void {
return slog.write(.crit, msg);
}
pub inline fn write_err(slog: *Syslog, msg: []const u8) !void {
return slog.write(.err, msg);
}
pub inline fn write_warning(slog: *Syslog, msg: []const u8) !void {
return slog.write(.warning, msg);
}
pub inline fn write_notice(slog: *Syslog, msg: []const u8) !void {
return slog.write(.notice, msg);
}
pub inline fn write_info(slog: *Syslog, msg: []const u8) !void {
return slog.write(.info, msg);
}
pub inline fn write_debug(slog: *Syslog, msg: []const u8) !void {
return slog.write(.debug, msg);
}
pub inline fn print_emerg(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.emerg, fmt, msg);
}
pub inline fn print_alert(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.alert, fmt, msg);
}
pub inline fn print_crit(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.crit, fmt, msg);
}
pub inline fn print_err(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.err, fmt, msg);
}
pub inline fn print_warning(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.warning, fmt, msg);
}
pub inline fn print_notice(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.notice, fmt, msg);
}
pub inline fn print_info(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.info, fmt, msg);
}
pub inline fn print_debug(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {
return slog.print(.debug, fmt, msg);
}
pub fn write(slog: *Syslog, svr: Severity, msg: []const u8) !void {
slog.mutex.lock();
defer slog.mutex.unlock();
if (!slog.ready) {
return error.NotReady;
}
if(slog.filter != null){
if (@intFromEnum(svr) >= @intFromEnum(slog.filter.?)) {
return;
}
}
_ = try slog.*.sndr.send(try slog.*.frmtr.build(svr, msg));
errdefer slog.ready = false;
return;
}
pub fn print(slog: *Syslog, svr: Severity, comptime fmt: []const u8, msg: anytype) !void {
slog.mutex.lock();
defer slog.mutex.unlock();
if (!slog.ready) {
return error.NotReady;
}
if(slog.filter != null){
if (@intFromEnum(svr) >= @intFromEnum(slog.filter.?)) {
return;
}
}
_ = try slog.*.sndr.send(try slog.*.frmtr.format(svr, fmt, msg));
errdefer slog.ready = false;
return;
}
pub fn setfilter(slog: *Syslog, svr: ?Severity) void {
slog.mutex.lock();
defer slog.mutex.unlock();
slog.filter = svr;
return;
}
};