diff --git a/dhcp_tools/log_fmt.c b/dhcp_tools/log_fmt.c new file mode 100644 index 0000000..f8cb357 --- /dev/null +++ b/dhcp_tools/log_fmt.c @@ -0,0 +1,69 @@ +// +// Created by HuangXin on 2023/5/8. +// +#include +#include +#include +#include +#include +#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); + } + } +} \ No newline at end of file diff --git a/dhcp_tools/main.h b/dhcp_tools/main.h index 0fe9139..39aaa7a 100644 --- a/dhcp_tools/main.h +++ b/dhcp_tools/main.h @@ -9,6 +9,9 @@ #include "uthash/uthash.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 { COL_INDEX = 0, COL_VNI, @@ -87,5 +90,7 @@ void details_wnd_show(PDHCP_INFO pInfo); void hex_wnd_create(GtkBuilder *builder); void hex_wnd_show(PDHCP_INFO pInfo); void statis_wnd_create(GtkBuilder *builder); +void write_log_msg(const char *pMsg, int mode); GtkBuilder *get_main_builder(); +void format_log_msg(int level, int mode, const char *pMsg, ...); #endif //VCPE_MAIN_H diff --git a/dhcp_tools/main_wnd.c b/dhcp_tools/main_wnd.c index cd71155..46b58ca 100644 --- a/dhcp_tools/main_wnd.c +++ b/dhcp_tools/main_wnd.c @@ -22,7 +22,6 @@ static GtkBuilder *g_mainBuilder = NULL; static PDHCP_INFO g_pDhcpInfo = NULL; -static PDHCP_INFO g_pDhcpFinish = NULL; static GThread *g_pEvLoopThread = NULL; static int g_runTask = FALSE; static U32 g_totalTask = 0; @@ -427,6 +426,7 @@ static gboolean tree_view_data_store_upgade(gpointer pInfo) { gtk_widget_set_sensitive(nicSelect, TRUE); g_object_set(G_OBJECT(tbExpand), "stock_id", "gtk-indent", 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; @@ -498,6 +498,8 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) { gettimeofday(&pInfo->pDiscBuf.tm, NULL); pInfo->pDiscBuf.snd += 1; 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) { @@ -508,10 +510,15 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) { if (pInfo->pDiscBuf.snd < DHCP_PKG_RETRY) { // 超时重传 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 { pInfo->status = STA_ERROR; g_errTask++; 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: if (pInfo->status == STA_RECV_RSP) { pInfo->pReqBuf.p = dhcp_create_request_req(pInfo, &size); - if (pInfo->pReqBuf.p) { pInfo->pReqBuf.buf_size = size; pInfo->step = STEP_REQUEST; pInfo->status = STA_WAIT_START; 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; @@ -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) { pInfo->status = STA_SEND_REQ; gettimeofday(&pInfo->pReqBuf.tm, NULL); - pInfo->pReqBuf.snd = 1; + pInfo->pReqBuf.snd += 1; 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) { @@ -550,10 +560,15 @@ _Noreturn static void *dhcpThreadCb(void *UNUSED(pData)) { if (pInfo->pReqBuf.snd < DHCP_PKG_RETRY) { // 超时重传 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 { pInfo->status = STA_ERROR; g_errTask++; 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; g_finshTask++; 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; case STEP_END: break; @@ -597,25 +614,28 @@ static void cleanupDHCPInfo() { } static void mainWnd_on_tb_start(GObject *object, gpointer UNUSED(user_data)) { - int i, j, index = 0; - GtkTreeIter iter; - PDHCP_INFO pInfo; - GtkTreeModel *cobModel; - gchar *pCobText; - unsigned char mac[6]; - U32 macVal; - GtkWidget *stopButton = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStop")); - GtkWidget *nicSelect = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "cbNicName")); - GtkWidget *macBegin = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtMacStart")); - GtkWidget *preHostname = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtHostname")); - GtkWidget *vniStart = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVni")); - GtkWidget *numVniCli = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVniClient")); - GtkWidget *numRequest = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbReqNum")); - const char *strMacBegin = gtk_entry_get_text(GTK_ENTRY(macBegin)); - 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 nVni = (U32)gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(vniStart)); - U32 nVniCnt = (U32)gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(numVniCli)); + int i, j, index = 0; + GtkTreeIter iter; + GtkTextIter it, it1; + PDHCP_INFO pInfo; + GtkTreeModel *cobModel; + gchar *pCobText; + unsigned char mac[6]; + U32 macVal; + GtkWidget *stopButton = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStop")); + GtkWidget *nicSelect = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "cbNicName")); + GtkWidget *macBegin = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtMacStart")); + GtkWidget *preHostname = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "txtHostname")); + GtkWidget *vniStart = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVni")); + GtkWidget *numVniCli = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "sbVniClient")); + 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 *strPreHostname = gtk_entry_get_text(GTK_ENTRY(preHostname)); + U32 nRequest = (U32)gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(numRequest)); + U32 nVni = (U32)gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(vniStart)); + U32 nVniCnt = (U32)gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(numVniCli)); g_totalTask = g_finshTask = g_errTask = g_endTask = 0; @@ -666,6 +686,11 @@ static void mainWnd_on_tb_start(GObject *object, gpointer UNUSED(user_data)) { dhcp_tools_init_network(pCobText); g_runTask = TRUE; 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)) { @@ -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)) { - GtkWidget *view = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "treeResult")); - GtkTreeStore *store = GTK_TREE_STORE(gtk_builder_get_object(g_mainBuilder, "tsDhcpInfo")); - GtkWidget *tbStop = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStop")); + GtkTextIter it, it1; + GtkWidget *view = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "treeResult")); + GtkTreeStore *store = GTK_TREE_STORE(gtk_builder_get_object(g_mainBuilder, "tsDhcpInfo")); + 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_store_clear(store); cleanupDHCPInfo(); 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)) { @@ -708,6 +740,33 @@ static void *uv_loop_thread(void *UNUSED(pData)) { 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 i, ret; GtkTreeIter iter; @@ -733,6 +792,9 @@ int main(int args, char **argv) { GtkWidget *mainWnd = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "wndMain")); 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")); g_signal_connect(tbStart, "clicked", G_CALLBACK(mainWnd_on_tb_start), g_mainBuilder); diff --git a/dhcp_tools/res/style.css b/dhcp_tools/res/style.css index 4ed82d8..9b46464 100644 --- a/dhcp_tools/res/style.css +++ b/dhcp_tools/res/style.css @@ -15,6 +15,10 @@ textview.view { font: 16px "Courier New"; } +#txtLogout { + font: 14px "Courier New"; +} + entry { font: 16px "Comic Sans"; } \ No newline at end of file