// -*- mode: c++ -*-
#ifndef LDASTOOLSAL__SIMPLE_LOGGER_INL
#define LDASTOOLSAL__SIMPLE_LOGGER_INL

#include "SimpleLogger.hh"
#include <sstream>

// Helper function to concatenate all arguments into a stringstream
template < typename... Args >
std::string
concatenate_args( Args&&... args )
{
    std::ostringstream ss;
    ( ss << ... << args );
    return ss.str();
}

// Implementation of the template methods with optimization
// Early exits prevent unnecessary string construction
template < typename... Args >
void
SimpleLogger::log_debug( int level, Args&&... args )
{
    // Early exit if system is shutting down
    if ( is_shutdown() ) {
        return;
    }
    
    // Early exit if debug level exceeds threshold
    // debug_level_ = 10 shows messages 1-10, filters out 11+
    if ( level > debug_level_ ) {
        return;
    }
    
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    // Use channel logger with lazy string construction
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_, custom_severity_level( level ) )
        << concatenate_args( std::forward< Args >( args )... );
}

template < typename... Args >
void
SimpleLogger::log_warning( Args&&... args )
{
    // Early exit if system is shutting down
    if ( is_shutdown() ) {
        return;
    }
    
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_, 
                          custom_severity_level( severity_level::warning ) )
        << concatenate_args( std::forward< Args >( args )... );
}

template < typename... Args >
void
SimpleLogger::log_ok( Args&&... args )
{
    if ( is_shutdown() ) return;
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_,
                          custom_severity_level( severity_level::ok ) )
        << concatenate_args( std::forward< Args >( args )... );
}

template < typename... Args >
void
SimpleLogger::log_note( Args&&... args )
{
    if ( is_shutdown() ) return;
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_,
                          custom_severity_level( severity_level::note ) )
        << concatenate_args( std::forward< Args >( args )... );
}

template < typename... Args >
void
SimpleLogger::log_error_non_fatal( Args&&... args )
{
    if ( is_shutdown() ) return;
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_,
                          custom_severity_level( severity_level::error_non_fatal ) )
        << concatenate_args( std::forward< Args >( args )... );
}

template < typename... Args >
void
SimpleLogger::log_error( Args&&... args )
{
    if ( is_shutdown() ) return;
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_,
                          custom_severity_level( severity_level::error ) )
        << concatenate_args( std::forward< Args >( args )... );
}

template < typename... Args >
void
SimpleLogger::log_email( Args&&... args )
{
    if ( is_shutdown() ) return;
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_,
                          custom_severity_level( severity_level::email ) )
        << concatenate_args( std::forward< Args >( args )... );
}

template < typename... Args >
void
SimpleLogger::log_phone( Args&&... args )
{
    if ( is_shutdown() ) return;
    auto core = boost::log::core::get( );
    if ( !core->get_logging_enabled( ) ) {
        return;
    }
    
    BOOST_LOG_CHANNEL_SEV( channel_logger_, channel_name_,
                          custom_severity_level( severity_level::phone ) )
        << concatenate_args( std::forward< Args >( args )... );
}

#endif /* LDASTOOLSAL__SIMPLE_LOGGER_INL */
