loggingmanager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. CLoggingManager::~CLoggingManager(void)
  17. {
  18. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  19. {
  20. loggingAgentThreads[x]->stop();
  21. loggingAgentThreads[x]->Release();
  22. }
  23. loggingAgentThreads.clear();
  24. }
  25. //Called when Logging manager is created. Create logging agents based on settings
  26. bool CLoggingManager::init(IPropertyTree* cfg, const char* service)
  27. {
  28. if (!cfg)
  29. {
  30. ERRLOG(EspLoggingErrors::ConfigurationFileEntryError, "Logging Manager setting not found for %s", service);
  31. return false;
  32. }
  33. Owned<IPTreeIterator> loggingAgentSettings = cfg->getElements("LogAgent");
  34. ForEach(*loggingAgentSettings)
  35. {
  36. IPropertyTree& loggingAgentTree = loggingAgentSettings->query();
  37. const char* agentName = loggingAgentTree.queryProp("@name");
  38. const char* agentType = loggingAgentTree.queryProp("@type");
  39. const char* agentPlugin = loggingAgentTree.queryProp("@plugin");
  40. if (!agentName || !*agentName || !agentPlugin || !*agentPlugin)
  41. continue;
  42. IEspLogAgent* loggingAgent = loadLoggingAgent(agentName, agentPlugin, service, cfg);
  43. if (!loggingAgent)
  44. {
  45. ERRLOG(-1, "Failed to create logging agent for %s", agentName);
  46. continue;
  47. }
  48. loggingAgent->init(agentName, agentType, &loggingAgentTree, service);
  49. IUpdateLogThread* logThread = createUpdateLogThread(&loggingAgentTree, service, agentName, loggingAgent);
  50. if(!logThread)
  51. throw MakeStringException(-1, "Failed to create update log thread for %s", agentName);
  52. loggingAgentThreads.push_back(logThread);
  53. }
  54. initialized = true;
  55. return !loggingAgentThreads.empty();
  56. }
  57. typedef IEspLogAgent* (*newLoggingAgent_t_)();
  58. IEspLogAgent* CLoggingManager::loadLoggingAgent(const char* name, const char* dll, const char* service, IPropertyTree* cfg)
  59. {
  60. StringBuffer plugin;
  61. plugin.append(SharedObjectPrefix).append(dll).append(SharedObjectExtension);
  62. HINSTANCE loggingAgentLib = LoadSharedObject(plugin.str(), true, false);
  63. if(!loggingAgentLib)
  64. throw MakeStringException(EspLoggingErrors::LoadLoggingLibraryError, "can't load library %s", plugin.str());
  65. newLoggingAgent_t_ xproc = (newLoggingAgent_t_)GetSharedProcedure(loggingAgentLib, "newLoggingAgent");
  66. if (!xproc)
  67. throw MakeStringException(EspLoggingErrors::LoadLoggingLibraryError, "procedure newLoggingAgent of %s can't be loaded", plugin.str());
  68. return (IEspLogAgent*) xproc();
  69. }
  70. IEspLogEntry* CLoggingManager::createLogEntry()
  71. {
  72. return new CEspLogEntry();
  73. }
  74. bool CLoggingManager::updateLog(IEspLogEntry* entry, StringBuffer& status)
  75. {
  76. if (entry->getLogContent())
  77. return updateLog(entry->getEspContext(), entry->getOption(), entry->getLogContent(), status);
  78. if (entry->getLogInfoTree())
  79. return updateLog(entry->getEspContext(), entry->getOption(), entry->getLogInfoTree(), entry->getExtraLog(), status);
  80. return updateLog(entry->getEspContext(), entry->getOption(), entry->getUserContextTree(), entry->getUserRequestTree(),
  81. entry->getBackEndResp(), entry->getUserResp(), entry->getLogDatasets(), status);
  82. }
  83. bool CLoggingManager::updateLog(IEspContext* espContext, const char* option, const char* logContent, StringBuffer& status)
  84. {
  85. if (!initialized)
  86. throw MakeStringException(-1,"LoggingManager not initialized");
  87. bool bRet = false;
  88. try
  89. {
  90. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(nullptr, option, logContent);
  91. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  92. bRet = updateLog(espContext, *req, *resp, status);
  93. }
  94. catch (IException* e)
  95. {
  96. status.set("Failed to update log: ");
  97. e->errorMessage(status);
  98. ERRLOG("%s", status.str());
  99. e->Release();
  100. }
  101. return bRet;
  102. }
  103. bool CLoggingManager::updateLog(IEspContext* espContext, const char* option, IPropertyTree* logInfo, IInterface* extraLog, StringBuffer& status)
  104. {
  105. if (!initialized)
  106. throw MakeStringException(-1,"LoggingManager not initialized");
  107. bool bRet = false;
  108. try
  109. {
  110. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(nullptr, option, LINK(logInfo), LINK(extraLog));
  111. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  112. bRet = updateLog(espContext, *req, *resp, status);
  113. }
  114. catch (IException* e)
  115. {
  116. status.set("Failed to update log: ");
  117. e->errorMessage(status);
  118. ERRLOG("%s", status.str());
  119. e->Release();
  120. }
  121. return bRet;
  122. }
  123. bool CLoggingManager::updateLog(IEspContext* espContext, const char* option, IPropertyTree* userContext, IPropertyTree* userRequest,
  124. const char* backEndResp, const char* userResp, const char* logDatasets, StringBuffer& status)
  125. {
  126. if (!initialized)
  127. throw MakeStringException(-1,"LoggingManager not initialized");
  128. bool bRet = false;
  129. try
  130. {
  131. Owned<IPropertyTree> espContextTree;
  132. if (espContext)
  133. {
  134. espContextTree.setown(createPTree("ESPContext"));
  135. short port;
  136. StringBuffer sourceIP;
  137. const char* esdlBindingID = espContext->queryESDLBindingID();
  138. espContext->getServAddress(sourceIP, port);
  139. espContextTree->addProp("SourceIP", sourceIP.str());
  140. if (!isEmptyString(esdlBindingID))
  141. espContextTree->addProp("ESDLBindingID", esdlBindingID);
  142. //More information in espContext may be added to the espContextTree later.
  143. const char* userId = espContext->queryUserId();
  144. if (userId && *userId)
  145. espContextTree->addProp("UserName", userId);
  146. espContextTree->addProp("ResponseTime", VStringBuffer("%.4f", (msTick()-espContext->queryCreationTime())/1000.0));
  147. }
  148. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(nullptr, option, espContextTree.getClear(), LINK(userContext), LINK(userRequest),
  149. backEndResp, userResp, logDatasets);
  150. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  151. bRet = updateLog(espContext, *req, *resp, status);
  152. }
  153. catch (IException* e)
  154. {
  155. status.set("Failed to update log: ");
  156. e->errorMessage(status);
  157. ERRLOG("%s", status.str());
  158. e->Release();
  159. }
  160. return bRet;
  161. }
  162. bool CLoggingManager::updateLog(IEspContext* espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp, StringBuffer& status)
  163. {
  164. bool bRet = updateLog(espContext, req, resp);
  165. if (bRet)
  166. status.set("Log request has been sent.");
  167. else
  168. {
  169. const char* statusMsg = resp.getStatusMessage();
  170. if (statusMsg && *statusMsg)
  171. status.setf("Failed to update log: %s", statusMsg);
  172. else
  173. status.set("Failed to update log");
  174. }
  175. return bRet;
  176. }
  177. bool CLoggingManager::updateLog(IEspContext* espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp)
  178. {
  179. if (!initialized)
  180. throw MakeStringException(-1,"LoggingManager not initialized");
  181. bool bRet = false;
  182. try
  183. {
  184. if (espContext)
  185. espContext->addTraceSummaryTimeStamp(LogMin, "LMgr:startQLog");
  186. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  187. {
  188. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  189. if (loggingThread->hasService(LGSTUpdateLOG))
  190. {
  191. loggingThread->queueLog(&req);
  192. bRet = true;
  193. }
  194. }
  195. if (espContext)
  196. espContext->addTraceSummaryTimeStamp(LogMin, "LMgr:endQLog");
  197. }
  198. catch (IException* e)
  199. {
  200. StringBuffer errorStr;
  201. e->errorMessage(errorStr);
  202. ERRLOG("Failed to update log: %s",errorStr.str());
  203. resp.setStatusCode(-1);
  204. resp.setStatusMessage(errorStr.str());
  205. e->Release();
  206. }
  207. return bRet;
  208. }
  209. bool CLoggingManager::getTransactionSeed(StringBuffer& transactionSeed, StringBuffer& status)
  210. {
  211. if (!initialized)
  212. throw MakeStringException(-1,"LoggingManager not initialized");
  213. bool bRet = false;
  214. try
  215. {
  216. Owned<IEspGetTransactionSeedRequest> req = createGetTransactionSeedRequest();
  217. Owned<IEspGetTransactionSeedResponse> resp = createGetTransactionSeedResponse();
  218. transactionSeed.set("Seed");
  219. bRet = getTransactionSeed(*req, *resp);
  220. if (bRet && !resp->getStatusCode())
  221. {
  222. const char* seed = resp->getSeedId();
  223. if (!seed || !*seed)
  224. status.set("Failed to get Transaction Seed");
  225. else
  226. {
  227. transactionSeed.set(seed);
  228. status.set("Transaction Seed returned.");
  229. bRet = true;
  230. }
  231. }
  232. else
  233. {
  234. const char* statusMsg = resp->getStatusMessage();
  235. if (statusMsg && *statusMsg)
  236. status.setf("Failed to get Transaction Seed: %s", statusMsg);
  237. else
  238. status.set("Failed to get Transaction Seed");
  239. }
  240. }
  241. catch (IException* e)
  242. {
  243. e->errorMessage(status);
  244. status.insert(0, "Failed to get Transaction Seed: ");
  245. ERRLOG("%s",status.str());
  246. e->Release();
  247. }
  248. return bRet;
  249. }
  250. bool CLoggingManager::getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp)
  251. {
  252. if (!initialized)
  253. throw MakeStringException(-1,"LoggingManager not initialized");
  254. bool bRet = false;
  255. try
  256. {
  257. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  258. {
  259. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  260. if (!loggingThread->hasService(LGSTGetTransactionSeed))
  261. continue;
  262. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  263. bRet = loggingAgent->getTransactionSeed(req, resp);
  264. if (bRet)
  265. break;
  266. }
  267. }
  268. catch (IException* e)
  269. {
  270. StringBuffer errorStr;
  271. e->errorMessage(errorStr);
  272. ERRLOG("Failed to get Transaction Seed: %s",errorStr.str());
  273. resp.setStatusCode(-1);
  274. resp.setStatusMessage(errorStr.str());
  275. e->Release();
  276. }
  277. return bRet;
  278. }
  279. bool CLoggingManager::getTransactionID(StringAttrMapping* transFields, StringBuffer& transactionID, StringBuffer& status)
  280. {
  281. if (!initialized)
  282. throw MakeStringException(-1,"LoggingManager not initialized");
  283. try
  284. {
  285. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  286. {
  287. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  288. if (!loggingThread->hasService(LGSTGetTransactionID))
  289. continue;
  290. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  291. loggingAgent->getTransactionID(transFields, transactionID);
  292. if (!transactionID.isEmpty())
  293. ESPLOG(LogMax, "Got TransactionID '%s'", transactionID.str());
  294. return true;
  295. }
  296. }
  297. catch (IException* e)
  298. {
  299. e->errorMessage(status);
  300. e->Release();
  301. }
  302. return false;
  303. }
  304. extern "C"
  305. {
  306. LOGGINGMANAGER_API ILoggingManager* newLoggingManager()
  307. {
  308. return new CLoggingManager();
  309. }
  310. }