88// https://github.com/restatedev/sdk-java/blob/main/LICENSE
99package dev .restate .sdk .core .sharedcore ;
1010
11+ import com .dylibso .chicory .annotations .HostModule ;
12+ import com .dylibso .chicory .annotations .WasmExport ;
13+ import com .dylibso .chicory .runtime .HostFunction ;
1114import com .dylibso .chicory .runtime .ImportValues ;
1215import com .dylibso .chicory .runtime .Instance ;
1316import com .dylibso .chicory .runtime .Memory ;
1720import dev .restate .sdk .core .sharedcore .generated .SharedCoreWasmMachine ;
1821import java .io .IOException ;
1922import java .util .function .Function ;
23+ import org .apache .logging .log4j .Level ;
24+ import org .apache .logging .log4j .LogManager ;
25+ import org .apache .logging .log4j .Logger ;
2026
2127class SharedCoreInstance {
2228
23- static final CBORMapper CBOR = CBORMapper .builder ()
24- .build ();
25- static final int LOG_LEVEL_INFO = 2 ;
26-
27- private static final WasmModule WASM_MODULE ;
28-
29- static {
30- try {
31- WASM_MODULE = dev .restate .sdk .core .sharedcore .generated .SharedCoreWasm .load ();
32- } catch (Exception e ) {
33- throw new ExceptionInInitializerError (e );
34- }
35- }
29+ private static final Logger LOG = LogManager .getLogger (SharedCoreInstance .class );
30+ private static final CBORMapper CBOR = CBORMapper .builder ().build ();
31+ private static final WasmModule WASM_MODULE =
32+ dev .restate .sdk .core .sharedcore .generated .SharedCoreWasm .load ();
33+ private static final ThreadLocal <SharedCoreInstance > THREAD_LOCAL =
34+ ThreadLocal .withInitial (SharedCoreInstance ::create );
3635
3736 private final Memory memory ;
3837 private final SharedCoreWasm_ModuleExports exports ;
@@ -42,22 +41,24 @@ private SharedCoreInstance(Memory memory, SharedCoreWasm_ModuleExports exports)
4241 this .exports = exports ;
4342 }
4443
45- public static SharedCoreInstance create () {
44+ static SharedCoreInstance get () {
45+ return THREAD_LOCAL .get ();
46+ }
47+
48+ private static SharedCoreInstance create () {
4649 ImportValues importValues =
4750 ImportValues .builder ().addFunction (SharedCoreImports .INSTANCE .toHostFunctions ()).build ();
4851
4952 Instance instance =
5053 Instance .builder (WASM_MODULE )
5154 .withMachineFactory (SharedCoreWasmMachine ::new )
52- .withMemoryFactory ()
5355 .withImportValues (importValues )
5456 .build ();
5557
5658 Memory mem = instance .memory ();
5759 SharedCoreWasm_ModuleExports exp = new SharedCoreWasm_ModuleExports (instance );
5860
59- // TODO make this configurable
60- exp .init (LOG_LEVEL_INFO );
61+ exp .init (toWasmLevel (LOG .getLevel ()));
6162 return new SharedCoreInstance (mem , exp );
6263 }
6364
@@ -136,4 +137,49 @@ public interface QuadFunction<W, X, Y, Z, R> {
136137 public interface TriConsumer <X , Y , Z > {
137138 void accept (X x , Y y , Z z );
138139 }
140+
141+ static Level toLog4jLevel (int level ) {
142+ return switch (level ) {
143+ case 0 -> Level .TRACE ;
144+ case 1 -> Level .DEBUG ;
145+ case 2 -> Level .INFO ;
146+ case 3 -> Level .WARN ;
147+ default -> Level .ERROR ;
148+ };
149+ }
150+
151+ static int toWasmLevel (Level level ) {
152+ if (level == Level .TRACE ) {
153+ return 0 ;
154+ } else if (level == Level .DEBUG ) {
155+ return 1 ;
156+ } else if (level == Level .INFO ) {
157+ return 2 ;
158+ } else if (level == Level .WARN ) {
159+ return 3 ;
160+ } else {
161+ return 4 ;
162+ }
163+ }
164+
165+ @ HostModule ("env" )
166+ static final class SharedCoreImports {
167+
168+ private SharedCoreImports () {}
169+
170+ public static final SharedCoreImports INSTANCE = new SharedCoreImports ();
171+
172+ @ WasmExport
173+ public void log (Memory memory , int level , int ptr , int len ) {
174+ if (len <= 0 ) {
175+ return ;
176+ }
177+ String message = memory .readString (ptr , len );
178+ LOG .atLevel (toLog4jLevel (level )).log (message );
179+ }
180+
181+ public HostFunction [] toHostFunctions () {
182+ return SharedCoreImports_ModuleFactory .toHostFunctions (this );
183+ }
184+ }
139185}
0 commit comments