Skip to content

Commit ff417d7

Browse files
committed
log crate only enabled on debug builds
1 parent 902866e commit ff417d7

6 files changed

Lines changed: 29 additions & 56 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ categories = [
2222
exclude = ["ci/*", ".github/*"]
2323

2424
[features]
25-
default = []
26-
# Logging feature enables debug logs through the log crate
27-
logging = ["log"]
25+
default = ["log/release_max_level_off"]
2826

2927
[dependencies]
3028
cfg-if = "1.0"
@@ -50,4 +48,3 @@ winapi = { version = "0.3", features = [
5048
raw_sync = "0.1"
5149
clap = "2.33"
5250
env_logger = "0"
53-
log = "0"

examples/basic.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::thread;
22

33
use clap::{App, Arg};
4-
use env_logger::Env;
5-
use log::*;
64
use shared_memory::*;
75

86
fn main() {
9-
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
7+
env_logger::init();
108

119
// Get number of thread argument
1210
let matches = App::new("Basic Example")
@@ -24,7 +22,7 @@ fn main() {
2422
.parse()
2523
.expect("Invalid number passed for num_threads");
2624
if num_threads < 1 {
27-
error!("Invalid number of threads");
25+
eprintln!("Invalid number of threads");
2826
return;
2927
}
3028

@@ -52,7 +50,7 @@ fn increment_value(shmem_flink: &str, thread_num: usize) {
5250
Ok(m) => m,
5351
Err(ShmemError::LinkExists) => ShmemConf::new().flink(shmem_flink).open().unwrap(),
5452
Err(e) => {
55-
info!(
53+
eprintln!(
5654
"Unable to create or open shmem flink {} : {}",
5755
shmem_flink, e
5856
);
@@ -69,7 +67,7 @@ fn increment_value(shmem_flink: &str, thread_num: usize) {
6967
// Increment shared value by one
7068
*raw_ptr += 1;
7169

72-
info!(
70+
println!(
7371
"[thread:{}] {}",
7472
thread_num,
7573
std::ptr::read_volatile(raw_ptr)

examples/event.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use log::*;
21
use raw_sync::{events::*, Timeout};
32
use shared_memory::*;
43

54
fn main() -> Result<(), Box<dyn std::error::Error>> {
65
env_logger::init();
76
// Attempt to create a mapping or open if it already exists
8-
info!("Getting the shared memory mapping");
7+
println!("Getting the shared memory mapping");
98
let shmem = match ShmemConf::new().size(4096).flink("event_mapping").create() {
109
Ok(m) => m,
1110
Err(ShmemError::LinkExists) => ShmemConf::new().flink("event_mapping").open()?,
@@ -14,24 +13,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1413

1514
if shmem.is_owner() {
1615
//Create an event in the shared memory
17-
info!("Creating event in shared memory");
16+
println!("Creating event in shared memory");
1817
let (evt, used_bytes) = unsafe { Event::new(shmem.as_ptr(), true)? };
19-
info!("\tUsed {} bytes", used_bytes);
18+
println!("\tUsed {} bytes", used_bytes);
2019

21-
info!("Launch another instance of this example to signal the event !");
20+
println!("Launch another instance of this example to signal the event !");
2221
evt.wait(Timeout::Infinite)?;
23-
info!("\tGot signal !");
22+
println!("\tGot signal !");
2423
} else {
2524
// Open existing event
26-
info!("Openning event from shared memory");
25+
println!("Openning event from shared memory");
2726
let (evt, used_bytes) = unsafe { Event::from_existing(shmem.as_ptr())? };
28-
info!("\tEvent uses {} bytes", used_bytes);
27+
println!("\tEvent uses {} bytes", used_bytes);
2928

30-
info!("Signaling event !");
29+
println!("Signaling event !");
3130
evt.set(EventState::Signaled)?;
32-
info!("\tSignaled !");
31+
println!("\tSignaled !");
3332
}
3433

35-
info!("Done !");
34+
println!("Done !");
3635
Ok(())
3736
}

examples/mutex.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::sync::atomic::{AtomicU8, Ordering};
22
use std::thread;
33

44
use clap::{App, Arg};
5-
use log::*;
65
use raw_sync::locks::*;
76
use shared_memory::*;
87

@@ -24,7 +23,7 @@ fn main() {
2423
.parse()
2524
.expect("Invalid number passed for num_threads");
2625
if num_threads < 1 {
27-
info!("num_threads should be 2 or more");
26+
eprintln!("num_threads should be 2 or more");
2827
return;
2928
}
3029
let mut threads = Vec::with_capacity(num_threads);
@@ -50,7 +49,7 @@ fn increment_value(shmem_flink: &str, thread_num: usize) {
5049
Ok(m) => m,
5150
Err(ShmemError::LinkExists) => ShmemConf::new().flink(shmem_flink).open().unwrap(),
5251
Err(e) => {
53-
info!(
52+
eprintln!(
5453
"Unable to create or open shmem flink {} : {}",
5554
shmem_flink, e
5655
);
@@ -102,12 +101,12 @@ fn increment_value(shmem_flink: &str, thread_num: usize) {
102101
// Cast mutex data to &mut u8
103102
let val: &mut u8 = unsafe { &mut **guard };
104103
if *val > 5 {
105-
info!("[thread#{}] done !", thread_num);
104+
println!("[thread#{}] done !", thread_num);
106105
return;
107106
}
108107

109108
// Print contents and increment value
110-
info!("[thread#{}] Val : {}", thread_num, *val);
109+
println!("[thread#{}] Val : {}", thread_num, *val);
111110
*val += 1;
112111

113112
// Hold lock for a second

src/lib.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,14 @@
22
//!
33
//! For help on how to get started, take a look at the [examples](https://github.com/elast0ny/shared_memory-rs/tree/master/examples) !
44
5-
// Allow dependents to disable logging through the "logging" feature
6-
cfg_if::cfg_if! {
7-
if #[cfg(feature = "logging")] {
8-
pub(crate) use log;
9-
} else {
10-
#[allow(unused_macros)]
11-
#[macro_use]
12-
pub (crate) mod log {
13-
macro_rules! trace (($($tt:tt)*) => {{}});
14-
macro_rules! debug (($($tt:tt)*) => {{}});
15-
macro_rules! info (($($tt:tt)*) => {{}});
16-
macro_rules! warn (($($tt:tt)*) => {{}});
17-
macro_rules! error (($($tt:tt)*) => {{}});
18-
}
19-
}
20-
}
21-
22-
#[allow(unused_imports)]
23-
use crate::log::*;
24-
255
use std::fs::{File, OpenOptions};
266
use std::io::{ErrorKind, Read, Write};
277

288
use std::fs::remove_file;
299
use std::path::{Path, PathBuf};
3010

31-
use ::cfg_if::*;
11+
use cfg_if::cfg_if;
12+
use log::*;
3213

3314
mod error;
3415
pub use error::*;

src/unix.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
use ::nix::fcntl::OFlag;
2-
use ::nix::sys::mman::{mmap, munmap, shm_open, shm_unlink, MapFlags, ProtFlags};
3-
use ::nix::sys::stat::{fstat, Mode};
4-
use ::nix::unistd::{close, ftruncate};
5-
6-
#[allow(unused_imports)]
7-
use crate::log::*;
8-
use crate::ShmemError;
9-
101
use std::os::unix::io::RawFd;
112
use std::ptr::null_mut;
123

4+
use log::*;
5+
use nix::fcntl::OFlag;
6+
use nix::sys::mman::{mmap, munmap, shm_open, shm_unlink, MapFlags, ProtFlags};
7+
use nix::sys::stat::{fstat, Mode};
8+
use nix::unistd::{close, ftruncate};
9+
10+
use crate::ShmemError;
11+
1312
pub struct MapData {
1413
//On linux, you must shm_unlink() the object created for the mapping. It wont disappear automatically.
1514
owner: bool,

0 commit comments

Comments
 (0)