Logger.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // <copyright file="Logger.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  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. // </copyright>
  16. namespace GooglePlayGames.OurUtils
  17. {
  18. using System;
  19. using UnityEngine;
  20. public class Logger
  21. {
  22. private static bool debugLogEnabled = false;
  23. public static bool DebugLogEnabled
  24. {
  25. get { return debugLogEnabled; }
  26. set { debugLogEnabled = value; }
  27. }
  28. private static bool warningLogEnabled = true;
  29. public static bool WarningLogEnabled
  30. {
  31. get { return warningLogEnabled; }
  32. set { warningLogEnabled = value; }
  33. }
  34. public static void d(string msg)
  35. {
  36. if (debugLogEnabled)
  37. {
  38. PlayGamesHelperObject.RunOnGameThread(() =>
  39. Debug.Log(ToLogMessage(string.Empty, "DEBUG", msg)));
  40. }
  41. }
  42. public static void w(string msg)
  43. {
  44. if (warningLogEnabled)
  45. {
  46. PlayGamesHelperObject.RunOnGameThread(() =>
  47. Debug.LogWarning(ToLogMessage("!!!", "WARNING", msg)));
  48. }
  49. }
  50. public static void e(string msg)
  51. {
  52. if (warningLogEnabled)
  53. {
  54. PlayGamesHelperObject.RunOnGameThread(() =>
  55. Debug.LogWarning(ToLogMessage("***", "ERROR", msg)));
  56. }
  57. }
  58. public static string describe(byte[] b)
  59. {
  60. return b == null ? "(null)" : "byte[" + b.Length + "]";
  61. }
  62. private static string ToLogMessage(string prefix, string logType, string msg)
  63. {
  64. string timeString = null;
  65. try
  66. {
  67. timeString = DateTime.Now.ToString("MM/dd/yy H:mm:ss zzz");
  68. }
  69. catch (Exception)
  70. {
  71. PlayGamesHelperObject.RunOnGameThread(() =>
  72. Debug.LogWarning("*** [Play Games Plugin " + PluginVersion.VersionString + "] ERROR: Failed to format DateTime.Now"));
  73. timeString = string.Empty;
  74. }
  75. return string.Format("{0} [Play Games Plugin " + PluginVersion.VersionString+ "] {1} {2}: {3}",
  76. prefix, timeString, logType, msg);
  77. }
  78. }
  79. }