123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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: <color=blue>{label}: {BufferToString(buffer, offset, length)}</color>");
- }
- [Conditional(SIMPLEWEB_LOG_ENABLED)]
- public static void DumpBuffer(string label, ArrayBuffer arrayBuffer)
- {
- if (level < Levels.verbose)
- return;
- Debug.Log($"VERBOSE: <color=blue>{label}: {BufferToString(arrayBuffer.array, 0, arrayBuffer.count)}</color>");
- }
- [Conditional(SIMPLEWEB_LOG_ENABLED)]
- public static void Verbose(string msg, bool showColor = true)
- {
- if (level < Levels.verbose)
- return;
- if (showColor)
- Debug.Log($"VERBOSE: <color=blue>{msg}</color>");
- 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: <color=blue>{msg}</color>");
- else
- Debug.Log($"INFO: {msg}");
- }
- /// <summary>
- /// An expected Exception was caught, useful for debugging but not important
- /// </summary>
- /// <param name="msg"></param>
- /// <param name="showColor"></param>
- [Conditional(SIMPLEWEB_LOG_ENABLED)]
- public static void InfoException(Exception e)
- {
- if (level < Levels.info)
- return;
- Debug.Log($"INFO_EXCEPTION: <color=blue>{e.GetType().Name}</color> 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: <color=orange>{msg}</color>");
- 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: <color=red>{msg}</color>");
- else
- Debug.LogError($"ERROR: {msg}");
- }
- public static void Exception(Exception e)
- {
- // always log Exceptions
- Debug.LogError($"EXCEPTION: <color=red>{e.GetType().Name}</color> Message: {e.Message}");
- }
- }
- }
|