-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement dynamic enum dispatch targets (#6)
Signed-off-by: tison <[email protected]>
- Loading branch information
Showing
12 changed files
with
316 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# LogForth Project | ||
# Logforth Project | ||
|
||
A versatile and extensible logging implementation. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 tison <[email protected]> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use log::LevelFilter; | ||
use logforth::BoxDynFilter; | ||
use logforth::BoxDynLayout; | ||
use logforth::DispatchAppend; | ||
use logforth::FilterResult; | ||
use logforth::Logger; | ||
use logforth::StdoutAppend; | ||
|
||
fn main() { | ||
let layout = BoxDynLayout::new(|record: &log::Record| { | ||
let message = format!("[box dyn] {}", record.args()); | ||
Ok(message.into_bytes()) | ||
// ...or | ||
// anyhow::bail!("boom: {}", message) | ||
}); | ||
|
||
let filter = BoxDynFilter::new(|metadata: &log::Metadata| { | ||
if metadata.level() <= LevelFilter::Info { | ||
FilterResult::Accept | ||
} else { | ||
FilterResult::Reject | ||
} | ||
}); | ||
|
||
let append = StdoutAppend::default().with_layout(layout); | ||
let append = DispatchAppend::new(append).filter(filter); | ||
Logger::new().add_append(append).apply().unwrap(); | ||
|
||
log::error!("Hello error!"); | ||
log::warn!("Hello warn!"); | ||
log::info!("Hello info!"); | ||
log::debug!("Hello debug!"); | ||
log::trace!("Hello trace!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 tison <[email protected]> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::fmt::Debug; | ||
|
||
use log::Metadata; | ||
use log::Record; | ||
|
||
use crate::Append; | ||
use crate::AppendImpl; | ||
|
||
pub struct BoxDynAppend(Box<dyn Append + Send + Sync>); | ||
|
||
impl BoxDynAppend { | ||
pub fn new(append: impl Append + Send + Sync + 'static) -> Self { | ||
Self(Box::new(append)) | ||
} | ||
} | ||
|
||
impl Debug for BoxDynAppend { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "BoxDynAppend {{ ... }}") | ||
} | ||
} | ||
|
||
impl Append for BoxDynAppend { | ||
fn enabled(&self, metadata: &Metadata) -> bool { | ||
(*self.0).enabled(metadata) | ||
} | ||
|
||
fn try_append(&self, record: &Record) -> anyhow::Result<()> { | ||
(*self.0).try_append(record) | ||
} | ||
|
||
fn flush(&self) { | ||
(*self.0).flush() | ||
} | ||
} | ||
|
||
impl From<BoxDynAppend> for AppendImpl { | ||
fn from(append: BoxDynAppend) -> Self { | ||
AppendImpl::BoxDyn(append) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024 tison <[email protected]> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::fmt::Debug; | ||
|
||
use log::Log; | ||
use log::Metadata; | ||
use log::Record; | ||
|
||
use crate::Append; | ||
use crate::AppendImpl; | ||
|
||
pub struct BoxLogAppend(Box<dyn Log>); | ||
|
||
impl Debug for BoxLogAppend { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "BoxLogAppend {{ ... }}") | ||
} | ||
} | ||
|
||
impl BoxLogAppend { | ||
pub fn new(log: impl Log + 'static) -> Self { | ||
Self(Box::new(log)) | ||
} | ||
} | ||
|
||
impl Append for BoxLogAppend { | ||
fn enabled(&self, metadata: &Metadata) -> bool { | ||
(*self.0).enabled(metadata) | ||
} | ||
|
||
fn try_append(&self, record: &Record) -> anyhow::Result<()> { | ||
(*self.0).log(record); | ||
Ok(()) | ||
} | ||
|
||
fn flush(&self) { | ||
(*self.0).flush() | ||
} | ||
} | ||
|
||
impl From<BoxLogAppend> for AppendImpl { | ||
fn from(append: BoxLogAppend) -> Self { | ||
AppendImpl::BoxLog(append) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 tison <[email protected]> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::fmt::Debug; | ||
|
||
use log::Metadata; | ||
use log::Record; | ||
|
||
use crate::Filter; | ||
use crate::FilterImpl; | ||
use crate::FilterResult; | ||
|
||
pub struct BoxDynFilter(Box<dyn Filter + Send + Sync>); | ||
|
||
impl Debug for BoxDynFilter { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "BoxDynFilter {{ ... }}") | ||
} | ||
} | ||
|
||
impl BoxDynFilter { | ||
pub fn new(filter: impl Filter + Send + Sync + 'static) -> Self { | ||
Self(Box::new(filter)) | ||
} | ||
} | ||
|
||
impl Filter for BoxDynFilter { | ||
fn filter(&self, record: &Record) -> FilterResult { | ||
(*self.0).filter(record) | ||
} | ||
|
||
fn filter_metadata(&self, metadata: &Metadata) -> FilterResult { | ||
(*self.0).filter_metadata(metadata) | ||
} | ||
} | ||
|
||
impl From<BoxDynFilter> for FilterImpl { | ||
fn from(filter: BoxDynFilter) -> Self { | ||
FilterImpl::BoxDyn(filter) | ||
} | ||
} | ||
|
||
impl<T: Fn(&Metadata) -> FilterResult> Filter for T { | ||
fn filter_metadata(&self, metadata: &Metadata) -> FilterResult { | ||
self(metadata) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.