From 1f56410405c6065e5a4143f917003ad798977c3d Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Tue, 30 May 2023 17:47:12 +0800 Subject: [PATCH] Log file operation --- DSView/pv/log.cpp | 31 ++++++++++++++++---- DSView/pv/log.h | 1 + DSView/pv/toolbars/logobar.cpp | 22 ++++++++++---- DSView/pv/toolbars/logobar.h | 4 +++ common/log/xlog.c | 53 +++++++++++++++++++++++++++++----- common/log/xlog.h | 5 ++++ 6 files changed, 98 insertions(+), 18 deletions(-) diff --git a/DSView/pv/log.cpp b/DSView/pv/log.cpp index 043737b7..11496032 100644 --- a/DSView/pv/log.cpp +++ b/DSView/pv/log.cpp @@ -77,13 +77,32 @@ void dsv_log_enalbe_logfile(bool append) } void dsv_clear_log_file() -{ - QString lf = get_dsv_log_path(); - std::string log_file = pv::path::ToUnicodePath(lf); - int ret = xlog_reset_log_file(log_ctx, log_file_index, log_file.c_str()); +{ + if (b_logfile && log_ctx) + { + QString lf = get_dsv_log_path(); + std::string log_file = pv::path::ToUnicodePath(lf); + int ret = xlog_reset_log_file(log_ctx, log_file_index, log_file.c_str()); - if (ret != 0){ - dsv_err("%s", "Clear log file error!"); + if (ret != 0){ + dsv_err("%s", "Clear log file error!"); + } + } + else{ + QDir dir; + QString filePath = get_dsv_log_path(); + if (dir.exists(filePath)) + { + dir.remove(filePath); + } + } +} + +void dsv_set_log_file_enable(bool flag) +{ + if (b_logfile && log_ctx) + { + xlog_set_receiver_enable(log_ctx, log_file_index, flag); } } diff --git a/DSView/pv/log.h b/DSView/pv/log.h index 0dc30cc3..a69433ef 100644 --- a/DSView/pv/log.h +++ b/DSView/pv/log.h @@ -36,6 +36,7 @@ void dsv_log_level(int l); void dsv_log_enalbe_logfile(bool append); void dsv_remove_log_file(); void dsv_clear_log_file(); +void dsv_set_log_file_enable(bool flag); QString get_dsv_log_path(); diff --git a/DSView/pv/toolbars/logobar.cpp b/DSView/pv/toolbars/logobar.cpp index ac091654..5efe7da0 100644 --- a/DSView/pv/toolbars/logobar.cpp +++ b/DSView/pv/toolbars/logobar.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -60,6 +59,8 @@ LogoBar::LogoBar(SigSession *session, QWidget *parent) : _logo_button(this) { _mainForm = NULL; + _log_open_bt = NULL; + _log_clear_bt = NULL; setMovable(false); setContentsMargins(0,0,0,0); @@ -261,10 +262,12 @@ void LogoBar::on_action_setting_log() QPushButton *btOpen = new QPushButton(); btOpen->setText(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_OPEN), "Open")); - connect(btOpen, SIGNAL(released()), this, SLOT(on_open_log_file())); + _log_open_bt = btOpen; + connect(btOpen, SIGNAL(released()), this, SLOT(on_open_log_file())); QPushButton *btClear = new QPushButton(); btClear->setText(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_CLEARE), "Clear")); + _log_clear_bt = btClear; connect(btClear, SIGNAL(released()), this, SLOT(on_clear_log_file())); QWidget *btWid = new QWidget(); @@ -299,10 +302,11 @@ void LogoBar::on_action_setting_log() dsv_log_level(level); - if (ableSave) + if (ableSave){ dsv_log_enalbe_logfile(false); - else - dsv_remove_log_file(); + } + + dsv_set_log_file_enable(ableSave); } } } @@ -326,6 +330,14 @@ void LogoBar::on_clear_log_file() QString strMsg(L_S(STR_PAGE_MSG, S_ID(IDS_MSG_TO_CLEAR_LOG), "Confirm!")); if (MsgBox::Confirm(strMsg)){ dsv_clear_log_file(); + + if (_log_open_bt != NULL && _log_clear_bt != NULL){ + QFile qf(get_dsv_log_path()); + if (qf.exists() == false){ + _log_open_bt->setEnabled(false); + _log_clear_bt->setEnabled(false); + } + } } } else{ diff --git a/DSView/pv/toolbars/logobar.h b/DSView/pv/toolbars/logobar.h index 513c9a42..6f41046e 100644 --- a/DSView/pv/toolbars/logobar.h +++ b/DSView/pv/toolbars/logobar.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "../sigsession.h" #include "../interface/icallbacks.h" @@ -93,6 +94,9 @@ private: QAction *_update; QAction *_log; + QPushButton *_log_open_bt; + QPushButton *_log_clear_bt; + IMainForm *_mainForm; }; diff --git a/common/log/xlog.c b/common/log/xlog.c index 6aa3bfa0..e9c252f1 100644 --- a/common/log/xlog.c +++ b/common/log/xlog.c @@ -42,7 +42,8 @@ typedef void (*xlog_print_func)(struct xlog_receiver_info *info, const char *dom struct xlog_receiver_info { - int _type; //see enum xlog_receiver_type + int _type; //see enum xlog_receiver_type + int _enable; FILE *_file; xlog_print_func _fn; // print function xlog_receive_callback _rev; //user callback @@ -149,13 +150,17 @@ static xlog_context* xlog_new_context(int bConsole) for(i=0; i< RECEIVER_MAX_COUNT; i++){ ctx->_receivers[i]._fn = NULL; ctx->_receivers[i]._file = NULL; + ctx->_receivers[i]._enable = 0; + ctx->_receivers[i]._rev = NULL; + ctx->_receivers[i]._type = -1; } ctx->_count = 0; ctx->_log_level = XLOG_LEVEL_INFO; if (bConsole){ ctx->_receivers[0]._fn = print_to_console; - ctx->_receivers[0]._type = RECEIVER_TYPE_CONSOLE; + ctx->_receivers[0]._type = RECEIVER_TYPE_CONSOLE; + ctx->_receivers[0]._enable = 1; ctx->_count = 1; } @@ -236,6 +241,7 @@ XLOG_API int xlog_add_receiver(xlog_context* ctx, xlog_receive_callback rev, int ctx->_receivers[i]._type = RECEIVER_TYPE_CALLBACK; ctx->_receivers[i]._rev = rev; ctx->_receivers[i]._fn = print_to_user_callback; + ctx->_receivers[i]._enable = 1; ctx->_count++; if (out_index) @@ -290,6 +296,7 @@ XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_pat ctx->_receivers[i]._type = RECEIVER_TYPE_FILE; ctx->_receivers[i]._fn = print_to_file; ctx->_receivers[i]._file = fh; + ctx->_receivers[i]._enable = 1; ctx->_count++; if (out_index) @@ -355,6 +362,7 @@ XLOG_API int xlog_remove_receiver_by_index(xlog_context* ctx, int index) } ctx->_receivers[index]._fn = NULL; + ctx->_receivers[index]._enable = 0; index++; while (index < RECEIVER_MAX_COUNT) @@ -363,6 +371,7 @@ XLOG_API int xlog_remove_receiver_by_index(xlog_context* ctx, int index) ctx->_receivers[index-1]._type = ctx->_receivers[index]._type; ctx->_receivers[index-1]._rev = ctx->_receivers[index]._rev; ctx->_receivers[index-1]._file = ctx->_receivers[index]._file; + ctx->_receivers[index-1]._enable = ctx->_receivers[index]._enable; index++; } ctx->_count--; @@ -372,6 +381,30 @@ XLOG_API int xlog_remove_receiver_by_index(xlog_context* ctx, int index) return 0; } +/** + * Set the log receiver enable to disable. +*/ +XLOG_API int xlog_set_receiver_enable(xlog_context* ctx, int index, int bEnable) +{ + if (ctx == NULL){ + return -1; + } + + pthread_mutex_lock(&ctx->_mutext); + + if (index < 0 || index >= ctx->_count){ + strcpy(ctx->_error, "index out of range"); + pthread_mutex_unlock(&ctx->_mutext); + return -1; + } + + ctx->_receivers[index]._enable = bEnable > 0 ? 1 : 0; + + pthread_mutex_unlock(&ctx->_mutext); + + return 0; +} + /** * clear all receiver,return 0 if success. */ @@ -386,6 +419,12 @@ XLOG_API int xlog_clear_all_receiver(xlog_context* ctx) for (i = 0; i < RECEIVER_MAX_COUNT; i++){ ctx->_receivers[i]._fn = NULL; + ctx->_receivers[i]._enable = 0; + + if (ctx->_receivers[i]._file != NULL){ + fclose(ctx->_receivers[i]._file); + ctx->_receivers[i]._file = NULL; + } } ctx->_count = 0; @@ -492,7 +531,7 @@ XLOG_API int xlog_err(xlog_writer *wr, const char *format, ...) for (i = 0; i < ctx->_count; i++){ inf = &ctx->_receivers[i]; - if (inf->_fn != NULL){ + if (inf->_fn != NULL && inf->_enable){ va_start(args, format); inf->_fn(inf, wr->_domain, format, args); va_end(args); @@ -528,7 +567,7 @@ XLOG_API int xlog_warn(xlog_writer *wr, const char *format, ...) for (i = 0; i < ctx->_count; i++){ inf = &ctx->_receivers[i]; - if (inf->_fn != NULL){ + if (inf->_fn != NULL && inf->_enable){ va_start(args, format); inf->_fn(inf, wr->_domain, format, args); va_end(args); @@ -564,7 +603,7 @@ XLOG_API int xlog_info(xlog_writer *wr, const char *format, ...) for (i = 0; i < ctx->_count; i++){ inf = &ctx->_receivers[i]; - if (inf->_fn != NULL){ + if (inf->_fn != NULL && inf->_enable){ va_start(args, format); inf->_fn(inf, wr->_domain, format, args); va_end(args); @@ -600,7 +639,7 @@ XLOG_API int xlog_dbg(xlog_writer *wr, const char *format, ...) for (i = 0; i < ctx->_count; i++){ inf = &ctx->_receivers[i]; - if (inf->_fn != NULL){ + if (inf->_fn != NULL && inf->_enable){ va_start(args, format); inf->_fn(inf, wr->_domain, format, args); va_end(args); @@ -636,7 +675,7 @@ XLOG_API int xlog_detail(xlog_writer *wr, const char *format, ...) for (i = 0; i < ctx->_count; i++){ inf = &ctx->_receivers[i]; - if (inf->_fn != NULL){ + if (inf->_fn != NULL && inf->_enable){ va_start(args, format); inf->_fn(inf, wr->_domain, format, args); va_end(args); diff --git a/common/log/xlog.h b/common/log/xlog.h index 0977cbaa..249f080f 100644 --- a/common/log/xlog.h +++ b/common/log/xlog.h @@ -102,6 +102,11 @@ XLOG_API int xlog_reset_log_file(xlog_context* ctx, int receiver_index, const ch */ XLOG_API int xlog_remove_receiver_by_index(xlog_context* ctx, int index); +/** + * Set the log receiver enable to disable. +*/ +XLOG_API int xlog_set_receiver_enable(xlog_context* ctx, int index, int bEnable); + /** * clear all receiver,return 0 if success. */