/*
 * @Author: zhy
 * @Date: 2022-10-11 10:08:57
 * @LastEditors: zhanghongyang zhanghy@bmser.com
 * @Description: 
 */

#ifndef BMSER_LOG_H_
#define BMSER_LOG_H_

#include <syslog.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <libgen.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "bmser_utils.h"

#ifdef __cplusplus
extern "C" {
#endif

extern long long g_log_rate_limit;
long long get_log_rate_limit() ;
#define dy_syslog(level, fmt, ...)                                          \
    do                                                                      \
    {                                                                       \
        static long long logTimeStamp_##__LINE__ = 0;                        \
        long long * p_logTimeStamp = &logTimeStamp_##__LINE__;                \
        long long currentTime = bmser_clocke_get_time_monotonic_ms();                    \
        long long log_rate_limit = get_log_rate_limit();           \
        if(currentTime - *p_logTimeStamp >= log_rate_limit) {              \
            char __buf[4096] = {0};                                             \
            snprintf(__buf, sizeof(__buf), fmt, ##__VA_ARGS__);                 \
            syslog(LOG_USER | level, "%s:%d %s", basename(__FILE__), __LINE__, __buf); \
            *p_logTimeStamp = currentTime;                                  \
        }                                                                   \
    } while (0)


#ifdef __cplusplus
}
#endif

#endif
