# @Copyright: CEA-LIST/DIASI/SIALV/ (2023-    )
# @Author: CEA-LIST/DIASI/SIALV/ <julien.denize@cea.fr>
# @License: CECILL-C
#
# Code partially based on Torchvision (BSD 3-Clause License), available at:
#   https://github.com/pytorch/vision

from types import FunctionType
from typing import Any

import torch


def _log_api_usage_once(obj: Any) -> None:
    # Adapted from Torchvision.
    """Logs API usage(module and name) within an organization.

    In a large ecosystem, it's often useful to track the
    PyTorch and Torchaug APIs usage. This API provides the similar functionality to the logging module in the
    Python stdlib. It can be used for debugging purpose to log which methods are used and by default it is
    inactive, unless the user manually subscribes a logger via the `SetAPIUsageLogger method.

    <https://github.com/pytorch/pytorch/blob/eb3b9fe719b21fae13c7a7cf3253f970290a573e/c10/util/Logging.cpp#L114>`_.
    Please note it is triggered only once for the same API call within a process. It does not collect any data from
    open-source users since it is no-op by default. For more information, please refer to.

    * PyTorch note: https://pytorch.org/docs/stable/notes/large_scale_deployments.html#api-usage-logging;
    * Logging policy: https://github.com/pytorch/vision/issues/5052;

    Args:
        obj (class instance or method): an object to extract info from.
    """
    module = obj.__module__
    if not module.startswith("torchaug"):
        module = f"torchaug.internal.{module}"
    name = obj.__class__.__name__
    if isinstance(obj, FunctionType):
        name = obj.__name__
    torch._C._log_api_usage_once(f"{module}.{name}")
