增加TWAIN日志功能
This commit is contained in:
parent
c8becc7e7a
commit
027f627365
|
@ -4,6 +4,7 @@ EXPORTS
|
|||
open_scanner
|
||||
is_scanner_online
|
||||
uninitialize
|
||||
log_info
|
||||
sane_hgsane_init
|
||||
sane_hgsane_exit
|
||||
sane_hgsane_get_devices
|
||||
|
|
114
sane/scanner.cpp
114
sane/scanner.cpp
|
@ -21,7 +21,12 @@ static IMPLEMENT_OPTION_STRING_COMPARE(compare_sane_opt);
|
|||
}
|
||||
|
||||
|
||||
|
||||
#ifdef EXPORT_SANE_API
|
||||
__declspec(dllexport)
|
||||
#else
|
||||
__declspec(dllimport)
|
||||
#endif
|
||||
void __stdcall log_info(const wchar_t* info, int level);
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -110,7 +115,7 @@ namespace callback
|
|||
// class scanner
|
||||
scanner::scanner(SCANNERID id) : handle_(NULL), id_(id), ex_id_(EXTENSION_ID_BASE), prev_start_result_(SCANNER_ERR_NOT_START)
|
||||
, dpi_(200), tmp_path_(L""), img_ind_(0)
|
||||
, scanner_name_(L""), cfg_(NULL)
|
||||
, scanner_name_(L""), cfg_(NULL), is_ui_wait_img_(false), is_scanning_(false)
|
||||
{
|
||||
cfg_ = new gb::sane_config();
|
||||
tmp_path_ = local_trans::a2u(hg_sane_middleware::sane_path().c_str());
|
||||
|
@ -249,7 +254,16 @@ void scanner::on_ui_event(int uev, void* sender)
|
|||
if (prev_start_result_ != SANE_STATUS_GOOD && sender == indicator_.get())
|
||||
indicator_.reset();
|
||||
else
|
||||
{
|
||||
events_.save(uev);
|
||||
if (/*events_.count() > 5 && !is_ui_wait_img_ &&*/
|
||||
(uev == SANE_EVENT_UI_CLOSE_CANCEL || uev == SANE_EVENT_UI_CLOSE_NORMAL))
|
||||
{
|
||||
events_.clear();
|
||||
ui_hide();
|
||||
events_.save(SANE_EVENT_SCAN_FINISHED);
|
||||
}
|
||||
}
|
||||
}
|
||||
int scanner::open(void)
|
||||
{
|
||||
|
@ -1643,6 +1657,8 @@ COM_API_IMPLEMENT(scanner, int, start(void))
|
|||
{
|
||||
int ret = SANE_STATUS_GOOD;
|
||||
|
||||
events_.clear();
|
||||
images_.clear();
|
||||
scan_msg_ = "OK";
|
||||
scan_err_ = false;
|
||||
ret = hg_sane_middleware::instance()->start(handle_, NULL);
|
||||
|
@ -1667,6 +1683,7 @@ COM_API_IMPLEMENT(scanner, int, start(void))
|
|||
indicator_->notify_scan_over(hg_scanner_err_description(ret), true);
|
||||
}
|
||||
prev_start_result_ = ret;
|
||||
is_scanning_ = ret == SANE_STATUS_GOOD;
|
||||
|
||||
return local_utility::sane_statu_2_scanner_err(ret);
|
||||
}
|
||||
|
@ -1689,7 +1706,8 @@ COM_API_IMPLEMENT(scanner, int, get_scanned_images(DWORD milliseconds))
|
|||
size_t count = images_.count();
|
||||
DWORD elapse = 10;
|
||||
|
||||
while (count == 0 && milliseconds)
|
||||
is_ui_wait_img_ = true;
|
||||
while (is_scanning_ && count == 0 && milliseconds)
|
||||
{
|
||||
MSG msg = { 0 };
|
||||
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
||||
|
@ -1726,6 +1744,14 @@ COM_API_IMPLEMENT(scanner, int, get_scanned_images(DWORD milliseconds))
|
|||
milliseconds -= elapse;
|
||||
}
|
||||
}
|
||||
is_ui_wait_img_ = false;
|
||||
count = images_.count();
|
||||
|
||||
{
|
||||
wchar_t msg[128] = { 0 };
|
||||
swprintf_s(msg, _countof(msg) - 1, L"Wait image count = %d\r\n", count);
|
||||
log_info(msg, 0);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
@ -2149,6 +2175,8 @@ int scanner::handle_device_event(int ev_code, void* data, unsigned int* len)
|
|||
indicator_->notify_working();
|
||||
else
|
||||
events_.save(ev_code);
|
||||
|
||||
log_info(L"Scanning ...\r\n", 0);
|
||||
}
|
||||
else if (ev_code == SANE_EVENT_IMAGE_OK)
|
||||
{
|
||||
|
@ -2168,6 +2196,12 @@ int scanner::handle_device_event(int ev_code, void* data, unsigned int* len)
|
|||
}
|
||||
if (indicator_.get())
|
||||
indicator_->notify_data_arrived(true);
|
||||
|
||||
{
|
||||
wchar_t msg[128] = { 0 };
|
||||
swprintf_s(msg, _countof(msg) - 1, L"New image(%u) received with %u bytes\r\n", img_ind_, simg->bytes);
|
||||
log_info(msg, 0);
|
||||
}
|
||||
}
|
||||
else if (ev_code == SANE_EVENT_USB_DATA_RECEIVED)
|
||||
{
|
||||
|
@ -2180,6 +2214,13 @@ int scanner::handle_device_event(int ev_code, void* data, unsigned int* len)
|
|||
indicator_->notify_scan_over((char*)data, *len != SCANNER_ERR_OK);
|
||||
else
|
||||
events_.save(ev_code);
|
||||
is_scanning_ = false;
|
||||
|
||||
{
|
||||
wchar_t msg[128] = { 0 };
|
||||
swprintf_s(msg, _countof(msg) - 1, L"Scan finished with error: %u\r\n", *len);
|
||||
log_info(msg, 0);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -2200,6 +2241,61 @@ int scanner::handle_device_event(int ev_code, void* data, unsigned int* len)
|
|||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// log ...
|
||||
#include <direct.h>
|
||||
|
||||
std::mutex g_lock_;
|
||||
FILE* g_file_ = NULL;
|
||||
|
||||
void init_log(void)
|
||||
{
|
||||
char* tmp = getenv("LOCALAPPDATA");
|
||||
if (tmp)
|
||||
{
|
||||
std::string path(tmp);
|
||||
char name[MAX_PATH] = { 0 }, * last = NULL;
|
||||
|
||||
path += std::string("\\") + PRODUCT_VENDOR + "Scan\\Log\\";
|
||||
mkdir(path.c_str());
|
||||
GetModuleFileNameA(NULL, name, _countof(name) - 1);
|
||||
last = strrchr(name, '\\');
|
||||
if (last++ == NULL)
|
||||
last = name;
|
||||
path += last;
|
||||
path += "_twain.log";
|
||||
g_file_ = fopen(path.c_str(), "wb");
|
||||
}
|
||||
else
|
||||
{
|
||||
char name[MAX_PATH] = { 0 }, * last = NULL;
|
||||
std::string path("");
|
||||
|
||||
GetModuleFileNameA(NULL, name, _countof(name) - 1);
|
||||
path = std::string(name) + "_twain.log";
|
||||
g_file_ = fopen(path.c_str(), "wb");
|
||||
}
|
||||
}
|
||||
void close_log(void)
|
||||
{
|
||||
if (g_file_)
|
||||
fclose(g_file_);
|
||||
g_file_ = NULL;
|
||||
}
|
||||
void log(const wchar_t* info)
|
||||
{
|
||||
if (g_file_)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_lock_);
|
||||
|
||||
fwrite(info, 2, lstrlenW(info), g_file_);
|
||||
fflush(g_file_);
|
||||
if (ftell(g_file_) > 10 * 1024 * 1024)
|
||||
fseek(g_file_, 0, SEEK_SET);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// exports
|
||||
#ifdef EXPORT_SANE_API
|
||||
|
@ -2209,6 +2305,7 @@ __declspec(dllimport)
|
|||
#endif
|
||||
int __stdcall initialize(void* reserve)
|
||||
{
|
||||
init_log();
|
||||
hg_sane_middleware::set_callback(callback::sane_event_callback, NULL);
|
||||
hg_sane_middleware::instance();
|
||||
|
||||
|
@ -2250,8 +2347,17 @@ int __stdcall uninitialize(void* reserve)
|
|||
{
|
||||
hg_sane_middleware::set_callback(NULL, NULL);
|
||||
hg_sane_middleware::clear();
|
||||
close_log();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef EXPORT_SANE_API
|
||||
__declspec(dllexport)
|
||||
#else
|
||||
__declspec(dllimport)
|
||||
#endif
|
||||
void __stdcall log_info(const wchar_t* info, int level)
|
||||
{
|
||||
log(info);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,8 @@ class scanner : public ISaneInvoker, virtual public refer
|
|||
std::wstring cfg_path_;
|
||||
std::string scan_msg_;
|
||||
bool scan_err_;
|
||||
volatile bool is_ui_wait_img_;
|
||||
volatile bool is_scanning_;
|
||||
twain_xfer xfer_;
|
||||
safe_img_queue images_;
|
||||
safe_queue<int> events_; //如果有界面,则全部保存从界面传回的消息;否则只保存开始扫描和结束扫描的事件
|
||||
|
|
|
@ -11,6 +11,7 @@ namespace load_sane_util
|
|||
static int(__stdcall* is_on)(SCANNERID) = NULL;
|
||||
static int(__stdcall* init)(void*) = NULL;
|
||||
static int(__stdcall* uninit)(void*) = NULL;
|
||||
static void(__stdcall* log)(const wchar_t*, int) = NULL;
|
||||
|
||||
static std::string u2m(const wchar_t* u, int page)
|
||||
{
|
||||
|
@ -124,6 +125,7 @@ namespace load_sane_util
|
|||
*((FARPROC*)&sane_inst) = GetProcAddress(sane_module, "open_scanner");
|
||||
*((FARPROC*)&is_on) = GetProcAddress(sane_module, "is_scanner_online");
|
||||
*((FARPROC*)&uninit) = GetProcAddress(sane_module, "uninitialize");
|
||||
*((FARPROC*)&log) = GetProcAddress(sane_module, "log_info");
|
||||
ret = is_ok();
|
||||
if (ret)
|
||||
ret = init(NULL) == 0;
|
||||
|
@ -172,6 +174,11 @@ namespace load_sane_util
|
|||
sane_path = L"";
|
||||
sane_inst = NULL;
|
||||
}
|
||||
void log_info(const wchar_t* info, int level)
|
||||
{
|
||||
if (log)
|
||||
log(info, level);
|
||||
}
|
||||
|
||||
std::string utf82ansi(const char* utf8)
|
||||
{
|
||||
|
@ -181,4 +188,8 @@ namespace load_sane_util
|
|||
{
|
||||
return u2m(m2u(ansi, CP_ACP).c_str(), CP_UTF8);
|
||||
}
|
||||
std::wstring ansi2unic(const char* ansi)
|
||||
{
|
||||
return m2u(ansi, CP_ACP);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -12,8 +12,10 @@ namespace load_sane_util
|
|||
bool is_online(SCANNERID guid);
|
||||
ISaneInvoker* open(SCANNERID guid, int* err);
|
||||
void uninitialize(void);
|
||||
void log_info(const wchar_t* info, int level);
|
||||
|
||||
std::string utf82ansi(const char* utf8);
|
||||
std::string ansi2utf8(const char* ansi);
|
||||
std::wstring ansi2unic(const char* ansi);
|
||||
};
|
||||
|
||||
|
|
|
@ -727,16 +727,23 @@ Result huagao_ds::eventProcess(const Identity&, Event& event)
|
|||
// event.setMessage(Msg::Null);
|
||||
if (scanner_.get())
|
||||
{
|
||||
switch (scanner_->get_event())
|
||||
int ev = scanner_->get_event();
|
||||
wchar_t msg[128] = { 0 };
|
||||
swprintf_s(msg, _countof(msg) - 1, L"ds::eventProcess(0x%x)\r\n", ev);
|
||||
load_sane_util::log_info(msg, 0);
|
||||
|
||||
switch (ev)
|
||||
{
|
||||
case SANE_EVENT_WORKING:
|
||||
notifyXferReady();
|
||||
break;
|
||||
case SANE_EVENT_UI_CLOSE_CANCEL:
|
||||
scanner_->stop();
|
||||
notifyEndWithoutImages();
|
||||
// notifyEndWithoutImages();
|
||||
break;
|
||||
case SANE_EVENT_UI_CLOSE_NORMAL:
|
||||
scanner_->ui_hide();
|
||||
case SANE_EVENT_SCAN_FINISHED:
|
||||
notifyCloseOk();
|
||||
break;
|
||||
case SANE_EVENT_UI_SCAN_COMMAND:
|
||||
|
@ -975,7 +982,7 @@ Result huagao_ds::imageLayoutGet(const Identity&, ImageLayout& data)
|
|||
{
|
||||
SANE_Parameters head = { 0 };
|
||||
|
||||
if (!scanner_.get() || scanner_->get_scanned_images() == 0)
|
||||
if (!scanner_.get() || scanner_->get_scanned_images(-1) == 0)
|
||||
return seqError();
|
||||
|
||||
int res = 200;
|
||||
|
@ -1011,7 +1018,7 @@ Result huagao_ds::imageMemXferGet(const Identity& origin, ImageMemXfer& data)
|
|||
if (!scanner_.get())
|
||||
return seqError();
|
||||
|
||||
if (scanner_->get_scanned_images() == 0 && !pending_xfer_.img)
|
||||
if (scanner_->get_scanned_images(-1) == 0 && !pending_xfer_.img)
|
||||
{
|
||||
if (!cur_head_ || !scanner_->wait_image())
|
||||
{
|
||||
|
@ -1079,7 +1086,7 @@ Result huagao_ds::imageMemXferGet(const Identity& origin, ImageMemXfer& data)
|
|||
}
|
||||
Result huagao_ds::imageNativeXferGet(const Identity& id, ImageNativeXfer& data)
|
||||
{
|
||||
if (!scanner_.get() || scanner_->get_scanned_images() == 0)
|
||||
if (!scanner_.get() || scanner_->get_scanned_images(-1) == 0)
|
||||
return seqError();
|
||||
|
||||
IScanImg* img = scanner_->take_first_image(TWAIN_XFER_Native);
|
||||
|
@ -1153,10 +1160,21 @@ Twpp::Result huagao_ds::imageFileXferGet(const Twpp::Identity& origin)
|
|||
|
||||
fclose(dst);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::wstring f(L"CreateFile(" + load_sane_util::ansi2unic(m_fileXfer.filePath().string().c_str()));
|
||||
wchar_t msg[128] = { 0 };
|
||||
|
||||
swprintf_s(msg, _countof(msg) - 1, L") = %d\r\n", GetLastError());
|
||||
f += msg;
|
||||
load_sane_util::log_info(f.c_str(), 0);
|
||||
}
|
||||
img->release();
|
||||
}
|
||||
else
|
||||
{
|
||||
load_sane_util::log_info((L"Map file to " + load_sane_util::ansi2unic(m_fileXfer.filePath().string().c_str()) + L"\r\n").c_str(), 0);
|
||||
|
||||
img->keep_file(true);
|
||||
img->release();
|
||||
if (MoveFileA(file.c_str(), m_fileXfer.filePath().string().c_str()))
|
||||
|
@ -1189,6 +1207,13 @@ Twpp::Result huagao_ds::imageFileXferGet(const Twpp::Identity& origin)
|
|||
ret = { ReturnCode::Failure, ConditionCode::OperationError };
|
||||
DeleteFileA(file.c_str());
|
||||
}
|
||||
|
||||
{
|
||||
std::wstring info(load_sane_util::ansi2unic(m_fileXfer.filePath().string().c_str()));
|
||||
wchar_t r[80] = { 0 };
|
||||
swprintf_s(r, _countof(r) - 1, L": result = %d\r\n", ret.returnCode());
|
||||
load_sane_util::log_info((info + r).c_str(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
Loading…
Reference in New Issue