增加MESA_handle_logger
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
add_library(common src/addr_tuple4.cpp src/session_table.cpp src/raw_packet.cpp src/ctrl_packet.cpp src/bfd.cpp src/utils.cpp src/g_vxlan.cpp)
|
||||
add_library(common src/addr_tuple4.cpp src/session_table.cpp src/raw_packet.cpp src/ctrl_packet.cpp src/bfd.cpp src/utils.cpp src/g_vxlan.cpp src/log.cpp)
|
||||
target_link_libraries(common PUBLIC cjson)
|
||||
target_link_libraries(common PUBLIC mrzcpd)
|
||||
target_link_libraries(common PUBLIC MESA_handle_logger)
|
||||
|
||||
target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
|
||||
|
||||
|
||||
@@ -7,24 +7,40 @@ extern "C"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <MESA/MESA_handle_logger.h>
|
||||
|
||||
#define LOG_DEBUG(format, ...) \
|
||||
{ \
|
||||
fprintf(stdout, "DEBUG " format "\n", ##__VA_ARGS__); \
|
||||
fflush(stdout); \
|
||||
}
|
||||
extern void *g_default_logger;
|
||||
|
||||
#define LOG_INFO(format, ...) \
|
||||
{ \
|
||||
fprintf(stdout, "INFO " format "\n", ##__VA_ARGS__); \
|
||||
fflush(stdout); \
|
||||
}
|
||||
int LOG_INIT(const char *profile);
|
||||
void LOG_CLOSE(void);
|
||||
void LOG_RELOAD(void);
|
||||
|
||||
#define LOG_ERROR(format, ...) \
|
||||
{ \
|
||||
fprintf(stderr, "ERROR " format "\n", ##__VA_ARGS__); \
|
||||
fflush(stderr); \
|
||||
}
|
||||
#define LOG_DEBUG(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (g_default_logger) \
|
||||
MESA_handle_runtime_log(g_default_logger, RLOG_LV_DEBUG, __FUNCTION__, format, ##__VA_ARGS__); \
|
||||
else \
|
||||
fprintf(stdout, "DEBUG " format "\n", ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define LOG_INFO(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (g_default_logger) \
|
||||
MESA_handle_runtime_log(g_default_logger, RLOG_LV_INFO, __FUNCTION__, format, ##__VA_ARGS__); \
|
||||
else \
|
||||
fprintf(stdout, "INFO " format "\n", ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define LOG_ERROR(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (g_default_logger) \
|
||||
MESA_handle_runtime_log(g_default_logger, RLOG_LV_FATAL, __FUNCTION__, format, ##__VA_ARGS__); \
|
||||
else \
|
||||
fprintf(stderr, "ERROR " format "\n", ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
|
||||
33
common/src/log.cpp
Normal file
33
common/src/log.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "log.h"
|
||||
|
||||
void *g_default_logger = NULL;
|
||||
|
||||
// return 0 : success
|
||||
// return -1 : error
|
||||
int LOG_INIT(const char *profile)
|
||||
{
|
||||
if (0 != MESA_handle_runtime_log_creation(profile))
|
||||
{
|
||||
fprintf(stderr, "FATAL: unable to create runtime logger\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_default_logger = MESA_create_runtime_log_handle("sce", RLOG_LV_DEBUG);
|
||||
if (g_default_logger == NULL)
|
||||
{
|
||||
fprintf(stderr, "FATAL: unable to create log handle\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LOG_CLOSE(void)
|
||||
{
|
||||
MESA_handle_runtime_log_destruction();
|
||||
}
|
||||
|
||||
void LOG_RELOAD(void)
|
||||
{
|
||||
MESA_handle_runtime_log_reconstruction(NULL);
|
||||
}
|
||||
@@ -82,9 +82,7 @@ static const void *parse_ipv4(struct raw_pkt_parser *handler, const void *data,
|
||||
static const void *parse_ipv6(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
static const void *parse_tcp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
static const void *parse_ppp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
static const void *parse_pppoe_ses(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
// static const void *parse_hdlc(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
static const void *parse_vxlan(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
static const void *parse_vlan8021q(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
static const void *parse_gtpv1_u(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type);
|
||||
@@ -768,39 +766,6 @@ static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, s
|
||||
}
|
||||
}
|
||||
|
||||
static const void *parse_ppp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type)
|
||||
{
|
||||
if (length < PPP_HDRLEN)
|
||||
{
|
||||
LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, err: data not enough", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type));
|
||||
return data;
|
||||
}
|
||||
|
||||
if (raw_packet_parser_status(handler, data, this_type) == PARSE_STATUS_STOP)
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
uint16_t next_proto = PPP_PROTOCOL(data);
|
||||
uint16_t hdr_len = PPP_HDRLEN;
|
||||
const void *data_next_layer = (const char *)data + hdr_len;
|
||||
size_t data_next_length = length - hdr_len;
|
||||
|
||||
LOG_DEBUG("%s: pkt_trace_id: %lu, this_layer: %s, payload_len: [%lu/%lu]", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), data_next_length, length);
|
||||
switch (next_proto)
|
||||
{
|
||||
case PPP_IP:
|
||||
// TODO
|
||||
return parse_ipv4(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV4);
|
||||
case PPP_IPV6:
|
||||
// TODO
|
||||
return parse_ipv6(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV6);
|
||||
default:
|
||||
LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, stop parse next protocol %d", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), next_proto);
|
||||
return data_next_layer;
|
||||
}
|
||||
}
|
||||
|
||||
static const void *parse_pppoe_ses(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type)
|
||||
{
|
||||
if (length < 8)
|
||||
@@ -836,42 +801,6 @@ static const void *parse_pppoe_ses(struct raw_pkt_parser *handler, const void *d
|
||||
}
|
||||
}
|
||||
|
||||
static const void *parse_hdlc(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type)
|
||||
{
|
||||
if (length < 4)
|
||||
{
|
||||
LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, err: data not enough", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type));
|
||||
return data;
|
||||
}
|
||||
|
||||
if (raw_packet_parser_status(handler, data, this_type) == PARSE_STATUS_STOP)
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
uint16_t next_proto = ntohs(*(const uint16_t *)((const char *)data + 2));
|
||||
uint16_t hdr_len = 4;
|
||||
const void *data_next_layer = (const char *)data + hdr_len;
|
||||
size_t data_next_length = length - hdr_len;
|
||||
|
||||
LOG_DEBUG("%s: pkt_trace_id: %lu, this_layer: %s, payload_len: [%lu/%lu]", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), data_next_length, length);
|
||||
// HDLC的协议字段与以太网的类似,对于IPv4,为0x0800
|
||||
switch (next_proto)
|
||||
{
|
||||
// ETHER_TYPE_IPV4 0x0800
|
||||
case 0x0800:
|
||||
// TODO
|
||||
return parse_ipv4(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV4);
|
||||
// ETHER_TYPE_IPV6 0x86DD
|
||||
case 0x86DD:
|
||||
// TODO
|
||||
return parse_ipv6(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV6);
|
||||
default:
|
||||
LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, stop parse next protocol %d", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), next_proto);
|
||||
return data_next_layer;
|
||||
}
|
||||
}
|
||||
|
||||
static const void *parse_vxlan(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type)
|
||||
{
|
||||
if (length < sizeof(struct vxlan_hdr))
|
||||
@@ -893,9 +822,6 @@ static const void *parse_vxlan(struct raw_pkt_parser *handler, const void *data,
|
||||
LOG_DEBUG("%s: pkt_trace_id: %lu, this_layer: %s, payload_len: [%lu/%lu]", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), data_next_length, length);
|
||||
// TESTED
|
||||
return parse_ether(handler, data_next_layer, data_next_length, LAYER_TYPE_ETHER);
|
||||
|
||||
// TODO HDLC
|
||||
// TODO PPP
|
||||
}
|
||||
|
||||
static const void *parse_vlan8021q(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type)
|
||||
|
||||
Reference in New Issue
Block a user