using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Runtime.Remoting.Messaging;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace Hydro.CommonBase
|
{
|
public class Log
|
{
|
public static int LengthLimit = 100000;
|
public static bool Enable = false;
|
public static bool isAdded = false;
|
public static object lockObj=new object();
|
//private static StringBuilder sb = new StringBuilder();
|
private static Dictionary<string, StringBuilder> sbs = new Dictionary<string, StringBuilder>();
|
public static string lastTime=DateTime.Now.ToString("HH:mm:ss");
|
private static Dictionary<string, List<DateTime>> ts = new Dictionary<string, List<DateTime>>();
|
public static void Add(string txt,string name="default",int mode=0, int level=0)
|
{
|
|
if (!Enable) return;
|
lock (lockObj)
|
{
|
isAdded = true;
|
if (!sbs.ContainsKey(name))
|
{
|
sbs.Add(name, new StringBuilder());
|
ts.Add(name,new List<DateTime>() { DateTime.Now });
|
}
|
|
var time = DateTime.Now;
|
var now = time.ToString("HH:mm:ss");
|
string nowtxt = "";
|
if (now != lastTime)
|
{
|
nowtxt= $"\r\n{now}";
|
}
|
|
string tt = "";
|
for (int i = 1;i<=level;i++)
|
{
|
tt += "\t";
|
if (ts[name].Count<i+1)
|
{
|
ts[name].Add(default(DateTime));
|
}
|
if (ts[name][i] < ts[name][i-1])
|
{
|
ts[name][i] = ts[name][i-1];
|
}
|
}
|
string signtxt = "";
|
if (mode == 1) signtxt = "{";
|
else if (mode == 2)
|
{
|
signtxt = "}\r\n>";
|
}
|
else if (level==0)
|
{
|
signtxt = ":";
|
}
|
sbs[name].Append($"({(time - ts[name][level]).TotalMilliseconds})\r\n{nowtxt}{signtxt}{tt}{time.ToString("fff")}\t{txt}\t");
|
lastTime = now;
|
ts[name][level] = time;
|
}
|
}
|
public static void Clear()
|
{
|
sbs.Clear();
|
ts.Clear();
|
}
|
//private static string Text { get { return sb.ToString(); } }
|
|
public static void Output()
|
{
|
if (!Enable) return;
|
lock (lockObj)
|
{
|
foreach (var key in sbs.Keys)
|
{
|
var name = key == "default" ? "" : key;
|
var file = $@"Log\log_{name}.lua";
|
|
if (!Directory.Exists("Log\\")) Directory.CreateDirectory("Log\\");
|
|
StreamWriter sw = null;
|
try
|
{
|
sw = new StreamWriter(file);
|
sw.WriteLine(sbs[key].ToString());
|
sw.Close();
|
if (sbs[key].Length>LengthLimit)
|
{
|
FileCopy.Copy(file, $@"Log\log_{name}_bk_{DateTime.Now.ToString("yyyyMMddHHmmss")}.lua", true);
|
sbs[key].Clear();
|
}
|
}
|
catch
|
{
|
if (sw != null) sw.Close();
|
}
|
}
|
|
}
|
|
}
|
|
public static void OpenFile()
|
{
|
foreach (var key in sbs.Keys)
|
{
|
var name = key == "default" ? "" : key;
|
var file = $@"Log\log_{name}.lua";
|
|
StreamWriter sw = null;
|
try
|
{
|
System.Diagnostics.Process.Start(file);
|
}
|
catch
|
{
|
|
}
|
}
|
}
|
}
|
|
}
|