Skip to content

Commit

Permalink
Improove logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Borna authored and Borna committed May 13, 2024
1 parent 848adb3 commit e30b67a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 28 deletions.
93 changes: 69 additions & 24 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class Program
private WeakReference? contextref;
Type? Server_class;
Type? Remote_class;
public Program()
private Program()
{
server = null;
remote = null;
Expand All @@ -31,38 +31,50 @@ public Program()
[MethodImpl(MethodImplOptions.NoInlining)]
private void Load(string name)
{
context = new(null, true);
contextref = new(context);
string? path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (path != null)
{
Assembly? loaded = context.LoadFromAssemblyPath(Path.Combine(path, name));
Server_class = loaded?.GetTypes().Where(t => typeof(IServer).IsAssignableFrom(t) && !t.IsInterface).FirstOrDefault();
Remote_class = loaded?.GetTypes().Where(t => typeof(IRemote).IsAssignableFrom(t) && !t.IsInterface).FirstOrDefault();
try
{
context = new(null, true);
contextref = new(context);
string? path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (path != null)
{
Assembly? loaded = context.LoadFromAssemblyPath(Path.Combine(path, name));
Server_class = loaded?.GetTypes().Where(t => typeof(IServer).IsAssignableFrom(t) && !t.IsInterface).FirstOrDefault();
Remote_class = loaded?.GetTypes().Where(t => typeof(IRemote).IsAssignableFrom(t) && !t.IsInterface).FirstOrDefault();
}
} catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private async Task Unload()
{
writelogasync = null;
Server_class = null;
Remote_class = null;
if (server != null)
try
{
await server.Close();
if (await server.Closed.Task)
writelogasync = null;
Server_class = null;
Remote_class = null;
if (server != null)
{
await server.Close();
if (await server.Closed.Task)
{
Console.WriteLine("Server closed");
}
server = null;
}
if (remote != null)
{
Console.WriteLine("Server closed");
remote.Close();
remote = null;
}
server = null;
}
if (remote != null)
context?.Unload();
context = null;
} catch (Exception ex)
{
remote.Close();
remote = null;
Console.WriteLine(ex.ToString());
}
context?.Unload();
context = null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void Clean()
Expand Down Expand Up @@ -240,6 +252,23 @@ private void StartRemote(int attempt = 0)
Console.WriteLine(ex.ToString());
}
}
private void Update()
{
Console.Write("Path to update folder: ");
string? path = Console.ReadLine();
if(path != null)
{
string? serverpath = Assembly.GetExecutingAssembly().Location;
string[] files = Directory.GetFiles(path);
foreach(string file in files) {
Console.WriteLine($"update paths {file} {serverpath}");
}
}
else
{
Console.WriteLine("Path error.");
}
}
static async Task Main()
{
Program program = new();
Expand All @@ -248,7 +277,23 @@ static async Task Main()
await program.StartServer();
while (true)
{
Console.ReadLine();
string? input = Console.ReadLine();
if (input != null)
{
if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
program.Unload().Wait();
break;
}
else if (input.Equals("update", StringComparison.OrdinalIgnoreCase))
{
program.Update();
}
else
{
Console.WriteLine("Unknown command.");
}
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Server_base/PluginAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,18 @@ public void GetServerInfo(string localip, string remoteip, int remoteport, int t
/// <summary>
/// Run when the plugin throws Exception.
/// </summary>
/// <param name="ex">Exception.</param>
public void WriteLog(Exception ex)
{
throw new NotImplementedException();
}
/// <summary>
/// Run when Server throws Exception.
/// </summary>
/// <param name="ex">Exception.</param>
public void ServerLog(Exception ex)
{
throw new NotImplementedException();
}
}
}
34 changes: 30 additions & 4 deletions Server_base/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ public Server(string name, List<Interface> interfaces, KeyPair ecdh, WriteLogAsy
servers = new ConcurrentDictionary<string, Servers>();
List<TListener> listeners1 = [];
plugins = [];
LoadPlugins();
my = ecdh;
if (logfile != null)
if (!string.IsNullOrEmpty(logfile))
{
this.logfile = logfile;
}
else
{
this.logfile = "Server.log";
}
LoadPlugins();
Closed = new();
foreach (Interface iface in interfaces)
{
Expand Down Expand Up @@ -703,11 +703,37 @@ public async Task WriteLog(Exception ex)
{
await Writelogasync(log);
}
foreach (PluginInfo plugininfo in plugins)
{
try
{
plugininfo.Plugin.ServerLog(ex);
}
catch (Exception ex1)
{
if (ex1 is NotImplementedException)
{
//Disregard
}
else
{
try
{
plugininfo.Plugin.WriteLog(ex);
}
catch
{
//Disregard
}
}
}
}
}
catch (Exception)
catch (Exception ex2)
{
Console.WriteLine("Can't save log to file.");
Console.WriteLine($"Can't save log to file {logfile}.");
Console.WriteLine(log);
Console.WriteLine(ex2.ToString());
}
}
/// <summary>
Expand Down

0 comments on commit e30b67a

Please sign in to comment.