From 618133f5f159a39f3e1bb00bdcd29ffdf580fb66 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Mon, 17 Apr 2023 12:08:43 -0500 Subject: [PATCH] feat: use Logger for logging --- glue/Cargo.lock | 9 +++++---- glue/Cargo.toml | 4 ++-- glue/src/dev/toad/mod.rs | 15 ++++++++++++--- glue/src/lib.rs | 26 ++++++++++++++++++++------ src/main/java/dev.toad/Toad.java | 20 ++++++++++++++++++-- src/test/scala/E2E.scala | 19 +++++-------------- 6 files changed, 62 insertions(+), 31 deletions(-) diff --git a/glue/Cargo.lock b/glue/Cargo.lock index 7ff5d11..98cb857 100644 --- a/glue/Cargo.lock +++ b/glue/Cargo.lock @@ -504,9 +504,9 @@ checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" [[package]] name = "toad" -version = "0.17.3" +version = "0.17.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d4000090e5f6d1380e1a4bfb9a5428019cb7a4374b3663792ef7965384968fc" +checksum = "4eeb47a7e30efe198acc91ee919d420b478d9874c9362611d61f2ad9a3d27848" dependencies = [ "embedded-time", "log", @@ -582,12 +582,13 @@ dependencies = [ [[package]] name = "toad-jni" -version = "0.11.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "125f835c70283545b5840dff39d9bd132324d929041d4eaec1ed9378f08ac166" +checksum = "93571aa0e40745d04eff12007637f0ed01ea1afbe112b1eca19a11fe30f638f6" dependencies = [ "embedded-time", "jni", + "log", "nb", "no-std-net", "tinyvec", diff --git a/glue/Cargo.toml b/glue/Cargo.toml index 75b4e7e..d048399 100644 --- a/glue/Cargo.toml +++ b/glue/Cargo.toml @@ -14,8 +14,8 @@ e2e = [] [dependencies] jni = "0.21.1" nb = "1" -toad = "0.17.3" -toad-jni = "0.11.0" +toad = "0.17.4" +toad-jni = "0.13.0" no-std-net = "0.6" toad-msg = "0.18.1" tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]} diff --git a/glue/src/dev/toad/mod.rs b/glue/src/dev/toad/mod.rs index a919308..b0bf833 100644 --- a/glue/src/dev/toad/mod.rs +++ b/glue/src/dev/toad/mod.rs @@ -13,6 +13,7 @@ use toad::retry::{Attempts, Strategy}; use toad::time::Millis; use toad_jni::java::net::InetSocketAddress; use toad_jni::java::nio::channels::{DatagramChannel, PeekableDatagramChannel}; +use toad_jni::java::util::Optional; use toad_jni::java::{self, Object}; use crate::mem::{Shared, SharedMemoryRegion}; @@ -39,7 +40,7 @@ impl Toad { } fn init_impl(e: &mut java::Env, cfg: Config, channel: PeekableDatagramChannel) -> i64 { - let r = || Runtime::new(cfg.to_toad(e), channel); + let r = || Runtime::new(&mut java::env(), cfg.log_level(e), cfg.to_toad(e), channel); unsafe { crate::mem::Shared::init(r).addr() as i64 } } @@ -135,13 +136,20 @@ impl Config { RUNTIME_CONFIG_CONCURRENCY.get(e, self).to_rust(e) } + pub fn log_level(&self, e: &mut java::Env) -> java::util::logging::Level { + static LOG_LEVEL: java::Method java::util::logging::Level> = + java::Method::new("logLevel"); + LOG_LEVEL.invoke(e, self) + } + pub fn msg(&self, e: &mut java::Env) -> Msg { static RUNTIME_CONFIG_MSG: java::Method Msg> = java::Method::new("msg"); RUNTIME_CONFIG_MSG.invoke(e, self) } pub fn new(e: &mut java::Env, c: toad::config::Config) -> Self { - static CTOR: java::Constructor = java::Constructor::new(); + static CTOR: java::Constructor, ffi::u8, Msg)> = + java::Constructor::new(); let con = Con::new(e, c.msg.con.unacked_retry_strategy, @@ -157,7 +165,8 @@ impl Config { let concurrency = ffi::u8::from_rust(e, c.max_concurrent_requests); - let jcfg = CTOR.invoke(e, concurrency, msg); + let log_level: Optional = Optional::empty(e); + let jcfg = CTOR.invoke(e, log_level, concurrency, msg); jcfg } diff --git a/glue/src/lib.rs b/glue/src/lib.rs index 7198356..348f3dd 100644 --- a/glue/src/lib.rs +++ b/glue/src/lib.rs @@ -14,7 +14,8 @@ mod runtime { use toad_jni::java::io::IOException; use toad_jni::java::lang::System; use toad_jni::java::nio::channels::PeekableDatagramChannel; - use toad_jni::java::{self, Object}; + use toad_jni::java::util::logging::{ConsoleHandler, Level, Logger}; + use toad_jni::java::{self, Object, ResultExt, Signature}; use toad_msg::{OptNumber, OptValue}; #[derive(Clone, Copy, Debug)] @@ -37,14 +38,28 @@ mod runtime { config: Config, channel: PeekableDatagramChannel, clock: toad::std::Clock, + logger: Logger, } impl Runtime { - pub fn new(config: Config, channel: PeekableDatagramChannel) -> Self { + pub fn new(e: &mut java::Env, + log_level: Level, + config: Config, + channel: PeekableDatagramChannel) + -> Self { + let handler = ConsoleHandler::new(e); + handler.set_level(e, log_level); + + let logger = Logger::get_logger(e, "dev.toad"); + logger.use_parent_handlers(e, false); + logger.add_handler(e, handler.to_handler()); + logger.set_level(e, log_level); + Self { steps: Default::default(), config, channel, - clock: toad::std::Clock::new() } + clock: toad::std::Clock::new(), + logger } } } @@ -54,9 +69,8 @@ mod runtime { fn log(&self, level: log::Level, msg: toad::todo::String<1000>) -> Result<(), Self::Error> { let mut e = java::env(); - let e = &mut e; - let (level, msg) = (level.to_string().downcast(e), msg.as_str().to_string().downcast(e)); - System::out(e).printf(e, "[%s]: %s", vec![level, msg]); + self.logger + .log(&mut e, Level::from_log_level(level), msg.as_str()); Ok(()) } diff --git a/src/main/java/dev.toad/Toad.java b/src/main/java/dev.toad/Toad.java index 957d0be..d22d91f 100644 --- a/src/main/java/dev.toad/Toad.java +++ b/src/main/java/dev.toad/Toad.java @@ -108,13 +108,14 @@ public final class Toad implements AutoCloseable { Optional ioException = Optional.empty(); Config.Msg.Builder msg = Config.Msg.builder(); Optional channel = Optional.empty(); + Optional logLevel = Optional.empty(); u8 concurrency = Toad.defaultConfig().concurrency; Builder() {} public Client buildClient() throws IOException { if (this.ioException.isEmpty()) { - var cfg = new Config(this.concurrency, this.msg.build()); + var cfg = new Config(this.logLevel, this.concurrency, this.msg.build()); var toad = new Toad(cfg, this.channel.get()); return new Client(toad); } else { @@ -131,6 +132,11 @@ public final class Toad implements AutoCloseable { return this.address(new InetSocketAddress(port)); } + public Builder logLevel(java.util.logging.Level level) { + this.logLevel = Optional.of(level); + return this; + } + public Builder address(InetSocketAddress addr) { try { DatagramChannel channel = DatagramChannel.open( @@ -157,10 +163,16 @@ public final class Toad implements AutoCloseable { public static final class Config { + final Optional logLevel; final u8 concurrency; final Msg msg; - Config(u8 concurrency, Msg msg) { + Config( + Optional logLevel, + u8 concurrency, + Msg msg + ) { + this.logLevel = logLevel; this.concurrency = concurrency; this.msg = msg; } @@ -173,6 +185,10 @@ public final class Toad implements AutoCloseable { }; } + public java.util.logging.Level logLevel() { + return this.logLevel.orElse(java.util.logging.Level.INFO); + } + public InetSocketAddress addr() { return this.addr(); } diff --git a/src/test/scala/E2E.scala b/src/test/scala/E2E.scala index ef148ca..324e7f6 100644 --- a/src/test/scala/E2E.scala +++ b/src/test/scala/E2E.scala @@ -4,6 +4,8 @@ import dev.toad.msg.option.ContentFormat import dev.toad.msg.option.Accept import mock.java.nio.channels.Mock import java.net.InetSocketAddress +import java.util.logging.Logger +import java.util.logging.Level import java.util.ArrayList import java.nio.ByteBuffer import java.util.concurrent.TimeUnit @@ -14,23 +16,12 @@ class E2E extends munit.FunSuite { val mock = Mock.Channel() - val ack = dev.toad.msg.build.Message - .builder() - .addr(InetSocketAddress("127.0.0.1", 1111)) - .`type`(Type.ACK) - .code(Code.EMPTY) - .id(Id(2)) - .token(Token(Array(1))) - .option(ContentFormat.TEXT) - .payload("foobar") - .build - val resp = dev.toad.msg.build.Message .builder() .addr(InetSocketAddress("127.0.0.1", 1111)) - .`type`(Type.NON) + .`type`(Type.ACK) .code(Code.OK_CONTENT) - .id(Id(3)) + .id(Id(2)) .token(Token(Array(1))) .option(ContentFormat.TEXT) .payload("foobar") @@ -46,7 +37,7 @@ class E2E extends munit.FunSuite { .option(Accept.TEXT) .build - val client = Toad.builder.channel(mock).buildClient + val client = Toad.builder.channel(mock).logLevel(Level.INFO).buildClient val respFuture = client.send(req) var bufs = ArrayList[ByteBuffer]()