twain3.0/huagao/Device/filetools.h

99 lines
2.5 KiB
C
Raw Normal View History

#pragma once
#include <vector>
#include <io.h>
#include <fstream>
#include <time.h>
2021-04-13 09:30:05 +00:00
//#include <log4cplus/log4cplus.h>
#include "PublicFunc.h"
2021-04-13 09:30:05 +00:00
enum log_lv :int {
log_TRACE = 0,
log_DEBUG = 10000,
log_INFO = 20000,
log_WARN = 30000,
log_ERROR = 40000,
log_FATAL = 50000,
};
class FileTools
{
public:
static std::vector<std::string> getFiles(std::string path)
{
std::vector<std::string> files;
getFiles(path, files);
return files;
}
static void write_log(std::string log)
{
TCHAR szIniFile[MAX_PATH] = { 0 };
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
_tcscat(szIniFile, HUAGAO_SCAN);
_tcscat(szIniFile, TWAIN_INIPATH);
_tcscat(szIniFile, TEXT("\\"));
_tcscat(szIniFile, TWAIN_LOG_NAME);
std::string savepath = TCHAR2STRING(szIniFile);
write_log(savepath, log);
}
static void write_log(std::string filename, std::string log)
{
//std::string savepath;
//std::string str = "D:";
//savepath = str+"\\"+filename;
std::ofstream ofs(filename, std::ios::app);
time_t timp;
tm* p;
time(&timp);
p=localtime(&timp);
ofs << p->tm_year+1900 << "/" << p->tm_mon+1 << "/" << p->tm_mday << " " << p->tm_hour << ":" << p->tm_min << ":" << p->tm_sec << " "<<log << std::endl;
}
2021-04-13 09:30:05 +00:00
//static void writelog(std::wstring path, int lv, std::string log)
//{
// log4cplus::SharedAppenderPtr rf(new log4cplus::RollingFileAppender(
2021-04-13 09:30:05 +00:00
// path,
// 200 * 1024,
// 5
// ));
// rf->setName(LOG4CPLUS_TEXT("file"));
// log4cplus::tstring pattern = LOG4CPLUS_TEXT("%D{%m/%d/%y %H:%M:%S,%Q} [%t] %-5p %c - %m [%l]%n");
// rf->setLayout(std::unique_ptr<log4cplus::Layout>(new log4cplus::PatternLayout(pattern)));
2021-04-13 09:30:05 +00:00
// log4cplus::Logger logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("hglog"));
// logger.setLogLevel(lv);
// logger.addAppender(rf);
// LOG4CPLUS_WARN(logger, log.c_str());
// logger.removeAllAppenders();
//}
private:
static void getFiles(std::string path, std::vector<std::string>& files)
{
//<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
long hFile = 0;
//<2F>ļ<EFBFBD><C4BC><EFBFBD>Ϣ
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo))!=-1)
{
do
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼,<2C><><EFBFBD><EFBFBD>֮
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
};