Browse Source

added rotating log

pull/123/head
notgiven688 3 years ago
parent
commit
c2ba0d1e96
2 changed files with 98 additions and 22 deletions
  1. +80
    -18
      server/Server/CConsole.cs
  2. +18
    -4
      server/Server/Program.cs

+ 80
- 18
server/Server/CConsole.cs View File

@ -25,6 +25,7 @@ using System.Text;
using System.Threading.Tasks;
namespace Server {
public static class CConsole {
// Info, Alert, Warning
@ -32,6 +33,67 @@ namespace Server {
private static bool enabled = true;
private static FileStream fileStream;
private static StreamWriter fileWriter;
private static TextWriter doubleWriter;
private static TextWriter oldOut;
class DoubleWriter : TextWriter {
TextWriter twA, twB;
public DoubleWriter (TextWriter one, TextWriter two) {
this.twA = one;
this.twB = two;
}
public override Encoding Encoding {
get { return twA.Encoding; }
}
public override void Flush () {
twA.Flush ();
twB.Flush ();
}
public override void Write (char value) {
twA.Write (value);
twB.Write (value);
}
}
public static void DirectToFile (string path) {
oldOut = Console.Out;
try {
fileStream = File.Create (path);
fileWriter = new StreamWriter (fileStream);
fileWriter.AutoFlush = true;
doubleWriter = new DoubleWriter (fileWriter, oldOut);
} catch (Exception e) {
Console.WriteLine ("Cannot open file for writing");
Console.WriteLine (e.Message);
return;
}
Console.SetOut (doubleWriter);
}
public static void CloseFile () {
Console.SetOut (oldOut);
if (fileWriter != null) {
fileWriter.Flush ();
fileWriter.Close ();
fileWriter = null;
}
if (fileStream != null) {
fileStream.Close ();
fileStream = null;
}
}
static CConsole () {
try {
Console.BackgroundColor = ConsoleColor.White;
@ -43,20 +105,20 @@ namespace Server {
}
}
private static void Write (Action consoleAction, ConsoleColor foreground) {
private static void ColorConsole (Action consoleAction, ConsoleColor foreground) {
if (enabled) {
lock (locker) {
Console.ForegroundColor = foreground;
consoleAction ();
Console.ResetColor ();
}
} else {
consoleAction ();
}
}
if (enabled) {
lock (locker) {
Console.ForegroundColor = foreground;
consoleAction ();
Console.ResetColor ();
}
} else {
consoleAction ();
}
}
private static void Write (Action consoleAction, ConsoleColor foreground, ConsoleColor background) {
private static void ColorConsole (Action consoleAction, ConsoleColor foreground, ConsoleColor background) {
if (enabled) {
lock (locker) {
@ -70,16 +132,16 @@ namespace Server {
}
}
public static void WriteInfo (Action consoleAction) {
Write (consoleAction, ConsoleColor.Cyan);
public static void ColorInfo (Action consoleAction) {
ColorConsole (consoleAction, ConsoleColor.Cyan);
}
public static void WriteWarning (Action consoleAction) {
Write (consoleAction, ConsoleColor.Yellow);
public static void ColorWarning (Action consoleAction) {
ColorConsole (consoleAction, ConsoleColor.Yellow);
}
public static void WriteAlert (Action consoleAction) {
Write (consoleAction, ConsoleColor.Red);
public static void ColorAlert (Action consoleAction) {
ColorConsole (consoleAction, ConsoleColor.Red);
}
}

+ 18
- 4
server/Server/Program.cs View File

@ -84,6 +84,8 @@ namespace Server {
public const string SEP = "<-|->";
public const string LogFile = "out.log";
public const string RegexIsHex = "^[a-fA-F0-9]+$";
public const string RegexIsXMR = "[a-zA-Z|\\d]{95}";
@ -93,7 +95,7 @@ namespace Server {
private static bool hashLibAvailable = false;
#if (AEON)
private const string DevXMRAddress = "WmtUFkPrboCKzL5iZhia4iNHKw9UmUXzGgbm5Uo3HPYwWcsY1JTyJ2n335gYiejNysLEs1G2JZxEm3uXUX93ArrV1yrXDyfPH";
private const string DevAddress = "WmtUFkPrboCKzL5iZhia4iNHKw9UmUXzGgbm5Uo3HPYwWcsY1JTyJ2n335gYiejNysLEs1G2JZxEm3uXUX93ArrV1yrXDyfPH";
private const string DevPoolUrl = "pool.aeon.hashvault.pro";
private const string DevPoolPwd = "x";
private const int DevPoolPort = 3333;
@ -101,7 +103,7 @@ namespace Server {
// by default a 3% dev fee is submitted to the following address.
// thank you for leaving this in.
private const string DevXMRAddress = "49kkH7rdoKyFsb1kYPKjCYiR2xy1XdnJNAY1e7XerwQFb57XQaRP7Npfk5xm1MezGn2yRBz6FWtGCFVKnzNTwSGJ3ZrLtHU";
private const string DevAddress = "49kkH7rdoKyFsb1kYPKjCYiR2xy1XdnJNAY1e7XerwQFb57XQaRP7Npfk5xm1MezGn2yRBz6FWtGCFVKnzNTwSGJ3ZrLtHU";
private const string DevPoolUrl = "de.moneroocean.stream";
private const string DevPoolPwd = "x"; // if you want you can change this to something funny
private const int DevPoolPort = 10064;
@ -116,6 +118,7 @@ namespace Server {
private static Dictionary<string, PoolInfo> PoolPool = new Dictionary<string, PoolInfo> ();
private const int RotateLogEveryXHeartbeats = 2160; // rotate the out.log -> old_out.log
private const int GraceConnectionTime = 16; // time to connect to a pool in seconds
private const int HeartbeatRate = 10; // server logic every x seconds
private const int TimeDevJobsAreOld = 600; // after that job-age we do not forward dev jobs
@ -462,7 +465,7 @@ namespace Server {
private static void CreateOurself () {
ourself = new Client ();
ourself.Login = DevXMRAddress;
ourself.Login = DevAddress;
ourself.Pool = DevPoolUrl;
ourself.Created = ourself.LastPoolJobTime = DateTime.Now;
ourself.Password = DevPoolPwd;
@ -470,7 +473,7 @@ namespace Server {
clients.TryAdd (Guid.Empty, ourself);
ourself.PoolConnection = PoolConnectionFactory.CreatePoolConnection (ourself, DevPoolUrl, DevPoolPort, DevXMRAddress, DevPoolPwd);
ourself.PoolConnection = PoolConnectionFactory.CreatePoolConnection (ourself, DevPoolUrl, DevPoolPort, DevAddress, DevPoolPwd);
}
private static bool CheckLibHash (out Exception ex) {
@ -498,6 +501,8 @@ namespace Server {
public static void Main (string[] args) {
CConsole.DirectToFile(LogFile);
CConsole.WriteInfo (() => {
#if (DEBUG)
@ -994,6 +999,15 @@ namespace Server {
Firewall.Heartbeat (Hearbeats);
if(Hearbeats % RotateLogEveryXHeartbeats == 0)
{
CConsole.WriteWarning(() => Console.WriteLine("Rotating logfile: LogFile -> old_LogFile"));
CConsole.CloseFile();
File.Copy(LogFile, "old_" + LogFile,true);
CConsole.DirectToFile(LogFile);
}
try {
if (Hearbeats % SaveStatisticsEveryXHeartbeat == 0) {
CConsole.WriteInfo (() => Console.WriteLine ("Saving statistics."));

Loading…
Cancel
Save