/******************************************************************************
 * file    include/better_log.h 
 * author  YuLiang
 * version 1.0.0
 * date    14-Sep-2021
 * brief   This file provides all the headers of the log functions.
 ******************************************************************************
 * Attention
 *
 * 
© COPYRIGHT(c) 2021 LandPower
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of LandPower nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/
#ifndef _BETTER_LOG_H_
#define _BETTER_LOG_H_
/* Includes ------------------------------------------------------------------*/
#include 
/* Define --------------------------------------------------------------------*/
#define LOG_FILE "PowerIoT.db"
#define LOG_STR_LEN 512
#define LOG_TABLE_NAME "better_log"
#define LOG_MAX_DECORD 5000
#define LOG_DEFAULT_SHOW_CNT 100
#define LOG_DB_FIFO "LOG_DB_FIFO"
#define LOG_STR "Display default most 100 logs\n"
#define LOG_LEVELS "(errors|warnings|notifications|informational|debug)"
#define LOG_LEVEL_DESC \
  "Error messages\n" \
  "Warning messages\n" \
  "Normal but significant messages\n" \
  "Informational messages\n" \
  "Debug messages\n"
/* Exported types ------------------------------------------------------------*/
typedef enum
{
    LOG_LVL_ERR,
    LOG_LVL_WARN,
    LOG_LVL_NOTIF,
    LOG_LVL_INFO,
    LOG_LVL_DBG,
    LOG_LVL_MAX
} LOG_LVL_E;
typedef enum
{
    LOG_MODE_STDOUT,
    LOG_MODE_FILE,
    LOG_MODE_MONITOR,
    LOG_MODE_MAX
} LOG_MODE_E;
typedef enum 
{
    LOG_DEFAULT,
    LOG_CLI,
    LOG_MEMORY,
    LOG_FIFO,
    LOG_GPIO,
    LOG_PD,
    LOG_DAU,
    LOG_CSG,
    LOG_STORAGE,
    LOG_DEBUG,
    LOG_MAX
} LOG_MODULE_E;
typedef enum
{
    LOG_SHOW_CNT,
    LOG_SHOW_LVL,
    LOG_SHOW_KEYWORD,
    LOG_SHOW_MAX
} LOG_SHOW_E;
typedef struct _log_lvl
{
    int32_t lvl;
    char *describe;
} log_lvl_t;
typedef struct _log_module
{
    int32_t module;
    char *describe;
} log_module_t;
/* log全局数据结构体. */
typedef struct _log_sys
{
    int32_t enable_lvl[LOG_MODE_MAX];   /* log模块使能等级,位图表示. */
    int32_t default_lvl;                /* 默认log模块使能等级,位图表示. */
    sqlite3 *db;                        /* log数据库指针. */
    char *filename;                     /* log数据库名字. */
    int32_t timestamp_precision;        /* 默认log时间戳精度. */
    pthread_mutex_t log_mutex;      
    pthread_mutex_t log_out_mutex;
    pthread_mutex_t log_db_mutex;       /* 数据库读写互斥锁 */
} log_sys_t;
typedef struct
{
    int32_t log_fifo_id;
    pthread_mutex_t mutex;              /* log fifo mutex */
} _log_fifo_t;
typedef struct
{
    LOG_SHOW_E type;
    int32_t param;
    char log_show_str[LOG_STR_LEN];
} _log_show_t;
typedef struct
{
    LOG_MODULE_E module;
    LOG_LVL_E lvl;
    char log_out_str[LOG_STR_LEN];
} _log_out_t;
typedef struct
{
    uint32_t type;
    void *data;
} _log_msg_t;
/* Exported macro ------------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/
extern log_sys_t *log_sys;
/* Extern functions ----------------------------------------------------------*/
/* 配置函数属性,让gcc按printf一样检查函数参数,第b个参数为可变参数.  */
#define BTLOG_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
extern void log_out(LOG_MODULE_E module, LOG_LVL_E priority, const char *format, ...)
    BTLOG_ATTRIBUTE(3, 4);
extern void log_err(LOG_MODULE_E module, const char *format, ...)
    BTLOG_ATTRIBUTE(2, 3);
extern void log_warn(LOG_MODULE_E module, const char *format, ...)
    BTLOG_ATTRIBUTE(2, 3);
extern void log_info(LOG_MODULE_E module, const char *format, ...)
    BTLOG_ATTRIBUTE(2, 3);
extern void log_notice(LOG_MODULE_E module, const char *format, ...)
    BTLOG_ATTRIBUTE(2, 3);
extern void log_debug(LOG_MODULE_E module, const char *format, ...)
    BTLOG_ATTRIBUTE(2, 3);
extern int32_t log_open();
extern void log_set_level(LOG_MODE_E mode, LOG_LVL_E log_level);
extern void log_unset_level(LOG_MODE_E mode, LOG_LVL_E log_level);
extern int32_t log_level_get_by_name(const char *lvl_name);
extern int32_t log_module_get_by_name( const char *module_name );
extern void log_backtrace(int32_t priority);
extern void log_clean(void);
extern void log_show( int32_t cnt, LOG_LVL_E priority, const char *key_word );
extern int32_t log_handle_init(void);
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/