| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.IO;
- using System.Text;
- namespace Base
- {
- public static class Log
- {
- private static StreamWriter info;
- private static StreamWriter error;
- // 每多少秒发一次
- public static long SendToServerFrequency = 20 * 1000;
- public static long SendToServerTime;
- #if UNITY_EDITOR
- private static bool IsNeedFlush = true;
- #else
- private static bool IsNeedFlush = false;
- #endif
- static Log()
- {
- if (!Directory.Exists("../Logs"))
- {
- Directory.CreateDirectory("../Logs");
- }
- info = new StreamWriter($"../Logs/Log-Client-Info.txt", false, Encoding.Unicode, 1024);
- error = new StreamWriter($"../Logs/Log-Client-Error.txt", false, Encoding.Unicode, 1024);
- }
- public static void Warning(string msg)
- {
- DateTime dateTime = DateTime.Now;
- string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {msg}";
- info.WriteLine(s);
- if (IsNeedFlush)
- {
- info.Flush();
- }
- #if UNITY_EDITOR
- UnityEngine.Debug.LogWarning(s);
- #endif
- }
- public static void Info(string msg)
- {
- DateTime dateTime = DateTime.Now;
- string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {msg}";
- info.WriteLine(s);
- if (IsNeedFlush)
- {
- info.Flush();
- }
- #if UNITY_EDITOR
- UnityEngine.Debug.Log(s);
- #endif
- }
- public static void Error(string msg)
- {
- DateTime dateTime = DateTime.Now;
- string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {TimeHelper.ClientNow()} {msg}";
- error.WriteLine(s);
- if (IsNeedFlush)
- {
- error.Flush();
- }
- #if UNITY_EDITOR
- UnityEngine.Debug.LogError(s);
- #endif
- long timeNow = TimeHelper.ClientNow();
- if (timeNow - SendToServerTime > SendToServerFrequency)
- {
- SendToServerTime = timeNow;
- }
- }
- public static void Debug(string msg)
- {
- #if UNITY_EDITOR
- DateTime dateTime = DateTime.Now;
- string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {TimeHelper.ClientNow()} {msg}";
- UnityEngine.Debug.Log(s);
- #endif
- }
- public static void Flush()
- {
- info.Flush();
- error.Flush();
- }
- }
- }
|