feat: use Logger for logging

This commit is contained in:
Orion Kindel 2023-04-17 12:08:43 -05:00
parent c13f3b9dbc
commit 618133f5f1
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
6 changed files with 62 additions and 31 deletions

9
glue/Cargo.lock generated
View File

@ -504,9 +504,9 @@ checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
[[package]] [[package]]
name = "toad" name = "toad"
version = "0.17.3" version = "0.17.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d4000090e5f6d1380e1a4bfb9a5428019cb7a4374b3663792ef7965384968fc" checksum = "4eeb47a7e30efe198acc91ee919d420b478d9874c9362611d61f2ad9a3d27848"
dependencies = [ dependencies = [
"embedded-time", "embedded-time",
"log", "log",
@ -582,12 +582,13 @@ dependencies = [
[[package]] [[package]]
name = "toad-jni" name = "toad-jni"
version = "0.11.0" version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "125f835c70283545b5840dff39d9bd132324d929041d4eaec1ed9378f08ac166" checksum = "93571aa0e40745d04eff12007637f0ed01ea1afbe112b1eca19a11fe30f638f6"
dependencies = [ dependencies = [
"embedded-time", "embedded-time",
"jni", "jni",
"log",
"nb", "nb",
"no-std-net", "no-std-net",
"tinyvec", "tinyvec",

View File

@ -14,8 +14,8 @@ e2e = []
[dependencies] [dependencies]
jni = "0.21.1" jni = "0.21.1"
nb = "1" nb = "1"
toad = "0.17.3" toad = "0.17.4"
toad-jni = "0.11.0" toad-jni = "0.13.0"
no-std-net = "0.6" no-std-net = "0.6"
toad-msg = "0.18.1" toad-msg = "0.18.1"
tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]} tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]}

View File

@ -13,6 +13,7 @@ use toad::retry::{Attempts, Strategy};
use toad::time::Millis; use toad::time::Millis;
use toad_jni::java::net::InetSocketAddress; use toad_jni::java::net::InetSocketAddress;
use toad_jni::java::nio::channels::{DatagramChannel, PeekableDatagramChannel}; use toad_jni::java::nio::channels::{DatagramChannel, PeekableDatagramChannel};
use toad_jni::java::util::Optional;
use toad_jni::java::{self, Object}; use toad_jni::java::{self, Object};
use crate::mem::{Shared, SharedMemoryRegion}; use crate::mem::{Shared, SharedMemoryRegion};
@ -39,7 +40,7 @@ impl Toad {
} }
fn init_impl(e: &mut java::Env, cfg: Config, channel: PeekableDatagramChannel) -> i64 { 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 } unsafe { crate::mem::Shared::init(r).addr() as i64 }
} }
@ -135,13 +136,20 @@ impl Config {
RUNTIME_CONFIG_CONCURRENCY.get(e, self).to_rust(e) 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<Config, fn() -> java::util::logging::Level> =
java::Method::new("logLevel");
LOG_LEVEL.invoke(e, self)
}
pub fn msg(&self, e: &mut java::Env) -> Msg { pub fn msg(&self, e: &mut java::Env) -> Msg {
static RUNTIME_CONFIG_MSG: java::Method<Config, fn() -> Msg> = java::Method::new("msg"); static RUNTIME_CONFIG_MSG: java::Method<Config, fn() -> Msg> = java::Method::new("msg");
RUNTIME_CONFIG_MSG.invoke(e, self) RUNTIME_CONFIG_MSG.invoke(e, self)
} }
pub fn new(e: &mut java::Env, c: toad::config::Config) -> Self { pub fn new(e: &mut java::Env, c: toad::config::Config) -> Self {
static CTOR: java::Constructor<Config, fn(ffi::u8, Msg)> = java::Constructor::new(); static CTOR: java::Constructor<Config, fn(Optional<java::util::logging::Level>, ffi::u8, Msg)> =
java::Constructor::new();
let con = Con::new(e, let con = Con::new(e,
c.msg.con.unacked_retry_strategy, c.msg.con.unacked_retry_strategy,
@ -157,7 +165,8 @@ impl Config {
let concurrency = ffi::u8::from_rust(e, c.max_concurrent_requests); let concurrency = ffi::u8::from_rust(e, c.max_concurrent_requests);
let jcfg = CTOR.invoke(e, concurrency, msg); let log_level: Optional<java::util::logging::Level> = Optional::empty(e);
let jcfg = CTOR.invoke(e, log_level, concurrency, msg);
jcfg jcfg
} }

View File

@ -14,7 +14,8 @@ mod runtime {
use toad_jni::java::io::IOException; use toad_jni::java::io::IOException;
use toad_jni::java::lang::System; use toad_jni::java::lang::System;
use toad_jni::java::nio::channels::PeekableDatagramChannel; 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}; use toad_msg::{OptNumber, OptValue};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -37,14 +38,28 @@ mod runtime {
config: Config, config: Config,
channel: PeekableDatagramChannel, channel: PeekableDatagramChannel,
clock: toad::std::Clock, clock: toad::std::Clock,
logger: Logger,
} }
impl Runtime { 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(), Self { steps: Default::default(),
config, config,
channel, 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> { fn log(&self, level: log::Level, msg: toad::todo::String<1000>) -> Result<(), Self::Error> {
let mut e = java::env(); let mut e = java::env();
let e = &mut e; self.logger
let (level, msg) = (level.to_string().downcast(e), msg.as_str().to_string().downcast(e)); .log(&mut e, Level::from_log_level(level), msg.as_str());
System::out(e).printf(e, "[%s]: %s", vec![level, msg]);
Ok(()) Ok(())
} }

View File

@ -108,13 +108,14 @@ public final class Toad implements AutoCloseable {
Optional<IOException> ioException = Optional.empty(); Optional<IOException> ioException = Optional.empty();
Config.Msg.Builder msg = Config.Msg.builder(); Config.Msg.Builder msg = Config.Msg.builder();
Optional<DatagramChannel> channel = Optional.empty(); Optional<DatagramChannel> channel = Optional.empty();
Optional<java.util.logging.Level> logLevel = Optional.empty();
u8 concurrency = Toad.defaultConfig().concurrency; u8 concurrency = Toad.defaultConfig().concurrency;
Builder() {} Builder() {}
public Client buildClient() throws IOException { public Client buildClient() throws IOException {
if (this.ioException.isEmpty()) { 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()); var toad = new Toad(cfg, this.channel.get());
return new Client(toad); return new Client(toad);
} else { } else {
@ -131,6 +132,11 @@ public final class Toad implements AutoCloseable {
return this.address(new InetSocketAddress(port)); 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) { public Builder address(InetSocketAddress addr) {
try { try {
DatagramChannel channel = DatagramChannel.open( DatagramChannel channel = DatagramChannel.open(
@ -157,10 +163,16 @@ public final class Toad implements AutoCloseable {
public static final class Config { public static final class Config {
final Optional<java.util.logging.Level> logLevel;
final u8 concurrency; final u8 concurrency;
final Msg msg; final Msg msg;
Config(u8 concurrency, Msg msg) { Config(
Optional<java.util.logging.Level> logLevel,
u8 concurrency,
Msg msg
) {
this.logLevel = logLevel;
this.concurrency = concurrency; this.concurrency = concurrency;
this.msg = msg; 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() { public InetSocketAddress addr() {
return this.addr(); return this.addr();
} }

View File

@ -4,6 +4,8 @@ import dev.toad.msg.option.ContentFormat
import dev.toad.msg.option.Accept import dev.toad.msg.option.Accept
import mock.java.nio.channels.Mock import mock.java.nio.channels.Mock
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.util.logging.Logger
import java.util.logging.Level
import java.util.ArrayList import java.util.ArrayList
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -14,23 +16,12 @@ class E2E extends munit.FunSuite {
val mock = Mock.Channel() 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 val resp = dev.toad.msg.build.Message
.builder() .builder()
.addr(InetSocketAddress("127.0.0.1", 1111)) .addr(InetSocketAddress("127.0.0.1", 1111))
.`type`(Type.NON) .`type`(Type.ACK)
.code(Code.OK_CONTENT) .code(Code.OK_CONTENT)
.id(Id(3)) .id(Id(2))
.token(Token(Array(1))) .token(Token(Array(1)))
.option(ContentFormat.TEXT) .option(ContentFormat.TEXT)
.payload("foobar") .payload("foobar")
@ -46,7 +37,7 @@ class E2E extends munit.FunSuite {
.option(Accept.TEXT) .option(Accept.TEXT)
.build .build
val client = Toad.builder.channel(mock).buildClient val client = Toad.builder.channel(mock).logLevel(Level.INFO).buildClient
val respFuture = client.send(req) val respFuture = client.send(req)
var bufs = ArrayList[ByteBuffer]() var bufs = ArrayList[ByteBuffer]()