Skip to content

Commit 2449c55

Browse files
authored
chore: add trace logging (#17)
1 parent 269d83d commit 2449c55

File tree

6 files changed

+36
-14
lines changed

6 files changed

+36
-14
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Revision history for extism-pdk
22

3+
## 1.2.0.0
4+
5+
* Add bindings to `http_headers` Extism host funtion
6+
* Add `trace` level logging
7+
38
## 1.1.0.0
49

510
* Remove calls to free where Extism host expects to take ownership

examples/CountVowels.hs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ isVowel c =
2222
countVowels = do
2323
-- Get input string from Extism host
2424
s <- input
25+
26+
-- Log input
27+
() <- Extism.PDK.log LogInfo ("Got input: " ++ s)
28+
2529
-- Calculate the number of vowels
2630
let count = length (filter isVowel s)
2731
-- Return a JSON object {"count": count} back to the host

extism-pdk.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.0
22
name: extism-pdk
3-
version: 1.1.0.0
3+
version: 1.2.0.0
44

55
-- A short (one-line) description of the package.
66
synopsis: Extism Plugin Development Kit

src/Extism/PDK.hs

+18-13
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,23 @@ setError msg = do
103103
extismSetError $ memoryOffset s
104104

105105
-- | Log level
106-
data LogLevel = LogInfo | LogDebug | LogWarn | LogError
106+
data LogLevel = LogTrace | LogDebug | LogInfo | LogWarn | LogError deriving (Enum)
107107

108108
-- | Log to configured log file
109109
log :: LogLevel -> String -> IO ()
110-
log LogInfo msg = do
111-
s <- allocString msg
112-
extismLogInfo (memoryOffset s)
113-
log LogDebug msg = do
114-
s <- allocString msg
115-
extismLogDebug (memoryOffset s)
116-
log LogWarn msg = do
117-
s <- allocString msg
118-
extismLogWarn (memoryOffset s)
119-
log LogError msg = do
120-
s <- allocString msg
121-
extismLogError (memoryOffset s)
110+
log level msg = do
111+
configuredLevel <- extismGetLogLevel
112+
if fromIntegral (fromEnum level) < configuredLevel
113+
then return ()
114+
else do
115+
s <- allocString msg
116+
let offs = memoryOffset s
117+
case level of
118+
LogTrace -> extismLogTrace offs
119+
LogDebug -> extismLogDebug offs
120+
LogInfo -> extismLogInfo offs
121+
LogWarn -> extismLogWarn offs
122+
LogError -> extismLogError offs
122123

123124
-- Log with "error" level
124125
logError :: String -> IO ()
@@ -135,3 +136,7 @@ logDebug = Extism.PDK.log LogDebug
135136
-- Log with "warn" level
136137
logWarn :: String -> IO ()
137138
logWarn = Extism.PDK.log LogWarn
139+
140+
-- Log with "trace" level
141+
logTrace :: String -> IO ()
142+
logTrace = Extism.PDK.log LogTrace

src/Extism/PDK/Bindings.hs

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ foreign import ccall "extism_log_debug" extismLogDebug :: MemoryOffset -> IO ()
3737

3838
foreign import ccall "extism_log_error" extismLogError :: MemoryOffset -> IO ()
3939

40+
foreign import ccall "extism_log_trace" extismLogTrace :: MemoryOffset -> IO ()
41+
42+
foreign import ccall "extism_get_log_level" extismGetLogLevel :: IO Int32
43+
4044
foreign import ccall "extism_store_u8" extismStoreU8 :: MemoryOffset -> Word8 -> IO ()
4145

4246
foreign import ccall "extism_store_u64" extismStoreU64 :: MemoryOffset -> Word64 -> IO ()

src/extism-pdk.c

+4
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ DEFINE(log_warn, void, ExtismPointer)
7676
void extism_log_warn(ExtismPointer p) { return _log_warn(p); }
7777
DEFINE(log_error, void, ExtismPointer)
7878
void extism_log_error(ExtismPointer p) { return _log_error(p); }
79+
DEFINE(log_trace, void, ExtismPointer)
80+
void extism_log_trace(ExtismPointer p) { return _log_trace(p); }
81+
DEFINE(get_log_level, int32_t)
82+
int32_t extism_get_log_level() { return _get_log_level(); }

0 commit comments

Comments
 (0)