SafeNet/ConsoleApplication1/SM.cs

123 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Timers;
using System.IO;
namespace ConsoleApplication1
{
public class SM
{
public static bool FILE_DEBUG = false;
private static InterthreadMessageQueue<string> msgQueue;
public SM()
{
if (FILE_DEBUG)
{
msgQueue = new InterthreadMessageQueue<string>();
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 1 second.
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
string path = System.Environment.CurrentDirectory;
string dirPath = System.Environment.CurrentDirectory +
Path.DirectorySeparatorChar + "Logs";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
string filePath = dirPath + Path.DirectorySeparatorChar + "log_" + DateTime.Now.ToString("dd_MMM") + ".log";
if (!File.Exists(filePath))
{
FileStream fs = File.Create(filePath);
fs.Close();
}
try
{
StreamWriter sw = File.AppendText(filePath);
if (msgQueue != null)
{
string line = msgQueue.GetItem(100);
while (line != default(string))
{
sw.WriteLine(line);
line = msgQueue.GetItem(100);
}
}
sw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
//Console.WriteLine("Write to file timer!!!!!");
}
public static void Debug(string str)
{
string test = "";
foreach (char c in str)
{
if (((c > 31) && (c < 126)) || (c == 10) || (c == 13))
{
test += c;
}
else test += '.';
}
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss") + "--->" + test);
Console.WriteLine("---------------------------------------------------------");
if (msgQueue != null && FILE_DEBUG)
{
msgQueue.PostItem(DateTime.Now.ToString("hh:mm:ss") + "--->" + test);
msgQueue.PostItem("---------------------------------------------------------");
}
}
public static void Debug(string[] strs)
{
foreach (string str in strs)
Console.WriteLine(str);
Console.WriteLine("---------------------------------------------------------");
}
public static void Debug(string str, bool lines)
{
string test = "";
foreach (char c in str)
{
if (((c > 31) && (c < 126)) || (c == 10) || (c == 13))
{
test += c;
}
else test += '.';
}
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss") + "--->" + test);
if (lines) Console.WriteLine("---------------------------------------------------------");
if (msgQueue != null && FILE_DEBUG)
{
msgQueue.PostItem(DateTime.Now.ToString("hh:mm:ss") + "--->" + test);
if (lines) msgQueue.PostItem("---------------------------------------------------------");
}
}
public static void Print(string str)
{
Console.WriteLine(str);
}
}
}