Log file operation

This commit is contained in:
dreamsourcelabTAI 2023-05-30 17:47:12 +08:00
parent 403f48b9be
commit 1f56410405
6 changed files with 98 additions and 18 deletions

View File

@ -77,6 +77,8 @@ void dsv_log_enalbe_logfile(bool append)
} }
void dsv_clear_log_file() void dsv_clear_log_file()
{
if (b_logfile && log_ctx)
{ {
QString lf = get_dsv_log_path(); QString lf = get_dsv_log_path();
std::string log_file = pv::path::ToUnicodePath(lf); std::string log_file = pv::path::ToUnicodePath(lf);
@ -86,6 +88,23 @@ void dsv_clear_log_file()
dsv_err("%s", "Clear log file error!"); 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);
}
}
void dsv_remove_log_file() void dsv_remove_log_file()
{ {

View File

@ -36,6 +36,7 @@ void dsv_log_level(int l);
void dsv_log_enalbe_logfile(bool append); void dsv_log_enalbe_logfile(bool append);
void dsv_remove_log_file(); void dsv_remove_log_file();
void dsv_clear_log_file(); void dsv_clear_log_file();
void dsv_set_log_file_enable(bool flag);
QString get_dsv_log_path(); QString get_dsv_log_path();

View File

@ -33,7 +33,6 @@
#include <QWidget> #include <QWidget>
#include <QCheckBox> #include <QCheckBox>
#include <QLineEdit> #include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QFile> #include <QFile>
#include <QLabel> #include <QLabel>
@ -60,6 +59,8 @@ LogoBar::LogoBar(SigSession *session, QWidget *parent) :
_logo_button(this) _logo_button(this)
{ {
_mainForm = NULL; _mainForm = NULL;
_log_open_bt = NULL;
_log_clear_bt = NULL;
setMovable(false); setMovable(false);
setContentsMargins(0,0,0,0); setContentsMargins(0,0,0,0);
@ -261,10 +262,12 @@ void LogoBar::on_action_setting_log()
QPushButton *btOpen = new QPushButton(); QPushButton *btOpen = new QPushButton();
btOpen->setText(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_OPEN), "Open")); btOpen->setText(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_OPEN), "Open"));
_log_open_bt = btOpen;
connect(btOpen, SIGNAL(released()), this, SLOT(on_open_log_file())); connect(btOpen, SIGNAL(released()), this, SLOT(on_open_log_file()));
QPushButton *btClear = new QPushButton(); QPushButton *btClear = new QPushButton();
btClear->setText(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_CLEARE), "Clear")); 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())); connect(btClear, SIGNAL(released()), this, SLOT(on_clear_log_file()));
QWidget *btWid = new QWidget(); QWidget *btWid = new QWidget();
@ -299,10 +302,11 @@ void LogoBar::on_action_setting_log()
dsv_log_level(level); dsv_log_level(level);
if (ableSave) if (ableSave){
dsv_log_enalbe_logfile(false); 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!")); QString strMsg(L_S(STR_PAGE_MSG, S_ID(IDS_MSG_TO_CLEAR_LOG), "Confirm!"));
if (MsgBox::Confirm(strMsg)){ if (MsgBox::Confirm(strMsg)){
dsv_clear_log_file(); 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{ else{

View File

@ -28,6 +28,7 @@
#include <QAction> #include <QAction>
#include <QMenu> #include <QMenu>
#include <libsigrok.h> #include <libsigrok.h>
#include <QPushButton>
#include "../sigsession.h" #include "../sigsession.h"
#include "../interface/icallbacks.h" #include "../interface/icallbacks.h"
@ -93,6 +94,9 @@ private:
QAction *_update; QAction *_update;
QAction *_log; QAction *_log;
QPushButton *_log_open_bt;
QPushButton *_log_clear_bt;
IMainForm *_mainForm; IMainForm *_mainForm;
}; };

View File

@ -43,6 +43,7 @@ typedef void (*xlog_print_func)(struct xlog_receiver_info *info, const char *dom
struct xlog_receiver_info struct xlog_receiver_info
{ {
int _type; //see enum xlog_receiver_type int _type; //see enum xlog_receiver_type
int _enable;
FILE *_file; FILE *_file;
xlog_print_func _fn; // print function xlog_print_func _fn; // print function
xlog_receive_callback _rev; //user callback xlog_receive_callback _rev; //user callback
@ -149,6 +150,9 @@ static xlog_context* xlog_new_context(int bConsole)
for(i=0; i< RECEIVER_MAX_COUNT; i++){ for(i=0; i< RECEIVER_MAX_COUNT; i++){
ctx->_receivers[i]._fn = NULL; ctx->_receivers[i]._fn = NULL;
ctx->_receivers[i]._file = 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->_count = 0;
ctx->_log_level = XLOG_LEVEL_INFO; ctx->_log_level = XLOG_LEVEL_INFO;
@ -156,6 +160,7 @@ static xlog_context* xlog_new_context(int bConsole)
if (bConsole){ if (bConsole){
ctx->_receivers[0]._fn = print_to_console; 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; 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]._type = RECEIVER_TYPE_CALLBACK;
ctx->_receivers[i]._rev = rev; ctx->_receivers[i]._rev = rev;
ctx->_receivers[i]._fn = print_to_user_callback; ctx->_receivers[i]._fn = print_to_user_callback;
ctx->_receivers[i]._enable = 1;
ctx->_count++; ctx->_count++;
if (out_index) 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]._type = RECEIVER_TYPE_FILE;
ctx->_receivers[i]._fn = print_to_file; ctx->_receivers[i]._fn = print_to_file;
ctx->_receivers[i]._file = fh; ctx->_receivers[i]._file = fh;
ctx->_receivers[i]._enable = 1;
ctx->_count++; ctx->_count++;
if (out_index) 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]._fn = NULL;
ctx->_receivers[index]._enable = 0;
index++; index++;
while (index < RECEIVER_MAX_COUNT) 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]._type = ctx->_receivers[index]._type;
ctx->_receivers[index-1]._rev = ctx->_receivers[index]._rev; ctx->_receivers[index-1]._rev = ctx->_receivers[index]._rev;
ctx->_receivers[index-1]._file = ctx->_receivers[index]._file; ctx->_receivers[index-1]._file = ctx->_receivers[index]._file;
ctx->_receivers[index-1]._enable = ctx->_receivers[index]._enable;
index++; index++;
} }
ctx->_count--; ctx->_count--;
@ -372,6 +381,30 @@ XLOG_API int xlog_remove_receiver_by_index(xlog_context* ctx, int index)
return 0; 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. * 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++){ for (i = 0; i < RECEIVER_MAX_COUNT; i++){
ctx->_receivers[i]._fn = NULL; 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; 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++){ for (i = 0; i < ctx->_count; i++){
inf = &ctx->_receivers[i]; inf = &ctx->_receivers[i];
if (inf->_fn != NULL){ if (inf->_fn != NULL && inf->_enable){
va_start(args, format); va_start(args, format);
inf->_fn(inf, wr->_domain, format, args); inf->_fn(inf, wr->_domain, format, args);
va_end(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++){ for (i = 0; i < ctx->_count; i++){
inf = &ctx->_receivers[i]; inf = &ctx->_receivers[i];
if (inf->_fn != NULL){ if (inf->_fn != NULL && inf->_enable){
va_start(args, format); va_start(args, format);
inf->_fn(inf, wr->_domain, format, args); inf->_fn(inf, wr->_domain, format, args);
va_end(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++){ for (i = 0; i < ctx->_count; i++){
inf = &ctx->_receivers[i]; inf = &ctx->_receivers[i];
if (inf->_fn != NULL){ if (inf->_fn != NULL && inf->_enable){
va_start(args, format); va_start(args, format);
inf->_fn(inf, wr->_domain, format, args); inf->_fn(inf, wr->_domain, format, args);
va_end(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++){ for (i = 0; i < ctx->_count; i++){
inf = &ctx->_receivers[i]; inf = &ctx->_receivers[i];
if (inf->_fn != NULL){ if (inf->_fn != NULL && inf->_enable){
va_start(args, format); va_start(args, format);
inf->_fn(inf, wr->_domain, format, args); inf->_fn(inf, wr->_domain, format, args);
va_end(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++){ for (i = 0; i < ctx->_count; i++){
inf = &ctx->_receivers[i]; inf = &ctx->_receivers[i];
if (inf->_fn != NULL){ if (inf->_fn != NULL && inf->_enable){
va_start(args, format); va_start(args, format);
inf->_fn(inf, wr->_domain, format, args); inf->_fn(inf, wr->_domain, format, args);
va_end(args); va_end(args);

View File

@ -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); 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. * clear all receiver,return 0 if success.
*/ */