Skip to content

Commit aafd176

Browse files
committed
Replace println calls with log crate
Pulls in new changes from test client that already contains the switch to log commit. Adds an INFO log level in the test script, in the CI and in the systemd unit file. Signed-off-by: Hugues de Valon <[email protected]>
1 parent d0933e0 commit aafd176

File tree

11 files changed

+97
-61
lines changed

11 files changed

+97
-61
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- uses: actions/checkout@v1
3939
- run: cargo build --verbose
4040
- run: |
41-
cargo run &
41+
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
4242
cargo test --test normal
4343
4444
integ-test-persistence:
@@ -49,7 +49,7 @@ jobs:
4949
- run: cargo build --verbose
5050
- name: Tests executed before the shutdown
5151
run: |
52-
cargo run &
52+
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
5353
SERVER_PID=$!
5454
cargo test --test persistent-before
5555
kill $SERVER_PID
@@ -62,7 +62,7 @@ jobs:
6262
printf '\xe0\x19\xb2\x5c' > mappings/cm9vdA==/1/VGVzdCBLZXk\=
6363
- name: Tests executed after the shutdown
6464
run: |
65-
cargo run &
65+
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
6666
cargo test --test persistent-after
6767
6868
stress-test:
@@ -72,6 +72,6 @@ jobs:
7272
- uses: actions/checkout@v1
7373
- run: cargo build --verbose
7474
- run: |
75-
cargo run &
75+
RUST_BACKTRACE=1 RUST_LOG=info cargo run &
7676
cargo test --test stress_test
7777

Cargo.lock

Lines changed: 23 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ signal-hook = "0.1.10"
2020
sd-notify = { version = "0.1.0", optional = true }
2121
toml = "0.4.2"
2222
serde = { version = "1.0", features = ["derive"] }
23+
env_logger = "0.7.1"
24+
log = "0.4.8"
2325

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

2830
[build-dependencies]

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ This project uses the following third party crates:
137137
* uuid (Apache-2.0)
138138
* threadpool (Apache-2.0)
139139
* std-semaphore (MIT and Apache-2.0)
140-
* num_cpus (MIT and Apache-2.0)
140+
* num\_cpus (MIT and Apache-2.0)
141141
* signal-hook (MIT and Apache-2.0)
142142
* sd-notify (Apache-2.0)
143+
* log (Apache-2.0)
144+
* env\_logger (MIT and Apache-2.0)
143145

144146
This project uses the following third party libraries:
145147
* [Mbed Crypto](https://github.com/ARMmbed/mbed-crypto) (Apache-2.0)

src/bin/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
15+
use log::info;
1516
use parsec::utils::{ServiceBuilder, ServiceConfig};
1617
use signal_hook::{flag, SIGTERM};
1718
use std::io::Error;
@@ -25,6 +26,15 @@ const CONFIG_FILE_PATH: &str = "./config.toml";
2526
const MAIN_LOOP_DEFAULT_SLEEP: u64 = 10;
2627

2728
fn main() -> Result<(), Error> {
29+
if cfg!(feature = "systemd-daemon") {
30+
// Logs viewed with journalctl already contain timestamps.
31+
env_logger::builder().format_timestamp(None).init();
32+
} else {
33+
env_logger::init();
34+
}
35+
36+
info!("PARSEC started.");
37+
2838
let config_file =
2939
::std::fs::read_to_string(CONFIG_FILE_PATH).expect("Failed to read configuration file");
3040
let config: ServiceConfig =
@@ -33,6 +43,7 @@ fn main() -> Result<(), Error> {
3343
let front_end_handler = ServiceBuilder::build_service(&config);
3444

3545
let listener = ServiceBuilder::start_listener(&config.listener);
46+
3647
// Multiple threads can not just have a reference of the front end handler because they could
3748
// outlive the run function. It is needed to give them all ownership of the front end handler
3849
// through an Arc.
@@ -44,13 +55,15 @@ fn main() -> Result<(), Error> {
4455

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

58+
info!("PARSEC is ready.");
59+
4760
#[cfg(feature = "systemd-daemon")]
4861
// Notify systemd that the daemon is ready, the start command will block until this point.
4962
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
5063

5164
loop {
5265
if kill_signal.load(Ordering::Relaxed) {
53-
println!("SIGTERM signal received.");
66+
info!("SIGTERM signal received.");
5467
break;
5568
}
5669

@@ -69,7 +82,7 @@ fn main() -> Result<(), Error> {
6982
}
7083
}
7184

72-
println!("Shutting down PARSEC, waiting for all threads to finish.");
85+
info!("Shutting down PARSEC, waiting for all threads to finish.");
7386
threadpool.join();
7487

7588
Ok(())

src/front/domain_socket.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@
1212
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
15+
use super::listener;
16+
use listener::Listen;
17+
use listener::ReadWrite;
18+
use log::error;
1519
use std::fs;
1620
use std::io::ErrorKind;
21+
use std::os::unix::io::FromRawFd;
1722
use std::os::unix::net::UnixListener;
1823
use std::path::Path;
1924
use std::time::Duration;
2025

21-
use super::listener;
22-
23-
use listener::Listen;
24-
use listener::ReadWrite;
25-
use std::os::unix::io::FromRawFd;
26-
2726
static SOCKET_PATH: &str = "/tmp/security-daemon-socket";
2827

2928
/// Listener implementation for Unix sockets as the underlying IPC mechanism.
@@ -87,13 +86,13 @@ impl Listen for DomainSocketListener {
8786
match stream_result {
8887
Ok((stream, _)) => {
8988
if let Err(err) = stream.set_read_timeout(Some(self.timeout)) {
90-
println!("Failed to set read timeout ({})", err);
89+
error!("Failed to set read timeout ({})", err);
9190
None
9291
} else if let Err(err) = stream.set_write_timeout(Some(self.timeout)) {
93-
println!("Failed to set write timeout ({})", err);
92+
error!("Failed to set write timeout ({})", err);
9493
None
9594
} else if let Err(err) = stream.set_nonblocking(false) {
96-
println!("Failed to set stream as blocking ({})", err);
95+
error!("Failed to set stream as blocking ({})", err);
9796
None
9897
} else {
9998
Some(Box::from(stream))
@@ -113,7 +112,7 @@ impl Listen for DomainSocketListener {
113112
// Check if the error is because no connections are currently present.
114113
if err.kind() != ErrorKind::WouldBlock {
115114
// Only log the real errors.
116-
println!("Failed to connect with a UnixStream ({})", err);
115+
error!("Failed to connect with a UnixStream ({})", err);
117116
}
118117
None
119118
}

src/front/front_end.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515
use crate::authenticators::Authenticate;
1616
use crate::back::dispatcher::Dispatcher;
17+
use log::{error, info};
1718
use parsec_interface::requests::AuthType;
1819
use parsec_interface::requests::ResponseStatus;
1920
use parsec_interface::requests::{Request, Response};
@@ -44,11 +45,11 @@ impl FrontEndHandler {
4445
let request = match Request::read_from_stream(&mut stream) {
4546
Ok(request) => request,
4647
Err(status) => {
47-
println!("Failed to read request; status: {}", status);
48+
error!("Failed to read request; status: {}", status);
4849

4950
let response = Response::from_status(status);
5051
if let Err(status) = response.write_to_stream(&mut stream) {
51-
println!("Failed to write response; status: {}", status);
52+
error!("Failed to write response; status: {}", status);
5253
}
5354
return;
5455
}
@@ -75,8 +76,8 @@ impl FrontEndHandler {
7576
// Serialise the responso into bytes
7677
// Write bytes to stream
7778
match response.write_to_stream(&mut stream) {
78-
Ok(_) => println!("Request handled successfully"),
79-
Err(err) => println!("Failed to send response; error: {}", err),
79+
Ok(_) => info!("Request handled successfully"),
80+
Err(err) => error!("Failed to send response; error: {}", err),
8081
}
8182
}
8283
}

src/key_id_managers/on_disk_manager/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
//! For security reasons, only the PARSEC service should have the ability to modify these files.
2727
use super::{KeyTriple, ManageKeyIDs};
2828
use crate::authenticators::ApplicationName;
29+
use log::{error, info};
2930
use parsec_interface::requests::ProviderID;
3031
use std::collections::HashMap;
3132
use std::convert::TryFrom;
@@ -192,7 +193,7 @@ impl OnDiskKeyIDManager {
192193
for app_name_dir_path in list_dirs(&mappings_dir_path)?.iter() {
193194
for provider_dir_path in list_dirs(&app_name_dir_path)?.iter() {
194195
for key_name_file_path in list_files(&provider_dir_path)?.iter() {
195-
println!("Found mapping file: {:?}.", key_name_file_path);
196+
info!("Found mapping file: {:?}.", key_name_file_path);
196197
let mut key_id = Vec::new();
197198
let mut key_id_file = File::open(&key_name_file_path)?;
198199
key_id_file.read_to_end(&mut key_id)?;
@@ -211,7 +212,7 @@ impl OnDiskKeyIDManager {
211212
key_store.insert(key_triple, key_id);
212213
}
213214
Err(string) => {
214-
println!("Failed to convert the mapping path found to an UTF-8 string (error: {}).", string);
215+
error!("Failed to convert the mapping path found to an UTF-8 string (error: {}).", string);
215216
}
216217
}
217218
}

0 commit comments

Comments
 (0)