ILog.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_CRYCOMMON_ILOG_H
  9. #define CRYINCLUDE_CRYCOMMON_ILOG_H
  10. #pragma once
  11. #include <IMiniLog.h> // <> required for Interfuscator
  12. // enable this define to support log scopes to provide more context information for log lines
  13. // this code is disable by default due it's runtime cost
  14. //#define SUPPORT_LOG_IDENTER
  15. // Summary:
  16. // Callback interface to the ILog.
  17. struct ILogCallback
  18. {
  19. // <interfuscator:shuffle>
  20. virtual ~ILogCallback() {}
  21. //OnWrite will always be called even if verbosity settings cause OnWriteToConsole and OnWriteToFile to not be called.
  22. virtual void OnWrite(AZStd::string_view sText, IMiniLog::ELogType type) = 0;
  23. virtual void OnWriteToConsole(AZStd::string_view sText, bool bNewLine) = 0;
  24. virtual void OnWriteToFile(AZStd::string_view sText, bool bNewLine) = 0;
  25. // </interfuscator:shuffle>
  26. };
  27. // Summary:
  28. // Interface for logging operations based on IMiniLog.
  29. // Notes:
  30. // Logging in CryEngine should be done using the following global functions:
  31. // CryLog (eMessage)
  32. // CryLogAlways (eAlways)
  33. // CryError (eError)
  34. // CryWarning (eWarning)
  35. // CryComment (eComment)
  36. // ILog gives you more control on logging operations.
  37. // See also:
  38. // IMiniLog, CryLog, CryLogAlways, CryError, CryWarning
  39. struct ILog
  40. : public IMiniLog
  41. {
  42. // <interfuscator:shuffle>
  43. virtual void Release() = 0;
  44. // Summary:
  45. // Sets the file used to log to disk.
  46. // if you don't specify the full path it will be assumed relative to the 'log folder'
  47. // dobackups controls whether or not it will backup old logs when creating new ones.
  48. virtual bool SetFileName(const char* fileNameOrFullPath, bool doBackups) = 0;
  49. // Summary:
  50. // Gets the filename used to log to disk.
  51. virtual const char* GetFileName() = 0;
  52. // Summary:
  53. // Gets the filename where the current log backup was copied to on disk
  54. virtual const char* GetBackupFileName() = 0;
  55. //all the following functions will be removed are here just to be able to compile the project ---------------------------
  56. // Summary:
  57. // Logs the text both to file and console.
  58. virtual void Log(const char* szCommand, ...) PRINTF_PARAMS(2, 3) = 0;
  59. virtual void LogAlways(const char* szCommand, ...) PRINTF_PARAMS(2, 3) = 0;
  60. virtual void LogWarning(const char* szCommand, ...) PRINTF_PARAMS(2, 3) = 0;
  61. virtual void LogError(const char* szCommand, ...) PRINTF_PARAMS(2, 3) = 0;
  62. // Summary:
  63. // Logs the text both to the end of file and console by appending with the previous line.
  64. virtual void LogAppendWithPrevLine(const char* command, ...) PRINTF_PARAMS(2, 3) = 0;
  65. // Summary:
  66. // Logs to the file specified in SetFileName.
  67. // See also:
  68. // SetFileName
  69. virtual void LogToFile(const char* command, ...) PRINTF_PARAMS(2, 3) = 0;
  70. // Logs the text to the end of the file by appending it to the last line
  71. virtual void LogToFileAppendWithPrevLine(const char* command, ...) PRINTF_PARAMS(2, 3) = 0;
  72. // Summary:
  73. // Logs to console only.
  74. virtual void LogToConsole(const char* command, ...) PRINTF_PARAMS(2, 3) = 0;
  75. // Log the text to the end of the console by appending with the last line
  76. virtual void LogToConsoleAppendWithPrevLine(const char* command, ...) PRINTF_PARAMS(2, 3) = 0;
  77. //
  78. virtual void UpdateLoadingScreen(const char* command, ...) PRINTF_PARAMS(2, 3) = 0;
  79. //
  80. virtual void RegisterConsoleVariables() {}
  81. //
  82. virtual void UnregisterConsoleVariables() {}
  83. // Notes:
  84. // Full logging (to console and file) can be enabled with verbosity 4.
  85. // In the console 'log_Verbosity 4' command can be used.
  86. virtual void SetVerbosity(int verbosity) = 0;
  87. virtual int GetVerbosityLevel() = 0;
  88. virtual void AddCallback(ILogCallback* pCallback) = 0;
  89. virtual void RemoveCallback(ILogCallback* pCallback) = 0;
  90. // Notes:
  91. // The function called every frame by system.
  92. virtual void Update() = 0;
  93. virtual const char* GetModuleFilter() = 0;
  94. // Asset scope strings help to figure out asset dependencies in case of asset loading errors.
  95. // Should not be used directly, only by using define CRY_DEFINE_ASSET_SCOPE
  96. // @see CRY_DEFINE_ASSET_SCOPE
  97. virtual void PushAssetScopeName([[maybe_unused]] const char* sAssetType, [[maybe_unused]] const char* sName) {};
  98. virtual void PopAssetScopeName() {};
  99. virtual const char* GetAssetScopeString() { return ""; };
  100. // </interfuscator:shuffle>
  101. #if defined(SUPPORT_LOG_IDENTER)
  102. virtual void Indent(class CLogIndenter* indenter) = 0;
  103. virtual void Unindent(class CLogIndenter* indenter) = 0;
  104. #endif
  105. virtual void FlushAndClose() = 0;
  106. virtual void Flush() = 0;
  107. };
  108. #if !defined(SUPPORT_LOG_IDENTER)
  109. #define INDENT_LOG_DURING_SCOPE(...) (void)(0)
  110. #define CRY_DEFINE_ASSET_SCOPE(sAssetType, sAssetName) (void)(0)
  111. #else
  112. class CLogIndenter
  113. {
  114. public:
  115. CLogIndenter(ILog* log)
  116. : m_log(log)
  117. , m_enabled(false)
  118. , m_nextIndenter(NULL)
  119. , m_needToPrintSectionText(false)
  120. {
  121. }
  122. void Enable(bool enable = true, const char* sectionTextFormat = NULL, ...)
  123. {
  124. va_list args;
  125. va_start (args, sectionTextFormat);
  126. enable &= (m_log != NULL);
  127. if (enable != m_enabled)
  128. {
  129. if (sectionTextFormat && enable)
  130. {
  131. char buffer[1024];
  132. vsnprintf(buffer, sizeof(buffer), sectionTextFormat, args);
  133. buffer[sizeof(buffer) - 1] = '\0';
  134. m_sectionText = buffer;
  135. m_needToPrintSectionText = true;
  136. }
  137. else
  138. {
  139. m_sectionText = "";
  140. m_needToPrintSectionText = m_nextIndenter ? m_nextIndenter->m_needToPrintSectionText : false;
  141. }
  142. assert(m_log);
  143. if (enable)
  144. {
  145. m_log->Indent(this);
  146. }
  147. else
  148. {
  149. m_log->Unindent(this);
  150. }
  151. m_enabled = enable;
  152. }
  153. va_end (args);
  154. }
  155. CLogIndenter* GetNextIndenter()
  156. {
  157. return m_nextIndenter;
  158. }
  159. void SetNextIndenter(CLogIndenter* indenter)
  160. {
  161. m_nextIndenter = indenter;
  162. }
  163. void DisplaySectionText()
  164. {
  165. if (m_needToPrintSectionText)
  166. {
  167. m_needToPrintSectionText = false;
  168. string sectionText = m_sectionText;
  169. Enable(false);
  170. if (m_nextIndenter)
  171. {
  172. m_nextIndenter->DisplaySectionText();
  173. }
  174. if (!sectionText.empty())
  175. {
  176. assert (m_log);
  177. m_log->Log ("%s", sectionText.c_str());
  178. }
  179. Enable(true);
  180. }
  181. }
  182. ~CLogIndenter()
  183. {
  184. Enable(false);
  185. }
  186. private:
  187. bool m_enabled;
  188. bool m_needToPrintSectionText;
  189. ILog* m_log;
  190. CLogIndenter* m_nextIndenter;
  191. string m_sectionText;
  192. };
  193. class CLogAssetScopeName
  194. {
  195. ILog* m_pLog;
  196. public:
  197. CLogAssetScopeName(ILog* pLog, const char* sAssetType, const char* sAssetName)
  198. : m_pLog(pLog) { pLog->PushAssetScopeName(sAssetType, sAssetName); }
  199. ~CLogAssetScopeName() { m_pLog->PopAssetScopeName(); }
  200. };
  201. #define ILOG_CONCAT_IMPL(x, y) x##y
  202. #define ILOG_CONCAT_MACRO(x, y) ILOG_CONCAT_IMPL(x, y)
  203. #define INDENT_LOG_DURING_SCOPE(...) CLogIndenter ILOG_CONCAT_MACRO(indentMe, __LINE__) ((CryGetCurrentThreadId() == gEnv->mMainThreadId) ? gEnv->pLog : NULL); ILOG_CONCAT_MACRO(indentMe, __LINE__).Enable(__VA_ARGS__)
  204. #define CRY_DEFINE_ASSET_SCOPE(sAssetType, sAssetName) CLogAssetScopeName __asset_scope_name(gEnv->pLog, sAssetType, sAssetName);
  205. #endif
  206. #endif // CRYINCLUDE_CRYCOMMON_ILOG_H