/**
 * @file log.h
 * @author June6 (https://github.com/Junedoubling6)
 * @brief
 * @version 0.2
 * @date 2023-03-03
 *
 * @copyright Copyright (c) 2022 - , All rights reserved.
 *
 */
#ifndef LOG_H__
#define LOG_H__

#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "sdkconfig.h"

#if CONFIG_USE_LOG
#include "esp_log.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if CONFIG_USE_LOG
#define LOG_LEVEL_OFF 0
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_WARNING 2
#define LOG_LEVEL_INFO 3
#define LOG_LEVEL_DEBUG 4

#ifndef CONFIG_LOG_LEVEL
#define CONFIG_LOG_LEVEL LOG_LEVEL_INFO
#endif

#if (CONFIG_LOG_LEVEL >= LOG_LEVEL_INFO)
#define USER_LOGI(tag, format, ...) ESP_LOGI(tag, format, ##__VA_ARGS__)
#else
#define USER_LOGI(...)
#endif

#if (CONFIG_LOG_LEVEL >= LOG_LEVEL_ERROR)
#define USER_LOGE(tag, format, ...)                              \
  ESP_LOGE(tag,                                                  \
           format                                                \
           "\r\nThe error is in line %d of function %s in file " \
           "%s.\r\n",                                            \
           ##__VA_ARGS__, __LINE__, __FUNCTION__, __FILE__)
#else
#define USER_LOGE(...)
#endif

#if (CONFIG_LOG_LEVEL >= LOG_LEVEL_WARNING)
#define USER_LOGW(tag, format, ...)                                \
  ESP_LOGW(tag,                                                    \
           format                                                  \
           "\r\nThe warning is in line %d of function %s in file " \
           "%s.\r\n",                                              \
           ##__VA_ARGS__, __LINE__, __FUNCTION__, __FILE__)
#else
#define USER_LOGW(...)
#endif

#if (CONFIG_LOG_LEVEL >= LOG_LEVEL_DEBUG)
#define USER_LOGD(tag, format, ...)                              \
  ESP_LOGD(tag,                                                  \
           format                                                \
           "\r\nThe debug is in line %d of function %s in file " \
           "%s.\r\n",                                            \
           ##__VA_ARGS__, __LINE__, __FUNCTION__, __FILE__)
#else
#define USER_LOGD(...)
#endif

#else /* !USE_LOG */
#define USER_LOGI(...)
#define USER_LOGD(...)
#define USER_LOGW(...)
#define USER_LOGE(...)
#endif

#ifdef __cplusplus
}
#endif

#endif  // LOG_H__
