log_util.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. //相对独立,不用框架库提供的函数进行IP 获取 [Gifur@2025729]
  134. int GetLocalIP(char* ip_str)
  135. {
  136. #ifdef _WIN32
  137. struct hostent* ent;
  138. char tmp[256] = "";
  139. int i;
  140. __try {
  141. ent = gethostbyname(tmp);
  142. }
  143. __except (EXCEPTION_EXECUTE_HANDLER) {
  144. strcpy(ip_str, "0.0.0.0");
  145. return -1;
  146. }
  147. if (ent) {
  148. for (i = 0; ent->h_addr_list[i]; ++i) {
  149. if (ent->h_addrtype == AF_INET) {
  150. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  151. char* p = inet_ntoa(*in);
  152. if (p[0] != '0') {
  153. strcpy(ip_str, p);
  154. return 0;
  155. }
  156. }
  157. }
  158. }
  159. #else
  160. int sockfd = -1;
  161. struct ifconf ifconf;
  162. struct ifreq* ifreq = NULL;
  163. char strbuf[256] = { 0 };
  164. ifconf.ifc_len = 256;
  165. ifconf.ifc_buf = strbuf;
  166. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
  167. ioctl(sockfd, SIOCGIFCONF, &ifconf); //get all socket info
  168. ifreq = (struct ifreq*)ifconf.ifc_buf;
  169. for (int i = (ifconf.ifc_len / sizeof(struct ifreq)); i > 0; i--) {
  170. if (ifreq->ifr_flags == AF_INET) { //for ipv4
  171. char* strIP = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr);
  172. ifreq++;
  173. if (NULL != strIP) {
  174. if (NULL == strstr(strIP, "198.168.") && NULL == strstr(strIP, "127.0.0.1")) {
  175. strcpy(ip_str, strIP);
  176. close(sockfd);
  177. return 0;
  178. }
  179. }
  180. }
  181. }
  182. close(sockfd);
  183. }
  184. #endif //_WIN32
  185. return -1;
  186. }
  187. void convertUnCharTotr(char* str, unsigned char* UnChar, int ucLen)
  188. {
  189. int i = 0;
  190. for (i = 0; i < ucLen; i++)
  191. {
  192. //格式化输str,每unsigned char 转换字符占两位置%x写输%X写输
  193. sprintf(str + i * 2, "%02x", UnChar[i]);
  194. }
  195. }
  196. void convertStrToUnChar(char* str, unsigned char* UnChar)
  197. {
  198. int i = strlen(str), j = 0, counter = 0;
  199. char c[2];
  200. unsigned int bytes[2];
  201. for(j = 0; j < i; j += 2)
  202. {
  203. if (0 == j % 2)
  204. {
  205. c[0] = str[j];
  206. c[1] = str[j + 1];
  207. sscanf(c, "%02x", &bytes[0]);
  208. UnChar[counter] = bytes[0];
  209. counter++;
  210. }
  211. }
  212. return;
  213. }
  214. int mkdir_foreach(char* file_path, int length) {
  215. int i;
  216. char tmpDirPath[256] = { 0 };
  217. for (i = 0; i < length; i++)
  218. {
  219. tmpDirPath[i] = file_path[i];
  220. if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
  221. {
  222. //_access()判断文件是否存在,并判断文件是否可写
  223. //int _access(const char *pathname, int mode);
  224. //pathname: 文件路径或目录路径; mode: 访问权限(在不同系统中可能用不能的宏定义重新定义)
  225. //当pathname为文件时,_access函数判断文件是否存在,并判断文件是否可以用mode值指定的模式进行访问。
  226. //当pathname为目录时,_access只判断指定目录是否存在,在Windows NT和Windows 2000中,所有的目录都只有读写权限
  227. //0——>只检查文件是否存在
  228. #if defined(_MSC_VER)
  229. if (_access(tmpDirPath, 0) == -1) {
  230. int ret = _mkdir(tmpDirPath);
  231. if (ret == -1) {
  232. return ret;
  233. }
  234. }
  235. #else
  236. if (access(tmpDirPath, 0) == -1) {
  237. int ret = mkdir(tmpDirPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  238. if (ret == -1) {
  239. return ret;
  240. }
  241. }
  242. #endif //_MSC_VER
  243. }
  244. }
  245. return 0;
  246. }