loggingmanager.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. bool CLoggingManager::updateLog(IEspContext& espContext, const char* option, const char* logContent, StringBuffer& status)
  71. {
  72. if (!initialized)
  73. throw MakeStringException(-1,"LoggingManager not initialized");
  74. bool bRet = false;
  75. try
  76. {
  77. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(NULL, option, logContent);
  78. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  79. bRet = updateLog(espContext, *req, *resp, status);
  80. }
  81. catch (IException* e)
  82. {
  83. e->errorMessage(status);
  84. status.insert(0, "Failed to update log: ");
  85. ERRLOG("%s", status.str());
  86. e->Release();
  87. }
  88. return bRet;
  89. }
  90. bool CLoggingManager::updateLog(const char* option, IEspContext& espContext, IPropertyTree* userContext, IPropertyTree* userRequest,
  91. const char* backEndResp, const char* userResp, const char* logDatasets, StringBuffer& status)
  92. {
  93. if (!initialized)
  94. throw MakeStringException(-1,"LoggingManager not initialized");
  95. bool bRet = false;
  96. try
  97. {
  98. short port;
  99. StringBuffer sourceIP;
  100. espContext.getServAddress(sourceIP, port);
  101. Owned<IPropertyTree> espContextTree = createPTree("ESPContext");
  102. espContextTree->addProp("SourceIP", sourceIP.str());
  103. const char* userId = espContext.queryUserId();
  104. if (userId && *userId)
  105. espContextTree->addProp("UserName", userId);
  106. espContextTree->addProp("ResponseTime", VStringBuffer("%.4f", (msTick()-espContext.queryCreationTime())/1000.0));
  107. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(NULL, option, espContextTree.getClear(), LINK(userContext), LINK(userRequest),
  108. backEndResp, userResp, logDatasets);
  109. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  110. bRet = updateLog(espContext, *req, *resp, status);
  111. }
  112. catch (IException* e)
  113. {
  114. e->errorMessage(status);
  115. status.insert(0, "Failed to update log: ");
  116. ERRLOG("%s", status.str());
  117. e->Release();
  118. }
  119. return bRet;
  120. }
  121. bool CLoggingManager::updateLog(IEspContext& espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp, StringBuffer& status)
  122. {
  123. bool bRet = updateLog(espContext, req, resp);
  124. if (bRet)
  125. status.set("Log request has been sent.");
  126. else
  127. {
  128. const char* statusMsg = resp.getStatusMessage();
  129. if (statusMsg && *statusMsg)
  130. status.setf("Failed to update log: %s", statusMsg);
  131. else
  132. status.set("Failed to update log");
  133. }
  134. return bRet;
  135. }
  136. bool CLoggingManager::updateLog(IEspContext& espContext, IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp)
  137. {
  138. if (!initialized)
  139. throw MakeStringException(-1,"LoggingManager not initialized");
  140. bool bRet = false;
  141. try
  142. {
  143. espContext.addTraceSummaryTimeStamp(LogMin, "LMgr:startQLog");
  144. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  145. {
  146. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  147. if (loggingThread->hasService(LGSTUpdateLOG))
  148. {
  149. loggingThread->queueLog(&req);
  150. bRet = true;
  151. }
  152. }
  153. espContext.addTraceSummaryTimeStamp(LogMin, "LMgr:endQLog");
  154. }
  155. catch (IException* e)
  156. {
  157. StringBuffer errorStr;
  158. e->errorMessage(errorStr);
  159. ERRLOG("Failed to update log: %s",errorStr.str());
  160. resp.setStatusCode(-1);
  161. resp.setStatusMessage(errorStr.str());
  162. e->Release();
  163. }
  164. return bRet;
  165. }
  166. bool CLoggingManager::getTransactionSeed(StringBuffer& transactionSeed, StringBuffer& status)
  167. {
  168. if (!initialized)
  169. throw MakeStringException(-1,"LoggingManager not initialized");
  170. bool bRet = false;
  171. try
  172. {
  173. Owned<IEspGetTransactionSeedRequest> req = createGetTransactionSeedRequest();
  174. Owned<IEspGetTransactionSeedResponse> resp = createGetTransactionSeedResponse();
  175. transactionSeed.set("Seed");
  176. bRet = getTransactionSeed(*req, *resp);
  177. if (bRet && !resp->getStatusCode())
  178. {
  179. const char* seed = resp->getSeedId();
  180. if (!seed || !*seed)
  181. status.set("Failed to get Transaction Seed");
  182. else
  183. {
  184. transactionSeed.set(seed);
  185. status.set("Transaction Seed returned.");
  186. bRet = true;
  187. }
  188. }
  189. else
  190. {
  191. const char* statusMsg = resp->getStatusMessage();
  192. if (statusMsg && *statusMsg)
  193. status.setf("Failed to get Transaction Seed: %s", statusMsg);
  194. else
  195. status.set("Failed to get Transaction Seed");
  196. }
  197. }
  198. catch (IException* e)
  199. {
  200. e->errorMessage(status);
  201. status.insert(0, "Failed to get Transaction Seed: ");
  202. ERRLOG("%s",status.str());
  203. e->Release();
  204. }
  205. return bRet;
  206. }
  207. bool CLoggingManager::getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp)
  208. {
  209. if (!initialized)
  210. throw MakeStringException(-1,"LoggingManager not initialized");
  211. bool bRet = false;
  212. try
  213. {
  214. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  215. {
  216. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  217. if (!loggingThread->hasService(LGSTGetTransactionSeed))
  218. continue;
  219. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  220. bRet = loggingAgent->getTransactionSeed(req, resp);
  221. if (bRet)
  222. break;
  223. }
  224. }
  225. catch (IException* e)
  226. {
  227. StringBuffer errorStr;
  228. e->errorMessage(errorStr);
  229. ERRLOG("Failed to get Transaction Seed: %s",errorStr.str());
  230. resp.setStatusCode(-1);
  231. resp.setStatusMessage(errorStr.str());
  232. e->Release();
  233. }
  234. return bRet;
  235. }
  236. bool CLoggingManager::getTransactionID(StringAttrMapping* transFields, StringBuffer& transactionID, StringBuffer& status)
  237. {
  238. if (!initialized)
  239. throw MakeStringException(-1,"LoggingManager not initialized");
  240. try
  241. {
  242. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  243. {
  244. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  245. if (!loggingThread->hasService(LGSTGetTransactionID))
  246. continue;
  247. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  248. loggingAgent->getTransactionID(transFields, transactionID);
  249. return true;
  250. }
  251. }
  252. catch (IException* e)
  253. {
  254. e->errorMessage(status);
  255. e->Release();
  256. }
  257. return false;
  258. }
  259. extern "C"
  260. {
  261. LOGGINGMANAGER_API ILoggingManager* newLoggingManager()
  262. {
  263. return new CLoggingManager();
  264. }
  265. }