gwnavruntime/kernel/SF_Log.h Source File

SF_Log.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : KY_Log.h
11 Content : Logging support
12 Created : July 18, 2001
13 Authors : Brendan Iribe, Michael Antonov
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_Log_H
18 #define INC_KY_Kernel_Log_H
19 
24 #include <stdarg.h>
25 
26 
27 namespace Kaim {
28 
29 
30 // ***** LogMessageType
31 
32 // LogMessageType described the severity of the message and automatic prefix/suffix
33 // that should be given to it. LogMessageType bits are combined with LogChannel
34 // and an additional integer value to form LogMessageId.
35 
36 // Different log messages have separate purposes and should
37 // be used appropriately as follows:
38 //
39 // Text: General output of information to channel without prefix/newline.
40 // Debug-only messages should combine with with LogChannel_Debug.
41 //
42 // Warning: Warning messages should be emitted when the API is misused or
43 // is in such a state that the desired operation will most likely
44 // not perform as expected. However, the problem that caused the
45 // warning is still handled gracefully by the system and will
46 // not cause a crash.
47 
48 // Error: Error messages should be emitted if there is an internal problem
49 // that will cause a system or component to malfunction. These messages
50 // may be generates when certain critical resources (files, devices,
51 // memory) are not available or do not function properly. Error
52 // messages can also be generated if the condition is serious enough
53 // but will not be checked for in the release build (resource leak,
54 // for example). Note that conditions for these messages should not cause
55 // an immediate crash even in release build.
56 
57 // Assert: Assertion failures should be generated when the program data structure
58 // or parameter is in an invalid state that will cause an immediate
59 // failure or crash. Assertion failure usually indicates a program logic
60 // error or invalid use of an API (assert messages and should NOT be
61 // generated due to invalid user input or device/file states that can
62 // be handled more gracefully). Assert conditions will not be checked for in
63 // the release build and will most likely cause a crash.
64 
65 enum LogMessageType
66 {
67  LogMessage_Text = 0x00000, // No prefix, no newline.
68  LogMessage_Warning = 0x20000, // "Warning:". For unexpected conditions handled gracefully.
69  LogMessage_Error = 0x30000, // "Error:". For runtime errors resulting in API fail (but not crash).
70  LogMessage_Assert = 0x40000, // "Assert:". Used for condition leading to crash; debug only.
71  // GFx-specific
72  LogMessage_Report = 0x50000, // For reports (performance, memory, etc)
73  // Bit mask used to extract LogMessageType.
74  LogMessage_Mask = 0xF0000
75 };
76 
77 // *** Channels
78 
79 // The idea of message channels is that they correspond to separate
80 // logs that can be displayed individually. Several message sub-types
81 // can, however, be generated within the same channel. End users may
82 // choose to ignore the recommended channel structure and either
83 // combine all of the messages (default output), or separate them
84 // further into individual logs.
85 
86 enum LogChannel
87 {
88  LogChannel_General = 0x1000, // Used for end-user messages about I/O, errors, etc.
89  LogChannel_Debug = 0x2000, // Reserved flag for mapping GDebug messages into single log object.
90  LogChannel_Render = 0x3000, // Reserved flag for mapping GDebug messages into single log object.
91  // GFx-specific channels.
92  LogChannel_Script = 0x4000, // Used for script-related messages (bad function parameters, etc.)
93  LogChannel_Parse = 0x5000, // Used for parse log generated during loading
94  LogChannel_Action = 0x6000, // Used for action-script instruction execution log
95  LogChannel_Memory = 0x7000, // Used for memory reporting
96  LogChannel_Performance = 0x8000, // Used for performance reporting
97  LogChannel_MonitoringSystem = 0x9000, // Used for internal messages of the monitoring system
98 
99  LogChannel_Mask = 0xF000,
100 };
101 
102 
103 // LogMessageId wraps a message Id passed to Log::LogMessageVarg.
104 
105 class LogMessageId
106 {
107  int Id;
108 public:
109 
110  LogMessageId(int id = 0) : Id(id) { }
111  LogMessageId(const LogMessageId& other) : Id(other.Id) { }
112 
113  LogMessageId& operator = (int id) { Id = id; return *this; }
114  LogMessageId& operator = (LogMessageId& other) { Id = other.Id; return *this; }
115 
116  operator int () const { return Id; }
117 
118  LogMessageType GetMessageType() const { return (LogMessageType)(Id & LogMessage_Mask); }
119  LogChannel GetChannel() const { return (LogChannel)(Id & LogChannel_Mask); }
120 };
121 
122 // ***** Combined Log Message types
123 
124 // Log MessageId values used by Kernel.
125 enum LogConstants
126 {
127  // General I/O errors and warnings
128  Log_Message = LogChannel_General | LogMessage_Text,
129  Log_Warning = LogChannel_General | LogMessage_Warning,
130  Log_Error = LogChannel_General | LogMessage_Error,
131 
132  // Debug-only messages (not generated in release build)
133  Log_DebugMessage = LogChannel_Debug | LogMessage_Text,
134  Log_DebugWarning = LogChannel_Debug | LogMessage_Warning,
135  Log_DebugError = LogChannel_Debug | LogMessage_Error,
136  Log_DebugAssert = LogChannel_Debug | LogMessage_Assert
137 };
138 
139 
140 // GFXLOG_MESSAGE_FUNC macro, generates convenience printf-style inline functions for message types
141 #ifdef KY_CC_GNU
142 #define KY_LOG_VAARG_ATTRIBUTE(a,b) __attribute__((format (printf, a, b)))
143 #else
144 #define KY_LOG_VAARG_ATTRIBUTE(a,b)
145 #endif
146 
147 // Helper to extract filename from __FILE__ (cf KY_GET_COMPILED_FILENAME)
148 class LogHelper
149 {
150 public:
151  static const char* GetFilename(const char* file)
152  {
153  const char* separatorPos = SFstrrchr(file, Kaim::FilePath_Separator);
154  return (separatorPos ? separatorPos+1 : file);
155  }
156 };
157 #define KY_GET_COMPILED_FILENAME (Kaim::LogHelper::GetFilename(__FILE__))
158 
159 
160 //--------------------------------------------------------------------
161 // ***** Log
162 
163 // Log defines a base class interface that can be implemented to catch both
164 // debug and runtime messages.
165 // - Debug logging can be overridden by calling Log::SetGlobalLog.
166 // - GFx logging can also be configured per Intrnl::Loader/Movie by calling
167 // SetLog functions on those objects. Local movie log takes precedence
168 // over the globally installed one.
169 
170 class Log
171 {
172  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
173 
174  friend class System;
175 public:
176  Log() { }
177  virtual ~Log();
178 
179  // This virtual function receives all the messages,
180  // developers should override this function in order to do custom logging
181  virtual void LogMessageVarg(LogMessageId messageId, const char* fmt, va_list argList);
182 
183  // Call the logging function with specific message type, with no type filtering.
184  void LogMessageById(LogMessageId messageId,
185  const char* fmt, ...) KY_LOG_VAARG_ATTRIBUTE(3,4);
186 
187  // Log formating buffer size used by default LogMessageVarg. Longer strings are truncated.
188  enum { MaxLogBufferMessageSize = 2048 };
189 
190  // Helper used by LogMessageVarg to format the log message, writing the resulting
191  // string into buffer. It formats text based on fmt and appends prefix/new line
192  // based on LogMessageType.
193  static void FormatLog(char* buffer, unsigned bufferSize, LogMessageId messageId,
194  const char* fmt, va_list argList);
195 
196  // Default implementation of LogMessageVarg called by LogMessageVarg.
197  // Outputs content only in debug build.
198  static void DefaultLogMessageVarg(LogMessageId messageId,
199  const char* fmt, va_list argList);
200 
201  // Standard Log Inlines
202  inline void LogMessage(const char* fmt, ...) KY_LOG_VAARG_ATTRIBUTE(2,3)
203  {
204  va_list argList; va_start(argList, fmt);
205  LogMessageVarg(Log_Message, fmt, argList);
206  va_end(argList);
207  }
208  inline void LogError(const char* fmt, ...) KY_LOG_VAARG_ATTRIBUTE(2,3)
209  {
210  va_list argList; va_start(argList, fmt);
211  LogMessageVarg(Log_Error, fmt, argList);
212  va_end(argList);
213  }
214  inline void LogWarning(const char* fmt, ...) KY_LOG_VAARG_ATTRIBUTE(2,3)
215  {
216  va_list argList; va_start(argList, fmt);
217  LogMessageVarg(Log_Warning, fmt, argList);
218  va_end(argList);
219  }
220 
221 
222  // *** Global APIs
223 
224  // Global Log registration APIs.
225  // - Global log is used for KY_DEBUG messages.
226  // - Global log is NOT AddRefed, so users must keep it alive for logging.
227  // - It is also used by GFx by default Intrnl::Log when no log is specified.
228  static void SetGlobalLog(Log *log);
229  static Log* GetGlobalLog();
230 
231  // Returns a pointer to default log object used for debugging.
232  // This object is used by default for Global log if an alternative wasn't set.
233  // - Default implementation outputs to Debug output / console in
234  // DEBUG build only.
235  static Log* GetDefaultDebugLog();
236 };
237 
238 }
239 
240 #endif
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137