WebViewLogger.cs 2.9 KB

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