OCT 1. DHCP 测试工具增加日志消息功能,记录流程详细步骤

This commit is contained in:
黄昕 2023-05-08 15:38:50 +08:00
parent 5ee63c545d
commit 07de7b4c2f
4 changed files with 165 additions and 25 deletions

69
dhcp_tools/log_fmt.c Normal file
View File

@ -0,0 +1,69 @@
//
// Created by HuangXin on 2023/5/8.
//
#include <zlog_module.h>
#include <sys/time.h>
#include <bits/types/struct_tm.h>
#include <time.h>
#include <string.h>
#include "main.h"
#define SIZE_1K (1024)
static const char *logLevelStr(int level) {
switch (level) {
case ZLOG_LEVEL_TRACE:
return "T";
case ZLOG_LEVEL_DEBUG:
return "D";
case ZLOG_LEVEL_INFO:
return "I";
case ZLOG_LEVEL_NOTICE:
return "N";
case ZLOG_LEVEL_WARN:
return "W";
case ZLOG_LEVEL_ERROR:
return "E";
case ZLOG_LEVEL_FATAL:
return "F";
default:
return "U";
}
}
static void writeLogMsg(int level, char *pMsg, int mode) {
struct tm *local_t;
time_t t;
struct timeval tv;
char *pBuf = strdup(pMsg);
gettimeofday(&tv, NULL);
time(&t);
local_t = localtime(&t);
local_t->tm_isdst = (int)(tv.tv_usec / 1000);
snprintf(pMsg, SIZE_1K, "[%02d:%02d:%02d:%03d] [%s] %s", local_t->tm_hour, local_t->tm_min, local_t->tm_sec,
local_t->tm_isdst, logLevelStr(level), pBuf);
free(pBuf);
write_log_msg(pMsg, mode);
}
void format_log_msg(int level, int mode, const char *pMsg, ...) {
char *pBuf = (char *)malloc(SIZE_1K);
if (pBuf) {
__builtin_va_list arg_ptr;
memset(pBuf, 0, SIZE_1K);
va_start(arg_ptr, pMsg);
__builtin_vsnprintf(pBuf, SIZE_1K, pMsg, arg_ptr);
va_end(arg_ptr);
writeLogMsg(level, pBuf, mode);
if (mode == 0) {
free(pBuf);
}
}
}

View File

@ -9,6 +9,9 @@
#include "uthash/uthash.h" #include "uthash/uthash.h"
#include "rfc2131.h" #include "rfc2131.h"
#define ADD_LOG_MSG(level, m, format, ...) \
(format_log_msg(level, (m), "[%s] - %s(%d):" format, basename_v2(__FILE__), __FUNCTION__, __LINE__, ##__VA_ARGS__))
typedef enum { typedef enum {
COL_INDEX = 0, COL_INDEX = 0,
COL_VNI, COL_VNI,
@ -87,5 +90,7 @@ void details_wnd_show(PDHCP_INFO pInfo);
void hex_wnd_create(GtkBuilder *builder); void hex_wnd_create(GtkBuilder *builder);
void hex_wnd_show(PDHCP_INFO pInfo); void hex_wnd_show(PDHCP_INFO pInfo);
void statis_wnd_create(GtkBuilder *builder); void statis_wnd_create(GtkBuilder *builder);
void write_log_msg(const char *pMsg, int mode);
GtkBuilder *get_main_builder(); GtkBuilder *get_main_builder();
void format_log_msg(int level, int mode, const char *pMsg, ...);
#endif //VCPE_MAIN_H #endif //VCPE_MAIN_H

View File

@ -22,7 +22,6 @@
static GtkBuilder *g_mainBuilder = NULL; static GtkBuilder *g_mainBuilder = NULL;
static PDHCP_INFO g_pDhcpInfo = NULL; static PDHCP_INFO g_pDhcpInfo = NULL;
static PDHCP_INFO g_pDhcpFinish = NULL;
static GThread *g_pEvLoopThread = NULL; static GThread *g_pEvLoopThread = NULL;
static int g_runTask = FALSE; static int g_runTask = FALSE;
static U32 g_totalTask = 0; static U32 g_totalTask = 0;
@ -427,6 +426,7 @@ static gboolean tree_view_data_store_upgade(gpointer pInfo) {
gtk_widget_set_sensitive(nicSelect, TRUE); gtk_widget_set_sensitive(nicSelect, TRUE);
g_object_set(G_OBJECT(tbExpand), "stock_id", "gtk-indent", NULL); g_object_set(G_OBJECT(tbExpand), "stock_id", "gtk-indent", NULL);
upgrade_status_bar_msg(NULL); upgrade_status_bar_msg(NULL);
ADD_LOG_MSG(ZLOG_LEVEL_INFO, 0, " Finish test, successed %d, error %d\n", g_finshTask, g_errTask);
} }
return TRUE; return TRUE;
@ -498,6 +498,8 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) {
gettimeofday(&pInfo->pDiscBuf.tm, NULL); gettimeofday(&pInfo->pDiscBuf.tm, NULL);
pInfo->pDiscBuf.snd += 1; pInfo->pDiscBuf.snd += 1;
g_idle_add(tree_view_data_store_upgade, pInfo); g_idle_add(tree_view_data_store_upgade, pInfo);
ADD_LOG_MSG(ZLOG_LEVEL_INFO, 1, "User %u host send DHCP Discover\n", pInfo->vni,
pInfo->hostname);
} }
} }
} else if (pInfo->status == STA_SEND_REQ) { } else if (pInfo->status == STA_SEND_REQ) {
@ -508,10 +510,15 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) {
if (pInfo->pDiscBuf.snd < DHCP_PKG_RETRY) { if (pInfo->pDiscBuf.snd < DHCP_PKG_RETRY) {
// 超时重传 // 超时重传
pInfo->status = STA_WAIT_START; pInfo->status = STA_WAIT_START;
ADD_LOG_MSG(ZLOG_LEVEL_WARN, 1,
"User %u host receive DHCP Offer timeout, retry send Discover %d\n",
pInfo->vni, pInfo->hostname, pInfo->pDiscBuf.snd);
} else { } else {
pInfo->status = STA_ERROR; pInfo->status = STA_ERROR;
g_errTask++; g_errTask++;
g_idle_add(upgrade_status_bar_msg, NULL); g_idle_add(upgrade_status_bar_msg, NULL);
ADD_LOG_MSG(ZLOG_LEVEL_ERROR, 1, "User %u host receive DHCP Offer error, retry %d\n",
pInfo->vni, pInfo->hostname, pInfo->pDiscBuf.snd);
} }
} }
} }
@ -519,12 +526,13 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) {
case STEP_OFFER: case STEP_OFFER:
if (pInfo->status == STA_RECV_RSP) { if (pInfo->status == STA_RECV_RSP) {
pInfo->pReqBuf.p = dhcp_create_request_req(pInfo, &size); pInfo->pReqBuf.p = dhcp_create_request_req(pInfo, &size);
if (pInfo->pReqBuf.p) { if (pInfo->pReqBuf.p) {
pInfo->pReqBuf.buf_size = size; pInfo->pReqBuf.buf_size = size;
pInfo->step = STEP_REQUEST; pInfo->step = STEP_REQUEST;
pInfo->status = STA_WAIT_START; pInfo->status = STA_WAIT_START;
g_idle_add(tree_view_data_store_upgade, pInfo); g_idle_add(tree_view_data_store_upgade, pInfo);
ADD_LOG_MSG(ZLOG_LEVEL_INFO, 1, "User %u host receive DHCP Offer information\n", pInfo->vni,
pInfo->hostname);
} }
} }
break; break;
@ -538,8 +546,10 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) {
if (pkg_mmap_tx((U8 *)pkg, pInfo->pReqBuf.buf_size) == pInfo->pReqBuf.buf_size) { if (pkg_mmap_tx((U8 *)pkg, pInfo->pReqBuf.buf_size) == pInfo->pReqBuf.buf_size) {
pInfo->status = STA_SEND_REQ; pInfo->status = STA_SEND_REQ;
gettimeofday(&pInfo->pReqBuf.tm, NULL); gettimeofday(&pInfo->pReqBuf.tm, NULL);
pInfo->pReqBuf.snd = 1; pInfo->pReqBuf.snd += 1;
g_idle_add(tree_view_data_store_upgade, pInfo); g_idle_add(tree_view_data_store_upgade, pInfo);
ADD_LOG_MSG(ZLOG_LEVEL_INFO, 1, "User %u host send DHCP Request\n", pInfo->vni,
pInfo->hostname);
} }
} }
} else if (pInfo->status == STA_SEND_REQ) { } else if (pInfo->status == STA_SEND_REQ) {
@ -550,10 +560,15 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) {
if (pInfo->pReqBuf.snd < DHCP_PKG_RETRY) { if (pInfo->pReqBuf.snd < DHCP_PKG_RETRY) {
// 超时重传 // 超时重传
pInfo->status = STA_WAIT_START; pInfo->status = STA_WAIT_START;
ADD_LOG_MSG(ZLOG_LEVEL_WARN, 1,
"User %u host receive DHCP ACK timeout, retry send Request %d\n",
pInfo->vni, pInfo->hostname, pInfo->pReqBuf.snd);
} else { } else {
pInfo->status = STA_ERROR; pInfo->status = STA_ERROR;
g_errTask++; g_errTask++;
g_idle_add(upgrade_status_bar_msg, NULL); g_idle_add(upgrade_status_bar_msg, NULL);
ADD_LOG_MSG(ZLOG_LEVEL_WARN, 1, "User %u host receive DHCP ACK timeout, retry %d\n",
pInfo->vni, pInfo->hostname, pInfo->pReqBuf.snd);
} }
} }
} }
@ -562,6 +577,8 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) {
pInfo->step = STEP_END; pInfo->step = STEP_END;
g_finshTask++; g_finshTask++;
g_idle_add(upgrade_status_bar_msg, NULL); g_idle_add(upgrade_status_bar_msg, NULL);
ADD_LOG_MSG(ZLOG_LEVEL_INFO, 1, "User %u host receive DHCP ACK, finished.\n", pInfo->vni,
pInfo->hostname);
break; break;
case STEP_END: case STEP_END:
break; break;
@ -599,6 +616,7 @@ static void cleanupDHCPInfo() {
static void mainWnd_on_tb_start(GObject *object, gpointer UNUSED(user_data)) { static void mainWnd_on_tb_start(GObject *object, gpointer UNUSED(user_data)) {
int i, j, index = 0; int i, j, index = 0;
GtkTreeIter iter; GtkTreeIter iter;
GtkTextIter it, it1;
PDHCP_INFO pInfo; PDHCP_INFO pInfo;
GtkTreeModel *cobModel; GtkTreeModel *cobModel;
gchar *pCobText; gchar *pCobText;
@ -611,6 +629,8 @@ static void mainWnd_on_tb_start(GObject *object, gpointer UNUSED(user_data)) {
GtkWidget *vniStart = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVni")); GtkWidget *vniStart = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVni"));
GtkWidget *numVniCli = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVniClient")); GtkWidget *numVniCli = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVniClient"));
GtkWidget *numRequest = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbReqNum")); GtkWidget *numRequest = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbReqNum"));
GtkWidget *txLog = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtLogout"));
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txLog));
const char *strMacBegin = gtk_entry_get_text(GTK_ENTRY(macBegin)); const char *strMacBegin = gtk_entry_get_text(GTK_ENTRY(macBegin));
const char *strPreHostname = gtk_entry_get_text(GTK_ENTRY(preHostname)); const char *strPreHostname = gtk_entry_get_text(GTK_ENTRY(preHostname));
U32 nRequest = (U32)gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(numRequest)); U32 nRequest = (U32)gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(numRequest));
@ -666,6 +686,11 @@ static void mainWnd_on_tb_start(GObject *object, gpointer UNUSED(user_data)) {
dhcp_tools_init_network(pCobText); dhcp_tools_init_network(pCobText);
g_runTask = TRUE; g_runTask = TRUE;
gettimeofday(&g_StartTm, NULL); gettimeofday(&g_StartTm, NULL);
gtk_text_buffer_get_start_iter(buffer, &it);
gtk_text_buffer_get_end_iter(buffer, &it1);
gtk_text_buffer_delete(buffer, &it, &it1);
ADD_LOG_MSG(ZLOG_LEVEL_INFO, 0, "Begin test total user: %d, %d host/user\n", nRequest, nVniCnt);
} }
static void mainWnd_on_tb_stop(GObject *object, gpointer UNUSED(user_data)) { static void mainWnd_on_tb_stop(GObject *object, gpointer UNUSED(user_data)) {
@ -693,14 +718,21 @@ static void mainWnd_on_tb_expand(GObject *object, gpointer UNUSED(user_data)) {
} }
static void mainWnd_on_tb_cleanup(GObject *UNUSED(object), gpointer UNUSED(user_data)) { static void mainWnd_on_tb_cleanup(GObject *UNUSED(object), gpointer UNUSED(user_data)) {
GtkTextIter it, it1;
GtkWidget *view = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "treeResult")); GtkWidget *view = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "treeResult"));
GtkTreeStore *store = GTK_TREE_STORE(gtk_builder_get_object(g_mainBuilder, "tsDhcpInfo")); GtkTreeStore *store = GTK_TREE_STORE(gtk_builder_get_object(g_mainBuilder, "tsDhcpInfo"));
GtkWidget *tbStop = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStop")); GtkWidget *tbStop = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStop"));
GtkWidget *txLog = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtLogout"));
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txLog));
gtk_tree_view_set_model(GTK_TREE_VIEW(view), NULL); gtk_tree_view_set_model(GTK_TREE_VIEW(view), NULL);
gtk_tree_store_clear(store); gtk_tree_store_clear(store);
cleanupDHCPInfo(); cleanupDHCPInfo();
mainWnd_on_tb_stop(G_OBJECT(tbStop), NULL); mainWnd_on_tb_stop(G_OBJECT(tbStop), NULL);
gtk_text_buffer_get_start_iter(buffer, &it);
gtk_text_buffer_get_end_iter(buffer, &it1);
gtk_text_buffer_delete(buffer, &it, &it1);
} }
static void *uv_loop_thread(void *UNUSED(pData)) { static void *uv_loop_thread(void *UNUSED(pData)) {
@ -708,6 +740,33 @@ static void *uv_loop_thread(void *UNUSED(pData)) {
return NULL; return NULL;
} }
static GtkTextBuffer *g_pLogTxtBuf = NULL;
static gboolean upgrade_gtk_txt_msg(gpointer pInfo) {
GtkTextIter iter;
gtk_text_buffer_get_end_iter(g_pLogTxtBuf, &iter);
gtk_text_buffer_insert(g_pLogTxtBuf, &iter, (const char *)pInfo, -1);
free(pInfo);
return FALSE;
}
void write_log_msg(const char *pMsg, int mode) {
if (g_pLogTxtBuf == NULL) {
GtkWidget *txLog = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtLogout"));
g_pLogTxtBuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txLog));
}
if (pMsg && strlen(pMsg) > 0 && g_pLogTxtBuf) {
if (mode == 0) {
GtkTextIter iter;
gtk_text_buffer_get_end_iter(g_pLogTxtBuf, &iter);
gtk_text_buffer_insert(g_pLogTxtBuf, &iter, pMsg, -1);
} else {
g_idle_add(upgrade_gtk_txt_msg, (gpointer)pMsg);
}
}
}
int main(int args, char **argv) { int main(int args, char **argv) {
int i, ret; int i, ret;
GtkTreeIter iter; GtkTreeIter iter;
@ -733,6 +792,9 @@ int main(int args, char **argv) {
GtkWidget *mainWnd = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "wndMain")); GtkWidget *mainWnd = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "wndMain"));
g_signal_connect(mainWnd, "destroy", G_CALLBACK(mainWnd_on_destroy), g_mainBuilder); g_signal_connect(mainWnd, "destroy", G_CALLBACK(mainWnd_on_destroy), g_mainBuilder);
GtkWidget *txtLog = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtLogout"));
gtk_widget_set_name(txtLog, "txtLogout");
GtkWidget *tbStart = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStart")); GtkWidget *tbStart = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStart"));
g_signal_connect(tbStart, "clicked", G_CALLBACK(mainWnd_on_tb_start), g_mainBuilder); g_signal_connect(tbStart, "clicked", G_CALLBACK(mainWnd_on_tb_start), g_mainBuilder);

View File

@ -15,6 +15,10 @@ textview.view {
font: 16px "Courier New"; font: 16px "Courier New";
} }
#txtLogout {
font: 14px "Courier New";
}
entry { entry {
font: 16px "Comic Sans"; font: 16px "Comic Sans";
} }