可精確到1毫秒的用以在記錄文件中寫入自定義的調(diào)試信息(主要是時間)的組件
2024-07-21 02:24:17
供稿:網(wǎng)友
菜鳥學(xué)堂:
上次我發(fā)布了一個用以在記錄文件中寫入自定義的調(diào)試信息(主要是時間)的組件,但由于clr的限制,它只能精確到10毫秒左右。后來我參考了網(wǎng)絡(luò)上的一篇文章:http://blog.joycode.com/lostinet/archive/2005/04/24/49590.aspx(在這里首先向原作者表示感謝)通過調(diào)用系統(tǒng)api得到了可精確到1毫秒左右的時間記錄。故特重新用c#重寫了這個組件,與大家共享。
//====================================================================
//tracespy - 用以在記錄文件中寫入自定義的調(diào)試信息(開發(fā)者:林健)
//====================================================================
//
//屬性:
// tracefilename - 記錄文件名
//
//方法:
// ★文本寫入方面
// writetext - 寫入自定義文本
// clearalltext - 清除所有文本
// ★時間記錄方面
// settimepoint - 設(shè)置時間起點
// gettimespanfrominit - 詢問時間跨度(距離時間起點)
// gettimespanfromprev - 詢問時間跨度(距離上次詢問時間)
// ★自定義計數(shù)器
// setcounter - 設(shè)置自定義計數(shù)器
// addcounter - 累加自定義計數(shù)器
//
//====================================================================
using system;
namespace tracespy
{
public class thetrace
{
//記錄文件名
static public string tracefilename = "trace.txt";
//時間起點(初始為當(dāng)前時刻)
static private long inittimepoint = timecounter.getexactnow().ticks;
//上次詢問時間點(初始為當(dāng)前時刻)
static private long prevtimepoint = timecounter.getexactnow().ticks;
//自定義計數(shù)器
static private int counter = 0;
//寫入自定義文本
static public void writetext(string str)
{
writetext(str, false);
}
static public void writetext(string str, bool showtime)
{
filewriter.writetext(str, showtime);
}
//清除所有文本
static public void clearalltext()
{
filewriter.clearalltext();
}
//設(shè)置時間起點
static public void settimepoint()
{
settimepoint("");
}
static public void settimepoint(string note)
{
inittimepoint = timecounter.getexactnow().ticks;
prevtimepoint = timecounter.getexactnow().ticks;
filewriter.writetext("設(shè)置時間起點[" + note + "]。", false);
}
//詢問時間跨度(距離時間起點)
static public decimal gettimespanfrominit()
{
return gettimespanfrominit("");
}
static public decimal gettimespanfrominit(string note)
{
prevtimepoint = timecounter.getexactnow().ticks;
decimal span;
span = (decimal)(prevtimepoint - inittimepoint) / (decimal)10000;
filewriter.writetext("詢問時間跨度[" + note + "],距離時間起點為" + span.tostring() + "毫秒。", false);
return span;
}
//詢問時間跨度(距離上次詢問時間)
static public decimal gettimespanfromprev()
{
return gettimespanfromprev("");
}
static public decimal gettimespanfromprev(string note)
{
long recttimepoint =timecounter.getexactnow().ticks;
decimal span;
span = (decimal)(recttimepoint - prevtimepoint) / (decimal)10000;
prevtimepoint = recttimepoint;
filewriter.writetext("詢問時間跨度[" + note + "],距離上次詢問時間為" + span.tostring() + "毫秒。", false);
return span;
}
//設(shè)置自定義計數(shù)器
static public int setcounter()
{
return setcounter(0);
}
static public int setcounter(int num)
{
counter = num;
filewriter.writetext("自定義計數(shù)器值設(shè)置為" + counter + "。", false);
return counter;
}
//累加自定義計數(shù)器
static public int addcounter()
{
return addcounter(1);
}
static public int addcounter(int num)
{
counter += num;
filewriter.writetext("自定義計數(shù)器值累加到" + counter + "。", false);
return counter;
}
}
}
using system;
using system.runtime.interopservices;
namespace tracespy
{
internal class timecounter
{
[dllimport("kernel32.dll")]
static extern bool queryperformancecounter([in, out] ref long lpperformancecount);
[dllimport("kernel32.dll")]
static extern bool queryperformancefrequency([in, out] ref long lpfrequency);
static long _f = 0;
static private long gettickcount()
{
long f = _f;
if (f == 0)
{
if (queryperformancefrequency(ref f))
{
_f = f;
}
else
{
_f = -1;
}
}
if (f == -1)
{
return environment.tickcount * 10000;
}
long c = 0;
queryperformancecounter(ref c);
return (long)(((double)c) * 1000 * 10000 / ((double)f));
}
static long _tc = 0;
static internal datetime getexactnow()
{
if (_tc == 0)
{
long tc = gettickcount();
datetime dt = datetime.now;
_tc = dt.ticks - tc;
return dt;
}
return new datetime(_tc + gettickcount());
}
}
}
using system;
namespace tracespy
{
internal class filewriter
{
static private system.io.streamwriter writer;
//向文件中寫入一個字串
static internal void writetext(string str, bool showtime)
{
if(thetrace.tracefilename == string.empty)
return;
writer = new system.io.streamwriter(thetrace.tracefilename, true, system.text.encoding.default);
string words;
words = str;
if(showtime)
words += " @ " + timecounter.getexactnow().tolongdatestring() + " " + timecounter.getexactnow().tolongtimestring();
writer.writeline(words);
writer.close();
}
//清除記錄文件
static internal void clearalltext()
{
if(thetrace.tracefilename == string.empty)
return;
writer = new system.io.streamwriter(thetrace.tracefilename, false, system.text.encoding.default);
writer.write("");
writer.close();
}
}
}