loggingmanager.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. return !loggingAgentThreads.empty();
  55. }
  56. typedef IEspLogAgent* (*newLoggingAgent_t_)();
  57. IEspLogAgent* CLoggingManager::loadLoggingAgent(const char* name, const char* dll, const char* service, IPropertyTree* cfg)
  58. {
  59. StringBuffer plugin;
  60. plugin.append(SharedObjectPrefix).append(dll).append(SharedObjectExtension);
  61. HINSTANCE loggingAgentLib = LoadSharedObject(plugin.str(), true, false);
  62. if(!loggingAgentLib)
  63. throw MakeStringException(EspLoggingErrors::LoadLoggingLibraryError, "can't load library %s", plugin.str());
  64. newLoggingAgent_t_ xproc = (newLoggingAgent_t_)GetSharedProcedure(loggingAgentLib, "newLoggingAgent");
  65. if (!xproc)
  66. throw MakeStringException(EspLoggingErrors::LoadLoggingLibraryError, "procedure newLoggingAgent of %s can't be loaded", plugin.str());
  67. return (IEspLogAgent*) xproc();
  68. }
  69. bool CLoggingManager::updateLog(const char* option, const char* logContent, StringBuffer& status)
  70. {
  71. bool bRet = false;
  72. try
  73. {
  74. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(NULL, option, logContent);
  75. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  76. bRet = updateLog(*req, *resp, status);
  77. }
  78. catch (IException* e)
  79. {
  80. e->errorMessage(status);
  81. status.insert(0, "Failed to update log: ");
  82. ERRLOG("%s", status.str());
  83. e->Release();
  84. }
  85. return bRet;
  86. }
  87. bool CLoggingManager::updateLog(const char* option, IEspContext& espContext, IPropertyTree* userContext, IPropertyTree* userRequest,
  88. const char* backEndResp, const char* userResp, StringBuffer& status)
  89. {
  90. bool bRet = false;
  91. try
  92. {
  93. short port;
  94. StringBuffer sourceIP;
  95. espContext.getServAddress(sourceIP, port);
  96. Owned<IPropertyTree> espContextTree = createPTree("ESPContext");
  97. espContextTree->addProp("SourceIP", sourceIP.str());
  98. const char* userId = espContext.queryUserId();
  99. if (userId && *userId)
  100. espContextTree->addProp("UserName", userId);
  101. Owned<IEspUpdateLogRequestWrap> req = new CUpdateLogRequestWrap(NULL, option, espContextTree.getClear(), LINK(userContext), LINK(userRequest),
  102. backEndResp, userResp);
  103. Owned<IEspUpdateLogResponse> resp = createUpdateLogResponse();
  104. bRet = updateLog(*req, *resp, status);
  105. }
  106. catch (IException* e)
  107. {
  108. e->errorMessage(status);
  109. status.insert(0, "Failed to update log: ");
  110. ERRLOG("%s", status.str());
  111. e->Release();
  112. }
  113. return bRet;
  114. }
  115. bool CLoggingManager::updateLog(IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp, StringBuffer& status)
  116. {
  117. bool bRet = updateLog(req, resp);
  118. if (bRet)
  119. status.set("Log request has been sent.");
  120. else
  121. {
  122. const char* statusMsg = resp.getStatusMessage();
  123. if (statusMsg && *statusMsg)
  124. status.setf("Failed to update log: %s", statusMsg);
  125. else
  126. status.set("Failed to update log");
  127. }
  128. return bRet;
  129. }
  130. bool CLoggingManager::updateLog(IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp)
  131. {
  132. bool bRet = false;
  133. try
  134. {
  135. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  136. {
  137. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  138. if (loggingThread->hasService(LGSTUpdateLOG))
  139. {
  140. loggingThread->queueLog(&req);
  141. bRet = true;
  142. }
  143. }
  144. }
  145. catch (IException* e)
  146. {
  147. StringBuffer errorStr;
  148. e->errorMessage(errorStr);
  149. ERRLOG("Failed to update log: %s",errorStr.str());
  150. resp.setStatusCode(-1);
  151. resp.setStatusMessage(errorStr.str());
  152. e->Release();
  153. }
  154. return bRet;
  155. }
  156. bool CLoggingManager::getTransactionSeed(StringBuffer& transactionSeed, StringBuffer& status)
  157. {
  158. bool bRet = false;
  159. try
  160. {
  161. Owned<IEspGetTransactionSeedRequest> req = createGetTransactionSeedRequest();
  162. Owned<IEspGetTransactionSeedResponse> resp = createGetTransactionSeedResponse();
  163. transactionSeed.set("Seed");
  164. bRet = getTransactionSeed(*req, *resp);
  165. if (bRet && !resp->getStatusCode())
  166. {
  167. const char* seed = resp->getSeedId();
  168. if (!seed || !*seed)
  169. status.set("Failed to get Transaction Seed");
  170. else
  171. {
  172. transactionSeed.set(seed);
  173. status.set("Transaction Seed returned.");
  174. bRet = true;
  175. }
  176. }
  177. else
  178. {
  179. const char* statusMsg = resp->getStatusMessage();
  180. if (statusMsg && *statusMsg)
  181. status.setf("Failed to get Transaction Seed: %s", statusMsg);
  182. else
  183. status.set("Failed to get Transaction Seed");
  184. }
  185. }
  186. catch (IException* e)
  187. {
  188. e->errorMessage(status);
  189. status.insert(0, "Failed to get Transaction Seed: ");
  190. ERRLOG("%s",status.str());
  191. e->Release();
  192. }
  193. return bRet;
  194. }
  195. bool CLoggingManager::getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp)
  196. {
  197. bool bRet = false;
  198. try
  199. {
  200. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  201. {
  202. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  203. if (!loggingThread->hasService(LGSTGetTransactionSeed))
  204. continue;
  205. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  206. bRet = loggingAgent->getTransactionSeed(req, resp);
  207. if (bRet)
  208. break;
  209. }
  210. }
  211. catch (IException* e)
  212. {
  213. StringBuffer errorStr;
  214. e->errorMessage(errorStr);
  215. ERRLOG("Failed to get Transaction Seed: %s",errorStr.str());
  216. resp.setStatusCode(-1);
  217. resp.setStatusMessage(errorStr.str());
  218. e->Release();
  219. }
  220. return bRet;
  221. }
  222. extern "C"
  223. {
  224. LOGGINGMANAGER_API ILoggingManager* newLoggingManager()
  225. {
  226. return new CLoggingManager();
  227. }
  228. }