authorgravatar for heidezomp@protonmail.comheidezomp <heidezomp@protonmail.com> 2020-08-12 14:03:02+02:00
committergravatar for heidezomp@protonmail.comheidezomp <heidezomp@protonmail.com> 2020-08-12 14:03:02+02:00
log7db2c11537552250462a5f4ab162e5ef4183489c
tree3ff706490e57f2b164d495e2d93c43213154dc32
parent67d684d89aeb0bb3cfa86c57e8d77359d45743fa

std.log: add scoped logging struct

* Add a std.log.scoped function that returns a scoped logging struct * Add a std.log.default struct that logs using the .default scope Implementation of daurnimator's proposal: https://github.com/ziglang/zig/issues/5943#issuecomment-669043489 Note that I named the function "scoped" instead of "scope" so as not to clash with the scope parameter that is used everywhere; this seemed a better solution to me than renaming the scope parameter to "s" or "log_scope" or the like.

1 files changed, 84 insertions(+), 0 deletions(-)

lib/std/log.zig+84
......@@ -200,3 +200,87 @@ pub fn debug(
200200) void {
201201 log(.debug, scope, format, args);
202202}
203
204pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
205 return struct {
206 /// Log an emergency message to stderr. This log level is intended to be used
207 /// for conditions that cannot be handled and is usually followed by a panic.
208 pub fn emerg(
209 comptime format: []const u8,
210 args: anytype,
211 ) void {
212 @setCold(true);
213 log(.emerg, scope, format, args);
214 }
215
216 /// Log an alert message to stderr. This log level is intended to be used for
217 /// conditions that should be corrected immediately (e.g. database corruption).
218 pub fn alert(
219 comptime format: []const u8,
220 args: anytype,
221 ) void {
222 @setCold(true);
223 log(.alert, scope, format, args);
224 }
225
226 /// Log a critical message to stderr. This log level is intended to be used
227 /// when a bug has been detected or something has gone wrong and it will have
228 /// an effect on the operation of the program.
229 pub fn crit(
230 comptime format: []const u8,
231 args: anytype,
232 ) void {
233 @setCold(true);
234 log(.crit, scope, format, args);
235 }
236
237 /// Log an error message to stderr. This log level is intended to be used when
238 /// a bug has been detected or something has gone wrong but it is recoverable.
239 pub fn err(
240 comptime format: []const u8,
241 args: anytype,
242 ) void {
243 @setCold(true);
244 log(.err, scope, format, args);
245 }
246
247 /// Log a warning message to stderr. This log level is intended to be used if
248 /// it is uncertain whether something has gone wrong or not, but the
249 /// circumstances would be worth investigating.
250 pub fn warn(
251 comptime format: []const u8,
252 args: anytype,
253 ) void {
254 log(.warn, scope, format, args);
255 }
256
257 /// Log a notice message to stderr. This log level is intended to be used for
258 /// non-error but significant conditions.
259 pub fn notice(
260 comptime format: []const u8,
261 args: anytype,
262 ) void {
263 log(.notice, scope, format, args);
264 }
265
266 /// Log an info message to stderr. This log level is intended to be used for
267 /// general messages about the state of the program.
268 pub fn info(
269 comptime format: []const u8,
270 args: anytype,
271 ) void {
272 log(.info, scope, format, args);
273 }
274
275 /// Log a debug message to stderr. This log level is intended to be used for
276 /// messages which are only useful for debugging.
277 pub fn debug(
278 comptime format: []const u8,
279 args: anytype,
280 ) void {
281 log(.debug, scope, format, args);
282 }
283 };
284}
285
286pub const default = scoped(.default);