up_subscription/common/
helpers.rs

1/********************************************************************************
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * See the NOTICE file(s) distributed with this work for additional
5 * information regarding copyright ownership.
6 *
7 * This program and the accompanying materials are made available under the
8 * terms of the Apache License Version 2.0 which is available at
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 ********************************************************************************/
13
14use log::*;
15use std::future::Future;
16use std::sync::Once;
17use tokio::task;
18
19static INIT: Once = Once::new();
20
21pub fn init_once() {
22    INIT.call_once(env_logger::init);
23}
24
25type SpawnResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
26pub(crate) fn spawn_and_log_error<F>(fut: F) -> task::JoinHandle<()>
27where
28    F: Future<Output = SpawnResult<()>> + Send + 'static,
29{
30    task::spawn(async move {
31        if let Err(e) = fut.await {
32            error!("{}", e)
33        }
34    })
35}