Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/logger.js

Issue 10233013: Crawler, second version (Closed)
Patch Set: Created April 12, 2013, 1:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/instruction.js ('k') | lib/main.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /**
2 * Logging service. This is not the log function itself, but rather provide a fa ctory for making such.
3 *
4 * @param {String} module
5 * The name identifying the main module.
6 * @constructor
7 */
8 var Logger = function( module )
9 {
10 /**
11 * The module name
12 * @type {String}
13 */
14 this.module = module;
15
16 /**
17 * Global flag to suppress all output.
18 * @type {boolean}
19 * @private
20 */
21 this._suppressed = false;
22
23 /**
24 * The console service used to report messages. This instance is used to get a ccess to logMessage, which
25 * we use as a static function.
26 */
27 this.console_service =
28 Components.classes["@mozilla.org/consoleservice;1"].getService( Components.i nterfaces.nsIConsoleService );
29 };
30 exports.Logger = Logger;
31
32 Logger.prototype.suppress = function( suppressed )
33 {
34 this._suppressed = suppressed;
35 };
36
37 /**
38 * Create an ordinary log function with a consistent naming convention.
39 *
40 * @param {String} [submodule]
41 * The name identifying some piece of the main module.
42 * @return {Function}
43 * A two-argument function with the ordinary signature for a log message.
44 */
45 Logger.prototype.make_log = function( submodule )
46 {
47 var prefix = this.module;
48 if ( submodule && submodule.length > 0 )
49 {
50 prefix += "/" + submodule;
51 }
52 prefix += ": ";
53 return this.log.bind( this, prefix );
54 };
55
56 /**
57 * Display a log message whose location is reported as the source line of the ca ller, rather than some line within
58 * the log function itself.
59 * <p/>
60 * Note that this function would ordinarily be called from a function returned f rom make_log(), which computes the
61 * prefix argument. This is not a hard requirement, but rather a recommended pra ctice.
62 *
63 * @param {String} prefix
64 * String to be prepended before the message argument.
65 * @param {String} message
66 * The main error message.
67 * @param {Boolean} [allow]
68 * If present and false, suppresses the message. Allows disabling a log mes sage by arbitrary category,
69 * as implemented by the caller.
70 */
71 Logger.prototype.log = function( prefix, message, allow )
72 {
73 if ( ( arguments.length >= 3 && !allow ) || this._suppressed )
74 {
75 // Assert we have an explicit argument to disallow the message
76 return;
77 }
78 var error_report =
79 Components.classes["@mozilla.org/scripterror;1"].createInstance( Components. interfaces.nsIScriptError );
80 var caller = Components.stack.caller;
81
82 /*
83 * Remove the beginning of any filename value that contains a text arrow. This notation is used to indicate the
84 * complete link path by which the source came into context. While complete, i t makes the links presented in
85 * the error console non-clickable.
86 */
87 var filename = caller.filename;
88 var n = filename.lastIndexOf( " -> " );
89 if ( n > -1 )
90 {
91 filename = filename.substr( n + 4 );
92 }
93
94 error_report.init( prefix + message, filename, null, caller.lineNumber, null, 1, "javascript" );
95 this.console_service.logMessage( error_report );
96
97 /*
98 * This line was used during development to see just what was in the scripterr or object. Unfortunately, making a
99 * string message that exactly matches this format does not display as an erro r. In particular there is no
100 * clickable link displayed. Thus the best we can do is to display a warning w ith a link, rather than an ordinary
101 * console message, which doesn't display a clickable location.
102 */
103 //Cu.reportError( "ScriptError=" + scriptError.toString() );
104 };
105
106 /**
107 * Emit a stack trace to the log.
108 */
109 Logger.prototype.stack_trace = function()
110 {
111 var e = new Error();
112 this.log( "", e.stack );
113 };
114
115
116 function pad2( n )
117 {
118 var s = String( n );
119 if ( n < 10 )
120 s = "0" + s;
121 return s;
122 }
123
124 function pad3( n )
125 {
126 var s = String( n );
127 if ( n < 100 )
128 {
129 s = "0" + s;
130 if ( n < 10 )
131 s = "0" + s;
132 }
133 return s;
134 }
135
136 Logger.timestamp = function()
137 {
138 let date = new Date();
139 return date.getUTCFullYear() + "-" + pad2( date.getUTCMonth() + 1 ) + "-" + pa d2( date.getUTCDate() )
140 + " " + pad2( date.getUTCHours() ) + ":" + pad2( date.getUTCMinutes() ) + ": " + pad2( date.getUTCSeconds() )
141 + "." + pad3( date.getUTCMilliseconds() );
142 };
OLDNEW
« no previous file with comments | « lib/instruction.js ('k') | lib/main.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld