#include "stdafx.h" #include "baseEx.h" #include #include #include #include #include #include #include #include #include #include "exLog/log.h" using namespace boost::interprocess; #if (defined _WIN32 || defined _WIN64) #define BOOST_INTERPROCESS_SHARED_DIR_PATH "C:\\" #endif void* logProducer = nullptr; bool g_useMagic = false; bool g_loggerInitSuccess = false; #define NOR_LEN 50 #define MAX_LOG_LEN 4096 #define MAX_CEFLOG_LEN 5120 struct cefclientLog { LOG_LEVEL_E m_level; LOG_TYPE_E m_type; char m_logTime[NOR_LEN]; char m_filename[NOR_LEN]; int m_filePos; char m_cefLog[MAX_CEFLOG_LEN]; cefclientLog(LOG_LEVEL_E t_level, LOG_TYPE_E t_type, const char* t_logTime, const char* t_filename, int t_filePos, const char* t_cefLog) { m_level = t_level; m_type = t_type; strcpy_s(m_logTime, NOR_LEN, t_logTime); strcpy_s(m_filename, NOR_LEN, t_filename); m_filePos = t_filePos; strcpy_s(m_cefLog, MAX_CEFLOG_LEN, t_cefLog); } cefclientLog() :m_level(LOG_LEVEL_DEBUG), m_type(LOG_TYPE_SYSTEM), m_filePos(0) { memset(m_logTime, 0, NOR_LEN); memset(m_filename, 0, NOR_LEN); memset(m_cefLog, 0, MAX_CEFLOG_LEN); } }; #define CEF_GUID "{5ED197CD-9D64-4D10-9839-2A0773B6F5D2}" bool InitFileLogger(std::string dbgPath) { return g_loggerInitSuccess = logger::init(dbgPath.c_str(), "cefLog"); } void DbgToFileLogger(std::string msg) { if(g_loggerInitSuccess) logger::showLog(msg); } void DbgEx(const char* str, ...) { va_list arg; va_start(arg, str); #if (defined _WIN32 || defined _WIN64) int n = _vscprintf(str, arg); char* buf = (char*)_malloca(n + 1); vsprintf(buf, str, arg); auto modifyStr = CSimpleString::Format("<%d>--%s", GetCurrentThreadId(), buf); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", modifyStr.GetData()); _freea(buf); #else auto modifyStr = CSimpleString::Format("%s", str); vDbg(modifyStr.GetData(), arg); #endif va_end(arg); } bool SYSTEM_ON(std::string cmdLine, bool isWait) { #if (defined _WIN32 || defined _WIN64) auto waitSystem = [](const std::string cmd, const std::string par, BOOL nShow) ->DWORD { SHELLEXECUTEINFO ShExecInfo = { 0 }; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL; ShExecInfo.lpFile = cmd.c_str(); //调用的程序名 ShExecInfo.lpParameters = par.c_str(); //调用程序的命令行参数 ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = nShow ? SW_SHOW : SW_HIDE; //窗口状态为隐藏 ShExecInfo.hInstApp = NULL; ShellExecuteEx(&ShExecInfo); WaitForSingleObject(ShExecInfo.hProcess, INFINITE); //等到该进程结束 DWORD exitCode = 0; GetExitCodeProcess(ShExecInfo.hProcess, &exitCode); return exitCode; }; std::string strCmd, strPar; strPar = std::string("/c ") + cmdLine; strCmd = std::string("cmd ") + strPar; if (isWait) return waitSystem("cmd.exe", strPar, FALSE); else { unsigned int result = WinExec(strCmd.c_str(), SW_HIDE); return result > 31 ? true : false; } #else system(cmdLine.c_str()); return true; #endif return false; } DWORD SaveCefclientLog(std::string magicStr) { std::string dstGuid = magicStr + "_" + CEF_GUID; try { auto removeRet = message_queue::remove(dstGuid.c_str()); if (!removeRet) DbgEx("message_queue did not remove!"); boost::interprocess::permissions cur; cur.set_unrestricted(); message_queue mq(create_only, dstGuid.c_str(), 100, sizeof(cefclientLog), cur); g_useMagic = true; while (true) { cefclientLog t_data; #if (defined _WIN32 || defined _WIN64) unsigned int t_recvSize = 0; #else unsigned long t_recvSize = 0; #endif unsigned int t_priority = 0; mq.receive(&t_data, sizeof(cefclientLog), t_recvSize, t_priority); std::string source = std::string(t_data.m_filename) + "(" + std::to_string((ULONGLONG)t_data.m_filePos) + ")"; DbgWithLink(t_data.m_level, t_data.m_type).withLogProducer(logProducer).setSourceType(source.c_str()).withExtendLog(false).setResultMsg(t_data.m_cefLog)(); if (sizeof(cefclientLog) != t_recvSize) break; /*特么制品库太恶心了 cefclientLog tmp; std::istringstream iss(t_data); boost::archive::binary_iarchive iarchive(iss); iarchive >> tmp;//从一个保存序列化数据的string里面反序列化,从而得到原来的对象 std::string source = tmp.m_filename + "(" + std::to_string((ULONGLONG)tmp.m_filePos) + ")"; DbgWithLink(tmp.m_level, tmp.m_type).setSourceType(source.c_str())(tmp.m_cefLog.c_str()); */ } } catch (interprocess_exception& ex) { message_queue::remove(dstGuid.c_str()); g_useMagic = false; DbgEx("%s run exception, for %s", dstGuid.c_str(), ex.what()); return 1; } g_useMagic = false; message_queue::remove(dstGuid.c_str()); return 0; } std::vector find_files(const std::string srcPath, const std::string fileName, bool isDir) { static boost::xpressive::sregex_compiler rc; if (!rc[fileName].regex_id()) { std::string str = boost::algorithm::replace_all_copy( boost::algorithm::replace_all_copy(fileName, ".", "\\."), "*", ".*"); rc[fileName] = rc.compile(str); } std::vector ret; if (!boost::filesystem::exists(srcPath) || !boost::filesystem::is_directory(srcPath)) return ret; typedef boost::filesystem::recursive_directory_iterator rd_iterator; rd_iterator end; for (rd_iterator pos(srcPath); pos != end; ++pos) { if ((isDir ? boost::filesystem::is_directory(*pos) : !boost::filesystem::is_directory(*pos)) && boost::xpressive::regex_match(pos->path().filename().string(), rc[fileName])) ret.emplace_back(pos->path().string()); } return ret; } bool deleteDir_byFileSystem(const std::string srcPath) { boost::system::error_code ec; return boost::filesystem::remove_all(srcPath, ec); } std::string generateTimeStr(bool isSimple) { auto tt = boost::chrono::system_clock::to_time_t(boost::chrono::system_clock::now()); struct tm* ptm = localtime(&tt); char date[60] = { 0 }; if (isSimple) sprintf(date, "%02d%02d%02d", (int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec); else sprintf(date, "%d-%02d-%02d-%02d:%02d:%02d", (int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday, (int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec); return std::string(date); } //singal,退出函数 #if (defined _WIN32 || defined _WIN64) #else #include // ::signal, ::raise #include #include "CModTools.h" #include "CWebsocketServer.h" std::string g_backtracePath = ""; void seg_signal_handler(int signum) { //boost::stacktrace::safe_dump_to(g_backtracePath.c_str()); Dbg("receive signal:%d, seg_signal_handler", signum); auto dumpArr = boost::stacktrace::stacktrace().as_vector(); if (dumpArr.size() > 20) { for(auto i = 0; i < 10; i++) Dbg("Pre SEG[%d]%X---%s(%s-%d)", i, dumpArr[i].address(), dumpArr[i].name().c_str(), dumpArr[i].source_file().c_str(), dumpArr[i].source_line()); for (auto i = dumpArr.size() - 10; i < dumpArr.size(); i++) Dbg("Post SEG[%d]%X---%s(%s-%d)", i, dumpArr[i].address(), dumpArr[i].name().c_str(), dumpArr[i].source_file().c_str(), dumpArr[i].source_line()); } else { for (auto i = 0; i < dumpArr.size(); i++) Dbg("SEG[%d]%X---%s(%s-%d)", i, dumpArr[i].address(), dumpArr[i].name().c_str(), dumpArr[i].source_file().c_str(), dumpArr[i].source_line()); } Chromium::CModTools::get_mutable_instance().killAllChromium(); Chromium::CWebsocketServer::stopServer(); signal(signum, SIG_DFL); raise(signum); } void normal_signal_handle(int signum) { Dbg("receive signal:%d, normal_signal_handle", signum); signal(signum, SIG_DFL); raise(signum); } void set_traceback_path(std::string path) { g_backtracePath = path + "/mod_chromium/" + generateTimeStr(true) + "_chromium_backtrace.dump"; Dbg("set_traceback_path %s", g_backtracePath.c_str()); } #endif