增加日志文件获取接口

This commit is contained in:
gb 2022-07-20 14:57:26 +08:00
parent 0e51486c14
commit bcf3063818
3 changed files with 56 additions and 1 deletions

View File

@ -662,6 +662,20 @@ scanner_err hg_scanner_mgr::hg_scanner_control(scanner_handle h, unsigned long c
if (code == IO_CTRL_CODE_ABOUT_INFO)
return get_about_info(h, data, len);
else if (code == IO_CTRL_CODE_GET_LOG_FILE && len && *len == LOG_FILE_DRIVER)
{
if (!data)
return SCANNER_ERR_INVALID_PARAMETER;
std::string f(hg_log::log_file_path());
if (f.empty())
*((char*)data) = 0;
else
strcpy((char*)data, f.c_str());
return SCANNER_ERR_OK;
}
else if (!h)
return SCANNER_ERR_INVALID_PARAMETER;
else

View File

@ -26,6 +26,7 @@
#include <time.h>
#include "../wrapper/hg_log.h"
#include <algorithm>
#include <mutex>
#include "ini_file.h"
#define MAX_LOG_FILE_SIZE 100 * 1024 * 1024
@ -46,6 +47,7 @@ class log_cls
log_to log_;
int level_;
log_callback lcb_;
std::mutex lock_;
static log_cls* inst_;
@ -111,7 +113,7 @@ public:
if (param)
{
path_file_ = (char*)param;
file_ = fopen(path_file_.c_str(), "wb");
file_ = fopen(path_file_.c_str(), "w+b");
if (file_)
{
unsigned char bom[] = { 0x0ef, 0x0bb, 0x0bf };
@ -144,8 +146,42 @@ public:
void log(const char* info)
{
std::lock_guard<std::mutex> lock(lock_);
log_(info, (void*)file_);
}
std::string get_log_file_path(bool copy)
{
std::string file("");
if (log_ == &log_cls::log_file)
{
file = path_file_;
if (copy)
{
file += ".txt";
FILE* dst = fopen(file.c_str(), "wb");
if (!dst)
file = "";
else
{
std::lock_guard<std::mutex> lock(lock_);
char buf[1024] = { 0 };
size_t l = 0;
fseek(file_, 0, SEEK_SET);
while ((l = fread(buf, 1, sizeof(buf), file_)))
fwrite(buf, 1, l, dst);
fclose(dst);
}
}
}
return file;
}
};
log_cls* log_cls::inst_ = NULL;
std::string log_cls::g_time_tag = "=====";
@ -554,6 +590,10 @@ extern "C"
{
return simple_ini::temporary_path();
}
std::string log_file_path(void)
{
return log_cls::instance()->get_log_file_path(true);
}
int init(void)
{

View File

@ -51,6 +51,7 @@ extern "C"
std::string get_scanner_path(void);
std::string local_data_path(void);
std::string temporary_path(void);
std::string log_file_path(void);
unsigned int get_page_size(unsigned int* map_unit = nullptr);
unsigned long long available_memory(void);
void str_tolower(std::string& str);