可精确到1毫秒的用以在记录文件中写入自定义的调试信息(主要是时间)的组件
2024-07-21 02:24:17
供稿:网友
菜鸟学堂:
上次我发布了一个用以在记录文件中写入自定义的调试信息(主要是时间)的组件,但由于clr的限制,它只能精确到10毫秒左右。后来我参考了网络上的一篇文章:http://blog.joycode.com/lostinet/archive/2005/04/24/49590.aspx(在这里首先向原作者表示感谢)通过调用系统api得到了可精确到1毫秒左右的时间记录。故特重新用c#重写了这个组件,与大家共享。
//====================================================================
//tracespy - 用以在记录文件中写入自定义的调试信息(开发者:林健)
//====================================================================
//
//属性:
// tracefilename - 记录文件名
//
//方法:
// ★文本写入方面
// writetext - 写入自定义文本
// clearalltext - 清除所有文本
// ★时间记录方面
// settimepoint - 设置时间起点
// gettimespanfrominit - 询问时间跨度(距离时间起点)
// gettimespanfromprev - 询问时间跨度(距离上次询问时间)
// ★自定义计数器
// setcounter - 设置自定义计数器
// addcounter - 累加自定义计数器
//
//====================================================================
using system;
namespace tracespy
{
public class thetrace
{
//记录文件名
static public string tracefilename = "trace.txt";
//时间起点(初始为当前时刻)
static private long inittimepoint = timecounter.getexactnow().ticks;
//上次询问时间点(初始为当前时刻)
static private long prevtimepoint = timecounter.getexactnow().ticks;
//自定义计数器
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();
}
//设置时间起点
static public void settimepoint()
{
settimepoint("");
}
static public void settimepoint(string note)
{
inittimepoint = timecounter.getexactnow().ticks;
prevtimepoint = timecounter.getexactnow().ticks;
filewriter.writetext("设置时间起点[" + 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;
}
//设置自定义计数器
static public int setcounter()
{
return setcounter(0);
}
static public int setcounter(int num)
{
counter = num;
filewriter.writetext("自定义计数器值设置为" + counter + "。", false);
return counter;
}
//累加自定义计数器
static public int addcounter()
{
return addcounter(1);
}
static public int addcounter(int num)
{
counter += num;
filewriter.writetext("自定义计数器值累加到" + 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();
}
}
}