loggingmanager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. espContext->getServAddress(sourceIP, port);
  138. espContextTree->addProp("SourceIP", sourceIP.str());
  139. const char* userId = espContext->queryUserId();
  140. if (userId && *userId)
  141. espContextTree->addProp("UserName", userId);
  142. espContextTree->addProp("ResponseTime", VStringBuffer("%.4f", (msTick()-espContext->queryCreationTime())/1000.0));
  143. }
  144. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(nullptr, option, espContextTree.getClear(), LINK(userContext), LINK(userRequest),
  145. backEndResp, userResp, logDatasets);
  146. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  147. bRet = updateLog(espContext, *req, *resp, status);
  148. }
  149. catch (IException* e)
  150. {
  151. status.set("Failed to update log: ");
  152. e->errorMessage(status);
  153. ERRLOG("%s", status.str());
  154. e->Release();
  155. }
  156. return bRet;
  157. }
  158. bool CLoggingManager::updateLog(IEspContext* espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp, StringBuffer& status)
  159. {
  160. bool bRet = updateLog(espContext, req, resp);
  161. if (bRet)
  162. status.set("Log request has been sent.");
  163. else
  164. {
  165. const char* statusMsg = resp.getStatusMessage();
  166. if (statusMsg && *statusMsg)
  167. status.setf("Failed to update log: %s", statusMsg);
  168. else
  169. status.set("Failed to update log");
  170. }
  171. return bRet;
  172. }
  173. bool CLoggingManager::updateLog(IEspContext* espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp)
  174. {
  175. if (!initialized)
  176. throw MakeStringException(-1,"LoggingManager not initialized");
  177. bool bRet = false;
  178. try
  179. {
  180. if (espContext)
  181. espContext->addTraceSummaryTimeStamp(LogMin, "LMgr:startQLog");
  182. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  183. {
  184. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  185. if (loggingThread->hasService(LGSTUpdateLOG))
  186. {
  187. loggingThread->queueLog(&req);
  188. bRet = true;
  189. }
  190. }
  191. if (espContext)
  192. espContext->addTraceSummaryTimeStamp(LogMin, "LMgr:endQLog");
  193. }
  194. catch (IException* e)
  195. {
  196. StringBuffer errorStr;
  197. e->errorMessage(errorStr);
  198. ERRLOG("Failed to update log: %s",errorStr.str());
  199. resp.setStatusCode(-1);
  200. resp.setStatusMessage(errorStr.str());
  201. e->Release();
  202. }
  203. return bRet;
  204. }
  205. bool CLoggingManager::getTransactionSeed(StringBuffer& transactionSeed, StringBuffer& status)
  206. {
  207. if (!initialized)
  208. throw MakeStringException(-1,"LoggingManager not initialized");
  209. bool bRet = false;
  210. try
  211. {
  212. Owned<IEspGetTransactionSeedRequest> req = createGetTransactionSeedRequest();
  213. Owned<IEspGetTransactionSeedResponse> resp = createGetTransactionSeedResponse();
  214. transactionSeed.set("Seed");
  215. bRet = getTransactionSeed(*req, *resp);
  216. if (bRet && !resp->getStatusCode())
  217. {
  218. const char* seed = resp->getSeedId();
  219. if (!seed || !*seed)
  220. status.set("Failed to get Transaction Seed");
  221. else
  222. {
  223. transactionSeed.set(seed);
  224. status.set("Transaction Seed returned.");
  225. bRet = true;
  226. }
  227. }
  228. else
  229. {
  230. const char* statusMsg = resp->getStatusMessage();
  231. if (statusMsg && *statusMsg)
  232. status.setf("Failed to get Transaction Seed: %s", statusMsg);
  233. else
  234. status.set("Failed to get Transaction Seed");
  235. }
  236. }
  237. catch (IException* e)
  238. {
  239. e->errorMessage(status);
  240. status.insert(0, "Failed to get Transaction Seed: ");
  241. ERRLOG("%s",status.str());
  242. e->Release();
  243. }
  244. return bRet;
  245. }
  246. bool CLoggingManager::getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp)
  247. {
  248. if (!initialized)
  249. throw MakeStringException(-1,"LoggingManager not initialized");
  250. bool bRet = false;
  251. try
  252. {
  253. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  254. {
  255. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  256. if (!loggingThread->hasService(LGSTGetTransactionSeed))
  257. continue;
  258. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  259. bRet = loggingAgent->getTransactionSeed(req, resp);
  260. if (bRet)
  261. break;
  262. }
  263. }
  264. catch (IException* e)
  265. {
  266. StringBuffer errorStr;
  267. e->errorMessage(errorStr);
  268. ERRLOG("Failed to get Transaction Seed: %s",errorStr.str());
  269. resp.setStatusCode(-1);
  270. resp.setStatusMessage(errorStr.str());
  271. e->Release();
  272. }
  273. return bRet;
  274. }
  275. bool CLoggingManager::getTransactionID(StringAttrMapping* transFields, StringBuffer& transactionID, StringBuffer& status)
  276. {
  277. if (!initialized)
  278. throw MakeStringException(-1,"LoggingManager not initialized");
  279. try
  280. {
  281. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  282. {
  283. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  284. if (!loggingThread->hasService(LGSTGetTransactionID))
  285. continue;
  286. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  287. loggingAgent->getTransactionID(transFields, transactionID);
  288. return true;
  289. }
  290. }
  291. catch (IException* e)
  292. {
  293. e->errorMessage(status);
  294. e->Release();
  295. }
  296. return false;
  297. }
  298. extern "C"
  299. {
  300. LOGGINGMANAGER_API ILoggingManager* newLoggingManager()
  301. {
  302. return new CLoggingManager();
  303. }
  304. }