bizlog.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "stdafx.h"
  2. #include "bizlog.h"
  3. #include <memutil.h>
  4. #include <y2k_time.h>
  5. typedef struct session_t {
  6. char *id;
  7. FILE *fp;
  8. char file[MAX_PATH];
  9. }session_t;
  10. struct bizlog_t
  11. {
  12. char base_dir[MAX_PATH];
  13. session_t *curr_session;
  14. };
  15. static void session_destroy(session_t *session)
  16. {
  17. if (session) {
  18. char tmp[MAX_PATH];
  19. fclose(session->fp);
  20. session->fp = NULL;
  21. //strcpy(tmp, session->file);
  22. //strcat(tmp, ".xml");
  23. //MoveFileExA(session->file, tmp, MOVEFILE_REPLACE_EXISTING);
  24. free(session->id);
  25. free(session);
  26. }
  27. }
  28. bizlog_t *bizlog_create(const char *base_dir)
  29. {
  30. bizlog_t *log;
  31. if (!base_dir) {
  32. return NULL;
  33. }
  34. log = ZALLOC_T(bizlog_t);
  35. strcpy(log->base_dir, base_dir);
  36. return log;
  37. }
  38. void bizlog_destroy(bizlog_t *log)
  39. {
  40. if (log) {
  41. if (log->curr_session) {
  42. session_destroy(log->curr_session);
  43. log->curr_session = NULL;
  44. }
  45. free(log);
  46. }
  47. }
  48. int bizlog_has_session(bizlog_t *log)
  49. {
  50. return !!log->curr_session;
  51. }
  52. // <SessionBegin Time="" SessionID=""/>
  53. int bizlog_session_begin(bizlog_t *log, const char *id,const char*filename)
  54. {
  55. session_t *session;
  56. if (!log || !id)
  57. return -1;
  58. if (log->curr_session) {
  59. session_destroy(log->curr_session);
  60. log->curr_session = NULL;
  61. }
  62. session = ZALLOC_T(session_t);
  63. #ifdef RVC_OS_WIN
  64. sprintf(session->file, "%s\\%s.xml", log->base_dir, filename);
  65. #else
  66. sprintf(session->file, "%s/%s.xml", log->base_dir, filename);
  67. #endif
  68. session->fp = fopen(session->file, "wb+");
  69. if (!session->fp) {
  70. free(session);
  71. return -1;
  72. }
  73. session->id = _strdup(id);
  74. y2k_time_t now = y2k_time_now();
  75. fprintf(session->fp, "<SessionBegin Time=\"0x%08X\" SessionID=\"%s\"/>\r\n", now, id);
  76. fflush(session->fp);
  77. log->curr_session = session;
  78. return 0;
  79. }
  80. // <SessionEnd Time="" SessionID=""/>
  81. int bizlog_session_end(bizlog_t *log)
  82. {
  83. if (log->curr_session) {
  84. y2k_time_t now = y2k_time_now();
  85. fprintf(log->curr_session->fp, "<SessionEnd Time=\"0x%08X\" SessionID=\"%s\"/>\r\n", now, log->curr_session->id);
  86. fflush(log->curr_session->fp);
  87. fclose(log->curr_session->fp);
  88. log->curr_session->fp = NULL;
  89. session_destroy(log->curr_session);
  90. log->curr_session = NULL;
  91. }
  92. return 0;
  93. }
  94. // <Function Time=”” Display=”” ServiceCode=”” ActionID=”0xAABBCCDD”/>XXX</Function>
  95. int bizlog_function(bizlog_t *log, const char *name, const char *display_name, const char *product, const char *service_code, int action_id)
  96. {
  97. if (!log || !log->curr_session)
  98. return -1;
  99. y2k_time_t now = y2k_time_now();
  100. fprintf(log->curr_session->fp, "<Function Time=\"0x%08X\" Name=\"%s\" Product=\"%s\" Display=\"%s\" ServiceCode=\"%s\" ActionID=\"0x%08X\"/>\r\n", now, name, product, display_name, service_code, action_id);
  101. fflush(log->curr_session->fp);
  102. return 0;
  103. }
  104. // <CustomerID Time=”” Method=”RI” Level=”5” ActionID=”0xAABBCCDD”>XXX</CustomerID>
  105. int bizlog_call_id(bizlog_t *log, const char *call_id, const char *skillcode, const char *agent_id, int action_id)
  106. {
  107. if (!log || !log->curr_session)
  108. return -1;
  109. y2k_time_t now = y2k_time_now();
  110. fprintf(log->curr_session->fp, "<CallID Time=\"0x%08X\" ID=\"%s\" SkillCode=\"%s\" AgentID=\"%s\" ActionID=\"0x%08X\"/>\r\n", now, call_id, skillcode, agent_id, action_id);
  111. fflush(log->curr_session->fp);
  112. return 0;
  113. }
  114. // <CustomerID Time=”” Method=”RI” Level=”5” ActionID=”0xAABBCCDD”>XXX</CustomerID>
  115. int bizlog_customer_id(bizlog_t *log, const char *customer_id, const char *verify_method, int level, int action_id)
  116. {
  117. if (!log || !log->curr_session)
  118. return -1;
  119. y2k_time_t now = y2k_time_now();
  120. fprintf(log->curr_session->fp, "<CustomerID Time=\"0x%08X\" ID=\"%s\" Method=\"%s\" Level=\"%d\" ActionID=\"0x%08X\"/>\r\n", now, customer_id, verify_method, level, action_id);
  121. fflush(log->curr_session->fp);
  122. return 0;
  123. }
  124. // <Agreement Time=”” ActionID=”0xAABBCCDD”>确认要素</Agree>
  125. int bizlog_agreement(bizlog_t *log, const char *content, int action_id)
  126. {
  127. if (!log || !log->curr_session)
  128. return -1;
  129. y2k_time_t now = y2k_time_now();
  130. fprintf(log->curr_session->fp, "<Agreement Time=\"0x%08X\" ActionID=\"0x%08X\">%s</Agreement>\r\n", now, action_id, content);
  131. fflush(log->curr_session->fp);
  132. return 0;
  133. }
  134. // <Receipt Time=”” ActionID=”0xAABBCCDD” bDone=”1”>结果要素</Receipt>
  135. int bizlog_receipt(bizlog_t *log, const char *content, int action_id)
  136. {
  137. if (!log || !log->curr_session)
  138. return -1;
  139. y2k_time_t now = y2k_time_now();
  140. fprintf(log->curr_session->fp, "<Receipt Time=\"0x%08X\" ActionID=\"0x%08X\">%s</Receipt>\r\n", now, action_id, content);
  141. fflush(log->curr_session->fp);
  142. return 0;
  143. }
  144. // <Operation Time=”” Code=”” >xxxx</Operation>
  145. int bizlog_operation(bizlog_t *log, const char *op_code, const char *content, int action_id)
  146. {
  147. if (!log || !log->curr_session)
  148. return -1;
  149. y2k_time_t now = y2k_time_now();
  150. fprintf(log->curr_session->fp, "<Operation Time=\"0x%08X\" Code=\"%s\" ActionID=\"0x%08X\">%s</Operation>\r\n", now, op_code, action_id, content);
  151. fflush(log->curr_session->fp);
  152. return 0;
  153. }
  154. // <Response Time=”” Code=””>xxxx</Response>
  155. int bizlog_response(bizlog_t *log, const char *ret_code, const char *content, int action_id)
  156. {
  157. if (!log || !log->curr_session)
  158. return -1;
  159. y2k_time_t now = y2k_time_now();
  160. fprintf(log->curr_session->fp, "<Response Time=\"0x%08X\" Code=\"%s\" ActionID=\"0x%08X\">%s</Response>\r\n", now, ret_code, action_id, content);
  161. fflush(log->curr_session->fp);
  162. return 0;
  163. }
  164. // <Agent Time=”” Code=”” ActionID=”0xAABBCCDD”>XXX</Agent>
  165. int bizlog_agent(bizlog_t *log, const char *op_code, const char *content, int action_id)
  166. {
  167. if (!log || !log->curr_session)
  168. return -1;
  169. y2k_time_t now = y2k_time_now();
  170. fprintf(log->curr_session->fp, "<Agent Time=\"0x%08X\" Code=\"%s\" ActionID=\"0x%08X\">%s</Agent>\r\n", now, op_code, action_id, content);
  171. fflush(log->curr_session->fp);
  172. return 0;
  173. }