Added logging utility
This commit is contained in:
parent
d4d803fa15
commit
790bbab303
@ -4,5 +4,6 @@
|
|||||||
(using [libaco](https://libaco.org)).
|
(using [libaco](https://libaco.org)).
|
||||||
- [game/](game/): An example game using raylib.
|
- [game/](game/): An example game using raylib.
|
||||||
- [ini.tm](ini.tm): An INI configuration file reader tool.
|
- [ini.tm](ini.tm): An INI configuration file reader tool.
|
||||||
|
- [log.tm](log.tm): A logging utility.
|
||||||
- [vectors.tm](vectors.tm): A math vector library.
|
- [vectors.tm](vectors.tm): A math vector library.
|
||||||
- [wrap.tm](wrap.tm): A command-line program to wrap text.
|
- [wrap.tm](wrap.tm): A command-line program to wrap text.
|
||||||
|
53
examples/log.tm
Normal file
53
examples/log.tm
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
use <time.h>
|
||||||
|
use <stdio.h>
|
||||||
|
|
||||||
|
timestamp_format := CString("%F %T")
|
||||||
|
|
||||||
|
logfiles := {:Path}
|
||||||
|
|
||||||
|
func _timestamp()->Text:
|
||||||
|
return inline C (
|
||||||
|
({
|
||||||
|
char *str = GC_MALLOC_ATOMIC(20);
|
||||||
|
time_t t;
|
||||||
|
time(&t);
|
||||||
|
struct tm *tm_info;
|
||||||
|
tm_info = localtime(&t);
|
||||||
|
strftime(str, 20, "%F %T", tm_info);
|
||||||
|
Text$format("%s", str);
|
||||||
|
})
|
||||||
|
) : Text
|
||||||
|
|
||||||
|
func info(text:Text, newline=yes):
|
||||||
|
say("$\[2]⚫ $text$\[]", newline)
|
||||||
|
for file in logfiles:
|
||||||
|
file:append("$(_timestamp()) [info] $text$\n")
|
||||||
|
|
||||||
|
func debug(text:Text, newline=yes):
|
||||||
|
say("$\[32]🟢 $text$\[]", newline)
|
||||||
|
for file in logfiles:
|
||||||
|
file:append("$(_timestamp()) [debug] $text$\n")
|
||||||
|
|
||||||
|
func warn(text:Text, newline=yes):
|
||||||
|
say("$\[33;1]🟡 $text$\[]", newline)
|
||||||
|
for file in logfiles:
|
||||||
|
file:append("$(_timestamp()) [warn] $text$\n")
|
||||||
|
|
||||||
|
func error(text:Text, newline=yes):
|
||||||
|
say("$\[31;1]🔴 $text$\[]", newline)
|
||||||
|
for file in logfiles:
|
||||||
|
file:append("$(_timestamp()) [error] $text$\n")
|
||||||
|
|
||||||
|
func add_logfile(file:Path):
|
||||||
|
logfiles:add(file)
|
||||||
|
|
||||||
|
func remove_logfile(file:Path):
|
||||||
|
logfiles:remove(file)
|
||||||
|
|
||||||
|
func main():
|
||||||
|
add_logfile((./log.txt))
|
||||||
|
>> info("Hello")
|
||||||
|
>> debug("Hello")
|
||||||
|
>> warn("Hello")
|
||||||
|
>> error("Hello")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user