SafeNet/.svn/pristine/e4/e48a0133cd2c77fa6a68e1e1533cc328e4200d2a.svn-base

123 lines
3.9 KiB
Plaintext
Raw Normal View History

2021-02-24 11:50:23 +00:00
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);
}
}
}