log_api.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "RestfulFunc.h"
  2. #include "JsonConvertHelper.hpp"
  3. #include <sstream>
  4. #include <string>
  5. #include <locale>
  6. #include "log_util.h"
  7. #include "log_api.h"
  8. #include <string.h>
  9. #include "sds.h"
  10. #include "inner_log.h"
  11. //#define GBK_COMPACT
  12. static int is_utf8(const char* str)
  13. {
  14. int i;
  15. const unsigned char* bytes = (const unsigned char*)str;
  16. int num;
  17. if (str == NULL)
  18. return 1;
  19. #if 0
  20. while (*bytes != 0x00) {
  21. if ((*bytes & 0x80) == 0x00) {
  22. // U+0000 to U+007F
  23. num = 1;
  24. } else if ((*bytes & 0xE0) == 0xC0) {
  25. // U+0080 to U+07FF
  26. num = 2;
  27. } else if ((*bytes & 0xF0) == 0xE0) {
  28. // U+0800 to U+FFFF
  29. num = 3;
  30. } else if ((*bytes & 0xF8) == 0xF0) {
  31. // U+10000 to U+10FFFF
  32. num = 4;
  33. } else {
  34. return 0;
  35. }
  36. bytes += 1;
  37. for (i = 1; i < num; ++i) {
  38. if ((*bytes & 0xC0) != 0x80)
  39. return 0;
  40. bytes += 1;
  41. }
  42. }
  43. return 1;
  44. #else
  45. while (*bytes) {
  46. if ((// ASCII
  47. // use bytes[0] <= 0x7F to allow ASCII control characters
  48. bytes[0] == 0x09 ||
  49. bytes[0] == 0x0A ||
  50. bytes[0] == 0x0D ||
  51. (0x20 <= bytes[0] && bytes[0] <= 0x7E)
  52. )
  53. ) {
  54. bytes += 1;
  55. continue;
  56. }
  57. if ((// non-overlong 2-byte
  58. (0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
  59. (0x80 <= bytes[1] && bytes[1] <= 0xBF)
  60. )
  61. ) {
  62. bytes += 2;
  63. continue;
  64. }
  65. if ((// excluding overlongs
  66. bytes[0] == 0xE0 &&
  67. (0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
  68. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  69. ) ||
  70. (// straight 3-byte
  71. ((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
  72. bytes[0] == 0xEE ||
  73. bytes[0] == 0xEF) &&
  74. (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
  75. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  76. ) ||
  77. (// excluding surrogates
  78. bytes[0] == 0xED &&
  79. (0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
  80. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  81. )
  82. ) {
  83. bytes += 3;
  84. continue;
  85. }
  86. if ((// planes 1-3
  87. bytes[0] == 0xF0 &&
  88. (0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
  89. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  90. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  91. ) ||
  92. (// planes 4-15
  93. (0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
  94. (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
  95. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  96. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  97. ) ||
  98. (// plane 16
  99. bytes[0] == 0xF4 &&
  100. (0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
  101. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  102. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  103. )
  104. ) {
  105. bytes += 4;
  106. continue;
  107. }
  108. return 0;
  109. }
  110. return 1;
  111. #endif
  112. }
  113. std::wstring s2w(const std::string str)
  114. {
  115. if (str.empty()) {
  116. return L"";
  117. }
  118. std::wstring w_str(L"");
  119. const char* origin = setlocale(LC_CTYPE, NULL);
  120. unsigned len = str.size() + 1;
  121. setlocale(LC_CTYPE, "zh_CN.gbk");
  122. wchar_t* p = new wchar_t[len];
  123. if (-1 != mbstowcs(p, str.c_str(), len)) {
  124. w_str = p;
  125. } else {
  126. aos_error_log((LB, "s2w::mbstowcs failed"));
  127. }
  128. delete[] p;
  129. setlocale(LC_CTYPE, origin);
  130. return w_str;
  131. }
  132. std::string w2utf8(const std::wstring wstr)
  133. {
  134. if (wstr.empty()) {
  135. return "";
  136. }
  137. std::string str("");
  138. char* origin = setlocale(LC_CTYPE, NULL);
  139. unsigned len = wstr.size() + 1;
  140. setlocale(LC_CTYPE, "zh_CN.utf8");
  141. char* p = new char[len];
  142. if (-1 != wcstombs(p, wstr.c_str(), len)) {
  143. str = p;
  144. } else {
  145. aos_error_log((LB, "w2utf8::wcstombs failed"));
  146. }
  147. setlocale(LC_CTYPE, origin);
  148. return str;
  149. }
  150. std::string gbk2utf8(const std::string str)
  151. {
  152. std::wstring wstr = s2w(str);
  153. return w2utf8(wstr);
  154. }
  155. /*
  156. if (result->statusCode / 100 == 2)
  157. {
  158. return LOG_SEND_OK;
  159. }
  160. if (result->statusCode <= 0)
  161. {
  162. return LOG_SEND_NETWORK_ERROR;
  163. }
  164. if (result->statusCode >= 500 || result->requestID == NULL)
  165. {
  166. return LOG_SEND_SERVER_ERROR;
  167. }
  168. if (result->statusCode == 403)
  169. {
  170. return LOG_SEND_QUOTA_EXCEED;
  171. }
  172. if (result->statusCode == 401 || result->statusCode == 404)
  173. {
  174. return LOG_SEND_UNAUTHORIZED;
  175. }
  176. if (result->errorMessage != NULL && strstr(result->errorMessage, LOGE_TIME_EXPIRED) != NULL)
  177. {
  178. return LOG_SEND_TIME_ERROR;
  179. }
  180. return LOG_SEND_DISCARD_ERROR;
  181. */
  182. int LOG_OS_TestLogPost(const char* url, const char* body)
  183. {
  184. HttpClientResponseResult result;
  185. HttpClientRequestConfig config(HttpRequestMethod::POST, url);
  186. std::string str((const char*)body);
  187. config.SetJsonBody(str.c_str());
  188. RestfulClient client = RestfulClient::getInstance();
  189. config.PreDo();
  190. client.Do(&config, &result);
  191. return 0;
  192. }
  193. int LOG_OS_HttpPost(const char* url,
  194. char** header_array,
  195. int header_count,
  196. const void* data,
  197. int data_len, const char* channelId, const char* token, const char* terminalno, const char* reserve1)
  198. {
  199. int retCode = 480;
  200. HttpClientResponseResult result;
  201. HttpClientRequestConfig config(HttpRequestMethod::POST, url);
  202. config.SetTimeout(60);
  203. std::string str((const char*)data);
  204. #ifdef GBK_COMPACT
  205. if (!str.empty() && !is_utf8(str.c_str())) {
  206. std::string t = gbk2utf8(str);
  207. str = t;
  208. }
  209. #endif //GBK_COMPACT
  210. config.SetJsonBody(str.c_str());
  211. RestfulClient client = RestfulClient::getInstance();
  212. config.PreDo();
  213. client.Do(&config, &result);
  214. if (result.ResponseOK()) {
  215. retCode = 481;
  216. struct CommResponse
  217. {
  218. bool success;
  219. std::string errorCode;
  220. std::string returnCode;
  221. std::string errorMsg;
  222. std::string message;
  223. JSONCONVERT2OBJECT_MEMEBER_REGISTER(success, errorCode, returnCode, errorMsg, message)
  224. JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("success", "code", "return_code", "error_msg", "message")
  225. } responseIns;
  226. Json::Value rawRoot;
  227. if (GetJsonRootObject(rawRoot, result.content) && Json2Object(responseIns, rawRoot)) {
  228. if (responseIns.success) {
  229. retCode = 200;
  230. } else {
  231. if (responseIns.errorCode.find("RTI1002") != -1)
  232. retCode = 300;
  233. else if (!is_utf8((const char*)str.c_str())) {
  234. aos_warn_log((LB, "detect that content is not utf8-type ,skip it: %s", str.c_str()));
  235. retCode = 200;
  236. }
  237. }
  238. } else {
  239. aos_error_log((LB, "http %s extract failed: %s", url, result.content.c_str()));
  240. aos_info_log((LB, "content len: %d", data_len));
  241. }
  242. } else {
  243. retCode = result.statusCode;
  244. }
  245. return retCode;
  246. }
  247. extern long LOG_GET_TIME();
  248. log_status_t sls_log_init(int32_t log_global_flag)
  249. {
  250. return 0;
  251. }
  252. void sls_log_destroy()
  253. {
  254. }
  255. static const char sls_month_snames[12][4] =
  256. {
  257. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  258. };
  259. static const char sls_day_snames[7][4] =
  260. {
  261. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  262. };
  263. void sls_rfc822_date(char *date_str, struct tm * xt)
  264. {
  265. const char *s = NULL;
  266. int real_year = 2000;
  267. /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
  268. /* 12345678901234567890123456789 */
  269. s = &sls_day_snames[xt->tm_wday][0];
  270. *date_str++ = *s++;
  271. *date_str++ = *s++;
  272. *date_str++ = *s++;
  273. *date_str++ = ',';
  274. *date_str++ = ' ';
  275. *date_str++ = xt->tm_mday / 10 + '0';
  276. *date_str++ = xt->tm_mday % 10 + '0';
  277. *date_str++ = ' ';
  278. s = &sls_month_snames[xt->tm_mon][0];
  279. *date_str++ = *s++;
  280. *date_str++ = *s++;
  281. *date_str++ = *s++;
  282. *date_str++ = ' ';
  283. real_year = 1900 + xt->tm_year;
  284. /* This routine isn't y10k ready. */
  285. *date_str++ = real_year / 1000 + '0';
  286. *date_str++ = real_year % 1000 / 100 + '0';
  287. *date_str++ = real_year % 100 / 10 + '0';
  288. *date_str++ = real_year % 10 + '0';
  289. *date_str++ = ' ';
  290. *date_str++ = xt->tm_hour / 10 + '0';
  291. *date_str++ = xt->tm_hour % 10 + '0';
  292. *date_str++ = ':';
  293. *date_str++ = xt->tm_min / 10 + '0';
  294. *date_str++ = xt->tm_min % 10 + '0';
  295. *date_str++ = ':';
  296. *date_str++ = xt->tm_sec / 10 + '0';
  297. *date_str++ = xt->tm_sec % 10 + '0';
  298. *date_str++ = ' ';
  299. *date_str++ = 'G';
  300. *date_str++ = 'M';
  301. *date_str++ = 'T';
  302. *date_str++ = 0;
  303. return;
  304. }
  305. void get_now_time_str(char * buffer, int bufLen, int timeOffset)
  306. {
  307. time_t rawtime = LOG_GET_TIME();
  308. struct tm * timeinfo;
  309. if (timeOffset != 0)
  310. {
  311. rawtime += timeOffset;
  312. }
  313. timeinfo = gmtime(&rawtime);
  314. sls_rfc822_date(buffer, timeinfo);
  315. }
  316. void post_log_result_destroy(post_log_result * result)
  317. {
  318. if (result != NULL)
  319. {
  320. if (result->errorMessage != NULL)
  321. {
  322. sdsfree(result->errorMessage);
  323. }
  324. if (result->requestID != NULL)
  325. {
  326. sdsfree(result->requestID);
  327. }
  328. free(result);
  329. }
  330. }
  331. struct cur_slist {
  332. char *data;
  333. struct cur_slist *next;
  334. };
  335. struct cur_slist * cur_slist_append(struct cur_slist *lst, const char *s)
  336. {
  337. struct cur_slist* orig;
  338. struct cur_slist *t = (struct cur_slist *)malloc(sizeof(struct cur_slist));
  339. #ifdef WIN32
  340. t->data = _strdup(s);
  341. #else
  342. t->data = strdup(s);
  343. #endif
  344. t->next = NULL;
  345. if(lst == NULL)
  346. return t;
  347. orig = lst;
  348. while(lst->next) {
  349. lst = lst->next;
  350. }
  351. lst->next = t;
  352. return orig;
  353. }
  354. void cur_slist_free_all(struct cur_slist *lst)
  355. {
  356. while(lst != NULL) {
  357. struct cur_slist *n = lst->next;
  358. free(lst->data);
  359. free(lst);
  360. lst = n;
  361. }
  362. }
  363. post_log_result* post_logs(const char* endpoint, const char* accesskeyId, const char* accessKey, const char* stsToken, lz4_log_buf* buffer, log_post_option* option,
  364. const char* channelId, const char* token, const char *terminalno, const char *reserve1)
  365. {
  366. post_log_result * result = (post_log_result *)malloc(sizeof(post_log_result));
  367. memset(result, 0, sizeof(post_log_result));
  368. {
  369. //headers
  370. struct cur_slist* headers = NULL;
  371. int res;
  372. char* header_array[50];
  373. int header_count = 0;
  374. // url
  375. sds url = NULL;
  376. if (option->using_https) {
  377. url = sdsnew("https://");
  378. } else {
  379. url = sdsnew("http://");
  380. }
  381. url = sdscat(url, endpoint);
  382. if(buffer->data != NULL && buffer->length != 0)
  383. res = LOG_OS_HttpPost(url, header_array, header_count, (const void *) buffer->data, buffer->length, channelId, token, terminalno, reserve1);
  384. result->statusCode = res;
  385. cur_slist_free_all(headers); /* free the list again */
  386. sdsfree(url);
  387. }
  388. return result;
  389. }