ZeroTierDebug.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. /**
  27. * @file
  28. *
  29. * Debug macros
  30. */
  31. #ifndef ZT_DEBUG_H
  32. #define ZT_DEBUG_H
  33. #if defined(__linux__) || defined(__APPLE__)
  34. #include <sys/syscall.h>
  35. #include <pthread.h>
  36. #include <unistd.h>
  37. #endif
  38. #include <string.h>
  39. #define ZT_MSG_INFO true
  40. #define ZT_COLOR true
  41. // Debug output colors
  42. #if defined(__APPLE__)
  43. #include "TargetConditionals.h"
  44. #endif
  45. #if defined(ZT_COLOR) && !defined(_WIN32) && !defined(__ANDROID__) && !defined(TARGET_OS_IPHONE) && !defined(TARGET_IPHONE_SIMULATOR) && !defined(__APP_FRAMEWORK__)
  46. #define ZT_RED "\x1B[31m"
  47. #define ZT_GRN "\x1B[32m"
  48. #define ZT_YEL "\x1B[33m"
  49. #define ZT_BLU "\x1B[34m"
  50. #define ZT_MAG "\x1B[35m"
  51. #define ZT_CYN "\x1B[36m"
  52. #define ZT_WHT "\x1B[37m"
  53. #define ZT_RESET "\x1B[0m"
  54. #else
  55. #define ZT_RED
  56. #define ZT_GRN
  57. #define ZT_YEL
  58. #define ZT_BLU
  59. #define ZT_MAG
  60. #define ZT_CYN
  61. #define ZT_WHT
  62. #define ZT_RESET
  63. #endif
  64. #define ZT_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) // short
  65. #ifdef __linux__
  66. #define ZT_THREAD_ID (long)0 // syscall(SYS_gettid)
  67. #endif
  68. #ifdef __APPLE__
  69. #define ZT_THREAD_ID (long)0 // (long)gettid()
  70. #endif
  71. #ifdef __FreeBSD__
  72. #define ZT_THREAD_ID (long)0 // (long)gettid()
  73. #endif
  74. #ifdef _WIN32
  75. #define ZT_THREAD_ID (long)0 //
  76. #endif
  77. #if defined(__JNI_LIB__)
  78. #include <jni.h>
  79. #endif
  80. #if defined(__ANDROID__)
  81. #include <android/log.h>
  82. #define ZT_LOG_TAG "ZTSDK"
  83. #endif
  84. #if defined(ZT_DEBUG_TRACE)
  85. #if ZT_MSG_INFO == true
  86. #if defined(__ANDROID__)
  87. #define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
  88. "INFO : %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
  89. #endif
  90. #if defined(_WIN32)
  91. #define DEBUG_INFO(fmt, ...) fprintf(stderr, ZT_GRN "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" \
  92. ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
  93. #endif
  94. #if defined(__linux__) or defined(__APPLE__) or defined(__FreeBSD__)
  95. #define DEBUG_INFO(fmt, args ...) fprintf(stderr, ZT_GRN "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" \
  96. ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
  97. #endif
  98. #else
  99. #define DEBUG_INFO(fmt, args...)
  100. #endif
  101. #else // blank
  102. #if defined(_WIN32)
  103. #define DEBUG_INFO(...)
  104. #else
  105. #define DEBUG_INFO(fmt, args...)
  106. #endif
  107. #endif
  108. #endif // _H