ZeroTierDebug.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. /**
  14. * @file
  15. *
  16. * Debug macros
  17. */
  18. #ifndef ZT_DEBUG_H
  19. #define ZT_DEBUG_H
  20. #if defined(__linux__) || defined(__APPLE__)
  21. #include <pthread.h>
  22. #include <sys/syscall.h>
  23. #include <unistd.h>
  24. #endif
  25. #include <string.h>
  26. #define ZT_MSG_INFO true
  27. #define ZT_COLOR true
  28. // Debug output colors
  29. #if defined(__APPLE__)
  30. #include "TargetConditionals.h"
  31. #endif
  32. #if defined(ZT_COLOR) && ! defined(_WIN32) && ! defined(__ANDROID__) && ! defined(TARGET_OS_IPHONE) && ! defined(TARGET_IPHONE_SIMULATOR) && ! defined(__APP_FRAMEWORK__)
  33. #define ZT_RED "\x1B[31m"
  34. #define ZT_GRN "\x1B[32m"
  35. #define ZT_YEL "\x1B[33m"
  36. #define ZT_BLU "\x1B[34m"
  37. #define ZT_MAG "\x1B[35m"
  38. #define ZT_CYN "\x1B[36m"
  39. #define ZT_WHT "\x1B[37m"
  40. #define ZT_RESET "\x1B[0m"
  41. #else
  42. #define ZT_RED
  43. #define ZT_GRN
  44. #define ZT_YEL
  45. #define ZT_BLU
  46. #define ZT_MAG
  47. #define ZT_CYN
  48. #define ZT_WHT
  49. #define ZT_RESET
  50. #endif
  51. #define ZT_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) // short
  52. #ifdef __linux__
  53. #define ZT_THREAD_ID (long)0 // syscall(SYS_gettid)
  54. #endif
  55. #ifdef __APPLE__
  56. #define ZT_THREAD_ID (long)0 // (long)gettid()
  57. #endif
  58. #ifdef __FreeBSD__
  59. #define ZT_THREAD_ID (long)0 // (long)gettid()
  60. #endif
  61. #ifdef _WIN32
  62. #define ZT_THREAD_ID (long)0 //
  63. #endif
  64. #if defined(__JNI_LIB__)
  65. #include <jni.h>
  66. #endif
  67. #if defined(__ANDROID__)
  68. #include <android/log.h>
  69. #define ZT_LOG_TAG "ZTSDK"
  70. #endif
  71. #if defined(ZT_DEBUG_TRACE)
  72. #if ZT_MSG_INFO == true
  73. #if defined(__ANDROID__)
  74. #define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, "INFO : %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
  75. #endif
  76. #if defined(_WIN32)
  77. #define DEBUG_INFO(fmt, ...) fprintf(stderr, ZT_GRN "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
  78. #endif
  79. #if defined(__linux__) or defined(__APPLE__) or defined(__FreeBSD__)
  80. #define DEBUG_INFO(fmt, args...) fprintf(stderr, ZT_GRN "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
  81. #endif
  82. #else
  83. #define DEBUG_INFO(fmt, args...)
  84. #endif
  85. #else // blank
  86. #if defined(_WIN32)
  87. #define DEBUG_INFO(...)
  88. #else
  89. #define DEBUG_INFO(fmt, args...)
  90. #endif
  91. #endif
  92. #endif // _H