//------------------------------------------------------------------------------
/*
    This file is part of Beast: https://github.com/vinniefalco/Beast
    Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>

    Permission to use, copy, modify, and/or distribute this software for any
    purpose  with  or without fee is hereby granted, provided that the above
    copyright notice and this permission notice appear in all copies.

    THE  SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH  REGARD  TO  THIS  SOFTWARE  INCLUDING  ALL  IMPLIED  WARRANTIES  OF
    MERCHANTABILITY  AND  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY  SPECIAL ,  DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER  RESULTING  FROM  LOSS  OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION  OF  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <xrpl/beast/utility/Journal.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <cassert>
#ifdef BEAST_ENHANCED_LOGGING
#include <xrpl/beast/utility/EnhancedLogging.h>
#include <cstdlib>
#include <cstring>
#endif

namespace beast {

//------------------------------------------------------------------------------

// A Sink that does nothing.
class NullJournalSink : public Journal::Sink
{
public:
    NullJournalSink() : Sink(severities::kDisabled, false)
    {
    }

    ~NullJournalSink() override = default;

    bool
    active(severities::Severity) const override
    {
        return false;
    }

    bool
    console() const override
    {
        return false;
    }

    void
    console(bool) override
    {
    }

    severities::Severity
    threshold() const override
    {
        return severities::kDisabled;
    }

    void
    threshold(severities::Severity) override
    {
    }

    void
    write(severities::Severity, std::string const&) override
    {
    }

    void
    writeAlways(severities::Severity, std::string const&) override
    {
    }
};

//------------------------------------------------------------------------------

Journal::Sink&
Journal::getNullSink()
{
    static NullJournalSink sink;
    return sink;
}

//------------------------------------------------------------------------------

Journal::Sink::Sink(Severity thresh, bool console)
    : thresh_(thresh), m_console(console)
{
}

Journal::Sink::~Sink() = default;

bool
Journal::Sink::active(Severity level) const
{
    return level >= thresh_;
}

bool
Journal::Sink::console() const
{
    return m_console;
}

void
Journal::Sink::console(bool output)
{
    m_console = output;
}

severities::Severity
Journal::Sink::threshold() const
{
    return thresh_;
}

void
Journal::Sink::threshold(Severity thresh)
{
    thresh_ = thresh;
}

//------------------------------------------------------------------------------

Journal::ScopedStream::ScopedStream(Sink& sink, Severity level)
    : m_sink(sink), m_level(level)
{
    // Modifiers applied from all ctors
    m_ostream << std::boolalpha << std::showbase;
}

Journal::ScopedStream::ScopedStream(
    Stream const& stream,
    std::ostream& manip(std::ostream&))
    : ScopedStream(stream.sink(), stream.level())
{
    m_ostream << manip;
}

#ifdef BEAST_ENHANCED_LOGGING
Journal::ScopedStream::ScopedStream(
    Sink& sink,
    Severity level,
    const char* file,
    int line)
    : m_sink(sink), m_level(level), file_(file), line_(line)
{
    // Modifiers applied from all ctors
    m_ostream << std::boolalpha << std::showbase;
}
#endif

Journal::ScopedStream::~ScopedStream()
{
    std::string s(m_ostream.str());

#ifdef BEAST_ENHANCED_LOGGING
    // Add suffix if location is enabled
    if (file_ && detail::should_show_location() && !s.empty())
    {
        // Single optimized scan from the end
        size_t const lastNonWhitespace = s.find_last_not_of(" \n\r\t");

        // Skip if message is only whitespace (e.g., just "\n" or "  \n\n")
        if (lastNonWhitespace != std::string::npos)
        {
            // Count only the trailing newlines (tiny range)
            size_t trailingNewlines = 0;
            for (size_t i = lastNonWhitespace + 1; i < s.length(); ++i)
            {
                if (s[i] == '\n')
                    ++trailingNewlines;
            }

            // Build location string once
            std::ostringstream locStream;
            detail::log_write_location_string(locStream, file_, line_);
            std::string const location = locStream.str();

            // Pre-allocate exact size → zero reallocations
            size_t const finalSize = lastNonWhitespace + 1 + 1 +
                location.length() + trailingNewlines;

            std::string result;
            result.reserve(finalSize);

            // Direct string ops (no ostringstream overhead)
            result.append(s, 0, lastNonWhitespace + 1);
            result.push_back(' ');
            result += location;
            if (trailingNewlines > 0)
                result.append(trailingNewlines, '\n');

            s = std::move(result);  // Move, no copy
        }
    }
#endif

    if (!s.empty())
    {
        if (s == "\n")
            m_sink.write(m_level, "");
        else
            m_sink.write(m_level, s);
    }
}

std::ostream&
Journal::ScopedStream::operator<<(std::ostream& manip(std::ostream&)) const
{
    return m_ostream << manip;
}

//------------------------------------------------------------------------------

Journal::ScopedStream
Journal::Stream::operator<<(std::ostream& manip(std::ostream&)) const
{
    return ScopedStream(*this, manip);
}

#ifdef BEAST_ENHANCED_LOGGING

// Implementation moved to use new constructor
Journal::ScopedStream
Journal::StreamWithLocation::operator<<(
    std::ostream& manip(std::ostream&)) const
{
    // Create a ScopedStream with location info
    ScopedStream scoped(stream_.sink(), stream_.level(), file_, line_);
    scoped.ostream() << manip;
    return scoped;
}
#endif

}  // namespace beast
