carb/logging/LoggingUtils.h

File members: carb/logging/LoggingUtils.h

// Copyright (c) 2023-2024, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//

#pragma once

#include "Log.h"

#if CARB_ASSERT_ENABLED
#    include "../thread/Util.h"
#endif

namespace carb
{
namespace logging
{

#if CARB_VERSION_ATLEAST(carb_logging_ILogging, 1, 1)
class ScopedFilePause
{
public:
    CARB_DEPRECATED("Use StandardLogger2 instead")
    ScopedFilePause(StandardLogger* inst) : ScopedFilePause(g_carbLogging->getStandardLogger2(inst))
    {
    }

    ScopedFilePause(StandardLogger2* inst) : m_inst(inst)
    {
        if (m_inst)
        {
            m_inst->addRef();
            m_inst->pauseFileLogging();
        }
    }

    ~ScopedFilePause()
    {
        if (m_inst)
        {
            m_inst->resumeFileLogging();
            m_inst->release();
        }
    }

    CARB_PREVENT_COPY_AND_MOVE(ScopedFilePause);

private:
    StandardLogger2* m_inst;
};
#else
class ScopedFilePause
{
public:
    ScopedFilePause(StandardLogger* inst) : m_inst(inst)
    {
        if (m_inst)
        {
            m_inst->pauseFileLogging(m_inst);
        }
    }
    ~ScopedFilePause()
    {
        if (m_inst)
        {
            m_inst->resumeFileLogging(m_inst);
        }
    }

    CARB_PREVENT_COPY_AND_MOVE(ScopedFilePause);

private:
    StandardLogger* m_inst;
};
#endif

#if CARB_VERSION_ATLEAST(carb_logging_ILogging, 1, 1)
class ScopedLevelThreadOverride
{
public:
    CARB_DEPRECATED("Use StandardLogger2 instead")
    ScopedLevelThreadOverride(StandardLogger* logger, OutputType type, int32_t level)
        : ScopedLevelThreadOverride(g_carbLogging->getStandardLogger2(logger), type, level)
    {
    }

    ScopedLevelThreadOverride(StandardLogger2* logger, OutputType type, int32_t level)
        : m_logger(logger),
          m_type(type)
#    if CARB_ASSERT_ENABLED
          ,
          m_threadId(carb::this_thread::getId())
#    endif
    {
        if (m_logger)
        {
            m_logger->addRef();
            m_logger->setLevelThresholdThreadOverride(m_type, level);
        }
    }

    ~ScopedLevelThreadOverride()
    {
        // Make sure we are on the same thread
        CARB_ASSERT(m_threadId == carb::this_thread::getId());
        if (m_logger)
        {
            m_logger->clearLevelThresholdThreadOverride(m_type);
            m_logger->release();
        }
    }

    CARB_PREVENT_COPY_AND_MOVE(ScopedLevelThreadOverride);

private:
    StandardLogger2* m_logger;
    OutputType m_type;
#    if CARB_ASSERT_ENABLED
    thread::ThreadId m_threadId;
#    endif
};
#endif

} // namespace logging
} // namespace carb