您的位置:首页 > Web前端 > JavaScript

[Javascript] A tool for JS debugging

2009-07-21 03:16 549 查看
/*----------------------------------------------------------------------------/
|  Description  :  a write-log class for debugging.                           |
|  Copyright(c) :  Tencent Inc.                                               |
|-----------------------------------------------------------------------------|
|                                 Changes log                                 |
|-------------+---------------------------------------------------------------|
|  2006/06/03 | Create by Moky (mokymo@tencent.com)                           |
|-------------+---------------------------------------------------------------|
|  Created 2006/06/03                                last updated 2006/06/03  |
/----------------------------------------------------------------------------*/
function CWinLog()
{
private:
this.m_pLogWin = null;
}
CWinLog.prototype.Init = function(pLogWin)
{
var ret = 0;
if (!pLogWin)
{
try
{
ret = this.Destroy();
ret = this.Create();
}
catch(e)
{
ret = -1;
}
}
else
{
this.m_pLogWin = pLogWin;
}
if (ret != 0) return -1;
if (!this.m_pLogWin) return -1;
return 0;
}
CWinLog.prototype.Create = function(sURL, sName, sFeatures, bReplace)
{
if (!sURL) sURL = "";
if (!sName) sName = "log_win";
if (!sFeatures) sFeatures = "menu=no,status=no,scrollbars=yes";
if (!bReplace) bReplace = false;
var pop_win = null;
try
{
this.Destroy();
pop_win = window.open(sURL, sName, sFeatures, bReplace);
this.m_pLogWin = pop_win;
this.m_pLogWin.document.writeln('<title>Javascript Log (v1.0 by Moky)</title>');
this.m_pLogWin.document.writeln('<body bgcolor="#333333"><pre>');
this.m_pLogWin.document.writeln('<font color="#CCCCCC" style="font-color:#CCCCCC; font-size:16px; font-family:" mce_style="font-color:#CCCCCC; font-size:16px; font-family:"Courier New">');
}
catch(e)
{
return -1;
}
window.focus();
if (!this.m_pLogWin) return -1;
return 0;
}
CWinLog.prototype.Destroy = function()
{
if (!this.m_pLogWin) return -1;
try
{
this.m_pLogWin.close();
}
catch(e)
{
return -1;
}
return 0;
}
CWinLog.prototype.WriteLog = function()
{
if (!this.m_pLogWin) return -1;
var now = new Date();
var h  = now.getHours();
var m  = now.getMinutes();
var s  = now.getSeconds();
var ms = now.getMilliseconds();
if (h < 10) h = "0" + h;
if (m < 10) m = "0" + m;
if (s < 10) s = "0" + s;
if (ms < 10) ms = "00" + ms; else if (ms < 100) ms = "0" + ms;
var line_head = "[" + h + ":" + m + ":" + s + "." + ms + "] ";
var arg = "";
for (var x = 0; x < arguments.length; x++)
{
arg += arguments[x];
}
arg = arg.replace(/</g, "<");
try
{
this.m_pLogWin.document.writeln(line_head, arg);
this.m_pLogWin.scrollBy(0, 256);
}
catch(e)
{
return -1;
}
return 0;
}
////////////////////////////////////////////////////////
var g_Log = new CWinLog();
//发布时需要关闭log,把下面一行注释掉即可
g_Log.Init();
/*
//test...
g_Log.WriteLog("---- starting ----");
g_Log.WriteLog("Hello world!", " -- ", 9527);
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐