SafeDispatch/SafeMobileLIB_DLL/SM.cs

135 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Timers;
using System.IO;
namespace SafeMobileLib
{
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());
}
}
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);
if ( msgQueue!=null && FILE_DEBUG)
{
msgQueue.PostItem(DateTime.Now.ToString("hh:mm:ss") + "--->" + test);
}
}
public static void Debug(string[] strs)
{
foreach(string str in strs)
Console.WriteLine(str);
}
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 DebugSharp(string str)
{
string test = "";
foreach (char c in str)
{
if (((c > 31) && (c < 126)) || (c == 10) || (c == 13))
{
test += c;
}
else test += '.';
}
Console.WriteLine(String.Format("[### {0:H:mm:ss} ###] {1}", DateTime.Now, test));
if (msgQueue != null && FILE_DEBUG)
msgQueue.PostItem(DateTime.Now.ToString("hh:mm:ss") + "--->" + test);
}
public static void Print(string str)
{
Console.WriteLine(str);
}
}
}