log_util.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #if defined(_MSC_VER)
  2. #include <Windows.h>
  3. #include <WinSock.h>
  4. #include <IPHlpApi.h>
  5. #include <direct.h>
  6. #include <io.h>
  7. #include <Rpc.h>
  8. #pragma comment(lib, "Ws2_32.lib")
  9. #pragma comment(lib, "IPHlpApi.lib")
  10. #pragma comment(lib, "Rpcrt4.lib")
  11. #else
  12. #include <netdb.h>
  13. #include <unistd.h>
  14. #include <sys/un.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/socket.h>
  17. #include <arpa/inet.h>
  18. #include <netinet/in.h>
  19. #include <netinet/tcp.h>
  20. #include <net/if.h>
  21. #include <sys/stat.h>
  22. #include <sys/time.h>
  23. #endif
  24. #include <string.h>
  25. #include "log_util.h"
  26. #include "md5.h"
  27. #include <stdio.h>
  28. #include "uuid4.h"
  29. static const char *g_hex_hash = "0123456789ABCDEF";
  30. void md5_to_string(const char * buffer, int bufLen, char * md5)
  31. {
  32. int i = 0;
  33. unsigned char md5Buf[16];
  34. mbedtls_md5((const unsigned char *)buffer, bufLen, md5Buf);
  35. for(; i < 32; i+=2)
  36. {
  37. md5[i] = g_hex_hash[md5Buf[i >> 1] >> 4];
  38. md5[i+1] = g_hex_hash[md5Buf[i >> 1] & 0xF];
  39. }
  40. }
  41. int aos_base64_encode(const unsigned char *in, int inLen, char *out)
  42. {
  43. static const char *ENC =
  44. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  45. char *original_out = out;
  46. while (inLen) {
  47. // first 6 bits of char 1
  48. *out++ = ENC[*in >> 2];
  49. if (!--inLen) {
  50. // last 2 bits of char 1, 4 bits of 0
  51. *out++ = ENC[(*in & 0x3) << 4];
  52. *out++ = '=';
  53. *out++ = '=';
  54. break;
  55. }
  56. // last 2 bits of char 1, first 4 bits of char 2
  57. *out++ = ENC[((*in & 0x3) << 4) | (*(in + 1) >> 4)];
  58. in++;
  59. if (!--inLen) {
  60. // last 4 bits of char 2, 2 bits of 0
  61. *out++ = ENC[(*in & 0xF) << 2];
  62. *out++ = '=';
  63. break;
  64. }
  65. // last 4 bits of char 2, first 2 bits of char 3
  66. *out++ = ENC[((*in & 0xF) << 2) | (*(in + 1) >> 6)];
  67. in++;
  68. // last 6 bits of char 3
  69. *out++ = ENC[*in & 0x3F];
  70. in++, inLen--;
  71. }
  72. return (int)(out - original_out);
  73. }
  74. /*
  75. int signature_to_base64(const char * sig, int sigLen, const char * key, int keyLen, char * base64)
  76. {
  77. unsigned char sha1Buf[20];
  78. hmac_sha1(sha1Buf, key, keyLen << 3, sig, sigLen << 3);
  79. return aos_base64_encode((const unsigned char *)sha1Buf, 20, base64);
  80. }
  81. */
  82. void get_format_uuid(char* strbuffer, int ulen)
  83. {
  84. int uuidlen = 0;
  85. uuid4_init();
  86. uuid4_generate(strbuffer);
  87. }
  88. void GetTimeStr(time_t time, char* szTime)
  89. {
  90. #if defined(_MSC_VER)
  91. LARGE_INTEGER liTime, liFreq;
  92. double dTime;
  93. struct tm* tm = localtime(&time);
  94. QueryPerformanceFrequency(&liFreq);
  95. QueryPerformanceCounter(&liTime);
  96. dTime = (double)liTime.QuadPart / (double)liFreq.QuadPart;
  97. sprintf(szTime, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
  98. tm->tm_year + 1900,
  99. tm->tm_mon + 1,
  100. tm->tm_mday,
  101. tm->tm_hour,
  102. tm->tm_min,
  103. tm->tm_sec,
  104. (long)(dTime * 1000) % 1000
  105. );
  106. #else
  107. struct timeval tv;
  108. struct tm* tm = localtime(&time);
  109. gettimeofday(&tv, NULL);
  110. sprintf(szTime, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
  111. tm->tm_year + 1900,
  112. tm->tm_mon + 1,
  113. tm->tm_mday,
  114. tm->tm_hour,
  115. tm->tm_min,
  116. tm->tm_sec,
  117. tv.tv_usec / 1000
  118. );
  119. #endif //_MSC_VER
  120. }
  121. void GetUnitedTimeStr(time_t time, char* szTime)
  122. {
  123. struct tm* tm = localtime(&time);
  124. sprintf(szTime, "%04d-%02d-%02dT%02d:%02d:%02d.000000000Z",
  125. tm->tm_year + 1900,
  126. tm->tm_mon + 1,
  127. tm->tm_mday,
  128. tm->tm_hour,
  129. tm->tm_min,
  130. tm->tm_sec
  131. );
  132. }
  133. int GetLocalIP(char* ip_str)
  134. {
  135. #ifdef _WIN32
  136. struct hostent* ent;
  137. char tmp[256] = "";
  138. int i;
  139. __try {
  140. ent = gethostbyname(tmp);
  141. }
  142. __except (EXCEPTION_EXECUTE_HANDLER) {
  143. strcpy(ip_str, "0.0.0.0");
  144. return -1;
  145. }
  146. if (ent) {
  147. for (i = 0; ent->h_addr_list[i]; ++i) {
  148. if (ent->h_addrtype == AF_INET) {
  149. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  150. char* p = inet_ntoa(*in);
  151. if (p[0] != '0') {
  152. strcpy(ip_str, p);
  153. return 0;
  154. }
  155. }
  156. }
  157. }
  158. #else
  159. int sockfd = -1;
  160. struct ifconf ifconf;
  161. struct ifreq* ifreq = NULL;
  162. char strbuf[256] = { 0 };
  163. ifconf.ifc_len = 256;
  164. ifconf.ifc_buf = strbuf;
  165. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
  166. ioctl(sockfd, SIOCGIFCONF, &ifconf); //get all socket info
  167. ifreq = (struct ifreq*)ifconf.ifc_buf;
  168. for (int i = (ifconf.ifc_len / sizeof(struct ifreq)); i > 0; i--) {
  169. if (ifreq->ifr_flags == AF_INET) { //for ipv4
  170. char* strIP = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr);
  171. ifreq++;
  172. if (NULL != strIP) {
  173. if (NULL == strstr(strIP, "198.168.") && NULL == strstr(strIP, "127.0.0.1")) {
  174. strcpy(ip_str, strIP);
  175. close(sockfd);
  176. return 0;
  177. }
  178. }
  179. }
  180. }
  181. close(sockfd);
  182. }
  183. #endif //_WIN32
  184. return -1;
  185. }
  186. void convertUnCharTotr(char* str, unsigned char* UnChar, int ucLen)
  187. {
  188. int i = 0;
  189. for (i = 0; i < ucLen; i++)
  190. {
  191. //格式化输str,每unsigned char 转换字符占两位置%x写输%X写输
  192. sprintf(str + i * 2, "%02x", UnChar[i]);
  193. }
  194. }
  195. void convertStrToUnChar(char* str, unsigned char* UnChar)
  196. {
  197. int i = strlen(str), j = 0, counter = 0;
  198. char c[2];
  199. unsigned int bytes[2];
  200. for(j = 0; j < i; j += 2)
  201. {
  202. if (0 == j % 2)
  203. {
  204. c[0] = str[j];
  205. c[1] = str[j + 1];
  206. sscanf(c, "%02x", &bytes[0]);
  207. UnChar[counter] = bytes[0];
  208. counter++;
  209. }
  210. }
  211. return;
  212. }
  213. int mkdir_foreach(char* file_path, int length) {
  214. int i;
  215. char tmpDirPath[256] = { 0 };
  216. for (i = 0; i < length; i++)
  217. {
  218. tmpDirPath[i] = file_path[i];
  219. if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
  220. {
  221. //_access()判断文件是否存在,并判断文件是否可写
  222. //int _access(const char *pathname, int mode);
  223. //pathname: 文件路径或目录路径; mode: 访问权限(在不同系统中可能用不能的宏定义重新定义)
  224. //当pathname为文件时,_access函数判断文件是否存在,并判断文件是否可以用mode值指定的模式进行访问。
  225. //当pathname为目录时,_access只判断指定目录是否存在,在Windows NT和Windows 2000中,所有的目录都只有读写权限
  226. //0——>只检查文件是否存在
  227. #if defined(_MSC_VER)
  228. if (_access(tmpDirPath, 0) == -1) {
  229. int ret = _mkdir(tmpDirPath);
  230. if (ret == -1) {
  231. return ret;
  232. }
  233. }
  234. #else
  235. if (access(tmpDirPath, 0) == -1) {
  236. int ret = mkdir(tmpDirPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  237. if (ret == -1) {
  238. return ret;
  239. }
  240. }
  241. #endif //_MSC_VER
  242. }
  243. }
  244. return 0;
  245. }