using System; using UnityEngine; using Conditional = System.Diagnostics.ConditionalAttribute; namespace Mirror.SimpleWeb { public static class Log { // used for Conditional const string SIMPLEWEB_LOG_ENABLED = nameof(SIMPLEWEB_LOG_ENABLED); const string DEBUG = nameof(DEBUG); public enum Levels { none = 0, error = 1, warn = 2, info = 3, verbose = 4, } public static Levels level = Levels.none; public static string BufferToString(byte[] buffer, int offset = 0, int? length = null) { return BitConverter.ToString(buffer, offset, length ?? buffer.Length); } [Conditional(SIMPLEWEB_LOG_ENABLED)] public static void DumpBuffer(string label, byte[] buffer, int offset, int length) { if (level < Levels.verbose) return; Debug.Log($"VERBOSE: {label}: {BufferToString(buffer, offset, length)}"); } [Conditional(SIMPLEWEB_LOG_ENABLED)] public static void DumpBuffer(string label, ArrayBuffer arrayBuffer) { if (level < Levels.verbose) return; Debug.Log($"VERBOSE: {label}: {BufferToString(arrayBuffer.array, 0, arrayBuffer.count)}"); } [Conditional(SIMPLEWEB_LOG_ENABLED)] public static void Verbose(string msg, bool showColor = true) { if (level < Levels.verbose) return; if (showColor) Debug.Log($"VERBOSE: {msg}"); else Debug.Log($"VERBOSE: {msg}"); } [Conditional(SIMPLEWEB_LOG_ENABLED)] public static void Info(string msg, bool showColor = true) { if (level < Levels.info) return; if (showColor) Debug.Log($"INFO: {msg}"); else Debug.Log($"INFO: {msg}"); } /// /// An expected Exception was caught, useful for debugging but not important /// /// /// [Conditional(SIMPLEWEB_LOG_ENABLED)] public static void InfoException(Exception e) { if (level < Levels.info) return; Debug.Log($"INFO_EXCEPTION: {e.GetType().Name} Message: {e.Message}"); } [Conditional(SIMPLEWEB_LOG_ENABLED), Conditional(DEBUG)] public static void Warn(string msg, bool showColor = true) { if (level < Levels.warn) return; if (showColor) Debug.LogWarning($"WARN: {msg}"); else Debug.LogWarning($"WARN: {msg}"); } [Conditional(SIMPLEWEB_LOG_ENABLED), Conditional(DEBUG)] public static void Error(string msg, bool showColor = true) { if (level < Levels.error) return; if (showColor) Debug.LogError($"ERROR: {msg}"); else Debug.LogError($"ERROR: {msg}"); } public static void Exception(Exception e) { // always log Exceptions Debug.LogError($"EXCEPTION: {e.GetType().Name} Message: {e.Message}"); } } }