| | 1 | | // Copyright (c) 2020-2023 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | | using System.Runtime.InteropServices; |
| | 8 | | using GDX.Collections; |
| | 9 | | using GDX.Collections.Generic; |
| | 10 | | using UnityEngine; |
| | 11 | | using UnityEngine.Diagnostics; |
| | 12 | | using Object = UnityEngine.Object; |
| | 13 | |
|
| | 14 | | namespace GDX.Experimental.Logging |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// A managed-only categorized hierarchical logging solution. |
| | 18 | | /// </summary> |
| | 19 | | public static class ManagedLog |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// A ring buffer structure of <see cref="LogEntry" />s. |
| | 23 | | /// </summary> |
| 1 | 24 | | static ConcurrentCircularBuffer<LogEntry> s_Buffer = new ConcurrentCircularBuffer<LogEntry>(1000); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// An index of labels per category. |
| | 28 | | /// </summary> |
| 1 | 29 | | static IntKeyDictionary<string> s_CustomCategories = new IntKeyDictionary<string>(10); |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// A flag structure containing if a category should output to the system console. |
| | 33 | | /// </summary> |
| 1 | 34 | | static BitArray64 s_EchoToConsole = new BitArray64(uint.MaxValue); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// A flag structure containing if a category should output to the Unity log handler. |
| | 38 | | /// </summary> |
| 1 | 39 | | static BitArray64 s_EchoToUnity = new BitArray64(uint.MaxValue); |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// The next identifier used when creating a new <see cref="LogEntry" />. |
| | 43 | | /// </summary> |
| | 44 | | static uint s_IdentifierHead; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Thread-safety management object. |
| | 48 | | /// </summary> |
| 1 | 49 | | static readonly object k_Lock = new object(); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Clears the internal buffer of <see cref="LogEntry" />s. |
| | 53 | | /// </summary> |
| | 54 | | public static void Clear() |
| 0 | 55 | | { |
| 0 | 56 | | s_Buffer.Clear(); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Registers a category identifier for logging purposes. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="categoryIdentifier"> |
| | 63 | | /// The designated identifier, must fall within a valid range. See |
| | 64 | | /// <see cref="LogCategory.MinimumIdentifier" /> and <see cref="LogCategory.MaximumIdentifier" /> |
| | 65 | | /// </param> |
| | 66 | | /// <param name="name">The category's name.</param> |
| | 67 | | /// <param name="outputToUnity">Should this category output to Unity's log handler?</param> |
| | 68 | | /// <param name="outputToConsole">Should this category output to the Console.</param> |
| | 69 | | public static void RegisterCategory(int categoryIdentifier, string name, bool outputToUnity = true, |
| | 70 | | bool outputToConsole = false) |
| 0 | 71 | | { |
| 0 | 72 | | if (categoryIdentifier <= LogCategory.MinimumIdentifier) |
| 0 | 73 | | { |
| 0 | 74 | | Error(LogCategory.GDX, |
| | 75 | | $"Unable to register category {categoryIdentifier.ToString()}:{name} as identifier below threshold { |
| 0 | 76 | | } |
| 0 | 77 | | else if (categoryIdentifier > LogCategory.MaximumIdentifier) |
| 0 | 78 | | { |
| 0 | 79 | | Error(LogCategory.GDX, |
| | 80 | | $"Unable to register category {categoryIdentifier.ToString()}:{name} as identifier exceeds maximum a |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | if (s_CustomCategories.ContainsKey(categoryIdentifier)) |
| 0 | 84 | | { |
| 0 | 85 | | Error(LogCategory.GDX, |
| | 86 | | $"Unable to register category {categoryIdentifier.ToString()}:{name} as identifier already in use.") |
| 0 | 87 | | } |
| | 88 | | else |
| 0 | 89 | | { |
| 0 | 90 | | s_CustomCategories.AddWithExpandCheck(categoryIdentifier, name); |
| 0 | 91 | | s_EchoToConsole[(byte)categoryIdentifier] = outputToConsole; |
| 0 | 92 | | s_EchoToUnity[(byte)categoryIdentifier] = outputToUnity; |
| 0 | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// Unregisters a category identifier. |
| | 98 | | /// </summary> |
| | 99 | | /// <param name="categoryIdentifier"></param> |
| | 100 | | public static void UnregisterCategory(int categoryIdentifier) |
| 0 | 101 | | { |
| 0 | 102 | | if (categoryIdentifier <= LogCategory.MinimumIdentifier) |
| 0 | 103 | | { |
| 0 | 104 | | return; |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | s_CustomCategories.TryRemove(categoryIdentifier); |
| 0 | 108 | | s_EchoToConsole[(byte)categoryIdentifier] = true; |
| 0 | 109 | | s_EchoToUnity[(byte)categoryIdentifier] = true; |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Gets the name of a category. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="categoryIdentifier">The category to evaluate.</param> |
| | 116 | | /// <returns>The label for the category.</returns> |
| | 117 | | public static string GetCategoryLabel(int categoryIdentifier) |
| 4 | 118 | | { |
| 4 | 119 | | if (categoryIdentifier <= LogCategory.MinimumIdentifier) |
| 4 | 120 | | { |
| 4 | 121 | | switch (categoryIdentifier) |
| | 122 | | { |
| | 123 | | case LogCategory.GDX: |
| 4 | 124 | | return "GDX"; |
| | 125 | | case LogCategory.Default: |
| 0 | 126 | | return "Default"; |
| | 127 | | case LogCategory.Platform: |
| 0 | 128 | | return "Platform"; |
| | 129 | | case LogCategory.Unity: |
| 0 | 130 | | return "Unity"; |
| | 131 | | case LogCategory.Input: |
| 0 | 132 | | return "Input"; |
| | 133 | | case LogCategory.Gameplay: |
| 0 | 134 | | return "Gameplay"; |
| | 135 | | case LogCategory.UI: |
| 0 | 136 | | return "UI"; |
| | 137 | | case LogCategory.Test: |
| 0 | 138 | | return "Test"; |
| | 139 | | default: |
| 0 | 140 | | return LogCategory.UndefinedLabel; |
| | 141 | | } |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | return s_CustomCategories.TryGetValue(categoryIdentifier, out string customCategory) |
| | 145 | | ? customCategory |
| | 146 | | : LogCategory.UndefinedLabel; |
| 4 | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Gets an array of <see cref="LogEntry" /> based on category. |
| | 151 | | /// </summary> |
| | 152 | | /// <param name="categoryIdentifier">The category identifier to filter by.</param> |
| | 153 | | /// <returns>An array of filtered <see cref="LogEntry" />.</returns> |
| | 154 | | public static LogEntry[] GetEntriesByCategory(int categoryIdentifier) |
| 0 | 155 | | { |
| 0 | 156 | | int count = s_Buffer.Count; |
| 0 | 157 | | SimpleList<LogEntry> entries = new SimpleList<LogEntry>(count); |
| 0 | 158 | | for (int i = 0; i < count; i++) |
| 0 | 159 | | { |
| 0 | 160 | | if (s_Buffer[i].Identifier == categoryIdentifier) |
| 0 | 161 | | { |
| 0 | 162 | | entries.AddUnchecked(s_Buffer[i]); |
| 0 | 163 | | } |
| 0 | 164 | | } |
| | 165 | |
|
| 0 | 166 | | entries.Compact(); |
| 0 | 167 | | return entries.Array; |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | /// <summary> |
| | 171 | | /// Gets an array of <see cref="LogEntry" /> based on parent identifier. |
| | 172 | | /// </summary> |
| | 173 | | /// <param name="parentIdentifier">The parent identifier to filter by.</param> |
| | 174 | | /// <returns>An array of filtered <see cref="LogEntry" />.</returns> |
| | 175 | | public static LogEntry[] GetEntriesByParent(int parentIdentifier) |
| 0 | 176 | | { |
| 0 | 177 | | int count = s_Buffer.Count; |
| 0 | 178 | | SimpleList<LogEntry> entries = new SimpleList<LogEntry>(count); |
| 0 | 179 | | for (int i = 0; i < count; i++) |
| 0 | 180 | | { |
| 0 | 181 | | if (s_Buffer[i].ParentIdentifier == parentIdentifier) |
| 0 | 182 | | { |
| 0 | 183 | | entries.AddUnchecked(s_Buffer[i]); |
| 0 | 184 | | } |
| 0 | 185 | | } |
| | 186 | |
|
| 0 | 187 | | entries.Compact(); |
| 0 | 188 | | return entries.Array; |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// Gets an array of <see cref="LogEntry" /> based on contents. |
| | 193 | | /// </summary> |
| | 194 | | /// <param name="searchQuery">The text query to search for.</param> |
| | 195 | | /// <returns>An array of filtered <see cref="LogEntry" />.</returns> |
| | 196 | | public static LogEntry[] GetEntriesBySearch(string searchQuery) |
| 0 | 197 | | { |
| 0 | 198 | | int count = s_Buffer.Count; |
| 0 | 199 | | SimpleList<LogEntry> entries = new SimpleList<LogEntry>(count); |
| 0 | 200 | | for (int i = 0; i < count; i++) |
| 0 | 201 | | { |
| 0 | 202 | | if (s_Buffer[i].Message.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) != -1 || |
| | 203 | | s_Buffer[i].SourceFilePath.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) != -1) |
| 0 | 204 | | { |
| 0 | 205 | | entries.AddUnchecked(s_Buffer[i]); |
| 0 | 206 | | } |
| 0 | 207 | | } |
| | 208 | |
|
| 0 | 209 | | entries.Compact(); |
| 0 | 210 | | return entries.Array; |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | /// <summary> |
| | 214 | | /// Get the last <see cref="LogEntry" /> added to the backing buffer. |
| | 215 | | /// </summary> |
| | 216 | | /// <returns>The last log entry.</returns> |
| | 217 | | public static LogEntry GetLastEntry() |
| 2 | 218 | | { |
| 2 | 219 | | return s_Buffer.GetBack(); |
| 2 | 220 | | } |
| | 221 | |
|
| | 222 | | /// <summary> |
| | 223 | | /// Sets the size of the internal <see cref="ConcurrentCircularBuffer{T}" /> based on a specific memory size |
| | 224 | | /// allocation. |
| | 225 | | /// </summary> |
| | 226 | | /// <param name="byteAllocation">The size in bytes to use for the internal buffer.</param> |
| | 227 | | public static void SetBufferSizeByAllocation(long byteAllocation) |
| 0 | 228 | | { |
| 0 | 229 | | s_Buffer = new ConcurrentCircularBuffer<LogEntry>((int)(byteAllocation / |
| | 230 | | Marshal.SizeOf( |
| | 231 | | typeof(LogEntry)))); |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | /// <summary> |
| | 235 | | /// Sets the size of the internal <see cref="ConcurrentCircularBuffer{T}" /> to be a specific number of entr |
| | 236 | | /// </summary> |
| | 237 | | /// <param name="numberOfLogEntries">The number of <see cref="LogEntry" /> to size the buffer too.</param> |
| | 238 | | public static void SetBufferSizeByCount(int numberOfLogEntries) |
| 0 | 239 | | { |
| 0 | 240 | | s_Buffer = new ConcurrentCircularBuffer<LogEntry>(numberOfLogEntries); |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | /// <summary> |
| | 244 | | /// Set if a category should output to the system console. |
| | 245 | | /// </summary> |
| | 246 | | /// <param name="categoryIdentifier">The unique category identifier.</param> |
| | 247 | | /// <param name="output">Should the log entry be outputted to the console?</param> |
| | 248 | | public static void SetConsoleOutput(int categoryIdentifier, bool output) |
| 0 | 249 | | { |
| 0 | 250 | | s_EchoToConsole[(byte)categoryIdentifier] = output; |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | /// <summary> |
| | 254 | | /// Set if a category should output to the unity logger. |
| | 255 | | /// </summary> |
| | 256 | | /// <param name="categoryIdentifier">The unique category identifier.</param> |
| | 257 | | /// <param name="output">Should the log entry be outputted to the unity logger?</param> |
| | 258 | | public static void SetUnityOutput(int categoryIdentifier, bool output) |
| 0 | 259 | | { |
| 0 | 260 | | s_EchoToUnity[(byte)categoryIdentifier] = output; |
| 0 | 261 | | } |
| | 262 | |
|
| | 263 | | /// <summary> |
| | 264 | | /// Log a debug message. |
| | 265 | | /// </summary> |
| | 266 | | /// <param name="category">The category identifier.</param> |
| | 267 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 268 | | /// <param name="parentIdentifier"> |
| | 269 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 270 | | /// associated with another. |
| | 271 | | /// </param> |
| | 272 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 273 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 274 | | public static uint Debug(int category, string message, uint parentIdentifier = 0, |
| | 275 | | // ReSharper disable InvalidXmlDocComment |
| | 276 | | [CallerMemberName] string memberName = "", |
| | 277 | | [CallerFilePath] string sourceFilePath = "", |
| | 278 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 279 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 280 | | { |
| 0 | 281 | | LogEntry newEntry = NewEntry(LogLevel.Debug, category, message, |
| | 282 | | parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 283 | |
|
| 0 | 284 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 285 | | { |
| 0 | 286 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 287 | | } |
| | 288 | |
|
| 0 | 289 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 290 | | { |
| 0 | 291 | | UnityEngine.Debug.unityLogger.Log(LogType.Log, |
| | 292 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput()); |
| 0 | 293 | | } |
| | 294 | |
|
| 0 | 295 | | return newEntry.Identifier; |
| 0 | 296 | | } |
| | 297 | |
|
| | 298 | | /// <summary> |
| | 299 | | /// Log a debug message with context. |
| | 300 | | /// </summary> |
| | 301 | | /// <remarks>The context-based versions of logging are slower.</remarks> |
| | 302 | | /// <param name="category">The category identifier.</param> |
| | 303 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 304 | | /// <param name="contextObject">A Unity-based context object.</param> |
| | 305 | | /// <param name="parentIdentifier"> |
| | 306 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 307 | | /// associated with another. |
| | 308 | | /// </param> |
| | 309 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 310 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 311 | | public static uint DebugWithContext(int category, string message, Object contextObject = null, |
| | 312 | | uint parentIdentifier = 0, |
| | 313 | | // ReSharper disable InvalidXmlDocComment |
| | 314 | | [CallerMemberName] string memberName = "", |
| | 315 | | [CallerFilePath] string sourceFilePath = "", |
| | 316 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 317 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 318 | | { |
| 0 | 319 | | LogEntry newEntry = NewEntry(LogLevel.Debug, category, message, |
| | 320 | | parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 321 | |
|
| 0 | 322 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 323 | | { |
| 0 | 324 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 325 | | } |
| | 326 | |
|
| 0 | 327 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 328 | | { |
| 0 | 329 | | UnityEngine.Debug.unityLogger.Log(LogType.Log, |
| | 330 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput(), contextObject); |
| 0 | 331 | | } |
| | 332 | |
|
| 0 | 333 | | return newEntry.Identifier; |
| 0 | 334 | | } |
| | 335 | |
|
| | 336 | | /// <summary> |
| | 337 | | /// Log some information. |
| | 338 | | /// </summary> |
| | 339 | | /// <param name="category">The category identifier.</param> |
| | 340 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 341 | | /// <param name="parentIdentifier"> |
| | 342 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 343 | | /// associated with another. |
| | 344 | | /// </param> |
| | 345 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 346 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 347 | | public static uint Info(int category, string message, uint parentIdentifier = 0, |
| | 348 | | // ReSharper disable InvalidXmlDocComment |
| | 349 | | [CallerMemberName] string memberName = "", |
| | 350 | | [CallerFilePath] string sourceFilePath = "", |
| | 351 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 352 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 353 | | { |
| 0 | 354 | | LogEntry newEntry = NewEntry(LogLevel.Info, category, message, |
| | 355 | | parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 356 | |
|
| 0 | 357 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 358 | | { |
| 0 | 359 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 360 | | } |
| | 361 | |
|
| 0 | 362 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 363 | | { |
| 0 | 364 | | UnityEngine.Debug.unityLogger.Log( |
| | 365 | | LogType.Log, |
| | 366 | | GetCategoryLabel(newEntry.CategoryIdentifier), |
| | 367 | | newEntry.ToUnityOutput()); |
| 0 | 368 | | } |
| | 369 | |
|
| 0 | 370 | | return newEntry.Identifier; |
| 0 | 371 | | } |
| | 372 | |
|
| | 373 | | /// <summary> |
| | 374 | | /// Log some information with context. |
| | 375 | | /// </summary> |
| | 376 | | /// <remarks>The context-based versions of logging are slower.</remarks> |
| | 377 | | /// <param name="category">The category identifier.</param> |
| | 378 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 379 | | /// <param name="contextObject">A Unity-based context object.</param> |
| | 380 | | /// <param name="parentIdentifier"> |
| | 381 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 382 | | /// associated with another. |
| | 383 | | /// </param> |
| | 384 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 385 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 386 | | public static uint InfoWithContext(int category, string message, Object contextObject = null, |
| | 387 | | uint parentIdentifier = 0, |
| | 388 | | // ReSharper disable InvalidXmlDocComment |
| | 389 | | [CallerMemberName] string memberName = "", |
| | 390 | | [CallerFilePath] string sourceFilePath = "", |
| | 391 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 392 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 393 | | { |
| 0 | 394 | | LogEntry newEntry = NewEntry(LogLevel.Info, category, message, |
| | 395 | | parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 396 | |
|
| 0 | 397 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 398 | | { |
| 0 | 399 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 400 | | } |
| | 401 | |
|
| 0 | 402 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 403 | | { |
| 0 | 404 | | UnityEngine.Debug.unityLogger.Log( |
| | 405 | | LogType.Log, |
| | 406 | | GetCategoryLabel(newEntry.CategoryIdentifier), |
| | 407 | | newEntry.ToUnityOutput(), contextObject); |
| 0 | 408 | | } |
| | 409 | |
|
| 0 | 410 | | return newEntry.Identifier; |
| 0 | 411 | | } |
| | 412 | |
|
| | 413 | | /// <summary> |
| | 414 | | /// Log a warning. |
| | 415 | | /// </summary> |
| | 416 | | /// <param name="category">The category identifier.</param> |
| | 417 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 418 | | /// <param name="parentIdentifier"> |
| | 419 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 420 | | /// associated with another. |
| | 421 | | /// </param> |
| | 422 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 423 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 424 | | public static uint Warning(int category, string message, uint parentIdentifier = 0, |
| | 425 | | // ReSharper disable InvalidXmlDocComment |
| | 426 | | [CallerMemberName] string memberName = "", |
| | 427 | | [CallerFilePath] string sourceFilePath = "", |
| | 428 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 429 | | // ReSharper restore InvalidXmlDocComment |
| 2 | 430 | | { |
| 2 | 431 | | LogEntry newEntry = NewEntry(LogLevel.Warning, category, |
| | 432 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 433 | |
|
| 2 | 434 | | if (s_EchoToConsole[(byte)category]) |
| 2 | 435 | | { |
| 2 | 436 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 2 | 437 | | } |
| | 438 | |
|
| 2 | 439 | | if (s_EchoToUnity[(byte)category]) |
| 2 | 440 | | { |
| 2 | 441 | | UnityEngine.Debug.unityLogger.Log(LogType.Warning, |
| | 442 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput()); |
| 2 | 443 | | } |
| | 444 | |
|
| 2 | 445 | | return newEntry.Identifier; |
| 2 | 446 | | } |
| | 447 | |
|
| | 448 | | /// <summary> |
| | 449 | | /// Log a warning with context. |
| | 450 | | /// </summary> |
| | 451 | | /// <remarks>The context-based versions of logging are slower.</remarks> |
| | 452 | | /// <param name="category">The category identifier.</param> |
| | 453 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 454 | | /// <param name="contextObject">A Unity-based context object.</param> |
| | 455 | | /// <param name="parentIdentifier"> |
| | 456 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 457 | | /// associated with another. |
| | 458 | | /// </param> |
| | 459 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 460 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 461 | | public static uint WarningWithContext(int category, string message, Object contextObject = null, |
| | 462 | | uint parentIdentifier = 0, |
| | 463 | | // ReSharper disable InvalidXmlDocComment |
| | 464 | | [CallerMemberName] string memberName = "", |
| | 465 | | [CallerFilePath] string sourceFilePath = "", |
| | 466 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 467 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 468 | | { |
| 0 | 469 | | LogEntry newEntry = NewEntry(LogLevel.Warning, category, |
| | 470 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 471 | |
|
| 0 | 472 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 473 | | { |
| 0 | 474 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 475 | | } |
| | 476 | |
|
| 0 | 477 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 478 | | { |
| 0 | 479 | | UnityEngine.Debug.unityLogger.Log(LogType.Warning, |
| | 480 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput(), contextObject); |
| 0 | 481 | | } |
| | 482 | |
|
| 0 | 483 | | return newEntry.Identifier; |
| 0 | 484 | | } |
| | 485 | |
|
| | 486 | | /// <summary> |
| | 487 | | /// Log an error. |
| | 488 | | /// </summary> |
| | 489 | | /// <param name="category">The category identifier.</param> |
| | 490 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 491 | | /// <param name="parentIdentifier"> |
| | 492 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 493 | | /// associated with another. |
| | 494 | | /// </param> |
| | 495 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 496 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 497 | | public static uint Error(int category, string message, uint parentIdentifier = 0, |
| | 498 | | // ReSharper disable InvalidXmlDocComment |
| | 499 | | [CallerMemberName] string memberName = "", |
| | 500 | | [CallerFilePath] string sourceFilePath = "", |
| | 501 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 502 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 503 | | { |
| 0 | 504 | | LogEntry newEntry = NewEntry(LogLevel.Error, category, |
| | 505 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 506 | |
|
| 0 | 507 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 508 | | { |
| 0 | 509 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 510 | | } |
| | 511 | |
|
| 0 | 512 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 513 | | { |
| 0 | 514 | | UnityEngine.Debug.unityLogger.Log(LogType.Error, |
| | 515 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput()); |
| 0 | 516 | | } |
| | 517 | |
|
| 0 | 518 | | return newEntry.Identifier; |
| 0 | 519 | | } |
| | 520 | |
|
| | 521 | | /// <summary> |
| | 522 | | /// Log an error with context. |
| | 523 | | /// </summary> |
| | 524 | | /// <remarks>The context-based versions of logging are slower.</remarks> |
| | 525 | | /// <param name="category">The category identifier.</param> |
| | 526 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 527 | | /// <param name="contextObject">A Unity-based context object.</param> |
| | 528 | | /// <param name="parentIdentifier"> |
| | 529 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 530 | | /// associated with another. |
| | 531 | | /// </param> |
| | 532 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 533 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 534 | | public static uint ErrorWithContext(int category, string message, Object contextObject = null, |
| | 535 | | uint parentIdentifier = 0, |
| | 536 | | // ReSharper disable InvalidXmlDocComment |
| | 537 | | [CallerMemberName] string memberName = "", |
| | 538 | | [CallerFilePath] string sourceFilePath = "", |
| | 539 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 540 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 541 | | { |
| 0 | 542 | | LogEntry newEntry = NewEntry(LogLevel.Error, category, |
| | 543 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 544 | |
|
| 0 | 545 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 546 | | { |
| 0 | 547 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 548 | | } |
| | 549 | |
|
| 0 | 550 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 551 | | { |
| 0 | 552 | | UnityEngine.Debug.unityLogger.Log(LogType.Error, |
| | 553 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput(), contextObject); |
| 0 | 554 | | } |
| | 555 | |
|
| 0 | 556 | | return newEntry.Identifier; |
| 0 | 557 | | } |
| | 558 | |
|
| | 559 | | /// <summary> |
| | 560 | | /// Log an exception. |
| | 561 | | /// </summary> |
| | 562 | | /// <param name="category">The category identifier.</param> |
| | 563 | | /// <param name="exception">The exception object.</param> |
| | 564 | | /// <param name="parentIdentifier"> |
| | 565 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 566 | | /// associated with another. |
| | 567 | | /// </param> |
| | 568 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 569 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 570 | | public static uint Exception(int category, Exception exception, uint parentIdentifier = 0, |
| | 571 | | // ReSharper disable InvalidXmlDocComment |
| | 572 | | [CallerMemberName] string memberName = "", |
| | 573 | | [CallerFilePath] string sourceFilePath = "", |
| | 574 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 575 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 576 | | { |
| 0 | 577 | | LogEntry newEntry = NewEntry(LogLevel.Exception, category, |
| | 578 | | exception, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 579 | |
|
| 0 | 580 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 581 | | { |
| 0 | 582 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 583 | | } |
| | 584 | |
|
| 0 | 585 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 586 | | { |
| 0 | 587 | | UnityEngine.Debug.unityLogger.Log(LogType.Exception, |
| | 588 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput()); |
| 0 | 589 | | } |
| | 590 | |
|
| 0 | 591 | | return newEntry.Identifier; |
| 0 | 592 | | } |
| | 593 | |
|
| | 594 | | /// <summary> |
| | 595 | | /// Log an exception with context, |
| | 596 | | /// </summary> |
| | 597 | | /// <param name="category">The category identifier.</param> |
| | 598 | | /// <param name="exception">The exception object.</param> |
| | 599 | | /// <param name="contextObject">A Unity-based context object.</param> |
| | 600 | | /// <param name="parentIdentifier"> |
| | 601 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 602 | | /// associated with another. |
| | 603 | | /// </param> |
| | 604 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 605 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 606 | | public static uint ExceptionWithContext(int category, |
| | 607 | | Exception exception, |
| | 608 | | Object contextObject = null, uint parentIdentifier = 0, |
| | 609 | | // ReSharper disable InvalidXmlDocComment |
| | 610 | | [CallerMemberName] string memberName = "", |
| | 611 | | [CallerFilePath] string sourceFilePath = "", |
| | 612 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 613 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 614 | | { |
| 0 | 615 | | LogEntry newEntry = NewEntry(LogLevel.Exception, category, |
| | 616 | | exception, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 617 | |
|
| 0 | 618 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 619 | | { |
| 0 | 620 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 621 | | } |
| | 622 | |
|
| 0 | 623 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 624 | | { |
| 0 | 625 | | UnityEngine.Debug.unityLogger.Log(LogType.Exception, |
| | 626 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput(), contextObject); |
| 0 | 627 | | } |
| | 628 | |
|
| 0 | 629 | | return newEntry.Identifier; |
| 0 | 630 | | } |
| | 631 | |
|
| | 632 | | /// <summary> |
| | 633 | | /// Log an assertion failure. |
| | 634 | | /// </summary> |
| | 635 | | /// <param name="category">The category identifier.</param> |
| | 636 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 637 | | /// <param name="parentIdentifier"> |
| | 638 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 639 | | /// associated with another. |
| | 640 | | /// </param> |
| | 641 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 642 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 643 | | public static uint Assertion(int category, string message, uint parentIdentifier = 0, |
| | 644 | | // ReSharper disable InvalidXmlDocComment |
| | 645 | | [CallerMemberName] string memberName = "", |
| | 646 | | [CallerFilePath] string sourceFilePath = "", |
| | 647 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 648 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 649 | | { |
| 0 | 650 | | LogEntry newEntry = NewEntry(LogLevel.Assertion, category, |
| | 651 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 652 | |
|
| 0 | 653 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 654 | | { |
| 0 | 655 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 656 | | } |
| | 657 | |
|
| 0 | 658 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 659 | | { |
| 0 | 660 | | UnityEngine.Debug.unityLogger.Log(LogType.Assert, |
| | 661 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput()); |
| 0 | 662 | | } |
| | 663 | |
|
| 0 | 664 | | return newEntry.Identifier; |
| 0 | 665 | | } |
| | 666 | |
|
| | 667 | | /// <summary> |
| | 668 | | /// Log an assertion failure with context. |
| | 669 | | /// </summary> |
| | 670 | | /// <remarks>The context-based versions of logging are slower.</remarks> |
| | 671 | | /// <param name="category">The category identifier.</param> |
| | 672 | | /// <param name="message">The <see cref="LogEntry" /> message.</param> |
| | 673 | | /// <param name="contextObject">A Unity-based context object.</param> |
| | 674 | | /// <param name="parentIdentifier"> |
| | 675 | | /// The optional parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 676 | | /// associated with another. |
| | 677 | | /// </param> |
| | 678 | | /// <returns>The created <see cref="LogEntry" />'s Identifier.</returns> |
| | 679 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 680 | | public static uint AssertionWithContext(int category, string message, Object contextObject = null, |
| | 681 | | uint parentIdentifier = 0, |
| | 682 | | // ReSharper disable InvalidXmlDocComment |
| | 683 | | [CallerMemberName] string memberName = "", |
| | 684 | | [CallerFilePath] string sourceFilePath = "", |
| | 685 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 686 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 687 | | { |
| 0 | 688 | | LogEntry newEntry = NewEntry(LogLevel.Assertion, category, |
| | 689 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 690 | |
|
| 0 | 691 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 692 | | { |
| 0 | 693 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 694 | | } |
| | 695 | |
|
| 0 | 696 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 697 | | { |
| 0 | 698 | | UnityEngine.Debug.unityLogger.Log(LogType.Assert, |
| | 699 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput(), contextObject); |
| 0 | 700 | | } |
| | 701 | |
|
| 0 | 702 | | return newEntry.Identifier; |
| 0 | 703 | | } |
| | 704 | |
|
| | 705 | | /// <summary> |
| | 706 | | /// Logs a fatal event and forces Unity to crash. |
| | 707 | | /// </summary> |
| | 708 | | /// <remarks> |
| | 709 | | /// The crash ensures that we stop a cascade of problems, allowing for back tracing. The context object base |
| | 710 | | /// methods should no be the preferred method of logging as they require a main-thread lock internally insid |
| | 711 | | /// </remarks> |
| | 712 | | /// <param name="category">The category identifier to associate this event with.</param> |
| | 713 | | /// <param name="message"></param> |
| | 714 | | /// <param name="parentIdentifier"> |
| | 715 | | /// The parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 716 | | /// associated with another. |
| | 717 | | /// </param> |
| | 718 | | /// <returns>The newly created <see cref="LogEntry" />'s Identifier.</returns> |
| | 719 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 720 | | public static uint Fatal(int category, string message, uint parentIdentifier = 0, |
| | 721 | | // ReSharper disable InvalidXmlDocComment |
| | 722 | | [CallerMemberName] string memberName = "", |
| | 723 | | [CallerFilePath] string sourceFilePath = "", |
| | 724 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 725 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 726 | | { |
| 0 | 727 | | LogEntry newEntry = NewEntry(LogLevel.Fatal, category, |
| | 728 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 729 | |
|
| 0 | 730 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 731 | | { |
| 0 | 732 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 733 | | } |
| | 734 | |
|
| 0 | 735 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 736 | | { |
| 0 | 737 | | UnityEngine.Debug.unityLogger.Log(LogType.Error, |
| | 738 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput()); |
| 0 | 739 | | } |
| | 740 | |
|
| 0 | 741 | | Utils.ForceCrash(ForcedCrashCategory.FatalError); |
| | 742 | |
|
| 0 | 743 | | return newEntry.Identifier; |
| 0 | 744 | | } |
| | 745 | |
|
| | 746 | | /// <summary> |
| | 747 | | /// Logs a fatal event and forces Unity to crash. |
| | 748 | | /// </summary> |
| | 749 | | /// <remarks> |
| | 750 | | /// The crash ensures that we stop a cascade of problems, allowing for back tracing. The context object base |
| | 751 | | /// methods should no be the preferred method of logging as they require a main-thread lock internally insid |
| | 752 | | /// </remarks> |
| | 753 | | /// <param name="category">The category identifier to associate this event with.</param> |
| | 754 | | /// <param name="message"></param> |
| | 755 | | /// <param name="contextObject">A unity-based context object.</param> |
| | 756 | | /// <param name="parentIdentifier"> |
| | 757 | | /// The parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 758 | | /// associated with another. |
| | 759 | | /// </param> |
| | 760 | | /// <returns>The newly created <see cref="LogEntry" />'s Identifier.</returns> |
| | 761 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 762 | | public static uint FatalWithContext(int category, string message, Object contextObject = null, |
| | 763 | | uint parentIdentifier = 0, |
| | 764 | | // ReSharper disable InvalidXmlDocComment |
| | 765 | | [CallerMemberName] string memberName = "", |
| | 766 | | [CallerFilePath] string sourceFilePath = "", |
| | 767 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 768 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 769 | | { |
| 0 | 770 | | LogEntry newEntry = NewEntry(LogLevel.Fatal, category, |
| | 771 | | message, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 772 | |
|
| 0 | 773 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 774 | | { |
| 0 | 775 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 776 | | } |
| | 777 | |
|
| 0 | 778 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 779 | | { |
| 0 | 780 | | UnityEngine.Debug.unityLogger.Log(LogType.Error, |
| | 781 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput(), contextObject); |
| 0 | 782 | | } |
| | 783 | |
|
| 0 | 784 | | Utils.ForceCrash(ForcedCrashCategory.FatalError); |
| | 785 | |
|
| 0 | 786 | | return newEntry.Identifier; |
| 0 | 787 | | } |
| | 788 | |
|
| | 789 | | /// <summary> |
| | 790 | | /// Logs the current stack trace. |
| | 791 | | /// </summary> |
| | 792 | | /// <remarks>Pulled from <see cref="Environment.StackTrace " />.</remarks> |
| | 793 | | /// <param name="category">The category identifier to associate this event with.</param> |
| | 794 | | /// <param name="parentIdentifier"> |
| | 795 | | /// The parent <see cref="LogEntry" />'s Identifier, if this event is meant to be |
| | 796 | | /// associated with another. |
| | 797 | | /// </param> |
| | 798 | | /// <returns>The newly created <see cref="LogEntry" />'s Identifier.</returns> |
| | 799 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 800 | | public static uint Trace(int category = 0, uint parentIdentifier = 0, |
| | 801 | | // ReSharper disable InvalidXmlDocComment |
| | 802 | | [CallerMemberName] string memberName = "", |
| | 803 | | [CallerFilePath] string sourceFilePath = "", |
| | 804 | | [CallerLineNumber] int sourceLineNumber = 0) |
| | 805 | | // ReSharper restore InvalidXmlDocComment |
| 0 | 806 | | { |
| 0 | 807 | | LogEntry newEntry = NewEntry(LogLevel.Trace, category, |
| | 808 | | Environment.StackTrace, parentIdentifier, memberName, sourceFilePath, sourceLineNumber); |
| | 809 | |
|
| 0 | 810 | | if (s_EchoToConsole[(byte)category]) |
| 0 | 811 | | { |
| 0 | 812 | | Console.WriteLine(newEntry.ToConsoleOutput()); |
| 0 | 813 | | } |
| | 814 | |
|
| 0 | 815 | | if (s_EchoToUnity[(byte)category]) |
| 0 | 816 | | { |
| 0 | 817 | | UnityEngine.Debug.unityLogger.Log(LogType.Assert, |
| | 818 | | GetCategoryLabel(newEntry.CategoryIdentifier), newEntry.ToUnityOutput()); |
| 0 | 819 | | } |
| | 820 | |
|
| 0 | 821 | | return newEntry.Identifier; |
| 0 | 822 | | } |
| | 823 | |
|
| | 824 | | /// <summary> |
| | 825 | | /// Creates and adds a new log entry to the <see cref="s_Buffer" />. |
| | 826 | | /// </summary> |
| | 827 | | /// <param name="level">The type/level of message being emitted.</param> |
| | 828 | | /// <param name="category">The category of the message being emitted.</param> |
| | 829 | | /// <param name="message">The content.</param> |
| | 830 | | /// <param name="parentIdentifier">The parent <see cref="LogEntry" />'s Identifier.</param> |
| | 831 | | /// <param name="memberName">The invoking member's name.</param> |
| | 832 | | /// <param name="sourceFilePath">The source file where the member is found.</param> |
| | 833 | | /// <param name="sourceLineNumber">The line number in the source file where the member is invoking the emit.</pa |
| | 834 | | /// <returns>The newly created <see cref="LogEntry" />'s Identifier.</returns> |
| | 835 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 836 | | static LogEntry NewEntry(LogLevel level, int category, string message, uint parentIdentifier = 0u, |
| | 837 | | string memberName = "", string sourceFilePath = "", int sourceLineNumber = 0) |
| 2 | 838 | | { |
| 2 | 839 | | LogEntry newEntry = new LogEntry |
| | 840 | | { |
| | 841 | | Timestamp = DateTime.UtcNow, |
| | 842 | | Level = level, |
| | 843 | | Identifier = NextIdentifier(), |
| | 844 | | CategoryIdentifier = category, |
| | 845 | | ParentIdentifier = parentIdentifier, |
| | 846 | | Message = message, |
| | 847 | | MemberName = memberName, |
| | 848 | | SourceFilePath = sourceFilePath, |
| | 849 | | SourceLineNumber = sourceLineNumber |
| | 850 | | }; |
| 2 | 851 | | s_Buffer.Add(newEntry); |
| 2 | 852 | | return newEntry; |
| 2 | 853 | | } |
| | 854 | |
|
| | 855 | | /// <summary> |
| | 856 | | /// Creates and adds a new log entry to the <see cref="s_Buffer" /> from a <see cref="System.Exception" />. |
| | 857 | | /// </summary> |
| | 858 | | /// <param name="level">The type/level of message being emitted.</param> |
| | 859 | | /// <param name="category">The category of the message being emitted.</param> |
| | 860 | | /// <param name="exception">The emitted exception.</param> |
| | 861 | | /// <param name="parentIdentifier">The parent <see cref="LogEntry" />'s Identifier.</param> |
| | 862 | | /// <param name="memberName">The invoking member's name.</param> |
| | 863 | | /// <param name="sourceFilePath">The source file where the member is found.</param> |
| | 864 | | /// <param name="sourceLineNumber">The line number in the source file where the member is invoking the emit.</pa |
| | 865 | | /// <returns>The newly created <see cref="LogEntry" />'s Identifier.</returns> |
| | 866 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 867 | | static LogEntry NewEntry(LogLevel level, int category, Exception exception, uint parentIdentifier = 0u, |
| | 868 | | string memberName = "", string sourceFilePath = "", int sourceLineNumber = 0) |
| 0 | 869 | | { |
| 0 | 870 | | LogEntry newEntry = new LogEntry |
| | 871 | | { |
| | 872 | | Timestamp = DateTime.UtcNow, |
| | 873 | | Level = level, |
| | 874 | | Identifier = NextIdentifier(), |
| | 875 | | CategoryIdentifier = category, |
| | 876 | | ParentIdentifier = parentIdentifier, |
| | 877 | | Message = exception.Message, |
| | 878 | | MemberName = memberName, |
| | 879 | | SourceFilePath = sourceFilePath, |
| | 880 | | SourceLineNumber = sourceLineNumber |
| | 881 | | }; |
| 0 | 882 | | s_Buffer.Add(newEntry); |
| | 883 | |
|
| 0 | 884 | | return newEntry; |
| 0 | 885 | | } |
| | 886 | |
|
| | 887 | | /// <summary> |
| | 888 | | /// Get the next identifier to use for a log entry |
| | 889 | | /// </summary> |
| | 890 | | /// <remarks>This will eventually roll over.</remarks> |
| | 891 | | /// <returns>An unsigned integer representing a pseudo-unique log entry identifier.</returns> |
| | 892 | | static uint NextIdentifier() |
| 2 | 893 | | { |
| 2 | 894 | | lock (k_Lock) |
| 2 | 895 | | { |
| 2 | 896 | | s_IdentifierHead++; |
| | 897 | |
|
| | 898 | | // We need to ensure this never hits it in rollover |
| 2 | 899 | | if (s_IdentifierHead == 0) |
| 0 | 900 | | { |
| 0 | 901 | | s_IdentifierHead++; |
| 0 | 902 | | } |
| | 903 | |
|
| 2 | 904 | | return ++s_IdentifierHead; |
| | 905 | | } |
| 2 | 906 | | } |
| | 907 | | } |
| | 908 | | } |