1 | /** |
||
2 | * [Siteapp] - multi-purpose application & CMS theme |
||
3 | * |
||
4 | * Siteapp debugger |
||
5 | * |
||
6 | * @package [Siteapp] |
||
7 | * @subpackage [Siteapp] core |
||
8 | * @author Björn Bartels <[email protected]> |
||
9 | * @link https://gitlab.bjoernbartels.earth/groups/themes |
||
10 | * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
||
11 | * @copyright copyright (c) 2016 Björn Bartels <[email protected]> |
||
12 | * |
||
13 | * @namespace Siteapp |
||
14 | * @module Siteapp.Debugger |
||
15 | */ |
||
16 | /** global: Siteapp */ |
||
17 | |||
18 | var Debugger = { |
||
19 | /* manage debug data */ |
||
20 | debugentries : [], |
||
21 | |||
22 | types : ['log', 'debug', 'info', 'warn', 'error'], |
||
23 | |||
24 | add : function ( oEntry ) { |
||
25 | var entry = { |
||
26 | type : oEntry.type || 'info', |
||
27 | message : oEntry.message || '', |
||
28 | time : oEntry.time || (new Date()) |
||
29 | }; |
||
30 | this.debugentries.push(entry); |
||
31 | return (this); |
||
32 | }, |
||
33 | |||
34 | clear : function ( ) { |
||
35 | this.debugentries = []; |
||
36 | return (this); |
||
37 | }, |
||
38 | |||
39 | toEntry : function ( ) { |
||
40 | var entry = arguments; |
||
41 | if (entry.length == 1) { |
||
42 | entry[1] = entry[0]; |
||
43 | entry[0] = 'info'; |
||
44 | } |
||
45 | return { |
||
46 | type : entry[0] || 'info', |
||
47 | message : entry[1] || '', |
||
48 | time : (new Date()), |
||
49 | }; |
||
50 | }, |
||
51 | |||
52 | debug : function ( ) { |
||
53 | if ( (console || (typeof console.debug == 'function')) && $LCARS.getInstance().config.debug ) { |
||
0 ignored issues
–
show
|
|||
54 | console.debug(arguments); |
||
55 | } |
||
56 | return (this); |
||
57 | }, |
||
58 | |||
59 | log : function ( msg ) { var entry = this.toEntry('log', msg); this.add(entry).console(entry); return (this); }, |
||
60 | |||
61 | info : function ( msg ) { var entry = this.toEntry('info', msg); this.add(entry).console(entry); return (this); }, |
||
62 | |||
63 | warn : function ( msg ) { var entry = this.toEntry('warn', msg); this.add(entry).console(entry); return (this); }, |
||
64 | |||
65 | error : function ( msg ) { var entry = this.toEntry('error', msg); this.add(entry).console(entry); return (this); }, |
||
66 | |||
67 | console : function ( data ) { |
||
68 | if (!console || !data || !data.type || !data.message || !data.time || !$LCARS.getInstance().config.debug) { |
||
0 ignored issues
–
show
The variable
$LCARS seems to be never declared. If this is a global, consider adding a /** global: $LCARS */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
69 | return (this); |
||
70 | } |
||
71 | var txt = [ |
||
72 | String(data.type).toUpperCase(), ': ', |
||
73 | String(data.message), ' - ', |
||
74 | String(data.time), '' |
||
75 | ].join(''); |
||
76 | if ( (typeof console[data.type] == 'function') ) { |
||
77 | console[data.type](txt); |
||
78 | } else { |
||
79 | //console.//log(txt); |
||
80 | } |
||
81 | return (this); |
||
82 | }, |
||
83 | |||
84 | get : function ( ) { |
||
85 | if (arguments.length === 0) { |
||
86 | return ( this.debugentries ); |
||
87 | } else if (arguments.length == 1) { |
||
88 | var entries = []; |
||
89 | for ( var iEntry in this.debugentries ) |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
90 | if ( this.debugentries[iEntry].type == arguments[0] ) |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
91 | entries.push( this.debugentries[iEntry] ); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
92 | return ( entries ); |
||
93 | } |
||
94 | return (false); |
||
95 | }, |
||
96 | |||
97 | getInstance : function ( ) { |
||
98 | return (this); |
||
99 | }, |
||
100 | |||
101 | _app : ( context ) => { |
||
102 | if (context) { |
||
103 | this.__app = context; |
||
0 ignored issues
–
show
|
|||
104 | } else { |
||
105 | return this.__app; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | }; |
||
110 | |||
111 | export {Debugger}; |
||
112 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.