WebViewLogger.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using UnityEngine;
  17. namespace Vuplex.WebView {
  18. /// <summary>
  19. /// Utility class used internally for logging.
  20. /// </summary>
  21. public static class WebViewLogger {
  22. public static void Log(string message, bool enableFormatting = true) {
  23. if (enableFormatting) {
  24. Debug.Log(_format(message));
  25. } else {
  26. Debug.Log(PREFIX + message);
  27. }
  28. }
  29. public static void LogError(string message, bool enableFormatting = true) {
  30. if (enableFormatting) {
  31. Debug.LogError(_format(message));
  32. } else {
  33. Debug.LogError(PREFIX + message);
  34. }
  35. }
  36. public static void LogErrorFormat(string message, params object[] args) {
  37. Debug.LogErrorFormat(_format(message), args);
  38. }
  39. public static void LogErrorWithoutFormatting(string message) {
  40. Debug.LogError(PREFIX + message);
  41. }
  42. public static void LogFormat(string message, params object[] args) {
  43. Debug.LogFormat(_format(message), args);
  44. }
  45. public static void LogWarning(string message, bool enableFormatting = true) {
  46. if (enableFormatting) {
  47. Debug.LogWarning(_format(message));
  48. } else {
  49. Debug.LogWarning(PREFIX + message);
  50. }
  51. }
  52. public static void LogWarningFormat(string message, params object[] args) {
  53. Debug.LogWarningFormat(_format(message), args);
  54. }
  55. public static void LogWarningWithoutFormatting(string message) {
  56. Debug.LogWarning(PREFIX + message);
  57. }
  58. // Markup in log messages is only useful in the editor,
  59. // so omit the markup when not in the editor to prevent clutter.
  60. #if UNITY_EDITOR
  61. const string PREFIX = "<color=#12bae9ff>[3D WebView]</color> ";
  62. const string EM_OPENING_REPLACEMENT = "<color=#12bae9ff>";
  63. const string EM_CLOSING_REPLACEMENT = "</color>";
  64. #else
  65. const string PREFIX = "[3D WebView] ";
  66. const string EM_OPENING_REPLACEMENT = "";
  67. const string EM_CLOSING_REPLACEMENT = "";
  68. #endif
  69. static string _format(string originalMessage) {
  70. // Implement the <em> tag for highlighting.
  71. var formattedMessage = originalMessage;
  72. if (formattedMessage.Contains("<em>")) {
  73. formattedMessage = formattedMessage.Replace("<em>", EM_OPENING_REPLACEMENT)
  74. .Replace("</em>", EM_CLOSING_REPLACEMENT);
  75. }
  76. return PREFIX + formattedMessage;
  77. }
  78. }
  79. }