150 lines
5.2 KiB
C#
150 lines
5.2 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.IO;
|
|||
|
|
|||
|
namespace RadioBroadcaster.Server
|
|||
|
{
|
|||
|
static class LogManager
|
|||
|
{
|
|||
|
static string[] LogItems = new string[1024];
|
|||
|
static string[] SerialItems = new string[128];
|
|||
|
static int currentLogPosition = 0;
|
|||
|
static int lastLogAddition = 0;
|
|||
|
|
|||
|
static int currentSerialPosition = 0;
|
|||
|
static int lastSerialPosition = 0;
|
|||
|
|
|||
|
static string logLocation = System.Reflection.Assembly.GetExecutingAssembly().Location.Remove(System.Reflection.Assembly.GetExecutingAssembly().Location.LastIndexOf("\\")) + "\\outputLog";
|
|||
|
static string serialLocation = System.Reflection.Assembly.GetExecutingAssembly().Location.Remove(System.Reflection.Assembly.GetExecutingAssembly().Location.LastIndexOf("\\")) + "\\SerialLog";
|
|||
|
|
|||
|
static bool LogItemsIsRunning = false;
|
|||
|
static bool SerialItemsIsRunning = false;
|
|||
|
|
|||
|
static bool isDebug = false;
|
|||
|
|
|||
|
static bool IsCurrentlyActive = false;
|
|||
|
|
|||
|
public static void Init()
|
|||
|
{
|
|||
|
WriteLogTimer = new System.Timers.Timer(1000);
|
|||
|
WriteLogTimer.Elapsed += WriteLogTimer_Elapsed;
|
|||
|
WriteLogTimer.Start();
|
|||
|
try
|
|||
|
{
|
|||
|
if (DateTime.Now.Subtract(File.GetLastWriteTime(serialLocation)).TotalDays > 14) // As the serial log could contain a massive amount of data, we wipe it after some time
|
|||
|
{
|
|||
|
File.WriteAllBytes(serialLocation, new byte[] { 0x00 });
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
FileStream fs = File.OpenRead(serialLocation);
|
|||
|
if (fs.Length > 10737418240) // > 10GB
|
|||
|
{
|
|||
|
fs.Dispose();
|
|||
|
File.WriteAllBytes(serialLocation, new byte[] { 0x00 });
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
// We don't absolutely need to do anything here
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private static void WriteLogTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
|||
|
{
|
|||
|
if (!LogItemsIsRunning)
|
|||
|
{
|
|||
|
LogItemsIsRunning = true;
|
|||
|
WriteManager();
|
|||
|
LogItemsIsRunning = false;
|
|||
|
}
|
|||
|
if (!SerialItemsIsRunning)
|
|||
|
{
|
|||
|
SerialItemsIsRunning = true;
|
|||
|
SerialWriteManager();
|
|||
|
SerialItemsIsRunning = false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void Write(string TextToWrite)
|
|||
|
{
|
|||
|
lastLogAddition = lastLogAddition + 1;
|
|||
|
if (lastLogAddition > 1023)
|
|||
|
{
|
|||
|
lastLogAddition = 0;
|
|||
|
}
|
|||
|
LogItems[lastLogAddition] = TextToWrite;
|
|||
|
if (!IsCurrentlyActive)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
static void WriteManager()
|
|||
|
{
|
|||
|
FileStream fs = File.OpenWrite(logLocation);
|
|||
|
fs.Position = fs.Length; // Set position to the end of the stream.
|
|||
|
while (currentLogPosition != lastLogAddition)
|
|||
|
{
|
|||
|
DoWrite(fs);
|
|||
|
}
|
|||
|
fs.Close();
|
|||
|
IsCurrentlyActive = false;
|
|||
|
}
|
|||
|
static void DoWrite(FileStream fileStream)
|
|||
|
{
|
|||
|
currentLogPosition = currentLogPosition + 1;
|
|||
|
if (currentLogPosition > 1023)
|
|||
|
{
|
|||
|
currentLogPosition = 0;
|
|||
|
}
|
|||
|
byte[] utf8Bytes = Encoding.UTF8.GetBytes(LogItems[currentLogPosition] + Environment.NewLine);
|
|||
|
fileStream.Write(utf8Bytes, 0, utf8Bytes.Length);
|
|||
|
LogItems[currentLogPosition] = ""; // Clear some mem
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Logs serial data
|
|||
|
/// </summary>
|
|||
|
/// <param name="TextToWrite"></param>
|
|||
|
public static void WriteSerial(string TextToWrite)
|
|||
|
{
|
|||
|
if (isDebug)
|
|||
|
{
|
|||
|
lastSerialPosition = lastSerialPosition + 1;
|
|||
|
if (lastSerialPosition > 127)
|
|||
|
{
|
|||
|
lastSerialPosition = 0;
|
|||
|
}
|
|||
|
SerialItems[lastSerialPosition] = TextToWrite;
|
|||
|
}
|
|||
|
}
|
|||
|
public static void SerialWriteManager()
|
|||
|
{
|
|||
|
FileStream fs = File.OpenWrite(serialLocation);
|
|||
|
fs.Position = fs.Length; // Set position to the end of the stream.
|
|||
|
while (currentSerialPosition != lastSerialPosition)
|
|||
|
{
|
|||
|
SerialDoWrite(fs);
|
|||
|
}
|
|||
|
fs.Close();
|
|||
|
IsCurrentlyActive = false;
|
|||
|
}
|
|||
|
static void SerialDoWrite(FileStream fileStream)
|
|||
|
{
|
|||
|
currentSerialPosition = currentSerialPosition + 1;
|
|||
|
if (currentSerialPosition > 127)
|
|||
|
{
|
|||
|
currentSerialPosition = 0;
|
|||
|
}
|
|||
|
byte[] utf8Bytes = Encoding.UTF8.GetBytes(SerialItems[currentSerialPosition] + "\t"+ DateTime.Now.ToShortTimeString() + Environment.NewLine);
|
|||
|
fileStream.Write(utf8Bytes, 0, utf8Bytes.Length);
|
|||
|
SerialItems[currentSerialPosition] = ""; // Clear some mem
|
|||
|
}
|
|||
|
private static System.Timers.Timer WriteLogTimer;
|
|||
|
}
|
|||
|
}
|