loggingmanager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "LoggingErrors.hpp"
  14. #include "loggingcommon.hpp"
  15. #include "loggingmanager.hpp"
  16. #include "compressutil.hpp"
  17. CLoggingManager::~CLoggingManager(void)
  18. {
  19. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  20. {
  21. loggingAgentThreads[x]->stop();
  22. loggingAgentThreads[x]->Release();
  23. }
  24. loggingAgentThreads.clear();
  25. }
  26. //Called when Logging manager is created. Create logging agents based on settings
  27. bool CLoggingManager::init(IPropertyTree* cfg, const char* service)
  28. {
  29. if (!cfg)
  30. {
  31. ERRLOG(EspLoggingErrors::ConfigurationFileEntryError, "Logging Manager setting not found for %s", service);
  32. return false;
  33. }
  34. oneTankFile = cfg->getPropBool("FailSafe", true);
  35. if (oneTankFile)
  36. {
  37. logFailSafe.setown(createFailSafeLogger(cfg, service, cfg->queryProp("@name")));
  38. logContentFilter.readAllLogFilters(cfg);
  39. }
  40. Owned<IPTreeIterator> loggingAgentSettings = cfg->getElements("LogAgent");
  41. ForEach(*loggingAgentSettings)
  42. {
  43. IPropertyTree& loggingAgentTree = loggingAgentSettings->query();
  44. const char* agentName = loggingAgentTree.queryProp("@name");
  45. const char* agentType = loggingAgentTree.queryProp("@type");
  46. const char* agentPlugin = loggingAgentTree.queryProp("@plugin");
  47. if (!agentName || !*agentName || !agentPlugin || !*agentPlugin)
  48. continue;
  49. IEspLogAgent* loggingAgent = loadLoggingAgent(agentName, agentPlugin, service, cfg);
  50. if (!loggingAgent)
  51. {
  52. ERRLOG(-1, "Failed to create logging agent for %s", agentName);
  53. continue;
  54. }
  55. loggingAgent->init(agentName, agentType, &loggingAgentTree, service);
  56. IUpdateLogThread* logThread = createUpdateLogThread(&loggingAgentTree, service, agentName, loggingAgent);
  57. if(!logThread)
  58. throw MakeStringException(-1, "Failed to create update log thread for %s", agentName);
  59. loggingAgentThreads.push_back(logThread);
  60. }
  61. initialized = true;
  62. return !loggingAgentThreads.empty();
  63. }
  64. typedef IEspLogAgent* (*newLoggingAgent_t_)();
  65. IEspLogAgent* CLoggingManager::loadLoggingAgent(const char* name, const char* dll, const char* service, IPropertyTree* cfg)
  66. {
  67. StringBuffer plugin;
  68. plugin.append(SharedObjectPrefix).append(dll).append(SharedObjectExtension);
  69. HINSTANCE loggingAgentLib = LoadSharedObject(plugin.str(), true, false);
  70. if(!loggingAgentLib)
  71. throw MakeStringException(EspLoggingErrors::LoadLoggingLibraryError, "can't load library %s", plugin.str());
  72. newLoggingAgent_t_ xproc = (newLoggingAgent_t_)GetSharedProcedure(loggingAgentLib, "newLoggingAgent");
  73. if (!xproc)
  74. throw MakeStringException(EspLoggingErrors::LoadLoggingLibraryError, "procedure newLoggingAgent of %s can't be loaded", plugin.str());
  75. return (IEspLogAgent*) xproc();
  76. }
  77. IEspLogEntry* CLoggingManager::createLogEntry()
  78. {
  79. return new CEspLogEntry();
  80. }
  81. bool CLoggingManager::updateLog(IEspLogEntry* entry, StringBuffer& status)
  82. {
  83. if (entry->getLogContent())
  84. return updateLog(entry->getEspContext(), entry->getOption(), entry->getLogContent(), status);
  85. if (entry->getLogInfoTree())
  86. return updateLog(entry->getEspContext(), entry->getOption(), entry->getLogInfoTree(), entry->getExtraLog(), status);
  87. return updateLog(entry->getEspContext(), entry->getOption(), entry->getUserContextTree(), entry->getUserRequestTree(),
  88. entry->getBackEndReq(), entry->getBackEndResp(), entry->getUserResp(), entry->getLogDatasets(), status);
  89. }
  90. bool CLoggingManager::updateLog(IEspContext* espContext, const char* option, const char* logContent, StringBuffer& status)
  91. {
  92. if (!initialized)
  93. throw MakeStringException(-1,"LoggingManager not initialized");
  94. bool bRet = false;
  95. try
  96. {
  97. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(nullptr, option, logContent);
  98. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  99. bRet = updateLog(espContext, *req, *resp, status);
  100. }
  101. catch (IException* e)
  102. {
  103. status.set("Failed to update log: ");
  104. e->errorMessage(status);
  105. ERRLOG("%s", status.str());
  106. e->Release();
  107. }
  108. return bRet;
  109. }
  110. bool CLoggingManager::updateLog(IEspContext* espContext, const char* option, IPropertyTree* logInfo, IInterface* extraLog, StringBuffer& status)
  111. {
  112. if (!initialized)
  113. throw MakeStringException(-1,"LoggingManager not initialized");
  114. bool bRet = false;
  115. try
  116. {
  117. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(nullptr, option, LINK(logInfo), LINK(extraLog));
  118. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  119. bRet = updateLog(espContext, *req, *resp, status);
  120. }
  121. catch (IException* e)
  122. {
  123. status.set("Failed to update log: ");
  124. e->errorMessage(status);
  125. ERRLOG("%s", status.str());
  126. e->Release();
  127. }
  128. return bRet;
  129. }
  130. bool CLoggingManager::updateLog(IEspContext* espContext, const char* option, IPropertyTree* userContext, IPropertyTree* userRequest,
  131. const char* backEndReq, const char* backEndResp, const char* userResp, const char* logDatasets, StringBuffer& status)
  132. {
  133. if (!initialized)
  134. throw MakeStringException(-1,"LoggingManager not initialized");
  135. bool bRet = false;
  136. try
  137. {
  138. Owned<IPropertyTree> espContextTree;
  139. if (espContext)
  140. {
  141. espContextTree.setown(createPTree("ESPContext"));
  142. short port;
  143. StringBuffer sourceIP, peerStr;
  144. const char* esdlBindingID = espContext->queryESDLBindingID();
  145. espContext->getServAddress(sourceIP, port);
  146. espContextTree->addProp("SourceIP", sourceIP.str());
  147. espContext->getPeer(peerStr);
  148. espContextTree->addProp("Peer", peerStr.str());
  149. if (!isEmptyString(esdlBindingID))
  150. espContextTree->addProp("ESDLBindingID", esdlBindingID);
  151. //More information in espContext may be added to the espContextTree later.
  152. const char* userId = espContext->queryUserId();
  153. if (userId && *userId)
  154. espContextTree->addProp("UserName", userId);
  155. espContextTree->addProp("ResponseTime", VStringBuffer("%.4f", (msTick()-espContext->queryCreationTime())/1000.0));
  156. }
  157. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(nullptr, option, espContextTree.getClear(), LINK(userContext), LINK(userRequest),
  158. backEndReq, backEndResp, userResp, logDatasets);
  159. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  160. bRet = updateLog(espContext, *req, *resp, status);
  161. }
  162. catch (IException* e)
  163. {
  164. status.set("Failed to update log: ");
  165. e->errorMessage(status);
  166. ERRLOG("%s", status.str());
  167. e->Release();
  168. }
  169. return bRet;
  170. }
  171. bool CLoggingManager::updateLog(IEspContext* espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp, StringBuffer& status)
  172. {
  173. bool bRet = updateLog(espContext, req, resp);
  174. if (bRet)
  175. status.set("Log request has been sent.");
  176. else
  177. {
  178. const char* statusMsg = resp.getStatusMessage();
  179. if (statusMsg && *statusMsg)
  180. status.setf("Failed to update log: %s", statusMsg);
  181. else
  182. status.set("Failed to update log");
  183. }
  184. return bRet;
  185. }
  186. bool CLoggingManager::updateLog(IEspContext* espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp)
  187. {
  188. if (!initialized)
  189. throw MakeStringException(-1,"LoggingManager not initialized");
  190. try
  191. {
  192. if (espContext)
  193. espContext->addTraceSummaryTimeStamp(LogMin, "LMgr:startQLog");
  194. if (oneTankFile)
  195. {
  196. Owned<CLogRequestInFile> reqInFile = new CLogRequestInFile();
  197. if (!saveToTankFile(req, reqInFile))
  198. throw MakeStringException(-1, "LoggingManager: failed in saveToTankFile().");
  199. //Build new log request for logging agents
  200. StringBuffer logContent, v;
  201. appendXMLOpenTag(logContent, LOGCONTENTINFILE);
  202. appendXMLTag(logContent, LOGCONTENTINFILE_FILENAME, reqInFile->getFileName());
  203. appendXMLTag(logContent, LOGCONTENTINFILE_FILEPOS, v.append(reqInFile->getPos()));
  204. appendXMLTag(logContent, LOGCONTENTINFILE_FILESIZE, v.clear().append(reqInFile->getSize()));
  205. appendXMLTag(logContent, LOGREQUEST_GUID, reqInFile->getGUID());
  206. appendXMLCloseTag(logContent, LOGCONTENTINFILE);
  207. Owned<IEspUpdateLogRequest> logRequest = new CUpdateLogRequest("", "");
  208. logRequest->setOption(reqInFile->getOption());
  209. logRequest->setLogContent(logContent);
  210. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  211. {
  212. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  213. if (loggingThread->hasService(LGSTUpdateLOG))
  214. {
  215. loggingThread->queueLog(logRequest);
  216. }
  217. }
  218. }
  219. else
  220. {
  221. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  222. {
  223. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  224. if (loggingThread->hasService(LGSTUpdateLOG))
  225. {
  226. loggingThread->queueLog(&req);
  227. }
  228. }
  229. }
  230. if (espContext)
  231. espContext->addTraceSummaryTimeStamp(LogMin, "LMgr:endQLog");
  232. }
  233. catch (IException* e)
  234. {
  235. StringBuffer errorStr;
  236. e->errorMessage(errorStr);
  237. ERRLOG("Failed to update log: %s",errorStr.str());
  238. resp.setStatusCode(-1);
  239. resp.setStatusMessage(errorStr.str());
  240. e->Release();
  241. }
  242. return true;
  243. }
  244. bool CLoggingManager::saveToTankFile(IEspUpdateLogRequestWrap& logRequest, CLogRequestInFile* reqInFile)
  245. {
  246. if (!logFailSafe.get())
  247. {
  248. ERRLOG("CLoggingManager::saveToTankFile: logFailSafe not configured.");
  249. return false;
  250. }
  251. unsigned startTime = (getEspLogLevel()>=LogNormal) ? msTick() : 0;
  252. StringBuffer GUID;
  253. logFailSafe->GenerateGUID(GUID, NULL);
  254. reqInFile->setGUID(GUID);
  255. reqInFile->setOption(logRequest.getOption());
  256. StringBuffer reqBuf;
  257. Owned<IEspUpdateLogRequestWrap> logRequestFiltered = logContentFilter.filterLogContent(&logRequest);
  258. if (!serializeLogRequestContent(logRequestFiltered, GUID, reqBuf))
  259. {
  260. ERRLOG("CLoggingManager::saveToTankFile: failed in serializeLogRequestContent().");
  261. return false;
  262. }
  263. logFailSafe->AddACK(GUID);//Ack this logging request since the task will be done as soon as the next line is called.
  264. logFailSafe->Add(GUID, reqBuf, reqInFile);
  265. ESPLOG(LogNormal, "LThread:saveToTankFile: %dms\n", msTick() - startTime);
  266. return true;
  267. }
  268. unsigned CLoggingManager::serializeLogRequestContent(IEspUpdateLogRequestWrap* request, const char* GUID, StringBuffer& logData)
  269. {
  270. appendXMLTag(logData, LOGREQUEST_GUID, GUID);
  271. const char* option = request->getOption();
  272. if (!isEmptyString(option))
  273. appendXMLTag(logData, LOGREQUEST_OPTION, option);
  274. appendXMLOpenTag(logData, LOGREQUEST);
  275. const char* logRequest = request->getUpdateLogRequest();
  276. MemoryBuffer memBuf;
  277. LZWCompress(logRequest, strlen(logRequest), memBuf, 0x100);
  278. JBASE64_Encode(memBuf.toByteArray(), memBuf.length(), logData);
  279. appendXMLCloseTag(logData, LOGREQUEST);
  280. return logData.length();
  281. }
  282. bool CLoggingManager::getTransactionSeed(StringBuffer& transactionSeed, StringBuffer& status)
  283. {
  284. if (!initialized)
  285. throw MakeStringException(-1,"LoggingManager not initialized");
  286. bool bRet = false;
  287. try
  288. {
  289. Owned<IEspGetTransactionSeedRequest> req = createGetTransactionSeedRequest();
  290. Owned<IEspGetTransactionSeedResponse> resp = createGetTransactionSeedResponse();
  291. transactionSeed.set("Seed");
  292. bRet = getTransactionSeed(*req, *resp);
  293. if (bRet && !resp->getStatusCode())
  294. {
  295. const char* seed = resp->getSeedId();
  296. if (!seed || !*seed)
  297. status.set("Failed to get Transaction Seed");
  298. else
  299. {
  300. transactionSeed.set(seed);
  301. status.set("Transaction Seed returned.");
  302. bRet = true;
  303. }
  304. }
  305. else
  306. {
  307. const char* statusMsg = resp->getStatusMessage();
  308. if (statusMsg && *statusMsg)
  309. status.setf("Failed to get Transaction Seed: %s", statusMsg);
  310. else
  311. status.set("Failed to get Transaction Seed");
  312. }
  313. }
  314. catch (IException* e)
  315. {
  316. e->errorMessage(status);
  317. status.insert(0, "Failed to get Transaction Seed: ");
  318. ERRLOG("%s",status.str());
  319. e->Release();
  320. }
  321. return bRet;
  322. }
  323. bool CLoggingManager::getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp)
  324. {
  325. if (!initialized)
  326. throw MakeStringException(-1,"LoggingManager not initialized");
  327. bool bRet = false;
  328. try
  329. {
  330. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  331. {
  332. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  333. if (!loggingThread->hasService(LGSTGetTransactionSeed))
  334. continue;
  335. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  336. bRet = loggingAgent->getTransactionSeed(req, resp);
  337. if (bRet)
  338. break;
  339. }
  340. }
  341. catch (IException* e)
  342. {
  343. StringBuffer errorStr;
  344. e->errorMessage(errorStr);
  345. ERRLOG("Failed to get Transaction Seed: %s",errorStr.str());
  346. resp.setStatusCode(-1);
  347. resp.setStatusMessage(errorStr.str());
  348. e->Release();
  349. }
  350. return bRet;
  351. }
  352. bool CLoggingManager::getTransactionID(StringAttrMapping* transFields, StringBuffer& transactionID, StringBuffer& status)
  353. {
  354. if (!initialized)
  355. throw MakeStringException(-1,"LoggingManager not initialized");
  356. try
  357. {
  358. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  359. {
  360. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  361. if (!loggingThread->hasService(LGSTGetTransactionID))
  362. continue;
  363. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  364. loggingAgent->getTransactionID(transFields, transactionID);
  365. if (!transactionID.isEmpty())
  366. ESPLOG(LogMax, "Got TransactionID '%s'", transactionID.str());
  367. return true;
  368. }
  369. }
  370. catch (IException* e)
  371. {
  372. e->errorMessage(status);
  373. e->Release();
  374. }
  375. return false;
  376. }
  377. extern "C"
  378. {
  379. LOGGINGMANAGER_API ILoggingManager* newLoggingManager()
  380. {
  381. return new CLoggingManager();
  382. }
  383. }