log_util.cpp 6.7 KB

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