Skip to content

Commit c66f462

Browse files
committed
CI add a native test for JournaldSink on Linux
1 parent e9a077b commit c66f462

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

.github/workflows/ci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,43 @@ jobs:
218218
- name: Check MSRV for core with Rust ${{ env.rust_minver }}
219219
run: cargo +${{ env.rust_minver }} check --locked --all-features --verbose
220220

221+
test-native-linux:
222+
strategy:
223+
fail-fast: false
224+
runs-on: 'ubuntu-latest'
225+
steps:
226+
- name: Checkout repository
227+
uses: actions/checkout@v4
228+
- name: Disable bench dependencies
229+
run: ./.github/workflows/disable-bench-deps.sh
230+
- name: Install dependencies
231+
run: sudo bash ./.github/workflows/install-deps.sh
232+
- name: Restore cargo caches
233+
uses: Swatinem/rust-cache@v2
234+
- name: Build example
235+
run: |
236+
cargo build --example native_linux --features native,libsystemd,source-location --verbose
237+
mv ./target/debug/examples/native_linux ./target/debug/examples/native_linux_srcloc
238+
cargo build --example native_linux --features native,libsystemd --verbose
239+
- name: Run and test
240+
run: |
241+
set -x
242+
./target/debug/examples/native_linux
243+
./target/debug/examples/native_linux_srcloc
244+
245+
journalctl --no-pager -o verbose -t native_linux
246+
journalctl --no-pager -o verbose -t native_linux_srcloc
247+
248+
journalctl --no-pager -o json -t native_linux | jq -e -s $'.[0].MESSAGE == "[demo] [info] info message from spdlog-rs\'s JournaldSink\n"'
249+
journalctl --no-pager -o json -t native_linux | jq -e -s $'.[0].PRIORITY == "6" and .[0].CODE_FILE == null and .[0].CODE_LINE == null'
250+
journalctl --no-pager -o json -t native_linux | jq -e -s $'.[1].MESSAGE == "[demo] [error] error message from spdlog-rs\'s JournaldSink { error_code=114514 }\n"'
251+
journalctl --no-pager -o json -t native_linux | jq -e -s $'.[1].PRIORITY == "3" and .[1].CODE_FILE == null and .[1].CODE_LINE == null'
252+
253+
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[0].MESSAGE == "[demo] [info] info message from spdlog-rs\'s JournaldSink\n"'
254+
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[0].PRIORITY == "6" and .[0].CODE_FILE == "linux.rs" and .[0].CODE_LINE == "15"'
255+
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[1].MESSAGE == "[demo] [error] error message from spdlog-rs\'s JournaldSink { error_code=114514 }\n"'
256+
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[1].PRIORITY == "3" and .[1].CODE_FILE == "linux.rs" and .[1].CODE_LINE == "16"'
257+
221258
test-native-android:
222259
strategy:
223260
fail-fast: false

spdlog/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ required-features = ["log"]
193193
name = "07_async"
194194
required-features = ["multi-thread"]
195195
[[example]]
196+
name = "native_linux"
197+
path = "examples/native/linux.rs"
198+
required-features = ["native", "libsystemd"]
199+
[[example]]
196200
name = "native_android"
197201
path = "examples/native/android.rs"
198202
required-features = ["native", "android-ndk"]

spdlog/examples/native/linux.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[cfg(target_os = "linux")]
2+
fn main() -> Result<(), Box<dyn std::error::Error>> {
3+
use std::sync::Arc;
4+
5+
use spdlog::{prelude::*, sink::JournaldSink};
6+
7+
let sink = Arc::new(JournaldSink::builder().build()?);
8+
let logger = spdlog::default_logger().fork_with(|logger| {
9+
logger.set_name(Some("demo")).unwrap();
10+
logger.sinks_mut().push(sink);
11+
Ok(())
12+
})?;
13+
spdlog::set_default_logger(logger);
14+
15+
info!("info message from spdlog-rs's JournaldSink");
16+
error!("error message from spdlog-rs's JournaldSink", kv: { error_code = 114514 });
17+
Ok(())
18+
}
19+
20+
#[cfg(not(target_os = "linux"))]
21+
fn main() {
22+
panic!("this example is only available on Linux target");
23+
}

0 commit comments

Comments
 (0)