@shd101wyy/yo 0.1.25 → 0.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/skills/yo-async-effects/SKILL.md +4 -4
- package/.github/skills/yo-async-effects/async-effects-recipes.md +40 -40
- package/.github/skills/yo-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +30 -26
- package/.github/skills/yo-project-workflow/SKILL.md +6 -3
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
- package/.github/skills/yo-syntax/SKILL.md +7 -6
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +78 -60
- package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
- package/README.md +10 -8
- package/out/cjs/index.cjs +583 -567
- package/out/cjs/yo-cli.cjs +664 -632
- package/out/cjs/yo-lsp.cjs +510 -485
- package/out/esm/index.mjs +538 -522
- package/out/types/src/codegen/codegen-c.d.ts +2 -2
- package/out/types/src/codegen/functions/collection.d.ts +2 -2
- package/out/types/src/codegen/functions/context.d.ts +3 -2
- package/out/types/src/codegen/types/collection.d.ts +2 -2
- package/out/types/src/codegen/utils/index.d.ts +3 -1
- package/out/types/src/doc/builder.d.ts +2 -2
- package/out/types/src/evaluator/calls/closure-type.d.ts +2 -2
- package/out/types/src/evaluator/calls/record-type.d.ts +11 -0
- package/out/types/src/evaluator/context.d.ts +8 -9
- package/out/types/src/evaluator/index.d.ts +3 -3
- package/out/types/src/evaluator/types/record.d.ts +14 -0
- package/out/types/src/evaluator/types/validation.d.ts +2 -2
- package/out/types/src/evaluator/values/anonymous-module.d.ts +5 -5
- package/out/types/src/evaluator/values/impl.d.ts +1 -1
- package/out/types/src/expr.d.ts +1 -4
- package/out/types/src/formatter.d.ts +11 -0
- package/out/types/src/function-value.d.ts +1 -1
- package/out/types/src/lsp/document-manager.d.ts +1 -1
- package/out/types/src/lsp/formatting.d.ts +2 -0
- package/out/types/src/module-manager.d.ts +3 -3
- package/out/types/src/tests/formatter.test.d.ts +1 -0
- package/out/types/src/types/creators.d.ts +3 -4
- package/out/types/src/types/definitions.d.ts +8 -19
- package/out/types/src/types/guards.d.ts +3 -3
- package/out/types/src/types/tags.d.ts +0 -1
- package/out/types/src/types/utils.d.ts +1 -1
- package/out/types/src/value-tag.d.ts +0 -1
- package/out/types/src/value.d.ts +6 -13
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/std/alg/hash.yo +13 -21
- package/std/allocator.yo +25 -40
- package/std/async.yo +3 -7
- package/std/build.yo +105 -151
- package/std/cli/arg_parser.yo +184 -169
- package/std/collections/array_list.yo +350 -314
- package/std/collections/btree_map.yo +142 -131
- package/std/collections/deque.yo +132 -128
- package/std/collections/hash_map.yo +542 -566
- package/std/collections/hash_set.yo +623 -687
- package/std/collections/linked_list.yo +275 -293
- package/std/collections/ordered_map.yo +113 -85
- package/std/collections/priority_queue.yo +73 -73
- package/std/crypto/md5.yo +191 -95
- package/std/crypto/random.yo +56 -64
- package/std/crypto/sha256.yo +151 -107
- package/std/encoding/base64.yo +87 -81
- package/std/encoding/hex.yo +43 -50
- package/std/encoding/html.yo +56 -81
- package/std/encoding/html_char_utils.yo +7 -13
- package/std/encoding/html_entities.yo +2248 -2253
- package/std/encoding/json.yo +316 -224
- package/std/encoding/punycode.yo +86 -116
- package/std/encoding/toml.yo +67 -66
- package/std/encoding/utf16.yo +37 -44
- package/std/env.yo +62 -91
- package/std/error.yo +12 -20
- package/std/fmt/display.yo +5 -9
- package/std/fmt/index.yo +8 -14
- package/std/fmt/to_string.yo +330 -315
- package/std/fmt/writer.yo +58 -87
- package/std/fs/dir.yo +83 -102
- package/std/fs/file.yo +147 -180
- package/std/fs/metadata.yo +45 -78
- package/std/fs/temp.yo +55 -65
- package/std/fs/types.yo +27 -40
- package/std/fs/walker.yo +53 -68
- package/std/gc.yo +5 -8
- package/std/glob.yo +30 -43
- package/std/http/client.yo +107 -120
- package/std/http/http.yo +106 -96
- package/std/http/index.yo +4 -6
- package/std/imm/list.yo +88 -93
- package/std/imm/map.yo +528 -464
- package/std/imm/set.yo +52 -57
- package/std/imm/sorted_map.yo +340 -286
- package/std/imm/sorted_set.yo +57 -63
- package/std/imm/string.yo +404 -345
- package/std/imm/vec.yo +173 -181
- package/std/io/reader.yo +3 -6
- package/std/io/writer.yo +4 -8
- package/std/libc/assert.yo +5 -9
- package/std/libc/ctype.yo +32 -22
- package/std/libc/dirent.yo +26 -25
- package/std/libc/errno.yo +164 -90
- package/std/libc/fcntl.yo +52 -45
- package/std/libc/float.yo +66 -44
- package/std/libc/limits.yo +42 -33
- package/std/libc/math.yo +53 -82
- package/std/libc/signal.yo +72 -47
- package/std/libc/stdatomic.yo +217 -188
- package/std/libc/stdint.yo +5 -29
- package/std/libc/stdio.yo +5 -29
- package/std/libc/stdlib.yo +32 -39
- package/std/libc/string.yo +5 -23
- package/std/libc/sys/stat.yo +58 -56
- package/std/libc/time.yo +5 -19
- package/std/libc/unistd.yo +5 -20
- package/std/libc/wctype.yo +6 -9
- package/std/libc/windows.yo +26 -30
- package/std/log.yo +41 -55
- package/std/net/addr.yo +102 -97
- package/std/net/dns.yo +27 -28
- package/std/net/errors.yo +50 -49
- package/std/net/tcp.yo +113 -124
- package/std/net/udp.yo +55 -66
- package/std/os/env.yo +35 -33
- package/std/os/signal.yo +15 -25
- package/std/path.yo +276 -311
- package/std/prelude.yo +6316 -4333
- package/std/process/command.yo +87 -103
- package/std/process/index.yo +12 -31
- package/std/regex/compiler.yo +196 -95
- package/std/regex/flags.yo +58 -39
- package/std/regex/index.yo +157 -173
- package/std/regex/match.yo +20 -31
- package/std/regex/node.yo +134 -152
- package/std/regex/parser.yo +283 -259
- package/std/regex/unicode.yo +172 -202
- package/std/regex/vm.yo +155 -171
- package/std/string/index.yo +5 -7
- package/std/string/rune.yo +45 -55
- package/std/string/string.yo +937 -964
- package/std/string/string_builder.yo +94 -104
- package/std/string/unicode.yo +46 -64
- package/std/sync/channel.yo +72 -73
- package/std/sync/cond.yo +31 -36
- package/std/sync/mutex.yo +30 -32
- package/std/sync/once.yo +13 -16
- package/std/sync/rwlock.yo +26 -31
- package/std/sync/waitgroup.yo +20 -25
- package/std/sys/advise.yo +16 -24
- package/std/sys/bufio/buf_reader.yo +77 -93
- package/std/sys/bufio/buf_writer.yo +52 -65
- package/std/sys/clock.yo +4 -9
- package/std/sys/constants.yo +77 -61
- package/std/sys/copy.yo +4 -10
- package/std/sys/dir.yo +26 -43
- package/std/sys/dns.yo +41 -61
- package/std/sys/errors.yo +95 -103
- package/std/sys/events.yo +45 -57
- package/std/sys/externs.yo +319 -267
- package/std/sys/fallocate.yo +7 -11
- package/std/sys/fcntl.yo +14 -22
- package/std/sys/file.yo +26 -40
- package/std/sys/future.yo +5 -8
- package/std/sys/iov.yo +12 -25
- package/std/sys/lock.yo +12 -13
- package/std/sys/mmap.yo +38 -43
- package/std/sys/path.yo +3 -8
- package/std/sys/perm.yo +7 -21
- package/std/sys/pipe.yo +5 -12
- package/std/sys/process.yo +23 -29
- package/std/sys/seek.yo +10 -12
- package/std/sys/signal.yo +7 -13
- package/std/sys/signals.yo +52 -35
- package/std/sys/socket.yo +63 -58
- package/std/sys/socketpair.yo +3 -6
- package/std/sys/sockinfo.yo +11 -20
- package/std/sys/statfs.yo +11 -34
- package/std/sys/statx.yo +25 -52
- package/std/sys/sysinfo.yo +15 -20
- package/std/sys/tcp.yo +62 -92
- package/std/sys/temp.yo +5 -9
- package/std/sys/time.yo +5 -15
- package/std/sys/timer.yo +6 -11
- package/std/sys/tty.yo +10 -18
- package/std/sys/udp.yo +22 -39
- package/std/sys/umask.yo +3 -6
- package/std/sys/unix.yo +33 -52
- package/std/testing/bench.yo +49 -52
- package/std/thread.yo +10 -15
- package/std/time/datetime.yo +105 -89
- package/std/time/duration.yo +43 -56
- package/std/time/instant.yo +13 -18
- package/std/time/sleep.yo +5 -9
- package/std/url/index.yo +184 -209
- package/std/worker.yo +6 -10
- package/out/types/src/evaluator/calls/module-type.d.ts +0 -11
- package/out/types/src/evaluator/types/module.d.ts +0 -19
package/std/libc/time.yo
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
//! C11 `<time.h>` — date and time functions.
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
c_include(
|
|
3
|
+
"<time.h>",
|
|
4
4
|
// Types
|
|
5
5
|
time_t : Type,
|
|
6
6
|
clock_t : Type,
|
|
7
|
-
|
|
8
7
|
// Structure for broken-down time
|
|
9
8
|
tm : Type,
|
|
10
|
-
|
|
11
9
|
// C11 timespec structure
|
|
12
10
|
timespec : Type,
|
|
13
|
-
|
|
14
11
|
// Time manipulation functions
|
|
15
12
|
clock :
|
|
16
13
|
fn() -> clock_t,
|
|
@@ -20,7 +17,6 @@ c_include "<time.h>",
|
|
|
20
17
|
fn(time1 : time_t, time0 : time_t) -> f64,
|
|
21
18
|
mktime :
|
|
22
19
|
fn(timeptr : *(tm)) -> time_t,
|
|
23
|
-
|
|
24
20
|
// Time conversion functions
|
|
25
21
|
asctime :
|
|
26
22
|
fn(timeptr : *(tm)) -> *(char),
|
|
@@ -32,7 +28,6 @@ c_include "<time.h>",
|
|
|
32
28
|
fn(timer : *(time_t)) -> ?*(tm),
|
|
33
29
|
strftime :
|
|
34
30
|
fn(s : *(char), maxsize : usize, format : *(char), timeptr : *(tm)) -> usize,
|
|
35
|
-
|
|
36
31
|
// C11 Annex K - Safe time functions
|
|
37
32
|
asctime_s :
|
|
38
33
|
fn(s : *(char), maxsize : usize, timeptr : *(tm)) -> int,
|
|
@@ -42,51 +37,42 @@ c_include "<time.h>",
|
|
|
42
37
|
fn(timer : *(time_t), result : *(tm)) -> ?*(tm),
|
|
43
38
|
localtime_s :
|
|
44
39
|
fn(timer : *(time_t), result : *(tm)) -> ?*(tm),
|
|
45
|
-
|
|
46
40
|
// C11 - Additional time functions
|
|
47
41
|
timespec_get :
|
|
48
42
|
fn(ts : *(timespec), base : int) -> int,
|
|
49
|
-
|
|
50
43
|
// Constants
|
|
51
44
|
CLOCKS_PER_SEC : clock_t,
|
|
52
|
-
|
|
53
45
|
// C11 timespec_get base values
|
|
54
46
|
TIME_UTC : int,
|
|
55
|
-
|
|
56
47
|
// Additional useful constants (implementation-defined)
|
|
57
48
|
CLK_TCK : clock_t
|
|
58
|
-
;
|
|
59
|
-
|
|
49
|
+
);
|
|
60
50
|
// Export all time functionality
|
|
61
|
-
export
|
|
51
|
+
export(
|
|
62
52
|
// Types
|
|
63
53
|
time_t,
|
|
64
54
|
clock_t,
|
|
65
55
|
tm,
|
|
66
56
|
timespec,
|
|
67
|
-
|
|
68
57
|
// Time manipulation
|
|
69
58
|
clock,
|
|
70
59
|
time,
|
|
71
60
|
difftime,
|
|
72
61
|
mktime,
|
|
73
|
-
|
|
74
62
|
// Time conversion
|
|
75
63
|
asctime,
|
|
76
64
|
ctime,
|
|
77
65
|
gmtime,
|
|
78
66
|
localtime,
|
|
79
67
|
strftime,
|
|
80
|
-
|
|
81
68
|
// C11 safe functions
|
|
82
69
|
asctime_s,
|
|
83
70
|
ctime_s,
|
|
84
71
|
gmtime_s,
|
|
85
72
|
localtime_s,
|
|
86
73
|
timespec_get,
|
|
87
|
-
|
|
88
74
|
// Constants
|
|
89
75
|
CLOCKS_PER_SEC,
|
|
90
76
|
TIME_UTC,
|
|
91
77
|
CLK_TCK
|
|
92
|
-
;
|
|
78
|
+
);
|
package/std/libc/unistd.yo
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
//! POSIX `<unistd.h>` — standard symbolic constants and types.
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
c_include(
|
|
3
|
+
"<unistd.h>",
|
|
4
4
|
// Types
|
|
5
5
|
// usize : Type,
|
|
6
6
|
ssize_t : Type,
|
|
@@ -8,7 +8,6 @@ c_include "<unistd.h>",
|
|
|
8
8
|
pid_t : Type,
|
|
9
9
|
uid_t : Type,
|
|
10
10
|
gid_t : Type,
|
|
11
|
-
|
|
12
11
|
// File descriptor operations
|
|
13
12
|
read :
|
|
14
13
|
(fn(fd : int, buf : *(void), count : usize) -> ssize_t),
|
|
@@ -24,12 +23,10 @@ c_include "<unistd.h>",
|
|
|
24
23
|
(fn(oldfd : int) -> int),
|
|
25
24
|
dup2 :
|
|
26
25
|
(fn(oldfd : int, newfd : int) -> int),
|
|
27
|
-
|
|
28
26
|
// Standard file descriptors
|
|
29
27
|
STDIN_FILENO : int,
|
|
30
28
|
STDOUT_FILENO : int,
|
|
31
29
|
STDERR_FILENO : int,
|
|
32
|
-
|
|
33
30
|
// File access
|
|
34
31
|
access :
|
|
35
32
|
(fn(pathname : *(char), mode : int) -> int),
|
|
@@ -41,7 +38,6 @@ c_include "<unistd.h>",
|
|
|
41
38
|
(fn(path : *(char)) -> int),
|
|
42
39
|
getcwd :
|
|
43
40
|
(fn(buf : *(char), size : usize) -> ?*(char)),
|
|
44
|
-
|
|
45
41
|
// Process control
|
|
46
42
|
fork :
|
|
47
43
|
(fn() -> pid_t),
|
|
@@ -53,7 +49,6 @@ c_include "<unistd.h>",
|
|
|
53
49
|
(fn() -> pid_t),
|
|
54
50
|
getppid :
|
|
55
51
|
(fn() -> pid_t),
|
|
56
|
-
|
|
57
52
|
// User/Group IDs
|
|
58
53
|
getuid :
|
|
59
54
|
(fn() -> uid_t),
|
|
@@ -63,29 +58,25 @@ c_include "<unistd.h>",
|
|
|
63
58
|
(fn() -> gid_t),
|
|
64
59
|
getegid :
|
|
65
60
|
(fn() -> gid_t),
|
|
66
|
-
|
|
67
61
|
// Sleep
|
|
68
62
|
sleep :
|
|
69
63
|
(fn(seconds : u32) -> u32),
|
|
70
64
|
usleep :
|
|
71
65
|
(fn(usec : u32) -> int),
|
|
72
|
-
|
|
73
66
|
// File access modes for access()
|
|
74
67
|
R_OK : int,
|
|
75
68
|
W_OK : int,
|
|
76
69
|
X_OK : int,
|
|
77
70
|
F_OK : int
|
|
78
|
-
;
|
|
79
|
-
|
|
71
|
+
);
|
|
80
72
|
// Export all unistd functionality
|
|
81
|
-
export
|
|
73
|
+
export(
|
|
82
74
|
// Types
|
|
83
75
|
ssize_t,
|
|
84
76
|
off_t,
|
|
85
77
|
pid_t,
|
|
86
78
|
uid_t,
|
|
87
79
|
gid_t,
|
|
88
|
-
|
|
89
80
|
// File descriptor operations
|
|
90
81
|
read,
|
|
91
82
|
write,
|
|
@@ -94,39 +85,33 @@ export
|
|
|
94
85
|
pipe,
|
|
95
86
|
dup,
|
|
96
87
|
dup2,
|
|
97
|
-
|
|
98
88
|
// Standard file descriptors
|
|
99
89
|
STDIN_FILENO,
|
|
100
90
|
STDOUT_FILENO,
|
|
101
91
|
STDERR_FILENO,
|
|
102
|
-
|
|
103
92
|
// File access
|
|
104
93
|
access,
|
|
105
94
|
unlink,
|
|
106
95
|
rmdir,
|
|
107
96
|
chdir,
|
|
108
97
|
getcwd,
|
|
109
|
-
|
|
110
98
|
// Process control
|
|
111
99
|
fork,
|
|
112
100
|
execve,
|
|
113
101
|
_exit,
|
|
114
102
|
getpid,
|
|
115
103
|
getppid,
|
|
116
|
-
|
|
117
104
|
// User/Group IDs
|
|
118
105
|
getuid,
|
|
119
106
|
geteuid,
|
|
120
107
|
getgid,
|
|
121
108
|
getegid,
|
|
122
|
-
|
|
123
109
|
// Sleep
|
|
124
110
|
sleep,
|
|
125
111
|
usleep,
|
|
126
|
-
|
|
127
112
|
// File access modes
|
|
128
113
|
R_OK,
|
|
129
114
|
W_OK,
|
|
130
115
|
X_OK,
|
|
131
116
|
F_OK
|
|
132
|
-
;
|
|
117
|
+
);
|
package/std/libc/wctype.yo
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
//! C11 `<wctype.h>` — wide character classification and conversion functions.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
c_include "<wctype.h>",
|
|
2
|
+
{ wint_t } :: import("./stdint");
|
|
3
|
+
c_include(
|
|
4
|
+
"<wctype.h>",
|
|
6
5
|
// Wide character classification functions
|
|
7
6
|
iswalnum :
|
|
8
7
|
fn(wc : wint_t) -> int,
|
|
@@ -28,15 +27,13 @@ c_include "<wctype.h>",
|
|
|
28
27
|
fn(wc : wint_t) -> int,
|
|
29
28
|
iswxdigit :
|
|
30
29
|
fn(wc : wint_t) -> int,
|
|
31
|
-
|
|
32
30
|
// Wide character conversion functions
|
|
33
31
|
towlower :
|
|
34
32
|
fn(wc : wint_t) -> wint_t,
|
|
35
33
|
towupper :
|
|
36
34
|
fn(wc : wint_t) -> wint_t
|
|
37
|
-
;
|
|
38
|
-
|
|
39
|
-
export
|
|
35
|
+
);
|
|
36
|
+
export(
|
|
40
37
|
iswalnum,
|
|
41
38
|
iswalpha,
|
|
42
39
|
iswblank,
|
|
@@ -51,4 +48,4 @@ export
|
|
|
51
48
|
iswxdigit,
|
|
52
49
|
towlower,
|
|
53
50
|
towupper
|
|
54
|
-
;
|
|
51
|
+
);
|
package/std/libc/windows.yo
CHANGED
|
@@ -1,46 +1,42 @@
|
|
|
1
1
|
//! Windows-specific C API bindings.
|
|
2
2
|
//! Only available when compiling for Windows.
|
|
3
|
-
|
|
4
3
|
// Windows type aliases (using Yo types that match the C types)
|
|
5
|
-
DWORD :: ulong;
|
|
6
|
-
WCHAR :: ushort;
|
|
7
|
-
BOOL :: int;
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
c_include "<windows.h>",
|
|
4
|
+
DWORD :: ulong; // unsigned long on Windows
|
|
5
|
+
WCHAR :: ushort; // unsigned short (UTF-16 code unit)
|
|
6
|
+
BOOL :: int; // int
|
|
7
|
+
export(DWORD);
|
|
8
|
+
export(WCHAR);
|
|
9
|
+
export(BOOL);
|
|
10
|
+
c_include(
|
|
11
|
+
"<windows.h>",
|
|
14
12
|
// GetCurrentDirectoryW - Gets the current directory (Unicode version)
|
|
15
13
|
// Returns the length of the string copied to the buffer (excluding null terminator)
|
|
16
14
|
// If the buffer is too small, returns the required size (including null terminator)
|
|
17
15
|
GetCurrentDirectoryW : (fn(nBufferLength : DWORD, lpBuffer : ?*(WCHAR)) -> DWORD),
|
|
18
|
-
|
|
19
16
|
// SetCurrentDirectoryA - Sets the current directory (ANSI version)
|
|
20
17
|
// Returns nonzero if successful, zero on failure
|
|
21
18
|
SetCurrentDirectoryA : (fn(lpPathName : *(char)) -> BOOL),
|
|
22
|
-
|
|
23
19
|
// WideCharToMultiByte - Converts UTF-16 to UTF-8
|
|
24
|
-
WideCharToMultiByte : (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
20
|
+
WideCharToMultiByte : (
|
|
21
|
+
fn(
|
|
22
|
+
CodePage : u32,
|
|
23
|
+
dwFlags : DWORD,
|
|
24
|
+
lpWideCharStr : ?*(WCHAR),
|
|
25
|
+
cchWideChar : i32,
|
|
26
|
+
lpMultiByteStr : ?*(u8),
|
|
27
|
+
cbMultiByte : i32,
|
|
28
|
+
lpDefaultChar : ?*(u8),
|
|
29
|
+
lpUsedDefaultChar : ?*(i32)
|
|
30
|
+
) -> i32
|
|
31
|
+
),
|
|
35
32
|
// _putenv_s - Sets environment variable (Windows-specific)
|
|
36
33
|
// Returns 0 on success, non-zero on error
|
|
37
34
|
_putenv_s : (fn(varname : *(char), value_string : *(char)) -> int),
|
|
38
|
-
|
|
39
35
|
// Code page constants
|
|
40
36
|
CP_UTF8 : u32
|
|
41
|
-
;
|
|
42
|
-
export
|
|
43
|
-
export
|
|
44
|
-
export
|
|
45
|
-
export
|
|
46
|
-
export
|
|
37
|
+
);
|
|
38
|
+
export(GetCurrentDirectoryW);
|
|
39
|
+
export(SetCurrentDirectoryA);
|
|
40
|
+
export(WideCharToMultiByte);
|
|
41
|
+
export(_putenv_s);
|
|
42
|
+
export(CP_UTF8);
|
package/std/log.yo
CHANGED
|
@@ -1,71 +1,61 @@
|
|
|
1
1
|
//! Structured logging with configurable level and output destination.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
{
|
|
5
|
-
{ fwrite, stdout, stderr, FILE } :: import "./libc/stdio";
|
|
6
|
-
|
|
2
|
+
open(import("./string"));
|
|
3
|
+
{ ToString } :: import("./fmt");
|
|
4
|
+
{ fwrite, stdout, stderr, FILE } :: import("./libc/stdio");
|
|
7
5
|
/// Log severity level.
|
|
8
6
|
Level :: enum(Trace, Debug, Info, Warn, Error);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
impl(
|
|
8
|
+
Level,
|
|
9
|
+
ToString(
|
|
10
|
+
to_string : (fn(self : *(Self)) -> String)(
|
|
11
|
+
match(
|
|
12
|
+
self.*,
|
|
13
|
+
.Trace => String.from("TRACE"),
|
|
14
|
+
.Debug => String.from("DEBUG"),
|
|
15
|
+
.Info => String.from("INFO "),
|
|
16
|
+
.Warn => String.from("WARN "),
|
|
17
|
+
.Error => String.from("ERROR")
|
|
18
|
+
)
|
|
18
19
|
)
|
|
19
20
|
)
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
export Level;
|
|
23
|
-
|
|
21
|
+
);
|
|
22
|
+
export(Level);
|
|
24
23
|
/// Where log messages are written.
|
|
25
24
|
LogOutput :: enum(
|
|
26
25
|
Stderr,
|
|
27
26
|
Stdout
|
|
28
27
|
);
|
|
29
|
-
|
|
30
|
-
export LogOutput;
|
|
31
|
-
|
|
28
|
+
export(LogOutput);
|
|
32
29
|
// ============================================================================
|
|
33
30
|
// Global logger state (module-level mutable)
|
|
34
31
|
// ============================================================================
|
|
35
|
-
|
|
36
|
-
_global_level := Level.Info;
|
|
32
|
+
_global_level := Level.Info;
|
|
37
33
|
_global_output := LogOutput.Stderr;
|
|
38
|
-
|
|
39
34
|
// ============================================================================
|
|
40
35
|
// Configuration
|
|
41
36
|
// ============================================================================
|
|
42
|
-
|
|
43
|
-
set_level :: (fn(level: Level) -> unit)({
|
|
37
|
+
set_level :: (fn(level : Level) -> unit)({
|
|
44
38
|
_global_level = level;
|
|
45
39
|
});
|
|
46
|
-
|
|
47
|
-
set_output :: (fn(output: LogOutput) -> unit)({
|
|
40
|
+
set_output :: (fn(output : LogOutput) -> unit)({
|
|
48
41
|
_global_output = output;
|
|
49
42
|
});
|
|
50
|
-
|
|
51
|
-
export set_level, set_output;
|
|
52
|
-
|
|
43
|
+
export(set_level, set_output);
|
|
53
44
|
// ============================================================================
|
|
54
45
|
// Core log function
|
|
55
46
|
// ============================================================================
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
_level_value :: (fn(l : Level) -> i32)(
|
|
48
|
+
match(
|
|
49
|
+
l,
|
|
59
50
|
.Trace => i32(0),
|
|
60
51
|
.Debug => i32(1),
|
|
61
|
-
.Info
|
|
62
|
-
.Warn
|
|
52
|
+
.Info => i32(2),
|
|
53
|
+
.Warn => i32(3),
|
|
63
54
|
.Error => i32(4)
|
|
64
55
|
)
|
|
65
56
|
);
|
|
66
|
-
|
|
67
57
|
// Write all bytes from a String to the file handle.
|
|
68
|
-
_write_string :: (fn(s: String, dest: *(FILE)) -> unit)({
|
|
58
|
+
_write_string :: (fn(s : String, dest : *(FILE)) -> unit)({
|
|
69
59
|
bytes := s.as_bytes();
|
|
70
60
|
cond(
|
|
71
61
|
(bytes.len() > usize(0)) => {
|
|
@@ -75,19 +65,19 @@ _write_string :: (fn(s: String, dest: *(FILE)) -> unit)({
|
|
|
75
65
|
true => ()
|
|
76
66
|
);
|
|
77
67
|
});
|
|
78
|
-
|
|
79
|
-
log :: (fn(level: Level, msg: String) -> unit)({
|
|
68
|
+
log :: (fn(level : Level, msg : String) -> unit)({
|
|
80
69
|
cond(
|
|
81
|
-
(_level_value(level) < _level_value(_global_level)) => {
|
|
70
|
+
(_level_value(level) < _level_value(_global_level)) => {
|
|
71
|
+
return(());
|
|
72
|
+
},
|
|
82
73
|
true => ()
|
|
83
74
|
);
|
|
84
75
|
level_str := level.to_string();
|
|
85
|
-
|
|
86
|
-
|
|
76
|
+
dest := match(
|
|
77
|
+
_global_output,
|
|
87
78
|
.Stdout => stdout,
|
|
88
79
|
.Stderr => stderr
|
|
89
80
|
);
|
|
90
|
-
|
|
91
81
|
// Write "[LEVEL] msg\n"
|
|
92
82
|
fwrite(*(void)(*(u8)("[")), usize(1), usize(1), dest);
|
|
93
83
|
_write_string(level_str, dest);
|
|
@@ -95,17 +85,13 @@ log :: (fn(level: Level, msg: String) -> unit)({
|
|
|
95
85
|
_write_string(msg, dest);
|
|
96
86
|
fwrite(*(void)(*(u8)("\n")), usize(1), usize(1), dest);
|
|
97
87
|
});
|
|
98
|
-
|
|
99
|
-
export log;
|
|
100
|
-
|
|
88
|
+
export(log);
|
|
101
89
|
// ============================================================================
|
|
102
90
|
// Convenience functions
|
|
103
91
|
// ============================================================================
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
export trace, debug, info, warn, error;
|
|
92
|
+
trace :: (fn(msg : String) -> unit)(log(.Trace, msg));
|
|
93
|
+
debug :: (fn(msg : String) -> unit)(log(.Debug, msg));
|
|
94
|
+
info :: (fn(msg : String) -> unit)(log(.Info, msg));
|
|
95
|
+
warn :: (fn(msg : String) -> unit)(log(.Warn, msg));
|
|
96
|
+
error :: (fn(msg : String) -> unit)(log(.Error, msg));
|
|
97
|
+
export(trace, debug, info, warn, error);
|