-
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.
- Loading branch information
0 parents
commit 13ce96c
Showing
9 changed files
with
463 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target | ||
Cargo.lock |
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,24 @@ | ||
[package] | ||
name = "oslog" | ||
description = "A minimal safe wrapper around Apple's unified logging system" | ||
repository = "https://github.com/steven-joruk/oslog" | ||
version = "0.0.1" | ||
authors = ["Steven Joruk <[email protected]>"] | ||
edition = "2018" | ||
license = "MIT" | ||
readme = "README.md" | ||
keywords = ["log", "logging", "unified", "macos", "apple"] | ||
category = ["development-tools::debugging"] | ||
|
||
[features] | ||
|
||
default = ["logger"] | ||
|
||
# Enables support for the `log` crate | ||
logger = ["log"] | ||
|
||
[dependencies] | ||
log = { version = "0.4", features = ["std"], optional = true } | ||
|
||
[build-dependencies] | ||
cc = "1.0" |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Steven Joruk | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,43 @@ | ||
A minimal wrapper around Apple's unified logging system. | ||
|
||
By default support for the [log](https://docs.rs/log) crate is provided, but you can disable it using the feature flags if you like: | ||
|
||
```toml | ||
[dependencies] | ||
oslog = { version = "0.0.1", default-features = false } | ||
``` | ||
|
||
# Example | ||
|
||
```rust | ||
fn main() { | ||
OsLogger::new("com.example.test", "testing") | ||
.level_filter(LevelFilter::Debug) | ||
.init() | ||
.unwrap(); | ||
|
||
// Maps to OS_LOG_TYPE_DEBUG | ||
trace!("Trace"); | ||
|
||
// Maps to OS_LOG_TYPE_INFO | ||
debug!("Debug"); | ||
|
||
// Maps to OS_LOG_TYPE_DEFAULT | ||
info!("Info"); | ||
|
||
// Maps to OS_LOG_TYPE_ERROR | ||
warn!("Warn"); | ||
|
||
// Maps to OS_LOG_TYPE_CRITICAL | ||
error!("Error"); | ||
} | ||
``` | ||
|
||
# Missing features | ||
|
||
Almost everything :). | ||
|
||
* Multiple categories, although I'm planning on adding optional support for setting the category to the module name which invoked the log call. | ||
* Activities | ||
* Tracing | ||
* Native support for line numbers and file names. |
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,3 @@ | ||
fn main() { | ||
cc::Build::new().file("wrapper.c").compile("wrapper"); | ||
} |
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,156 @@ | ||
mod sys; | ||
|
||
#[cfg(feature = "logger")] | ||
mod logger; | ||
|
||
#[cfg(feature = "logger")] | ||
pub use logger::OsLogger as OsLogger; | ||
|
||
use crate::sys::*; | ||
use std::{ffi::{c_void, CString}}; | ||
|
||
#[inline] | ||
fn to_cstr(message: &str) -> CString { | ||
CString::new(message).expect( | ||
"Failed to convert message to a C string. Does it contain a NULL byte in the middle?", | ||
) | ||
} | ||
|
||
#[repr(u8)] | ||
pub enum Level { | ||
Debug = OS_LOG_TYPE_DEBUG, | ||
Info = OS_LOG_TYPE_INFO, | ||
Default = OS_LOG_TYPE_DEFAULT, | ||
Error = OS_LOG_TYPE_ERROR, | ||
Fault = OS_LOG_TYPE_FAULT, | ||
} | ||
|
||
#[cfg(feature = "logger")] | ||
impl From<log::Level> for Level { | ||
fn from(other: log::Level) -> Self { | ||
match other { | ||
log::Level::Trace => Self::Debug, | ||
log::Level::Debug => Self::Info, | ||
log::Level::Info => Self::Default, | ||
log::Level::Warn => Self::Error, | ||
log::Level::Error => Self::Fault, | ||
} | ||
} | ||
} | ||
|
||
pub struct OsLog { | ||
inner: os_log_t, | ||
} | ||
|
||
unsafe impl Send for OsLog {} | ||
unsafe impl Sync for OsLog {} | ||
|
||
impl Drop for OsLog { | ||
fn drop(&mut self) { | ||
unsafe { | ||
if self.inner != wrapped_get_default_log() { | ||
os_release(self.inner as *mut c_void); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl OsLog { | ||
pub fn new(subsystem: &str, category: &str) -> Self { | ||
let subsystem = to_cstr(subsystem); | ||
let category = to_cstr(category); | ||
|
||
let inner = unsafe { os_log_create(subsystem.as_ptr(), category.as_ptr()) }; | ||
|
||
assert!(!inner.is_null(), "Unexpected null value from os_log_create"); | ||
|
||
Self { inner } | ||
} | ||
|
||
pub fn global() -> Self { | ||
let inner = unsafe { wrapped_get_default_log() }; | ||
|
||
assert!(!inner.is_null(), "Unexpected null value for OS_DEFAULT_LOG"); | ||
|
||
Self { inner } | ||
} | ||
|
||
pub fn with_level(&self, level: Level, message: &str) { | ||
let message = to_cstr(message); | ||
unsafe { wrapped_os_log_with_type(self.inner, level as u8, message.as_ptr()) } | ||
} | ||
|
||
pub fn debug(&self, message: &str) { | ||
let message = to_cstr(message); | ||
unsafe { wrapped_os_log_debug(self.inner, message.as_ptr()) } | ||
} | ||
|
||
pub fn info(&self, message: &str) { | ||
let message = to_cstr(message); | ||
unsafe { wrapped_os_log_info(self.inner, message.as_ptr()) } | ||
} | ||
|
||
pub fn default(&self, message: &str) { | ||
let message = to_cstr(message); | ||
unsafe { wrapped_os_log_default(self.inner, message.as_ptr()) } | ||
} | ||
|
||
pub fn error(&self, message: &str) { | ||
let message = to_cstr(message); | ||
unsafe { wrapped_os_log_error(self.inner, message.as_ptr()) } | ||
} | ||
|
||
pub fn fault(&self, message: &str) { | ||
let message = to_cstr(message); | ||
unsafe { wrapped_os_log_fault(self.inner, message.as_ptr()) } | ||
} | ||
|
||
pub fn level_is_enabled(&self, level: Level) -> bool { | ||
unsafe { os_log_type_enabled(self.inner, level as u8) } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_global_log_with_level() { | ||
let log = OsLog::global(); | ||
log.with_level(Level::Debug, "Debug"); | ||
log.with_level(Level::Info, "Info"); | ||
log.with_level(Level::Default, "Default"); | ||
log.with_level(Level::Error, "Error"); | ||
log.with_level(Level::Fault, "Fault"); | ||
} | ||
|
||
#[test] | ||
fn test_global_log() { | ||
let log = OsLog::global(); | ||
log.debug("Debug"); | ||
log.info("Info"); | ||
log.default("Default"); | ||
log.error("Error"); | ||
log.fault("Fault"); | ||
} | ||
|
||
#[test] | ||
fn test_custom_log_with_level() { | ||
let log = OsLog::new("com.example.test", "testing"); | ||
log.with_level(Level::Debug, "Debug"); | ||
log.with_level(Level::Info, "Info"); | ||
log.with_level(Level::Default, "Default"); | ||
log.with_level(Level::Error, "Error"); | ||
log.with_level(Level::Fault, "Fault"); | ||
} | ||
|
||
#[test] | ||
fn test_custom_log() { | ||
let log = OsLog::new("com.example.test", "testing"); | ||
log.debug("Debug"); | ||
log.info("Info"); | ||
log.default("Default"); | ||
log.error("Error"); | ||
log.fault("Fault"); | ||
} | ||
} |
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,67 @@ | ||
use log::{LevelFilter, Log, Metadata, Record}; | ||
use crate::OsLog; | ||
|
||
pub struct OsLogger { | ||
logger: OsLog, | ||
level_filter: LevelFilter, | ||
} | ||
|
||
impl Log for OsLogger { | ||
fn enabled(&self, metadata: &Metadata) -> bool { | ||
if metadata.level() > self.level_filter { | ||
return false; | ||
} | ||
|
||
self.logger.level_is_enabled(metadata.level().into()) | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
let message = std::format!("{}", record.args()); | ||
self.logger.with_level(record.level().into(), &message); | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
impl OsLogger { | ||
/// Creates a new logger. You must also call `init` to finalize the set up. | ||
/// By default the level filter will be set to `LevelFilter::Trace`. | ||
pub fn new(subsystem: &str, category: &str) -> Self { | ||
Self { | ||
logger: OsLog::new(subsystem, category), | ||
level_filter: LevelFilter::Trace, | ||
} | ||
} | ||
|
||
/// Modifies the level filter, which by default is `LevelFilter::Trace`. | ||
/// Only levels at or above `level` will be logged. | ||
pub fn level_filter(mut self, level: LevelFilter) -> Self { | ||
self.level_filter = level; | ||
self | ||
} | ||
|
||
pub fn init(self) -> Result<(), log::SetLoggerError> { | ||
log::set_max_level(self.level_filter); | ||
log::set_boxed_logger(Box::new(self)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
#[test] | ||
fn test_basic_usage() { | ||
OsLogger::new("com.example.test", "testing") | ||
.level_filter(LevelFilter::Debug) | ||
.init() | ||
.unwrap(); | ||
|
||
trace!("Trace"); | ||
debug!("Debug"); | ||
info!("Info"); | ||
warn!("Warn"); | ||
error!("Error"); | ||
} | ||
} |
Oops, something went wrong.