// Copyright (c) 2021, 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 <carb/flatcache/IPath.h>
#include <carb/flatcache/IToken.h>

#include <iostream>

// To log all FlatCache methods that access a particular path and attribute,
// set the following three defines
#define ENABLE_FLATCACHE_API_LOG 0

#if ENABLE_FLATCACHE_API_LOG
#define attrToTrace "attrToLog"
#define pathToTrace "/primToLog"

namespace carb
{
namespace flatcache
{

struct ApiLogger
{
    bool& enabled;
    const char* desc;

    ApiLogger(const char* desc, bool& enabled, const TokenC& attrNameC) : desc(desc), enabled(enabled)
    {
        Token attrName(attrNameC);
        if (attrName == Token(attrToTrace))
        {
            std::cout << "begin " << desc << "\n";
            enabled = true;
        }
    }

    ApiLogger(const char* desc, bool& enabled, const PathC& pathC, const TokenC& attrNameC) : desc(desc), enabled(enabled)
    {
        Path path(pathC);
        Token attrName(attrNameC);
        if (path == Path(pathToTrace) && attrName == Token(attrToTrace))
        {
            std::cout << "begin " << desc << "\n";
            enabled = true;
        }
    }

    ~ApiLogger()
    {
        if (enabled)
        {
            std::cout << "end " << desc << "\n";
        }
        enabled = false;
    }
};

#define APILOGGER(...) ApiLogger logger(__VA_ARGS__)

}
}
#else
#define APILOGGER(...)
#endif
