mod_interactivelog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "Event.h"
  4. #ifndef _WIN32
  5. #include <time.h>
  6. #endif // !_WIN32
  7. #include "InteractiveLog_server_g.h"
  8. #include "../mod_customeraware/Event.h"
  9. using namespace InteractiveLog;
  10. #include "bizlog.h"
  11. class CInterativeLogEntity;
  12. class ServerSession : public LogService_ServerSessionBase
  13. {
  14. public:
  15. ServerSession(CInterativeLogEntity *pEntity) : m_pEntity(pEntity) {}
  16. virtual void Handle_LogFunction(SpOnewayCallContext<LogService_LogFunction_Info>::Pointer ctx);
  17. virtual void Handle_LogCustomerID(SpOnewayCallContext<LogService_LogCustomerID_Info>::Pointer ctx);
  18. virtual void Handle_LogAgreement(SpOnewayCallContext<LogService_LogAgreement_Info>::Pointer ctx);
  19. virtual void Handle_LogReceipt(SpOnewayCallContext<LogService_LogReceipt_Info>::Pointer ctx);
  20. virtual void Handle_LogOperation(SpOnewayCallContext<LogService_LogOperation_Info>::Pointer ctx);
  21. virtual void Handle_LogResponse(SpOnewayCallContext<LogService_LogResponse_Info>::Pointer ctx);
  22. virtual void Handle_LogAgent(SpOnewayCallContext<LogService_LogAgent_Info>::Pointer ctx);
  23. virtual void Handle_LogBegin(SpOnewayCallContext<LogService_LogBegin_Info>::Pointer ctx);
  24. virtual void Handle_LogEnd(SpOnewayCallContext<LogService_LogEnd_Info>::Pointer ctx);
  25. virtual void Handle_LogCallID(SpOnewayCallContext<LogService_LogCallID_Info>::Pointer ctx);
  26. private:
  27. CInterativeLogEntity *m_pEntity;
  28. };
  29. // 0x
  30. class CInterativeLogEntity : public CEntityBase, public ILogListener, public ISysVarListener
  31. {
  32. public:
  33. CInterativeLogEntity() : m_log(NULL) {}
  34. virtual ~CInterativeLogEntity() {}
  35. virtual const char *GetEntityName() const { return "InteractiveLog"; }
  36. virtual bool IsService()const{return true;}
  37. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  38. {
  39. ErrorCodeEnum Error;
  40. CSimpleStringA strPath;
  41. m_nDesktopType = 0;
  42. m_strSessionCount = "0";
  43. Error = GetFunction()->GetPath("InterLog", strPath);
  44. if (Error != Error_Succeed) {
  45. LOG_TRACE("getpath InterLog failed!");
  46. }
  47. m_log = bizlog_create((LPCSTR)strPath);
  48. if (!m_log)
  49. {
  50. Error = Error_Param;
  51. LOG_TRACE("create bizlog object failed!");
  52. }
  53. {
  54. CSmartPointer<IEntityFunction> Func = GetFunction();
  55. CSimpleStringA strValue;
  56. if (Func->RegistSysVarEvent(SYSVAR_DESKTOPTYPE, this) != Error_Succeed)
  57. {
  58. LOG_TRACE("register sysvar %s failed!", SYSVAR_DESKTOPTYPE);
  59. }
  60. else
  61. {
  62. Func->GetSysVar(SYSVAR_DESKTOPTYPE, strValue);
  63. if (strValue[0] ==DESKTOPTYPE_BUS) //切换到业务屏,打开LOG
  64. {
  65. m_nDesktopType = 0;
  66. }
  67. else if (strValue[0] == DESKTOPTYPE_INFO) //切换到信息屏,屏蔽LOG
  68. {
  69. m_nDesktopType = 1;
  70. }
  71. else if (strValue[0] == DESKTOPTYPE_USER) //切换到用户桌面,屏蔽LOG
  72. {
  73. m_nDesktopType = 2;
  74. }
  75. else
  76. {
  77. Dbg("unknown desktop type!");
  78. }
  79. }
  80. }
  81. /*{
  82. CSmartPointer<IEntityFunction> Func = GetFunction();
  83. CSimpleStringA strValue;
  84. if (Func->RegistSysVarEvent("SessionCount", this) != Error_Succeed)
  85. {
  86. LOG_TRACE("register sysvar %SessionCount failed!");
  87. }
  88. else
  89. {
  90. Func->GetSysVar("SessionCount", m_strSessionCount);
  91. Dbg("SessionCount = %s",m_strSessionCount);
  92. }
  93. }
  94. */
  95. GetFunction()->SubscribeLog(m_SubIDStopRecord, this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_CUSTOMERAWARE_END, NULL, false);
  96. pTransactionContext->SendAnswer(Error);
  97. }
  98. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  99. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  100. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
  101. {
  102. if (dwUserCode == EVENT_MOD_CUSTOMERAWARE_END)
  103. {
  104. ErrorCodeEnum errCode;
  105. errCode = GetFunction()->SetSysVar("CustomerID","N");
  106. if (errCode != Error_Succeed)
  107. {
  108. Dbg("set CustomerID failed (%d).",errCode);
  109. }
  110. else
  111. {
  112. Dbg("set customerid to N");
  113. }
  114. }
  115. }
  116. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  117. {
  118. if (m_log)
  119. {
  120. bizlog_destroy(m_log);
  121. m_log = NULL;
  122. }
  123. GetFunction()->UnsubscribeLog(m_SubIDStopRecord);
  124. pTransactionContext->SendAnswer(Error_Succeed);
  125. }
  126. virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  127. {
  128. if (_stricmp(pszKey, SYSVAR_DESKTOPTYPE) == 0)
  129. {
  130. Dbg("desktop type from : %c to %c", pszOldValue[0], pszValue[0]);
  131. if ((pszOldValue[0]==DESKTOPTYPE_BUS)) //如果从业务屏切换到其他屏,自动关闭当前interlog
  132. {
  133. LogEnd();
  134. }
  135. if (pszValue[0] == DESKTOPTYPE_BUS) //切换到业务屏,打开LOG
  136. {
  137. m_nDesktopType = 0;
  138. }
  139. else if (pszValue[0] == DESKTOPTYPE_INFO) //切换到信息屏,屏蔽LOG
  140. {
  141. m_nDesktopType = 1;
  142. }
  143. else if (pszValue[0] == DESKTOPTYPE_USER) //切换到用户桌面,屏蔽LOG
  144. {
  145. m_nDesktopType = 2;
  146. }
  147. else
  148. {
  149. Dbg("unknown desktop type!");
  150. }
  151. }
  152. //else if (_stricmp(pszKey, "SessionCount") == 0)
  153. //{
  154. // GetFunction()->GetSysVar("SessionCount", m_strSessionCount);
  155. // Dbg("SessionCount = %s",m_strSessionCount);
  156. //}
  157. }
  158. virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/)
  159. {
  160. LOG_FUNCTION();
  161. return new ServerSession(this);
  162. }
  163. void LogFunction(SpOnewayCallContext<LogService_LogFunction_Info>::Pointer ctx)
  164. {
  165. //Prepare();
  166. if (m_nDesktopType != 0) //如果不是业务状态
  167. {
  168. Dbg("LogFunction,desktop type is not B,ignore");
  169. return;
  170. }
  171. CSimpleStringA name = CSimpleStringW2A(ctx->Info.nam);
  172. CSimpleStringA display = CSimpleStringW2A(ctx->Info.display);
  173. CSimpleStringA product = CSimpleStringW2A(ctx->Info.product);
  174. CSimpleStringA service_code = CSimpleStringW2A(ctx->Info.service_code);
  175. bizlog_function(m_log, name, display, product, service_code, ctx->Info.action_id);
  176. }
  177. void LogCustomerID(SpOnewayCallContext<LogService_LogCustomerID_Info>::Pointer ctx)
  178. {
  179. if (m_nDesktopType != 0) //如果不是业务状态
  180. {
  181. Dbg("LogCustomerID,desktop type is not B,ignore");
  182. return;
  183. }
  184. //Prepare();
  185. CSimpleStringA customer_id = CSimpleStringW2A(ctx->Info.customer_id);
  186. ErrorCodeEnum errCode;
  187. errCode = GetFunction()->SetSysVar("CustomerID",customer_id);
  188. Dbg("set CustomerID to %s",customer_id);
  189. if (errCode != Error_Succeed)
  190. {
  191. Dbg("set CustomerID failed (%d).",errCode);
  192. }
  193. CSimpleStringA verify_method = CSimpleStringW2A(ctx->Info.verify_method);
  194. bizlog_customer_id(m_log, customer_id, verify_method, ctx->Info.level, ctx->Info.action_id);
  195. }
  196. void LogAgreement(SpOnewayCallContext<LogService_LogAgreement_Info>::Pointer ctx)
  197. {
  198. if (m_nDesktopType != 0) //如果不是业务状态
  199. {
  200. Dbg("LogAgreement,desktop type is not B,ignore");
  201. return;
  202. }
  203. //Prepare();
  204. CSimpleStringA content = CSimpleStringW2A(ctx->Info.content);
  205. bizlog_agreement(m_log, content, ctx->Info.action_id);
  206. }
  207. void LogReceipt(SpOnewayCallContext<LogService_LogReceipt_Info>::Pointer ctx)
  208. {
  209. if (m_nDesktopType != 0) //如果不是业务状态
  210. {
  211. Dbg("LogReceipt,desktop type is not B,ignore");
  212. return;
  213. }
  214. //Prepare();
  215. CSimpleStringA content = CSimpleStringW2A(ctx->Info.content);
  216. bizlog_receipt(m_log, content, ctx->Info.action_id);
  217. }
  218. void LogOperation(SpOnewayCallContext<LogService_LogOperation_Info>::Pointer ctx)
  219. {
  220. if (m_nDesktopType != 0) //如果不是业务状态
  221. {
  222. Dbg("LogOperation,desktop type is not B,ignore");
  223. return;
  224. }
  225. //Prepare();
  226. CSimpleStringA code = CSimpleStringW2A(ctx->Info.code);
  227. CSimpleStringA content = CSimpleStringW2A(ctx->Info.content);
  228. bizlog_operation(m_log, code, content, ctx->Info.action_id);
  229. }
  230. void LogResponse(SpOnewayCallContext<LogService_LogResponse_Info>::Pointer ctx)
  231. {
  232. if (m_nDesktopType != 0) //如果不是业务状态
  233. {
  234. Dbg("LogResponse,desktop type is not B,ignore");
  235. return;
  236. }
  237. //Prepare();
  238. CSimpleStringA code = CSimpleStringW2A(ctx->Info.code);
  239. CSimpleStringA content = CSimpleStringW2A(ctx->Info.content);
  240. bizlog_response(m_log, code, content, ctx->Info.action_id);
  241. }
  242. void LogAgent(SpOnewayCallContext<LogService_LogAgent_Info>::Pointer ctx)
  243. {
  244. if (m_nDesktopType != 0) //如果不是业务状态
  245. {
  246. Dbg("LogAgent,desktop type is not B,ignore");
  247. return;
  248. }
  249. //Prepare();
  250. CSimpleStringA code = CSimpleStringW2A(ctx->Info.code);
  251. CSimpleStringA content = CSimpleStringW2A(ctx->Info.content);
  252. bizlog_agent(m_log, code, content, ctx->Info.action_id);
  253. }
  254. void LogBegin(SpOnewayCallContext<LogService_LogBegin_Info>::Pointer ctx)
  255. {
  256. if (m_nDesktopType != 0) //如果不是业务状态
  257. {
  258. Dbg("LogBegin,desktop type is not B,ignore");
  259. return;
  260. }
  261. //Prepare();
  262. Dbg("log Begin get sessionid and sessioncnt");
  263. CSimpleStringA session_id = CSimpleStringW2A(ctx->Info.session_id);
  264. //文件名定为YYYYMMDD_XXX_SessionID。xxx为业务浏览器记录的运行时SessionID变化次数
  265. char strtimebuffer[256] = {0};
  266. #ifdef RVC_OS_WIN
  267. SYSTEMTIME nowTime; // 系统时间结构体
  268. GetLocalTime(&nowTime);
  269. sprintf_s(strtimebuffer, "%4d%02d%02d", nowTime.wYear, nowTime.wMonth, nowTime.wDay);
  270. #else
  271. struct tm* t;
  272. time_t tt;
  273. time(&tt);
  274. t = localtime(&tt);
  275. snprintf(strtimebuffer, 256, "%4d%02d%02d", t->tm_year+1900, t->tm_mon+1, t->tm_mday);
  276. #endif
  277. CSimpleStringA strTime = strtimebuffer;
  278. m_strSessionCount = CSimpleStringA::Format("%d",ctx->Info.session_count);
  279. Dbg("SessionCount = %s",m_strSessionCount);
  280. CSimpleStringA filename = strTime+"_"+m_strSessionCount+"_"+session_id;
  281. bizlog_session_begin(m_log, session_id,filename);
  282. Dbg("Begin new log -- %s",filename);
  283. }
  284. void LogEnd(SpOnewayCallContext<LogService_LogEnd_Info>::Pointer ctx)
  285. {
  286. if (m_nDesktopType != 0) //如果不是业务状态
  287. {
  288. Dbg("LogEnd,desktop type is not B,ignore");
  289. return;
  290. }
  291. //Prepare();
  292. bizlog_session_end(m_log);
  293. Dbg("End log");
  294. }
  295. void LogEnd()
  296. {
  297. bizlog_session_end(m_log);
  298. Dbg("Desktop type change to !B,Auto End log");
  299. }
  300. void LogCallID(SpOnewayCallContext<LogService_LogCallID_Info>::Pointer ctx)
  301. {
  302. if (m_nDesktopType != 0) //如果不是业务状态
  303. {
  304. Dbg("LogCallID,desktop type is not B,ignore");
  305. return;
  306. }
  307. CSimpleStringA callid = CSimpleStringW2A(ctx->Info.call_id);
  308. CSimpleStringA agentid = CSimpleStringW2A(ctx->Info.workNumber);
  309. CSimpleStringA skillCode = CSimpleStringW2A(ctx->Info.skillCode);
  310. bizlog_call_id(m_log, callid, skillCode, agentid, ctx->Info.action_id);
  311. }
  312. void Prepare()
  313. {
  314. //if (m_log)
  315. //{
  316. // if (!bizlog_has_session(m_log))
  317. // {
  318. // m_uid = CUUID::Create(m_uid);
  319. // bizlog_session_begin(m_log, m_uid.ToString());
  320. // }
  321. //}
  322. }
  323. private:
  324. CUUID m_SubIDStopRecord;
  325. bizlog_t *m_log;
  326. int m_nDesktopType; //0:业务屏,1:信息屏,2:用户桌面
  327. CSimpleStringA m_strSessionCount;
  328. };
  329. void ServerSession::Handle_LogFunction(SpOnewayCallContext<LogService_LogFunction_Info>::Pointer ctx)
  330. {
  331. m_pEntity->LogFunction(ctx);
  332. }
  333. void ServerSession::Handle_LogCustomerID(SpOnewayCallContext<LogService_LogCustomerID_Info>::Pointer ctx)
  334. {
  335. m_pEntity->LogCustomerID(ctx);
  336. }
  337. void ServerSession::Handle_LogAgreement(SpOnewayCallContext<LogService_LogAgreement_Info>::Pointer ctx)
  338. {
  339. m_pEntity->LogAgreement(ctx);
  340. }
  341. void ServerSession::Handle_LogReceipt(SpOnewayCallContext<LogService_LogReceipt_Info>::Pointer ctx)
  342. {
  343. m_pEntity->LogReceipt(ctx);
  344. }
  345. void ServerSession::Handle_LogOperation(SpOnewayCallContext<LogService_LogOperation_Info>::Pointer ctx)
  346. {
  347. m_pEntity->LogOperation(ctx);
  348. }
  349. void ServerSession::Handle_LogResponse(SpOnewayCallContext<LogService_LogResponse_Info>::Pointer ctx)
  350. {
  351. m_pEntity->LogResponse(ctx);
  352. }
  353. void ServerSession::Handle_LogAgent(SpOnewayCallContext<LogService_LogAgent_Info>::Pointer ctx)
  354. {
  355. m_pEntity->LogAgent(ctx);
  356. }
  357. void ServerSession::Handle_LogBegin(SpOnewayCallContext<LogService_LogBegin_Info>::Pointer ctx)
  358. {
  359. m_pEntity->LogBegin(ctx);
  360. }
  361. void ServerSession::Handle_LogEnd(SpOnewayCallContext<LogService_LogEnd_Info>::Pointer ctx)
  362. {
  363. m_pEntity->LogEnd(ctx);
  364. }
  365. void ServerSession::Handle_LogCallID(SpOnewayCallContext<LogService_LogCallID_Info>::Pointer ctx)
  366. {
  367. m_pEntity->LogCallID(ctx);
  368. }
  369. SP_BEGIN_ENTITY_MAP()
  370. SP_ENTITY(CInterativeLogEntity)
  371. SP_END_ENTITY_MAP()