Skip to content

Replace println calls with log crate #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- uses: actions/checkout@v1
- run: cargo build --verbose
- run: |
cargo run &
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
cargo test --test normal

integ-test-persistence:
Expand All @@ -49,7 +49,7 @@ jobs:
- run: cargo build --verbose
- name: Tests executed before the shutdown
run: |
cargo run &
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
SERVER_PID=$!
cargo test --test persistent-before
kill $SERVER_PID
Expand All @@ -62,7 +62,7 @@ jobs:
printf '\xe0\x19\xb2\x5c' > mappings/cm9vdA==/1/VGVzdCBLZXk\=
- name: Tests executed after the shutdown
run: |
cargo run &
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
cargo test --test persistent-after

stress-test:
Expand All @@ -72,6 +72,6 @@ jobs:
- uses: actions/checkout@v1
- run: cargo build --verbose
- run: |
cargo run &
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
cargo test --test stress_test

30 changes: 23 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ signal-hook = "0.1.10"
sd-notify = { version = "0.1.0", optional = true }
toml = "0.4.2"
serde = { version = "1.0", features = ["derive"] }
env_logger = "0.7.1"
log = "0.4.8"

[dev-dependencies]
parsec-client-test = { git = "https://github.com/parallaxsecond/parsec-client-test", tag = "0.1.2" }
parsec-client-test = { git = "https://github.com/parallaxsecond/parsec-client-test", tag = "0.1.3" }
num_cpus = "1.10.1"

[build-dependencies]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ This project uses the following third party crates:
* uuid (Apache-2.0)
* threadpool (Apache-2.0)
* std-semaphore (MIT and Apache-2.0)
* num_cpus (MIT and Apache-2.0)
* num\_cpus (MIT and Apache-2.0)
* signal-hook (MIT and Apache-2.0)
* sd-notify (Apache-2.0)
* log (Apache-2.0)
* env\_logger (MIT and Apache-2.0)

This project uses the following third party libraries:
* [Mbed Crypto](https://github.com/ARMmbed/mbed-crypto) (Apache-2.0)
Expand Down
17 changes: 15 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// 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::info;
use parsec::utils::{ServiceBuilder, ServiceConfig};
use signal_hook::{flag, SIGTERM};
use std::io::Error;
Expand All @@ -25,6 +26,15 @@ const CONFIG_FILE_PATH: &str = "./config.toml";
const MAIN_LOOP_DEFAULT_SLEEP: u64 = 10;

fn main() -> Result<(), Error> {
if cfg!(feature = "systemd-daemon") {
// Logs viewed with journalctl already contain timestamps.
env_logger::builder().format_timestamp(None).init();
} else {
env_logger::init();
}

info!("PARSEC started.");

let config_file =
::std::fs::read_to_string(CONFIG_FILE_PATH).expect("Failed to read configuration file");
let config: ServiceConfig =
Expand All @@ -33,6 +43,7 @@ fn main() -> Result<(), Error> {
let front_end_handler = ServiceBuilder::build_service(&config);

let listener = ServiceBuilder::start_listener(&config.listener);

// Multiple threads can not just have a reference of the front end handler because they could
// outlive the run function. It is needed to give them all ownership of the front end handler
// through an Arc.
Expand All @@ -44,13 +55,15 @@ fn main() -> Result<(), Error> {

let threadpool = ServiceBuilder::build_threadpool(config.core_settings.thread_pool_size);

info!("PARSEC is ready.");

#[cfg(feature = "systemd-daemon")]
// Notify systemd that the daemon is ready, the start command will block until this point.
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);

loop {
if kill_signal.load(Ordering::Relaxed) {
println!("SIGTERM signal received.");
info!("SIGTERM signal received.");
break;
}

Expand All @@ -69,7 +82,7 @@ fn main() -> Result<(), Error> {
}
}

println!("Shutting down PARSEC, waiting for all threads to finish.");
info!("Shutting down PARSEC, waiting for all threads to finish.");
threadpool.join();

Ok(())
Expand Down
19 changes: 9 additions & 10 deletions src/front/domain_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
// 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 super::listener;
use listener::Listen;
use listener::ReadWrite;
use log::error;
use std::fs;
use std::io::ErrorKind;
use std::os::unix::io::FromRawFd;
use std::os::unix::net::UnixListener;
use std::path::Path;
use std::time::Duration;

use super::listener;

use listener::Listen;
use listener::ReadWrite;
use std::os::unix::io::FromRawFd;

static SOCKET_PATH: &str = "/tmp/security-daemon-socket";

/// Listener implementation for Unix sockets as the underlying IPC mechanism.
Expand Down Expand Up @@ -87,13 +86,13 @@ impl Listen for DomainSocketListener {
match stream_result {
Ok((stream, _)) => {
if let Err(err) = stream.set_read_timeout(Some(self.timeout)) {
println!("Failed to set read timeout ({})", err);
error!("Failed to set read timeout ({})", err);
None
} else if let Err(err) = stream.set_write_timeout(Some(self.timeout)) {
println!("Failed to set write timeout ({})", err);
error!("Failed to set write timeout ({})", err);
None
} else if let Err(err) = stream.set_nonblocking(false) {
println!("Failed to set stream as blocking ({})", err);
error!("Failed to set stream as blocking ({})", err);
None
} else {
Some(Box::from(stream))
Expand All @@ -113,7 +112,7 @@ impl Listen for DomainSocketListener {
// Check if the error is because no connections are currently present.
if err.kind() != ErrorKind::WouldBlock {
// Only log the real errors.
println!("Failed to connect with a UnixStream ({})", err);
error!("Failed to connect with a UnixStream ({})", err);
}
None
}
Expand Down
9 changes: 5 additions & 4 deletions src/front/front_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
use crate::authenticators::Authenticate;
use crate::back::dispatcher::Dispatcher;
use log::{error, info};
use parsec_interface::requests::AuthType;
use parsec_interface::requests::ResponseStatus;
use parsec_interface::requests::{Request, Response};
Expand Down Expand Up @@ -44,11 +45,11 @@ impl FrontEndHandler {
let request = match Request::read_from_stream(&mut stream) {
Ok(request) => request,
Err(status) => {
println!("Failed to read request; status: {}", status);
error!("Failed to read request; status: {}", status);

let response = Response::from_status(status);
if let Err(status) = response.write_to_stream(&mut stream) {
println!("Failed to write response; status: {}", status);
error!("Failed to write response; status: {}", status);
}
return;
}
Expand All @@ -75,8 +76,8 @@ impl FrontEndHandler {
// Serialise the responso into bytes
// Write bytes to stream
match response.write_to_stream(&mut stream) {
Ok(_) => println!("Request handled successfully"),
Err(err) => println!("Failed to send response; error: {}", err),
Ok(_) => info!("Request handled successfully"),
Err(err) => error!("Failed to send response; error: {}", err),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/key_id_managers/on_disk_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//! For security reasons, only the PARSEC service should have the ability to modify these files.
use super::{KeyTriple, ManageKeyIDs};
use crate::authenticators::ApplicationName;
use log::{error, info};
use parsec_interface::requests::ProviderID;
use std::collections::HashMap;
use std::convert::TryFrom;
Expand Down Expand Up @@ -192,7 +193,7 @@ impl OnDiskKeyIDManager {
for app_name_dir_path in list_dirs(&mappings_dir_path)?.iter() {
for provider_dir_path in list_dirs(&app_name_dir_path)?.iter() {
for key_name_file_path in list_files(&provider_dir_path)?.iter() {
println!("Found mapping file: {:?}.", key_name_file_path);
info!("Found mapping file: {:?}.", key_name_file_path);
let mut key_id = Vec::new();
let mut key_id_file = File::open(&key_name_file_path)?;
key_id_file.read_to_end(&mut key_id)?;
Expand All @@ -211,7 +212,7 @@ impl OnDiskKeyIDManager {
key_store.insert(key_triple, key_id);
}
Err(string) => {
println!("Failed to convert the mapping path found to an UTF-8 string (error: {}).", string);
error!("Failed to convert the mapping path found to an UTF-8 string (error: {}).", string);
}
}
}
Expand Down
Loading