joularcore 0.1.0

Joular Core is a platform to measure power and energy across all systems, OSes and devices
Documentation
/*
 * Copyright (c) 2025-2026, Adel Noureddine.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the
 * GNU Lesser General Public License v3.0 only (LGPL-3.0-only)
 * which accompanies this distribution, and is available at
 * https://www.gnu.org/licenses/lgpl-3.0.en.html
 *
 * Author : Adel Noureddine
 */

use std::io::Write;
use std::sync::Once;
use tracing_subscriber::EnvFilter;

static INIT: Once = Once::new();

pub fn init() {
    INIT.call_once(|| {
        let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));

        let subscriber = tracing_subscriber::fmt()
            .with_env_filter(filter)
            .with_target(false)
            .compact()
            .finish();

        let _ = tracing::subscriber::set_global_default(subscriber);
    });
}

pub fn print_error(msg: &str) {
    tracing::error!("{}", msg);
    eprintln!("\x1b[1;31m✗ {}\x1b[0m", msg);
}

pub fn print_warning(msg: &str) {
    tracing::warn!("{}", msg);
    eprintln!("\x1b[1;33m⚠ {}\x1b[0m", msg);
}

pub fn print_success(msg: &str) {
    tracing::info!("{}", msg);
    println!("\x1b[1;32m✓ {}\x1b[0m", msg);
}

/// Re-enable the terminal cursor. Errors (e.g. closed stdout during shutdown)
/// are ignored so callers can safely use this on cleanup paths.
pub fn show_cursor() {
    print!("\x1b[?25h");
    let _ = std::io::stdout().flush();
}

/// Hide the terminal cursor for live single-line updates.
pub fn hide_cursor() {
    print!("\x1b[?25l");
    let _ = std::io::stdout().flush();
}