loggingmanager.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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(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(*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->addPropInt64("ResponseTime", (__int64)(msTick()-espContext.queryCreationTime())/1000);
  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(*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(IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp, StringBuffer& status)
  122. {
  123. bool bRet = updateLog(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(IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp)
  137. {
  138. if (!initialized)
  139. throw MakeStringException(-1,"LoggingManager not initialized");
  140. bool bRet = false;
  141. try
  142. {
  143. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  144. {
  145. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  146. if (loggingThread->hasService(LGSTUpdateLOG))
  147. {
  148. loggingThread->queueLog(&req);
  149. bRet = true;
  150. }
  151. }
  152. }
  153. catch (IException* e)
  154. {
  155. StringBuffer errorStr;
  156. e->errorMessage(errorStr);
  157. ERRLOG("Failed to update log: %s",errorStr.str());
  158. resp.setStatusCode(-1);
  159. resp.setStatusMessage(errorStr.str());
  160. e->Release();
  161. }
  162. return bRet;
  163. }
  164. bool CLoggingManager::getTransactionSeed(StringBuffer& transactionSeed, StringBuffer& status)
  165. {
  166. if (!initialized)
  167. throw MakeStringException(-1,"LoggingManager not initialized");
  168. bool bRet = false;
  169. try
  170. {
  171. Owned<IEspGetTransactionSeedRequest> req = createGetTransactionSeedRequest();
  172. Owned<IEspGetTransactionSeedResponse> resp = createGetTransactionSeedResponse();
  173. transactionSeed.set("Seed");
  174. bRet = getTransactionSeed(*req, *resp);
  175. if (bRet && !resp->getStatusCode())
  176. {
  177. const char* seed = resp->getSeedId();
  178. if (!seed || !*seed)
  179. status.set("Failed to get Transaction Seed");
  180. else
  181. {
  182. transactionSeed.set(seed);
  183. status.set("Transaction Seed returned.");
  184. bRet = true;
  185. }
  186. }
  187. else
  188. {
  189. const char* statusMsg = resp->getStatusMessage();
  190. if (statusMsg && *statusMsg)
  191. status.setf("Failed to get Transaction Seed: %s", statusMsg);
  192. else
  193. status.set("Failed to get Transaction Seed");
  194. }
  195. }
  196. catch (IException* e)
  197. {
  198. e->errorMessage(status);
  199. status.insert(0, "Failed to get Transaction Seed: ");
  200. ERRLOG("%s",status.str());
  201. e->Release();
  202. }
  203. return bRet;
  204. }
  205. bool CLoggingManager::getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp)
  206. {
  207. if (!initialized)
  208. throw MakeStringException(-1,"LoggingManager not initialized");
  209. bool bRet = false;
  210. try
  211. {
  212. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  213. {
  214. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  215. if (!loggingThread->hasService(LGSTGetTransactionSeed))
  216. continue;
  217. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  218. bRet = loggingAgent->getTransactionSeed(req, resp);
  219. if (bRet)
  220. break;
  221. }
  222. }
  223. catch (IException* e)
  224. {
  225. StringBuffer errorStr;
  226. e->errorMessage(errorStr);
  227. ERRLOG("Failed to get Transaction Seed: %s",errorStr.str());
  228. resp.setStatusCode(-1);
  229. resp.setStatusMessage(errorStr.str());
  230. e->Release();
  231. }
  232. return bRet;
  233. }
  234. bool CLoggingManager::getTransactionID(StringAttrMapping* transFields, StringBuffer& transactionID, StringBuffer& status)
  235. {
  236. if (!initialized)
  237. throw MakeStringException(-1,"LoggingManager not initialized");
  238. try
  239. {
  240. for (unsigned int x = 0; x < loggingAgentThreads.size(); x++)
  241. {
  242. IUpdateLogThread* loggingThread = loggingAgentThreads[x];
  243. if (!loggingThread->hasService(LGSTGetTransactionID))
  244. continue;
  245. IEspLogAgent* loggingAgent = loggingThread->getLogAgent();
  246. loggingAgent->getTransactionID(transFields, transactionID);
  247. return true;
  248. }
  249. }
  250. catch (IException* e)
  251. {
  252. e->errorMessage(status);
  253. e->Release();
  254. }
  255. return false;
  256. }
  257. extern "C"
  258. {
  259. LOGGINGMANAGER_API ILoggingManager* newLoggingManager()
  260. {
  261. return new CLoggingManager();
  262. }
  263. }