/*
 * @Author: jinxiangwzh jinxiangwzh@163.com
 * @Date: 2023-03-01 15:02:37
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-09-18 13:35:01
 * @FilePath: et_log.h
 * @Description: Enhanced logging system with color support and float formatting
 *
 * Copyright (c) 2023 by Inomec, All Rights Reserved.
 */
#ifndef __ET_LOG_H__
#define __ET_LOG_H__
#include "etk_def.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

/* log level definitions */
#define DEBUG_LEVEL   3
#define INFO_LEVEL    2
#define WARNING_LEVEL 1
#define ERROR_LEVEL   0

#define ET_LOG_BUF_SIZE 256

/* color codes for terminal output */
#define COLOR_RESET     "\033[0m" /* reset all colors */
#define COLOR_BOLD      "\033[1m" /* bold text */
#define COLOR_UNDERLINE "\033[4m" /* underlined text */

/* foreground colors */
#define COLOR_BLACK   "\033[30m" /* black text */
#define COLOR_RED     "\033[31m" /* red text */
#define COLOR_GREEN   "\033[32m" /* green text */
#define COLOR_YELLOW  "\033[33m" /* yellow text */
#define COLOR_BLUE    "\033[34m" /* blue text */
#define COLOR_MAGENTA "\033[35m" /* magenta text */
#define COLOR_CYAN    "\033[36m" /* cyan text */
#define COLOR_WHITE   "\033[37m" /* white text */

/* bright foreground colors */
#define BRIGHT_COLOR_BLACK   "\033[1;30m" /* bright black text */
#define BRIGHT_COLOR_RED     "\033[1;31m" /* bright red text */
#define BRIGHT_COLOR_GREEN   "\033[1;32m" /* bright green text */
#define BRIGHT_COLOR_YELLOW  "\033[1;33m" /* bright yellow text */
#define BRIGHT_COLOR_BLUE    "\033[1;34m" /* bright blue text */
#define BRIGHT_COLOR_MAGENTA "\033[1;35m" /* bright magenta text */
#define BRIGHT_COLOR_CYAN    "\033[1;36m" /* bright cyan text */
#define BRIGHT_COLOR_WHITE   "\033[1;37m" /* bright white text */

/* background colors */
#define COLOR_BG_BLACK   "\033[40m" /* black background */
#define COLOR_BG_RED     "\033[41m" /* red background */
#define COLOR_BG_GREEN   "\033[42m" /* green background */
#define COLOR_BG_YELLOW  "\033[43m" /* yellow background */
#define COLOR_BG_BLUE    "\033[44m" /* blue background */
#define COLOR_BG_MAGENTA "\033[45m" /* magenta background */
#define COLOR_BG_CYAN    "\033[46m" /* cyan background */
#define COLOR_BG_WHITE   "\033[47m" /* white background */

/* timestamp function type definition */
typedef unsigned long (*et_timestamp_func_t)(void);

#ifdef __cplusplus
extern "C"
{
#endif
/**
 * @brief set device log level.
 * @param level - DEBUG:3,INFO:2,WARN:1,ERROR:0
 * @param state - true:enable print function name and line number,false:disable
 * @param ts_state - true:enable print timestamp,false:disable
 * @deprecated Use et_log_init_ex instead
 */
void et_log_init(int level, bool line_state, bool ts_state);

/**
 * @brief Initialize logging system with external timestamp function
 * @param level - log level (DEBUG:3, INFO:2, WARN:1, ERROR:0)
 * @param line_state - enable/disable function name and line number
 * @param ts_state - enable/disable timestamp
 * @param timestamp_func - function pointer to get timestamp, can be NULL to disable
 */
void et_log_init_ex(int level, bool line_state, bool ts_state, et_timestamp_func_t timestamp_func);

void et_dbg(const char *func, int line, const char *fmt, ...);

void et_info(const char *func, int line, const char *fmt, ...);

void et_warn(const char *func, int line, const char *fmt, ...);

void et_err(const char *func, int line, const char *fmt, ...);

void et_print(const char *fmt, ...);

void et_print_float(float num, int precision);

/**
 * @brief print log with color.
 * @param color: color string
 * @param fmt: format string
 * @param ...: variable arguments
 */
void et_print_raw_with_color(const char *color, const char *fmt, ...);
#ifdef __cplusplus
}
#endif

/* macro definitions for logging with function name and line number */
#define ET_DBG(fmt, ...)  et_dbg(__func__, __LINE__, fmt, ##__VA_ARGS__)
#define ET_INFO(fmt, ...) et_info(__func__, __LINE__, fmt, ##__VA_ARGS__)
#define ET_WARN(fmt, ...) et_warn(__func__, __LINE__, fmt, ##__VA_ARGS__)
#define ET_ERR(fmt, ...)  et_err(__func__, __LINE__, fmt, ##__VA_ARGS__)

/* simplified float formatting macros */
#define ET_FLOAT_FMT_WITH_LABEL(label) label ": %d.%02d\n"
#define ET_FLOAT_ARGS(val)             (int)(val), abs((int)(((val) - (int)(val)) * 100))
#define ET_PRINTF_FLOAT(label, val)    et_print(ET_FLOAT_FMT_WITH_LABEL(label), ET_FLOAT_ARGS(val))
#endif // @page end