NetworkLogging.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file NetworkLogging.h
  13. @brief The LOG and LOGUSER macros. Provides an unified mechanism for logging. */
  14. // From http://cnicholson.net/2009/03/stupid-c-tricks-dowhile0-and-c4127/
  15. #ifdef _MSC_VER
  16. #define MULTI_LINE_MACRO_BEGIN do { \
  17. __pragma(warning(push)) \
  18. __pragma(warning(disable:4127))
  19. #define MULTI_LINE_MACRO_END \
  20. } while(0) \
  21. __pragma(warning(pop))
  22. #else
  23. #define MULTI_LINE_MACRO_BEGIN do {
  24. #define MULTI_LINE_MACRO_END } while(0)
  25. #endif
  26. namespace kNet
  27. {
  28. /// A bitfield type that describes single or multiple log channels (each bit represents a channel).
  29. typedef unsigned int LogChannel;
  30. namespace
  31. {
  32. const LogChannel LogUser = 1; ///< This log channel is free to use for user-level application messages.
  33. const LogChannel LogInfo = 2; ///< Successful connects and disconnects.
  34. const LogChannel LogError = 4; ///< All connection-related errors.
  35. const LogChannel LogObjectAlloc = 8; ///< For debugging: prints information about object allocations an deallocations.
  36. const LogChannel LogData = 16; ///< For debugging: dumps information about in- and outbound packet data.
  37. const LogChannel LogVerbose = 32; ///< For debugging: Print detailed internal information.
  38. const LogChannel LogWaits = 64; ///< Logs all long performance-related waits (thread/mutex blocks) that occur in the system.
  39. }
  40. /// Prints a variadic line to log.
  41. /// @param logChannel The log channel to print to. This variable must have exactly one bit set, and is ANDed against
  42. /// the currently active channels that can be set with a call to kNet::SetLogChannels.
  43. /// @param filename The C++ file from which the log message occurs, pass in __FILE__.
  44. /// @param lineNumber Pass in __LINE__.
  45. /// @param msg The printf-style message format specifier for the text to print.
  46. void TimeOutputDebugStringVariadic(LogChannel logChannel, const char *filename, int lineNumber, const char *msg, ...);
  47. /// Prints a message to the log. Same as kNet::TimeOutputDebugStringVariadic, but does not use printf formatting.
  48. void TimeOutputDebugString(LogChannel logChannel, const char *filename, int lineNumber, const char *msg);
  49. /// Sets the currently enabled log channels. Pass in a bitwise OR of the log channels you want to enable.
  50. /// If you pass in 0, all logging is disabled. By default, only the channel LogChannelUser is enabled.
  51. void SetLogChannels(LogChannel logChannel);
  52. /// Returns the currently active log channels.
  53. LogChannel GetLogChannels();
  54. /// Returns true if the specified log channel is not suppressed.
  55. bool IsLogChannelActive(LogChannel channel);
  56. /// Sets the output for log messages. By default all logging is output to std::cout. Setting a log file
  57. /// redirects all logging to that file. Calling this function with a null filename pointer restores
  58. /// logging to target std::cout.
  59. void SetLogFile(const char *filename);
  60. /// When called, sets the runtime to print out all memory leaks at program exit time. Win32-only. On
  61. /// linux, this is a no-op.
  62. void EnableMemoryLeakLoggingAtExit();
  63. } // ~kNet
  64. /// Prints out a variadic message to the log channel User.
  65. #define KNET_LOGUSER(msg, ...) \
  66. MULTI_LINE_MACRO_BEGIN \
  67. if (kNet::IsLogChannelActive(LogUser)) \
  68. kNet::TimeOutputDebugStringVariadic(LogUser, __FILE__, __LINE__, msg, ##__VA_ARGS__); \
  69. MULTI_LINE_MACRO_END
  70. #ifdef KNET_LOGGING_SUPPORT_ENABLED
  71. /// Prints out a variadic message to the given log channel.
  72. #define KNET_LOG(channel, msg, ...) \
  73. MULTI_LINE_MACRO_BEGIN \
  74. if (kNet::IsLogChannelActive(channel)) \
  75. kNet::TimeOutputDebugStringVariadic(channel, __FILE__, __LINE__, msg, ##__VA_ARGS__); \
  76. MULTI_LINE_MACRO_END
  77. #else
  78. /// If kNet logging is disabled, KNET_LOG() macro is a no-op. This avoids having to evaluate the arguments of the
  79. /// KNET_LOG() call, which improves performance.
  80. #define KNET_LOG(...) ((void)0)
  81. #endif