summaryrefslogtreecommitdiff
path: root/src/logger.h
blob: 4f658e4d7ea6aac49ff4aa737bfeece5f2116c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * \brief TFE结构化日志管理器
 *
 * 结构化日志用于发送业务日志,采用Json将日志结构化,通过Kafka发送至后端日志服务器分析。
 *
 * \author Lu Qiuwen<[email protected]>
 * \date 2018-5-25
 */


#pragma once

#include <memory>
#include <jsoncpp/json/json.h>
#include "cfgparser.h"

class StructLoggerBackend
{
public:
    virtual void WriteLog(const std::string & str_topic, const std::string & str) = 0;
    virtual void RegisterTopic(const std::string & str_topic) = 0;
};

enum
{
    LOG_TOPIC_CTRL_IP,
    LOG_TOPIC_CTRL_HTTP,
    LOG_TOPIC_CTRL_MAX
};

class StructLogger
{
public:
    explicit StructLogger() = default;
    virtual ~StructLogger() = default;

    Json::Value CommonLogMake(const sockaddr * sk_src, const sockaddr * sk_dst,
        int service_id, int cfg_id);
    void CommonLogMake(const sockaddr * sk_src, const sockaddr * sk_dst,
        int service_id, int cfg_id, Json::Value & result);

    void SendLog(int topic_id, Json::Value && struct_log);

public:
    static std::unique_ptr<StructLogger> Factory(TfeConfigParser & cfg);

private:
    std::string str_cap_ip_{};
    unsigned int entrance_id_{0};
    unsigned int device_id_{0};
    std::vector<std::unique_ptr<StructLoggerBackend>> backends_{};

private:
    void LoggerBackend(std::unique_ptr<StructLoggerBackend> backend_ptr)
    { backends_.push_back(std::move(backend_ptr)); }
};