laburnum 1.17.2

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

//! Lightweight stderr logging for IPC connection setup, gated behind a runtime
//! flag the implementing language controls.
//!
//! IPC connection setup (the version handshake in particular) is otherwise
//! invisible: the daemon runs in a separate process with its stderr redirected
//! to `/dev/null`, and a rejected handshake closes the socket before any reply,
//! so clients only ever observe a bare EOF. This module lets both sides narrate
//! that exchange.
//!
//! laburnum deliberately does not know how verbosity is configured — there is
//! no laburnum-specific env var, because that would never match the embedding
//! application's name. Instead the implementing language decides (its own
//! `--verbose` flag, its own `<LANG>_VERBOSE` env var, etc.) and pushes the
//! result in via [`set_enabled`]. The application is also responsible for
//! propagating verbosity to a spawned daemon process (e.g. by setting its own
//! env var, which the daemon inherits and re-applies on startup).

use std::sync::atomic::{
  AtomicBool,
  Ordering,
};

static ENABLED: AtomicBool = AtomicBool::new(false);

/// Enable or disable verbose IPC logging for this process.
///
/// Called by the embedding application once it has determined verbosity from
/// its own flag/env. Safe to call multiple times; the last call wins.
pub fn set_enabled(enabled: bool) {
  ENABLED.store(enabled, Ordering::Relaxed);
}

/// Returns `true` when verbose IPC logging has been enabled via [`set_enabled`].
pub fn is_enabled() -> bool {
  ENABLED.load(Ordering::Relaxed)
}

/// Emit a verbose log line to stderr, but only when [`is_enabled`] is `true`.
#[macro_export]
macro_rules! verbose {
  ($($arg:tt)*) => {
    if $crate::verbose::is_enabled() {
      ::std::eprintln!("{}", ::std::format_args!($($arg)*));
    }
  };
}