/******************************************************************
 * @file  log_interface.h
 * @brief:    [file description] 
 * @author: guicheng.liu
 * @Date 2021年3月9日
 * COPYRIGHT NOTICE: (c) smartlogictech. All rights reserved. 
 * Change_date         Owner         Change_content
 * 2021年3月9日        guicheng.liu       create file

*****************************************************************/

#ifndef     LOG_INTERFACE_H
#define     LOG_INTERFACE_H
#include <type_define.h>
#include "common.h"


//log 相关宏定义
//LOG_DD_HEADER_SIZE 由平台提供接口, 目前为12
#define OSP_MSG_HEAD_LEN    (12)
#define LOG_DD_HEADER_SIZE   (OSP_MSG_HEAD_LEN)
#define LOG_HEADER_SIZE      sizeof(log_msg_header_t)
#define LOG_TOTAL_HDR_SIZE   (LOG_DD_HEADER_SIZE + LOG_HEADER_SIZE)
#define LOG_MAX_LEN_BYTE     (512)
#define LOG_MAX_LEN_WORD     (LOG_MAX_LEN_BYTE >> 2)
#define LOG_MAX_CONTENT_SIZE (LOG_MAX_LEN_BYTE - LOG_TOTAL_HDR_SIZE)

#define LOG_DM_BUF_NUM       (8)



#define LOG_DEFINE_BASE   (0)
#define LOG_DEFINE_STEP   (100)

//msg_type 各模块分配号码段
#define STRING_LOG (LOG_DEFINE_BASE + 0*LOG_DEFINE_STEP)
#define MAIN_LOG   (LOG_DEFINE_BASE + 0*LOG_DEFINE_STEP + 1)
#define FAPI_LOG   (LOG_DEFINE_BASE + 1*LOG_DEFINE_STEP)
#define SSB_LOG    (LOG_DEFINE_BASE + 2*LOG_DEFINE_STEP)
#define PDCCH_LOG  (LOG_DEFINE_BASE + 3*LOG_DEFINE_STEP)
#define PDSCH_LOG  (LOG_DEFINE_BASE + 4*LOG_DEFINE_STEP)
#define DEOFDM_LOG (LOG_DEFINE_BASE + 5*LOG_DEFINE_STEP)
#define PUSCH_LOG  (LOG_DEFINE_BASE + 6*LOG_DEFINE_STEP)
#define PUCCH_LOG  (LOG_DEFINE_BASE + 7*LOG_DEFINE_STEP)
#define PRACH_LOG  (LOG_DEFINE_BASE + 8*LOG_DEFINE_STEP)




/*buf_type: 0：用户申请的静态DM空间，1:用户自定义的DM空间 
 msgAddr：当bufType为0时，给出申请的静态空间首地址，当bufType为1时，给出用户自定义的DM空间地址 */
//void log_output(uint8_t level, uint8_t task_id, uint16_t msg_type, 
//				uint16_t sfn, uint16_t slot, uint16_t slot_offset, uint8_t msg_len, uint8_t buf_type, char* msg_addr);
//void set_log_level(log_level_e level);
//
//void log_sprintf(log_level_e level, const char *fmt, ...);

//#define  LOG_INFO_S(level, fmt, ...) LOG_INFO_S(level, fmt, ## __VA_ARGS__)
//字符串log输出方式宏定义
#define LOG_CMD_S(fmt, ...)       log_sprintf(CMD, "[CMD]"fmt, ## __VA_ARGS__)
#define LOG_ERROR_S(fmt, ...)     log_sprintf(ERROR, "[ERR]"fmt, ## __VA_ARGS__)
#define LOG_WARN_S(fmt, ...)      log_sprintf(WARN, "[WARN]"fmt, ## __VA_ARGS__)
#define LOG_INFO_S(fmt, ...)      log_sprintf(INFO, "[INFO]"fmt, ## __VA_ARGS__)
#define LOG_DEBUG_S(fmt, ...)     log_sprintf(DEBUG, "[DBG]"fmt, ## __VA_ARGS__)


//字节流log输出方式宏定义
#define LOG_CMD(task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len, msg_addr)   \
    log_output(CMD, task_id, msg_type, sfn, slot,slot_offset, buf_type, msg_len, msg_addr)
#define LOG_ERROR(task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len, msg_addr) \
    log_output(ERROR, task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len, msg_addr)
#define LOG_WARN(task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len, msg_addr)  \
    log_output(WARN, task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len,  msg_addr)
#define LOG_INFO(task_id, msg_type, sfn, slot, slot_offset,buf_type,  msg_len, msg_addr)  \
    log_output(INFO, task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len, msg_addr)
#define LOG_DEBUG(task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len, msg_addr) \
    log_output(DEBUG, task_id, msg_type, sfn, slot, slot_offset, buf_type, msg_len, msg_addr)


//串口log输出宏定义
#define LOG_PRINTF(fmt, ...) //osp_printf(fmt"\n\r",  ## __VA_ARGS__)


//log 相关结构体定义
typedef enum
{
	CMD,
    ERROR,
    WARN,
    INFO,
    DEBUG
} log_level_e;

typedef struct log_msg_header
{
	uint32_t sync_flag : 9;// 0x13C
	uint32_t msg_level : 3;
	uint32_t msg_type : 12;//log msg type
	uint32_t slot : 8;

	uint32_t sfn : 10;//sfn + slot * 1024
	uint32_t slot_offset : 10;
	uint32_t msg_len : 9;//不包含log_header的长度
	uint32_t msgNo : 3;//消息号
	
	char buffer[];
}log_msg_header_t;

void set_log_level(log_level_e level);
void  log_pool_init();
char *log_buf_alloc_static();
void log_output(uint8_t level, uint8_t task_id, uint16_t msg_type,
                uint16_t sfn, uint16_t slot, uint16_t slot_offset, uint8_t buf_type, uint16_t msg_len, char* msg_addr);
void log_sprintf(log_level_e level, const char *fmt, ...);
#endif
