/*
 * SPDX-FileCopyrightText: 2025 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#ifndef TANGO_TEST_CATCH2_UTILS_CAPTURE_CERR_H
#define TANGO_TEST_CATCH2_UTILS_CAPTURE_CERR_H

#include <iostream>
#include <sstream>

struct CaptureCerr
{
    CaptureCerr() :
        old_buf(std::cerr.rdbuf(capture_stream.rdbuf()))
    {
    }

    ~CaptureCerr()
    {
        // Restore the original buffer
        std::cerr.rdbuf(old_buf);
    }

    // Get everything that was streamed into std::cerr
    std::string str() const
    {
        return capture_stream.str();
    }

  private:
    std::ostringstream capture_stream;
    std::streambuf *old_buf;
};

#endif
