diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 02:25:17 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 02:25:17 -0400 |
| commit | 790bbab30374f6317ebbc1e6be6a94236e11e370 (patch) | |
| tree | 3a16c14fc29260130d9bd06900cbfabc98c9cadf /examples/log.tm | |
| parent | d4d803fa15faedaf5d6cc032121d2c70292dbd69 (diff) | |
Added logging utility
Diffstat (limited to 'examples/log.tm')
| -rw-r--r-- | examples/log.tm | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/log.tm b/examples/log.tm new file mode 100644 index 00000000..765fc61f --- /dev/null +++ b/examples/log.tm @@ -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") + |
