Logger.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. //========================================================================
  3. // Logger.h :
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. //---------------------------------------------------------------------------------------------------------------------
  40. // TYPICAL USAGE:
  41. //
  42. // 1) Call the GCC_ASSERT() macro just like you would for _ASSERTE()
  43. // Example -> GCC_ASSERT(ptr != NULL);
  44. // 2) Call the GCC_ERROR() macro to send out an error message
  45. // Example -> GCC_ERROR("Something Blew Up!");
  46. //---------------------------------------------------------------------------------------------------------------------
  47. //---------------------------------------------------------------------------------------------------------------------
  48. // LOGGING:
  49. //
  50. // Calling GCC_LOG("tag", "Log message") will print:
  51. // [tag] Log Message
  52. // To enable these logs, you need to either manually call Logger::SetDisplayFlags() or set up a logging.xml file.
  53. // logging.xml should follow this form:
  54. //
  55. // <Logger>
  56. // <Log tag="Actor" debugger="1" file="0"/>
  57. // </Logger>
  58. //
  59. // The above chunk will cause all logs sent with the "Actor" tag to be displayed in the debugger. If you set file
  60. // to 1 as well, it would log out to a file as well. Don't check in logging.xml to SVN, it should be a local-only
  61. // file.
  62. //---------------------------------------------------------------------------------------------------------------------
  63. #include <string>
  64. #include "../Unix/GameCodeStd.h"
  65. //---------------------------------------------------------------------------------------------------------------------
  66. // Constants
  67. //---------------------------------------------------------------------------------------------------------------------
  68. // display flags
  69. const unsigned char LOGFLAG_WRITE_TO_LOG_FILE = 1 << 0;
  70. const unsigned char LOGFLAG_WRITE_TO_DEBUGGER = 1 << 1;
  71. //---------------------------------------------------------------------------------------------------------------------
  72. // This is the public Logger interface. You must call Init() before any logging can take place and Destroy() when
  73. // you're done to clean it up. Call SetDisplayFlags() to set the display flags for a particular logging tag. By
  74. // default, they are all off. Although you can, you probably shouldn't call Log() directly. Use the GCC_LOG() macro
  75. // instead since it can be stripped out by the compiler for Release builds.
  76. //---------------------------------------------------------------------------------------------------------------------
  77. namespace Logger
  78. {
  79. // This class is used by the debug macros and shouldn't be accessed externally.
  80. class ErrorMessenger
  81. {
  82. bool m_enabled;
  83. public:
  84. ErrorMessenger(void);
  85. void Show(const std::string& errorMessage, bool isFatal, const char* funcName, const char* sourceFile, unsigned int lineNum);
  86. };
  87. // construction; must be called at the beginning and end of the program
  88. void Init(const char* loggingConfigFilename);
  89. void Destroy(void);
  90. // logging functions
  91. void Log(const std::string& tag, const std::string& message, const char* funcName, const char* sourceFile, unsigned int lineNum);
  92. void SetDisplayFlags(const std::string& tag, unsigned char flags);
  93. }
  94. //---------------------------------------------------------------------------------------------------------------------
  95. // Debug macros
  96. //---------------------------------------------------------------------------------------------------------------------
  97. // Fatal Errors are fatal and are always presented to the user.
  98. #define GCC_FATAL(str) \
  99. do \
  100. { \
  101. static Logger::ErrorMessenger* pErrorMessenger = GCC_NEW Logger::ErrorMessenger; \
  102. std::string s((str)); \
  103. pErrorMessenger->Show(s, true, __FUNCTION__, __FILE__, __LINE__); \
  104. } \
  105. while (0)\
  106. #if 1 //ndef NDEBUG
  107. // Errors are bad and potentially fatal. They are presented as a dialog with Abort, Retry, and Ignore. Abort will
  108. // break into the debugger, retry will continue the game, and ignore will continue the game and ignore every subsequent
  109. // call to this specific error. They are ignored completely in release mode.
  110. #define GCC_ERROR(str) \
  111. do \
  112. { \
  113. static Logger::ErrorMessenger* pErrorMessenger = GCC_NEW Logger::ErrorMessenger; \
  114. std::string s((str)); \
  115. pErrorMessenger->Show(s, false, __FUNCTION__, __FILE__, __LINE__); \
  116. } \
  117. while (0)\
  118. // Warnings are recoverable. They are just logs with the "WARNING" tag that displays calling information. The flags
  119. // are initially set to WARNINGFLAG_DEFAULT (defined in debugger.cpp), but they can be overridden normally.
  120. #define GCC_WARNING(str) \
  121. do \
  122. { \
  123. std::string s((str)); \
  124. Logger::Log("WARNING", s, __FUNCTION__, __FILE__, __LINE__); \
  125. }\
  126. while (0)\
  127. // This is just a convenient macro for logging if you don't feel like dealing with tags. It calls Log() with a tag
  128. // of "INFO". The flags are initially set to LOGFLAG_DEFAULT (defined in debugger.cpp), but they can be overridden
  129. // normally.
  130. #define GCC_INFO(str) \
  131. do \
  132. { \
  133. std::string s((str)); \
  134. Logger::Log("INFO", s, NULL, NULL, 0); \
  135. } \
  136. while (0) \
  137. // This macro is used for logging and should be the preferred method of "printf debugging". You can use any tag
  138. // string you want, just make sure to enabled the ones you want somewhere in your initialization.
  139. #define GCC_LOG(tag, str) \
  140. do \
  141. { \
  142. std::string s((str)); \
  143. Logger::Log(tag, s, NULL, NULL, 0); \
  144. } \
  145. while (0) \
  146. // This macro replaces GCC_ASSERT().
  147. #define GCC_ASSERT(expr) \
  148. do \
  149. { \
  150. if (!(expr)) \
  151. { \
  152. static Logger::ErrorMessenger* pErrorMessenger = GCC_NEW Logger::ErrorMessenger; \
  153. pErrorMessenger->Show(#expr, false, __FUNCTION__, __FILE__, __LINE__); \
  154. } \
  155. } \
  156. while (0) \
  157. #else // NDEBUG is defined
  158. // These are the release mode definitions for the macros above. They are all defined in such a way as to be
  159. // ignored completely by the compiler.
  160. #define GCC_ERROR(str) do { (void)sizeof(str); } while(0)
  161. #define GCC_WARNING(str) do { (void)sizeof(str); } while(0)
  162. #define GCC_INFO(str) do { (void)sizeof(str); } while(0)
  163. #define GCC_LOG(tag, str) do { (void)sizeof(tag); (void)sizeof(str); } while(0)
  164. #define GCC_ASSERT(expr) do { (void)sizeof(expr); } while(0)
  165. #endif // !defined NDEBUG