baseEx.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "stdafx.h"
  2. #include "baseEx.h"
  3. #include <stdint.h>
  4. #include <iostream>
  5. #include <vector>
  6. #include <boost/xpressive/xpressive_dynamic.hpp>
  7. #include <boost/algorithm/string.hpp>
  8. #include <boost/filesystem.hpp>
  9. #include <boost/chrono.hpp>
  10. #include <boost/interprocess/ipc/message_queue.hpp>
  11. #include <boost/interprocess/permissions.hpp>
  12. #include "exLog/log.h"
  13. using namespace boost::interprocess;
  14. #if (defined _WIN32 || defined _WIN64)
  15. #define BOOST_INTERPROCESS_SHARED_DIR_PATH "C:\\"
  16. #endif
  17. void* logProducer = nullptr;
  18. bool g_useMagic = false;
  19. bool g_loggerInitSuccess = false;
  20. #define NOR_LEN 50
  21. #define MAX_LOG_LEN 4096
  22. #define MAX_CEFLOG_LEN 5120
  23. struct cefclientLog {
  24. LOG_LEVEL_E m_level;
  25. LOG_TYPE_E m_type;
  26. char m_logTime[NOR_LEN];
  27. char m_filename[NOR_LEN];
  28. int m_filePos;
  29. char m_cefLog[MAX_CEFLOG_LEN];
  30. 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) {
  31. m_level = t_level;
  32. m_type = t_type;
  33. strcpy_s(m_logTime, NOR_LEN, t_logTime);
  34. strcpy_s(m_filename, NOR_LEN, t_filename);
  35. m_filePos = t_filePos;
  36. strcpy_s(m_cefLog, MAX_CEFLOG_LEN, t_cefLog);
  37. }
  38. cefclientLog()
  39. :m_level(LOG_LEVEL_DEBUG), m_type(LOG_TYPE_SYSTEM), m_filePos(0)
  40. {
  41. memset(m_logTime, 0, NOR_LEN);
  42. memset(m_filename, 0, NOR_LEN);
  43. memset(m_cefLog, 0, MAX_CEFLOG_LEN);
  44. }
  45. };
  46. #define CEF_GUID "{5ED197CD-9D64-4D10-9839-2A0773B6F5D2}"
  47. bool InitFileLogger(std::string dbgPath)
  48. {
  49. return g_loggerInitSuccess = logger::init(dbgPath.c_str(), "cefLog");
  50. }
  51. void DbgToFileLogger(std::string msg)
  52. {
  53. if(g_loggerInitSuccess)
  54. logger::showLog(msg);
  55. }
  56. void DbgEx(const char* str, ...)
  57. {
  58. va_list arg;
  59. va_start(arg, str);
  60. #if (defined _WIN32 || defined _WIN64)
  61. int n = _vscprintf(str, arg);
  62. char* buf = (char*)_malloca(n + 1);
  63. vsprintf(buf, str, arg);
  64. auto modifyStr = CSimpleString::Format("<%d>--%s", GetCurrentThreadId(), buf);
  65. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", modifyStr.GetData());
  66. _freea(buf);
  67. #else
  68. auto modifyStr = CSimpleString::Format("%s", str);
  69. vDbg(modifyStr.GetData(), arg);
  70. #endif
  71. va_end(arg);
  72. }
  73. bool SYSTEM_ON(std::string cmdLine, bool isWait)
  74. {
  75. #if (defined _WIN32 || defined _WIN64)
  76. auto waitSystem = [](const std::string cmd, const std::string par, BOOL nShow) ->DWORD
  77. {
  78. SHELLEXECUTEINFO ShExecInfo = { 0 };
  79. ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  80. ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  81. ShExecInfo.hwnd = NULL;
  82. ShExecInfo.lpVerb = NULL;
  83. ShExecInfo.lpFile = cmd.c_str(); //调用的程序名
  84. ShExecInfo.lpParameters = par.c_str(); //调用程序的命令行参数
  85. ShExecInfo.lpDirectory = NULL;
  86. ShExecInfo.nShow = nShow ? SW_SHOW : SW_HIDE; //窗口状态为隐藏
  87. ShExecInfo.hInstApp = NULL;
  88. ShellExecuteEx(&ShExecInfo);
  89. WaitForSingleObject(ShExecInfo.hProcess, INFINITE); //等到该进程结束
  90. DWORD exitCode = 0;
  91. GetExitCodeProcess(ShExecInfo.hProcess, &exitCode);
  92. return exitCode;
  93. };
  94. std::string strCmd, strPar;
  95. strPar = std::string("/c ") + cmdLine;
  96. strCmd = std::string("cmd ") + strPar;
  97. if (isWait)
  98. return waitSystem("cmd.exe", strPar, FALSE);
  99. else {
  100. unsigned int result = WinExec(strCmd.c_str(), SW_HIDE);
  101. return result > 31 ? true : false;
  102. }
  103. #else
  104. system(cmdLine.c_str());
  105. return true;
  106. #endif
  107. return false;
  108. }
  109. DWORD SaveCefclientLog(std::string magicStr)
  110. {
  111. std::string dstGuid = magicStr + "_" + CEF_GUID;
  112. try {
  113. auto removeRet = message_queue::remove(dstGuid.c_str());
  114. if (!removeRet)
  115. DbgEx("message_queue did not remove!");
  116. boost::interprocess::permissions cur;
  117. cur.set_unrestricted();
  118. message_queue mq(create_only, dstGuid.c_str(), 100, sizeof(cefclientLog), cur);
  119. g_useMagic = true;
  120. while (true)
  121. {
  122. cefclientLog t_data;
  123. #if (defined _WIN32 || defined _WIN64)
  124. unsigned int t_recvSize = 0;
  125. #else
  126. unsigned long t_recvSize = 0;
  127. #endif
  128. unsigned int t_priority = 0;
  129. mq.receive(&t_data, sizeof(cefclientLog), t_recvSize, t_priority);
  130. std::string source = std::string(t_data.m_filename) + "(" + std::to_string((ULONGLONG)t_data.m_filePos) + ")";
  131. DbgWithLink(t_data.m_level, t_data.m_type).withLogProducer(logProducer).setSourceType(source.c_str()).withExtendLog(false).setResultMsg(t_data.m_cefLog)();
  132. if (sizeof(cefclientLog) != t_recvSize)
  133. break;
  134. /*特么制品库太恶心了
  135. cefclientLog tmp;
  136. std::istringstream iss(t_data);
  137. boost::archive::binary_iarchive iarchive(iss);
  138. iarchive >> tmp;//从一个保存序列化数据的string里面反序列化,从而得到原来的对象
  139. std::string source = tmp.m_filename + "(" + std::to_string((ULONGLONG)tmp.m_filePos) + ")";
  140. DbgWithLink(tmp.m_level, tmp.m_type).setSourceType(source.c_str())(tmp.m_cefLog.c_str());
  141. */
  142. }
  143. }
  144. catch (interprocess_exception& ex) {
  145. message_queue::remove(dstGuid.c_str());
  146. g_useMagic = false;
  147. DbgEx("%s run exception, for %s", dstGuid.c_str(), ex.what());
  148. return 1;
  149. }
  150. g_useMagic = false;
  151. message_queue::remove(dstGuid.c_str());
  152. return 0;
  153. }
  154. std::vector<std::string> find_files(const std::string srcPath, const std::string fileName, bool isDir)
  155. {
  156. static boost::xpressive::sregex_compiler rc;
  157. if (!rc[fileName].regex_id())
  158. {
  159. std::string str = boost::algorithm::replace_all_copy(
  160. boost::algorithm::replace_all_copy(fileName, ".", "\\."), "*", ".*");
  161. rc[fileName] = rc.compile(str);
  162. }
  163. std::vector<std::string> ret;
  164. if (!boost::filesystem::exists(srcPath) || !boost::filesystem::is_directory(srcPath))
  165. return ret;
  166. typedef boost::filesystem::recursive_directory_iterator rd_iterator;
  167. rd_iterator end;
  168. for (rd_iterator pos(srcPath); pos != end; ++pos)
  169. {
  170. if ((isDir ? boost::filesystem::is_directory(*pos) : !boost::filesystem::is_directory(*pos)) && boost::xpressive::regex_match(pos->path().filename().string(), rc[fileName]))
  171. ret.emplace_back(pos->path().string());
  172. }
  173. return ret;
  174. }
  175. bool deleteDir_byFileSystem(const std::string srcPath)
  176. {
  177. boost::system::error_code ec;
  178. return boost::filesystem::remove_all(srcPath, ec);
  179. }
  180. std::string generateTimeStr(bool isSimple)
  181. {
  182. auto tt = boost::chrono::system_clock::to_time_t(boost::chrono::system_clock::now());
  183. struct tm* ptm = localtime(&tt);
  184. char date[60] = { 0 };
  185. if (isSimple)
  186. sprintf(date, "%02d%02d%02d", (int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
  187. else
  188. 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);
  189. return std::string(date);
  190. }
  191. //singal,退出函数
  192. #if (defined _WIN32 || defined _WIN64)
  193. #else
  194. #include <signal.h> // ::signal, ::raise
  195. #include <boost/stacktrace.hpp>
  196. #include "CModTools.h"
  197. #include "CWebsocketServer.h"
  198. std::string g_backtracePath = "";
  199. void seg_signal_handler(int signum)
  200. {
  201. //boost::stacktrace::safe_dump_to(g_backtracePath.c_str());
  202. Dbg("receive signal:%d, seg_signal_handler", signum);
  203. auto dumpArr = boost::stacktrace::stacktrace().as_vector();
  204. if (dumpArr.size() > 20)
  205. {
  206. for(auto i = 0; i < 10; i++)
  207. 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());
  208. for (auto i = dumpArr.size() - 10; i < dumpArr.size(); i++)
  209. 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());
  210. }
  211. else
  212. {
  213. for (auto i = 0; i < dumpArr.size(); i++)
  214. 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());
  215. }
  216. Chromium::CModTools::get_mutable_instance().killAllChromium();
  217. Chromium::CWebsocketServer::stopServer();
  218. signal(signum, SIG_DFL);
  219. raise(signum);
  220. }
  221. void normal_signal_handle(int signum)
  222. {
  223. Dbg("receive signal:%d, normal_signal_handle", signum);
  224. signal(signum, SIG_DFL);
  225. raise(signum);
  226. }
  227. void set_traceback_path(std::string path)
  228. {
  229. g_backtracePath = path + "/mod_chromium/" + generateTimeStr(true) + "_chromium_backtrace.dump";
  230. Dbg("set_traceback_path %s", g_backtracePath.c_str());
  231. }
  232. #endif