code / tomo-log

Lines52 Tomo44 Markdown8
(55 lines)
1 use <time.h>
2 use <stdio.h>
4 timestamp_format := CString("%F %T")
6 log_writers : @[func(text:Text, close:Bool=no -> Result)]
8 func _timestamp(->Text)
9 c_str := C_code:CString`
10 char *str = GC_MALLOC_ATOMIC(20);
11 time_t t; time(&t);
12 struct tm *tm_info = localtime(&t);
13 strftime(str, 20, "%F %T", tm_info);
14 str
16 return c_str.as_text()
18 func info(text:Text, newline=yes)
19 say("\[2]⚫ $text\[]", newline)
20 for write in log_writers
21 write("$(_timestamp()) [info] $text\n")!
23 func debug(text:Text, newline=yes)
24 say("\[32]🟢 $text\[]", newline)
25 for write in log_writers
26 write("$(_timestamp()) [debug] $text\n")!
28 func warn(text:Text, newline=yes)
29 say("\[33;1]🟡 $text\[]", newline)
30 for write in log_writers
31 write("$(_timestamp()) [warn] $text\n")!
33 func error(text:Text, newline=yes)
34 say("\[31;1]🔴 $text\[]", newline)
35 for write in log_writers
36 write("$(_timestamp()) [error] $text\n")!
38 func log(text:Text, tag:Text="log", emoji="🔵", color="\[34]", newline=yes)
39 say("$color$emoji $text\[]", newline)
40 tag = "[$tag]".right_pad("[error]".length)
41 for write in log_writers
42 write("$(_timestamp()) $tag $text\n")!
44 func add_logfile(file:Path, append:Bool=yes)
45 log_writers.insert(file.writer(append=append))
47 func main()
48 add_logfile((./log.txt), append=no)
49 >> info("Hello")
50 >> debug("Hello")
51 >> warn("Hello")
52 >> error("Hello")
53 >> log("Hello")
54 >> log("Hello", tag="penguin", emoji="🐧", color="\[36]")