analogger 1.5.2 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -1
- package/README.md +12 -7
- package/dist/index-cjs.min.cjs +2 -2
- package/dist/index-esm.min.mjs +2 -734
- package/dist/index.css +83 -47
- package/package.json +2 -2
- package/.eslintrc.cjs +0 -25
- package/.nycrc +0 -20
- package/.releaserc +0 -27
- package/ci.md +0 -0
- package/todo.js +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
# [1.7.0](https://github.com/thimpat/analogger/compare/v1.6.2...v1.7.0) (2022-02-11)
|
|
2
|
+
|
|
3
|
+
## [1.6.2](https://github.com/thimpat/analogger/compare/v1.6.1...v1.6.2) (2022-02-11)
|
|
4
|
+
|
|
5
|
+
## [1.6.1](https://github.com/thimpat/analogger/compare/v1.6.0...v1.6.1) (2022-02-11)
|
|
6
|
+
|
|
7
|
+
# [1.6.0](https://github.com/thimpat/analogger/compare/v1.5.2...v1.6.0) (2022-02-11)
|
|
8
|
+
|
|
9
|
+
## [1.5.2](https://github.com/thimpat/analogger/compare/v1.5.1...v1.5.2) (2022-02-11)
|
|
10
|
+
|
|
1
11
|
## [1.5.1](https://github.com/thimpat/analogger/compare/v1.5.0...v1.5.1) (2022-02-11)
|
|
2
12
|
|
|
3
13
|
# [1.5.0](https://github.com/thimpat/analogger/compare/v1.4.1...v1.5.0) (2022-02-11)
|
|
@@ -10,4 +20,4 @@
|
|
|
10
20
|
|
|
11
21
|
## [1.3.1](https://github.com/thimpat/analogger/compare/v1.3.0...v1.3.1) (2022-02-09)
|
|
12
22
|
|
|
13
|
-
# [1.3.0](https://github.com/thimpat/analogger/compare/v1.2.0...v1.3.0) (2022-02-08)
|
|
23
|
+
# [1.3.0](https://github.com/thimpat/analogger/compare/v1.2.0...v1.3.0) (2022-02-08)
|
package/README.md
CHANGED
|
@@ -49,6 +49,10 @@ import {anaLogger} from "analogger"
|
|
|
49
49
|
|
|
50
50
|
#### DOM
|
|
51
51
|

|
|
52
|
+
|
|
53
|
+
#### FileSystem
|
|
54
|
+

|
|
55
|
+
|
|
52
56
|
<br/>
|
|
53
57
|
|
|
54
58
|
## API
|
|
@@ -80,13 +84,14 @@ Display the browser native message box if run from it; otherwise, it displays th
|
|
|
80
84
|
### setOptions()
|
|
81
85
|
|
|
82
86
|
|
|
83
|
-
| **Options**
|
|
84
|
-
|
|
85
|
-
| silent
|
|
86
|
-
| hideError
|
|
87
|
-
| hideHookMessage
|
|
88
|
-
| showPassingTests
|
|
89
|
-
| logToDom
|
|
87
|
+
| **Options** | **default** | **Expect** | **Description** |
|
|
88
|
+
|------------------|-------------|--------------|---------------------------------------------------------------------|
|
|
89
|
+
| silent | false | boolean | _No log will be displayed (only errors)_ |
|
|
90
|
+
| hideError | false | boolean | _Hide errors from console_ |
|
|
91
|
+
| hideHookMessage | false | boolean | _Hide the automatic message shown when some native console methods are overridden_ |
|
|
92
|
+
| showPassingTests | true | boolean | _Show Live test results_ |
|
|
93
|
+
| logToDom | undefined | string (DOM Selector) |_display log in a DOM container_ |
|
|
94
|
+
| logToFile | undefined | string (File path) | _write log to a file if running from Node_ |
|
|
90
95
|
|
|
91
96
|
|
|
92
97
|
```javascript
|
package/dist/index-cjs.min.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var require$$0=require("chalk-cjs"),require$$1=require("color-convert-cjs"),require$$2=require("rgb-hex-cjs");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var require$$0__default=_interopDefaultLegacy(require$$0),require$$1__default=_interopDefaultLegacy(require$$1),require$$2__default=_interopDefaultLegacy(require$$2),anaLogger={},constants$1={};const constants={COLOR_TABLE:["#d2466e","#FFA07A","#FF7F50","#FF6347","#FFE4B5","#ADFF2F","#808000","#40E0D0","#1E90FF","#EE82EE","#708090","#DEB887","#FE642E","#210B61","#088A4B","#5E610B","#FA8258","#088A68","#B40431"],SYSTEM:{BROWSER:"BROWSER",NODE:"NODE"}},chalk=(constants$1.COLOR_TABLE=constants.COLOR_TABLE,constants$1.SYSTEM=constants.SYSTEM,require$$0__default.default),colorConvert=require$$1__default.default,rgbHex=require$$2__default.default,{COLOR_TABLE,SYSTEM}=constants$1,EOL=`
|
|
2
|
-
`;class AnaLogger{system="";logIndex=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";keepLog=!1;logHistory=[];$containers=null;options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};originalFormatFunction;constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),this.originalFormatFunction=this.format,this.errorTargetHandler=this.onError.bind(this),this.errorUserTargetHandler=this.onErrorForUserTarget.bind(this),this.setOptions(this.options),this.realConsoleLog=console.log,this.realConsoleInfo=console.info,this.realConsoleWarn=console.warn,this.realConsoleError=console.error,this.ALIGN=AnaLogger.ALIGN,this.ENVIRONMENT_TYPE=AnaLogger.ENVIRONMENT_TYPE}keepLogHistory(){this.keepLog=!0}releaseLogHistory(){this.keepLog=!1}resetLogHistory(){this.logHistory=[]}getLogHistory(e=!0,t=EOL){const o=JSON.parse(JSON.stringify(this.logHistory.slice(0)));return e?o.join(t):o}isNode(){return this.system===SYSTEM.NODE}isBrowser(){return!this.isNode()}setOptions({contextLenMax:e=10,idLenMax:t=5,lidLenMax:o=5,symbolLenMax:r=2,messageLenMax:s=60,hideLog:
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var require$$0=require("chalk-cjs"),require$$1=require("color-convert-cjs"),require$$2=require("rgb-hex-cjs"),require$$4=require("path"),require$$5=require("fs"),require$$6=require("os");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var require$$0__default=_interopDefaultLegacy(require$$0),require$$1__default=_interopDefaultLegacy(require$$1),require$$2__default=_interopDefaultLegacy(require$$2),require$$4__default=_interopDefaultLegacy(require$$4),require$$5__default=_interopDefaultLegacy(require$$5),require$$6__default=_interopDefaultLegacy(require$$6),anaLogger={},constants$1={};const constants={COLOR_TABLE:["#d2466e","#FFA07A","#FF7F50","#FF6347","#FFE4B5","#ADFF2F","#808000","#40E0D0","#1E90FF","#EE82EE","#708090","#DEB887","#FE642E","#210B61","#088A4B","#5E610B","#FA8258","#088A68","#B40431"],SYSTEM:{BROWSER:"BROWSER",NODE:"NODE"}},chalk=(constants$1.COLOR_TABLE=constants.COLOR_TABLE,constants$1.SYSTEM=constants.SYSTEM,require$$0__default.default),colorConvert=require$$1__default.default,rgbHex=require$$2__default.default,{COLOR_TABLE,SYSTEM}=constants$1,EOL=`
|
|
2
|
+
`;class AnaLogger{system="";logIndex=0;logCounter=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";keepLog=!1;logHistory=[];$containers=null;options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};originalFormatFunction;constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),this.originalFormatFunction=this.format,this.errorTargetHandler=this.onError.bind(this),this.errorUserTargetHandler=this.onErrorForUserTarget.bind(this),this.setOptions(this.options),this.realConsoleLog=console.log,this.realConsoleInfo=console.info,this.realConsoleWarn=console.warn,this.realConsoleError=console.error,this.ALIGN=AnaLogger.ALIGN,this.ENVIRONMENT_TYPE=AnaLogger.ENVIRONMENT_TYPE}keepLogHistory(){this.keepLog=!0}releaseLogHistory(){this.keepLog=!1}resetLogHistory(){this.logHistory=[]}getLogHistory(e=!0,t=EOL){const o=JSON.parse(JSON.stringify(this.logHistory.slice(0)));return e?o.join(t):o}isNode(){return this.system===SYSTEM.NODE}isBrowser(){return!this.isNode()}setOptions({contextLenMax:e=10,idLenMax:t=5,lidLenMax:o=5,symbolLenMax:r=2,messageLenMax:s=60,hideLog:i=!1,hideError:n=!1,hideHookMessage:a=!1,showPassingTests:h=!0,logToDom:l=void 0,logToFile:g=void 0,silent:c=!1}={}){this.options.contextLenMax=e,this.options.idLenMax=t,this.options.lidLenMax=o,this.options.messageLenMax=s,this.options.symbolLenMax=r,this.options.hideLog=!!i,this.options.hideError=!!n,this.options.hideHookMessage=!!a,this.options.showPassingTests=!!h,void 0!==l&&(this.options.logToDom=l||"#analogger"),void 0!==g&&(this.isBrowser()||(this.options.logToFile=g||"./analogger.log",this.options.logToFile=require$$4__default.default.resolve(this.options.logToFile),this.logFile=require$$5__default.default.createWriteStream(this.options.logToFile,{flags:"w"}),this.EOL=require$$6__default.default.EOL)),c&&(this.options.hideLog=!0,this.options.hideHookMessage=!0,this.options.silent=!0)}getOptions(){return this.options}truncateMessage(e="",{fit:t=0,align:o=AnaLogger.ALIGN.LEFT}){return e=""+e,t&&e.length>=t+2&&(e=e.substring(0,t-3)+"..."),e=o===AnaLogger.ALIGN.LEFT?e.padEnd(t+1," "):e.padStart(t+1," ")}onBuildLog({contextName:e,message:t="",lid:o="",symbol:r=""}={}){const s=new Date;var i=("0"+s.getHours()).slice(-2)+":"+("0"+s.getMinutes()).slice(-2)+":"+("0"+s.getSeconds()).slice(-2),i=this.truncateMessage(i,{fit:7});return e=this.truncateMessage(e,{fit:this.options.contextLenMax,align:AnaLogger.ALIGN.RIGHT}),o=this.truncateMessage(o,{fit:this.options.lidLenMax}),t=this.truncateMessage(t,{fit:this.options.messageLenMax}),`[${i}] ${e}: (${o}) ${r=this.truncateMessage(r,{fit:this.options.symbolLenMax})} `+t}onErrorForUserTarget(e,...t){this.errorUserTargetHandler(e,...t)}onError(e,...t){e.target===this.targets.USER&&this.onErrorForUserTarget(e,...t)}onDisplayLog(...e){this.log(...e)}onDisplayError(...e){this.error(...e)}setLogFormat(e){if("function"!=typeof e)return console.error("Invalid parameter for setFormat. It is expecting a function or method."),!1;this.format=e.bind(this)}resetLogFormatter(){this.format=this.originalFormatFunction}setErrorHandler(e){this.errorTargetHandler=e.bind(this)}setErrorHandlerForUserTarget(e){this.errorUserTargetHandler=e.bind(this)}isContextValid(e){return"object"==typeof e&&!Array.isArray(e)&&null!==e&&(e.hasOwnProperty("contextName")&&e.hasOwnProperty("target"))}generateDefaultContext(){const e={name:"DEFAULT",contextName:"DEFAULT",target:"ALL",symbol:"⚡"};return e.id=this.logIndex++,e.color=COLOR_TABLE[1],e}generateNewContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[this.indexColor++%(COLOR_TABLE.length-3)+2],e.symbol="",e}generateErrorContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[0],e.symbol="v",e.error=!0,e}#allegeProperties(e){let t=e;e=this.generateNewContext();return t=Object.assign({},e,t),-1<t.color.toLowerCase().indexOf("rgb")?t.color="#"+rgbHex(t.color):-1===t.color.indexOf("#")&&colorConvert&&(t.color="#"+colorConvert.keyword.hex(t.color)),t}setContexts(o){const e=Object.keys(o);o.DEFAULT=this.contexts.DEFAULT=this.generateDefaultContext(),o.ERROR=this.contexts.ERROR=this.generateErrorContext(),e.forEach(e=>{const t=o[e]||{};t.contextName=e,t.name=e,this.contexts[e]=this.#allegeProperties(t),o[e]=this.contexts[e]})}setTargets(e={}){this.targets=Object.assign({},e,{ALL:"ALL",USER:"USER"})}setActiveTarget(e){this.activeTarget=e}isTargetAllowed(e){return!e||!this.activeTarget||(e===this.targets.ALL||this.activeTarget===e)}setColumns(e,t,o){let r=0;for(var s in t)if("name"!==s){var i=t[s];const n=document.createElement("span");n.classList.add("analogger-col","analogger-col-"+s,"analogger-col-"+r),++r,n.textContent=i,e.append(n)}const n=document.createElement("span");n.classList.add("analogger-col","analogger-col-text","analogger-col-"+r),n.textContent=o,e.append(n)}writeLogToDom(o,r){this.$containers=this.$containers||document.querySelectorAll(this.options.logToDom);for(let t=0;t<this.$containers.length;++t){const s=this.$containers[t];let e=s.querySelector(".analogger-view");e||(e=document.createElement("div"),e.classList.add("analogger-view"),s.append(e));const i=document.createElement("div");i.classList.add("to-esm-line"),i.style.color=o.color,i.setAttribute("data-log-counter",this.logCounter),i.setAttribute("data-log-index",this.logIndex),this.setColumns(i,o,r),e.append(i)}}writeLogToFile(e){this.logFile.write(e+this.EOL)}processOutput(o={}){try{if(!this.isTargetAllowed(o.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var r=e.join(" | ");let t="";var s=this.format({...o,message:r});if(++this.logCounter,this.isBrowser()?(o.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,this.options.logToDom&&this.writeLogToDom(o,s),t="%c"+s):(o.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,t=chalk.hex(o.color)(s),this.options.logToFile&&this.writeLogToFile(s)),this.keepLog&&this.logHistory.push(t),this.options.hideLog)return;this.isBrowser()?this.realConsoleLog(t,"color: "+o.color):this.realConsoleLog(t),this.errorTargetHandler(o,e)}catch(e){console.error("AnaLogger:",e.message)}}isExtendedOptionsPassed(e){return"object"==typeof e&&(e.hasOwnProperty("context")||e.hasOwnProperty("target"))}convertToContext(e,t){t=t||this.generateDefaultContext();let o=e=e||t;if(e.context&&"object"==typeof e.context){const r=Object.assign({},e);delete r.context,o=Object.assign({},e.context,r)}return o=Object.assign({},t,o),delete o.context,o}log(e,...t){if(!this.isExtendedOptionsPassed(e))return o=this.generateDefaultContext(),void this.processOutput.apply(this,[o,e,...t]);var o=this.convertToContext(e);this.processOutput.apply(this,[o,...t])}error(e,...t){if(!this.options.hideError){if(!this.isExtendedOptionsPassed(e))return o=this.generateErrorContext(),void this.processOutput.apply(this,[o,e,...t]);var o=this.generateErrorContext(),t=this.convertToContext(e,o),e=Array.prototype.slice.call(arguments);this.log(t,...e)}}overrideError(){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.error"),console.error=this.onDisplayError.bind(this)}overrideConsole({log:e=!0,info:t=!0,warn:o=!0,error:r=!1}={}){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.log"),e&&(console.log=this.onDisplayLog.bind(this)),t&&(console.info=this.onDisplayLog.bind(this)),o&&(console.warn=this.onDisplayLog.bind(this)),r&&this.overrideError()}removeOverrideError(){console.warn=this.realConsoleError}removeOverride({log:e=!0,info:t=!0,warn:o=!0,error:r=!1}={}){e&&(console.log=this.realConsoleLog),t&&(console.info=this.realConsoleInfo),o&&(console.warn=this.realConsoleWarn),r&&this.removeOverrideError()}info(...e){return this.log(...e)}warn(...e){return this.log(...e)}alert(...e){if(this.isNode())return this.log(...e);e=e.join(" | ");alert(e)}assert(e,t=!0,...o){try{return"function"==typeof e?e(...o)!==t?(this.error("Asset failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0):e!==t?(this.error("Assert failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0)}catch(e){this.error("Unexpected error in assert")}return!1}}var anaLogger_1=anaLogger.anaLogger=new AnaLogger;exports.anaLogger=anaLogger_1,exports.default=anaLogger;
|
package/dist/index-esm.min.mjs
CHANGED
|
@@ -1,734 +1,2 @@
|
|
|
1
|
-
function rgbHex(
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (typeof red === 'string') {
|
|
5
|
-
[red, green, blue, alpha] = red.match(/(0?\.?\d{1,3})%?\b/g).map(component => Number(component));
|
|
6
|
-
} else if (alpha !== undefined) {
|
|
7
|
-
alpha = Number.parseFloat(alpha);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (typeof red !== 'number' ||
|
|
11
|
-
typeof green !== 'number' ||
|
|
12
|
-
typeof blue !== 'number' ||
|
|
13
|
-
red > 255 ||
|
|
14
|
-
green > 255 ||
|
|
15
|
-
blue > 255
|
|
16
|
-
) {
|
|
17
|
-
throw new TypeError('Expected three numbers below 256');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (typeof alpha === 'number') {
|
|
21
|
-
if (!isPercent && alpha >= 0 && alpha <= 1) {
|
|
22
|
-
alpha = Math.round(255 * alpha);
|
|
23
|
-
} else if (isPercent && alpha >= 0 && alpha <= 100) {
|
|
24
|
-
alpha = Math.round(255 * alpha / 100);
|
|
25
|
-
} else {
|
|
26
|
-
throw new TypeError(`Expected alpha value (${alpha}) as a fraction or percentage`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
alpha = (alpha | 1 << 8).toString(16).slice(1); // eslint-disable-line no-mixed-operators
|
|
30
|
-
} else {
|
|
31
|
-
alpha = '';
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// TODO: Remove this ignore comment.
|
|
35
|
-
// eslint-disable-next-line no-mixed-operators
|
|
36
|
-
return ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1) + alpha;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
41
|
-
* This file is generated following the conversion of
|
|
42
|
-
* [src/cjs/constants.cjs]
|
|
43
|
-
*
|
|
44
|
-
**/
|
|
45
|
-
const constants = {
|
|
46
|
-
COLOR_TABLE: [
|
|
47
|
-
"#d2466e", // Error context color
|
|
48
|
-
"#FFA07A", // Default context color
|
|
49
|
-
"#FF7F50",
|
|
50
|
-
"#FF6347",
|
|
51
|
-
"#FFE4B5",
|
|
52
|
-
"#ADFF2F",
|
|
53
|
-
"#808000",
|
|
54
|
-
"#40E0D0",
|
|
55
|
-
"#1E90FF",
|
|
56
|
-
"#EE82EE",
|
|
57
|
-
"#708090",
|
|
58
|
-
"#DEB887",
|
|
59
|
-
"#FE642E",
|
|
60
|
-
"#210B61",
|
|
61
|
-
"#088A4B",
|
|
62
|
-
"#5E610B",
|
|
63
|
-
"#FA8258",
|
|
64
|
-
"#088A68",
|
|
65
|
-
"#B40431",
|
|
66
|
-
],
|
|
67
|
-
SYSTEM: {
|
|
68
|
-
BROWSER: "BROWSER",
|
|
69
|
-
NODE: "NODE"
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const COLOR_TABLE = constants.COLOR_TABLE;
|
|
75
|
-
const SYSTEM = constants.SYSTEM;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
79
|
-
* This file is generated following the conversion of
|
|
80
|
-
* [src/cjs/ana-logger.cjs]
|
|
81
|
-
*
|
|
82
|
-
**/
|
|
83
|
-
const chalk = null;
|
|
84
|
-
const EOL =`
|
|
85
|
-
`;
|
|
86
|
-
|
|
87
|
-
class AnaLogger
|
|
88
|
-
{
|
|
89
|
-
system = ""
|
|
90
|
-
|
|
91
|
-
logIndex = 0;
|
|
92
|
-
contexts = [];
|
|
93
|
-
targets = {};
|
|
94
|
-
|
|
95
|
-
activeTarget = null;
|
|
96
|
-
|
|
97
|
-
indexColor = 0;
|
|
98
|
-
|
|
99
|
-
format = "";
|
|
100
|
-
|
|
101
|
-
keepLog = false;
|
|
102
|
-
logHistory = []
|
|
103
|
-
|
|
104
|
-
$containers = null
|
|
105
|
-
|
|
106
|
-
options = {
|
|
107
|
-
hideHookMessage: false
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
static ALIGN = {
|
|
111
|
-
LEFT : "LEFT",
|
|
112
|
-
RIGHT: "RIGHT"
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
static ENVIRONMENT_TYPE = {
|
|
116
|
-
BROWSER: "BROWSER",
|
|
117
|
-
NODE : "NODE",
|
|
118
|
-
OTHER : "OTHER"
|
|
119
|
-
}
|
|
120
|
-
originalFormatFunction;
|
|
121
|
-
|
|
122
|
-
constructor()
|
|
123
|
-
{
|
|
124
|
-
this.system = (typeof process === "object") ? SYSTEM.NODE : SYSTEM.BROWSER;
|
|
125
|
-
this.format = this.onBuildLog.bind(this);
|
|
126
|
-
this.originalFormatFunction = this.format;
|
|
127
|
-
|
|
128
|
-
this.errorTargetHandler = this.onError.bind(this);
|
|
129
|
-
this.errorUserTargetHandler = this.onErrorForUserTarget.bind(this);
|
|
130
|
-
|
|
131
|
-
this.setOptions(this.options);
|
|
132
|
-
|
|
133
|
-
this.realConsoleLog = console.log;
|
|
134
|
-
this.realConsoleInfo = console.info;
|
|
135
|
-
this.realConsoleWarn = console.warn;
|
|
136
|
-
this.realConsoleError = console.error;
|
|
137
|
-
|
|
138
|
-
this.ALIGN = AnaLogger.ALIGN;
|
|
139
|
-
this.ENVIRONMENT_TYPE = AnaLogger.ENVIRONMENT_TYPE;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
keepLogHistory()
|
|
143
|
-
{
|
|
144
|
-
this.keepLog = true;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
releaseLogHistory()
|
|
148
|
-
{
|
|
149
|
-
this.keepLog = false;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
resetLogHistory()
|
|
153
|
-
{
|
|
154
|
-
this.logHistory = [];
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
getLogHistory(join = true, symbol = EOL)
|
|
158
|
-
{
|
|
159
|
-
const history = JSON.parse(JSON.stringify(this.logHistory.slice(0)));
|
|
160
|
-
if (!join)
|
|
161
|
-
{
|
|
162
|
-
return history;
|
|
163
|
-
}
|
|
164
|
-
return history.join(symbol);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Tell whether we are in a Node environment
|
|
169
|
-
* @returns {boolean}
|
|
170
|
-
*/
|
|
171
|
-
isNode()
|
|
172
|
-
{
|
|
173
|
-
return this.system === SYSTEM.NODE
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Tell whether the logger runs from a browser
|
|
178
|
-
* @returns {boolean}
|
|
179
|
-
*/
|
|
180
|
-
isBrowser()
|
|
181
|
-
{
|
|
182
|
-
return !this.isNode()
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
setOptions({
|
|
186
|
-
contextLenMax = 10,
|
|
187
|
-
idLenMax = 5,
|
|
188
|
-
lidLenMax = 5,
|
|
189
|
-
symbolLenMax = 2,
|
|
190
|
-
messageLenMax = 60,
|
|
191
|
-
hideLog = false,
|
|
192
|
-
hideError = false,
|
|
193
|
-
hideHookMessage = false,
|
|
194
|
-
showPassingTests = true,
|
|
195
|
-
logToDom = undefined,
|
|
196
|
-
silent = false
|
|
197
|
-
} = {})
|
|
198
|
-
{
|
|
199
|
-
this.options.contextLenMax = contextLenMax;
|
|
200
|
-
this.options.idLenMax = idLenMax;
|
|
201
|
-
this.options.lidLenMax = lidLenMax;
|
|
202
|
-
this.options.messageLenMax = messageLenMax;
|
|
203
|
-
this.options.symbolLenMax = symbolLenMax;
|
|
204
|
-
this.options.hideLog = !!hideLog;
|
|
205
|
-
this.options.hideError = !!hideError;
|
|
206
|
-
this.options.hideHookMessage = !!hideHookMessage;
|
|
207
|
-
this.options.showPassingTests = !!showPassingTests;
|
|
208
|
-
if (logToDom !== undefined)
|
|
209
|
-
{
|
|
210
|
-
this.options.logToDom = logToDom || "#analogger";
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (silent)
|
|
214
|
-
{
|
|
215
|
-
this.options.hideLog = true;
|
|
216
|
-
this.options.hideHookMessage = true;
|
|
217
|
-
this.options.silent = true;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
getOptions()
|
|
222
|
-
{
|
|
223
|
-
return this.options
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
truncateMessage(input = "", {fit = 0, align = AnaLogger.ALIGN.LEFT})
|
|
227
|
-
{
|
|
228
|
-
input = "" + input;
|
|
229
|
-
if (fit && input.length >= fit + 2)
|
|
230
|
-
{
|
|
231
|
-
input = input.substring(0, fit - 3) + "...";
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
input = align === AnaLogger.ALIGN.LEFT ? input.padEnd(fit + 1, " ") : input.padStart(fit + 1, " ");
|
|
235
|
-
return input
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Format inputs
|
|
240
|
-
* @see Override {@link setLogFormat}
|
|
241
|
-
* @param contextName
|
|
242
|
-
* @param id
|
|
243
|
-
* @param message
|
|
244
|
-
* @param lid
|
|
245
|
-
* @param symbol
|
|
246
|
-
* @returns {string}
|
|
247
|
-
*/
|
|
248
|
-
onBuildLog({contextName, id, message = "", lid = "", symbol = ""} = {})
|
|
249
|
-
{
|
|
250
|
-
// Time
|
|
251
|
-
const date = new Date();
|
|
252
|
-
let time = ('0' + date.getHours()).slice(-2) + ":" + ('0' + date.getMinutes()).slice(-2) + ":" + ('0' + date.getSeconds()).slice(-2);
|
|
253
|
-
|
|
254
|
-
// Display content in columns
|
|
255
|
-
time = this.truncateMessage(time, {fit: 7});
|
|
256
|
-
contextName = this.truncateMessage(contextName, {fit: this.options.contextLenMax, align: AnaLogger.ALIGN.RIGHT});
|
|
257
|
-
// id = this.truncateMessage(id, {fit: this.options.idLenMax})
|
|
258
|
-
lid = this.truncateMessage(lid, {fit: this.options.lidLenMax});
|
|
259
|
-
message = this.truncateMessage(message, {fit: this.options.messageLenMax});
|
|
260
|
-
symbol = this.truncateMessage(symbol, {fit: this.options.symbolLenMax});
|
|
261
|
-
|
|
262
|
-
return `[${time}] ${contextName}: (${lid}) ${symbol} ${message}`
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
onErrorForUserTarget(context, ...args)
|
|
266
|
-
{
|
|
267
|
-
this.errorUserTargetHandler(context, ...args);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
onError(context, ...args)
|
|
271
|
-
{
|
|
272
|
-
if (context.target === this.targets.USER)
|
|
273
|
-
{
|
|
274
|
-
this.onErrorForUserTarget(context, ...args);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Forward input to real console log
|
|
280
|
-
* @param args
|
|
281
|
-
*/
|
|
282
|
-
onDisplayLog(...args)
|
|
283
|
-
{
|
|
284
|
-
this.log(...args);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Forward input to real console log
|
|
289
|
-
* @param args
|
|
290
|
-
*/
|
|
291
|
-
onDisplayError(...args)
|
|
292
|
-
{
|
|
293
|
-
this.error(...args);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Set log template
|
|
298
|
-
* @param format
|
|
299
|
-
*/
|
|
300
|
-
setLogFormat(format)
|
|
301
|
-
{
|
|
302
|
-
if (typeof format !== 'function')
|
|
303
|
-
{
|
|
304
|
-
console.error(`Invalid parameter for setFormat. It is expecting a function or method.`);
|
|
305
|
-
return false
|
|
306
|
-
}
|
|
307
|
-
this.format = format.bind(this);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
resetLogFormatter()
|
|
311
|
-
{
|
|
312
|
-
this.format = this.originalFormatFunction;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
setErrorHandler(handler)
|
|
316
|
-
{
|
|
317
|
-
this.errorTargetHandler = handler.bind(this);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
setErrorHandlerForUserTarget(handler)
|
|
321
|
-
{
|
|
322
|
-
this.errorUserTargetHandler = handler.bind(this);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// ------------------------------------------------
|
|
326
|
-
// Color
|
|
327
|
-
// ------------------------------------------------
|
|
328
|
-
|
|
329
|
-
// ------------------------------------------------
|
|
330
|
-
// Log Contexts
|
|
331
|
-
// ------------------------------------------------
|
|
332
|
-
isContextValid(context)
|
|
333
|
-
{
|
|
334
|
-
if (
|
|
335
|
-
!(typeof context === 'object' &&
|
|
336
|
-
!Array.isArray(context) &&
|
|
337
|
-
context !== null)
|
|
338
|
-
)
|
|
339
|
-
{
|
|
340
|
-
return false
|
|
341
|
-
}
|
|
342
|
-
return (context.hasOwnProperty("contextName") && context.hasOwnProperty("target"))
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
generateDefaultContext()
|
|
346
|
-
{
|
|
347
|
-
const defaultContext = {
|
|
348
|
-
name : "DEFAULT",
|
|
349
|
-
contextName: "DEFAULT",
|
|
350
|
-
target : "ALL",
|
|
351
|
-
symbol : "⚡"
|
|
352
|
-
};
|
|
353
|
-
|
|
354
|
-
defaultContext.id = this.logIndex++;
|
|
355
|
-
defaultContext.color = COLOR_TABLE[1];
|
|
356
|
-
return defaultContext
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
generateNewContext()
|
|
360
|
-
{
|
|
361
|
-
const newContext = this.generateDefaultContext();
|
|
362
|
-
newContext.color = COLOR_TABLE[(this.indexColor++) % (COLOR_TABLE.length - 3) + 2];
|
|
363
|
-
newContext.symbol = "";
|
|
364
|
-
return newContext
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
generateErrorContext()
|
|
368
|
-
{
|
|
369
|
-
const errorContext = this.generateDefaultContext();
|
|
370
|
-
errorContext.color = COLOR_TABLE[0];
|
|
371
|
-
errorContext.symbol = "v";
|
|
372
|
-
errorContext.error = true;
|
|
373
|
-
return errorContext
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
#allegeProperties(entry)
|
|
377
|
-
{
|
|
378
|
-
let converted = entry;
|
|
379
|
-
|
|
380
|
-
const defaultContext = this.generateNewContext();
|
|
381
|
-
|
|
382
|
-
converted = Object.assign({}, defaultContext, converted);
|
|
383
|
-
|
|
384
|
-
if (converted.color.toLowerCase().indexOf("rgb") > -1)
|
|
385
|
-
{
|
|
386
|
-
converted.color = "#" + rgbHex(converted.color);
|
|
387
|
-
}
|
|
388
|
-
else if (converted.color.indexOf("#") === -1)
|
|
389
|
-
;
|
|
390
|
-
|
|
391
|
-
return converted;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Load the context names that should be available to the environment.
|
|
396
|
-
* They are defined by the user.
|
|
397
|
-
* @see Context definitions {@link ./example/cjs/contexts-def.cjs}
|
|
398
|
-
* @param contextTable
|
|
399
|
-
*/
|
|
400
|
-
setContexts(contextTable)
|
|
401
|
-
{
|
|
402
|
-
const arr = Object.keys(contextTable);
|
|
403
|
-
contextTable["DEFAULT"] = this.contexts["DEFAULT"] = this.generateDefaultContext();
|
|
404
|
-
contextTable["ERROR"] = this.contexts["ERROR"] = this.generateErrorContext();
|
|
405
|
-
arr.forEach((key) =>
|
|
406
|
-
{
|
|
407
|
-
const contextPassed = contextTable[key] || {};
|
|
408
|
-
contextPassed.contextName = key;
|
|
409
|
-
contextPassed.name = key;
|
|
410
|
-
this.contexts[key] = this.#allegeProperties(contextPassed);
|
|
411
|
-
contextTable[key] = this.contexts[key];
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
setTargets(targetTable = {})
|
|
416
|
-
{
|
|
417
|
-
this.targets = Object.assign({}, targetTable, {ALL: "ALL", USER: "USER"});
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
setActiveTarget(target)
|
|
421
|
-
{
|
|
422
|
-
this.activeTarget = target;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
isTargetAllowed(target)
|
|
426
|
-
{
|
|
427
|
-
if (!target || !this.activeTarget)
|
|
428
|
-
{
|
|
429
|
-
return true;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
if (target === this.targets.ALL)
|
|
433
|
-
{
|
|
434
|
-
return true;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
return this.activeTarget === target;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
// ------------------------------------------------
|
|
442
|
-
// Logging methods
|
|
443
|
-
// ------------------------------------------------
|
|
444
|
-
writeLogToDom(text)
|
|
445
|
-
{
|
|
446
|
-
this.$containers = this.$containers || document.querySelectorAll(this.options.logToDom);
|
|
447
|
-
|
|
448
|
-
for (let i = 0; i < this.$containers.length; ++i)
|
|
449
|
-
{
|
|
450
|
-
const $container = this.$containers[i];
|
|
451
|
-
|
|
452
|
-
let $view = $container.querySelector(".analogger-view");
|
|
453
|
-
if (!$view)
|
|
454
|
-
{
|
|
455
|
-
$view = document.createElement("div");
|
|
456
|
-
$view.classList.add("analogger-view");
|
|
457
|
-
$container.append($view);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
const line = document.createElement("div");
|
|
461
|
-
line.classList.add("to-esm-line");
|
|
462
|
-
line.textContent = text;
|
|
463
|
-
const row = document.createElement("span");
|
|
464
|
-
row.classList.add("to-esm-row");
|
|
465
|
-
line.append(row);
|
|
466
|
-
|
|
467
|
-
$view.append(line);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Display log following template
|
|
473
|
-
* @param context
|
|
474
|
-
*/
|
|
475
|
-
processOutput(context = {})
|
|
476
|
-
{
|
|
477
|
-
try
|
|
478
|
-
{
|
|
479
|
-
if (!this.isTargetAllowed(context.target))
|
|
480
|
-
{
|
|
481
|
-
return
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
let args = Array.prototype.slice.call(arguments);
|
|
485
|
-
args.shift();
|
|
486
|
-
|
|
487
|
-
const message = args.join(" | ");
|
|
488
|
-
|
|
489
|
-
let output = "";
|
|
490
|
-
const text = this.format({...context, message});
|
|
491
|
-
|
|
492
|
-
if (this.isBrowser())
|
|
493
|
-
{
|
|
494
|
-
context.environnment = AnaLogger.ENVIRONMENT_TYPE.BROWSER;
|
|
495
|
-
if (this.options.logToDom)
|
|
496
|
-
{
|
|
497
|
-
this.writeLogToDom(text);
|
|
498
|
-
}
|
|
499
|
-
output = `%c${text}`;
|
|
500
|
-
}
|
|
501
|
-
else
|
|
502
|
-
{
|
|
503
|
-
context.environnment = AnaLogger.ENVIRONMENT_TYPE.NODE;
|
|
504
|
-
output = chalk.hex(context.color)(text);
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
if (this.keepLog)
|
|
508
|
-
{
|
|
509
|
-
this.logHistory.push(output);
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
if (this.options.hideLog)
|
|
513
|
-
{
|
|
514
|
-
return
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
if (this.isBrowser())
|
|
518
|
-
{
|
|
519
|
-
this.realConsoleLog(output, `color: ${context.color}`);
|
|
520
|
-
}
|
|
521
|
-
else
|
|
522
|
-
{
|
|
523
|
-
this.realConsoleLog(output);
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
this.errorTargetHandler(context, args);
|
|
527
|
-
}
|
|
528
|
-
catch (e)
|
|
529
|
-
{
|
|
530
|
-
console.error(`AnaLogger:`, e.message);
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
/**
|
|
535
|
-
* Check that a parameter (should be the first) uses the expected format.
|
|
536
|
-
* @param options
|
|
537
|
-
* @returns {boolean}
|
|
538
|
-
*/
|
|
539
|
-
isExtendedOptionsPassed(options)
|
|
540
|
-
{
|
|
541
|
-
if (typeof options !== "object")
|
|
542
|
-
{
|
|
543
|
-
return false;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
return options.hasOwnProperty("context") || options.hasOwnProperty("target");
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
convertToContext(options, defaultContext)
|
|
550
|
-
{
|
|
551
|
-
defaultContext = defaultContext || this.generateDefaultContext();
|
|
552
|
-
options = options || defaultContext;
|
|
553
|
-
let context = options;
|
|
554
|
-
if (options.context && typeof options.context === "object")
|
|
555
|
-
{
|
|
556
|
-
const moreOptions = Object.assign({}, options);
|
|
557
|
-
delete moreOptions.context;
|
|
558
|
-
context = Object.assign({}, options.context, moreOptions);
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
context = Object.assign({}, defaultContext, context);
|
|
562
|
-
delete context.context;
|
|
563
|
-
|
|
564
|
-
return context
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
/**
|
|
568
|
-
* console.log with options set on the first parameter to dictate console log behaviours
|
|
569
|
-
* @param options
|
|
570
|
-
* @param args
|
|
571
|
-
*/
|
|
572
|
-
log(options, ...args)
|
|
573
|
-
{
|
|
574
|
-
if (!this.isExtendedOptionsPassed(options))
|
|
575
|
-
{
|
|
576
|
-
const defaultContext = this.generateDefaultContext();
|
|
577
|
-
this.processOutput.apply(this, [defaultContext, options, ...args]);
|
|
578
|
-
return;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
let context = this.convertToContext(options);
|
|
582
|
-
|
|
583
|
-
this.processOutput.apply(this, [context, ...args]);
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
error(options, ...args)
|
|
587
|
-
{
|
|
588
|
-
if (this.options.hideError)
|
|
589
|
-
{
|
|
590
|
-
return
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
const errorContext = this.generateErrorContext();
|
|
594
|
-
let context = this.convertToContext(options, errorContext);
|
|
595
|
-
|
|
596
|
-
let args0 = Array.prototype.slice.call(arguments);
|
|
597
|
-
this.log(context, ...args0);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
overrideError()
|
|
601
|
-
{
|
|
602
|
-
if (!this.options.hideHookMessage)
|
|
603
|
-
{
|
|
604
|
-
this.realConsoleLog(`AnaLogger: Hook placed on console.error`);
|
|
605
|
-
}
|
|
606
|
-
console.error = this.onDisplayError.bind(this);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
overrideConsole({log = true, info = true, warn = true, error = false} = {})
|
|
610
|
-
{
|
|
611
|
-
if (!this.options.hideHookMessage)
|
|
612
|
-
{
|
|
613
|
-
this.realConsoleLog(`AnaLogger: Hook placed on console.log`);
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
if (log)
|
|
617
|
-
{
|
|
618
|
-
console.log = this.onDisplayLog.bind(this);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
if (info)
|
|
622
|
-
{
|
|
623
|
-
console.info = this.onDisplayLog.bind(this);
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
if (warn)
|
|
627
|
-
{
|
|
628
|
-
console.warn = this.onDisplayLog.bind(this);
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
if (error)
|
|
632
|
-
{
|
|
633
|
-
this.overrideError();
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
removeOverrideError()
|
|
638
|
-
{
|
|
639
|
-
console.warn = this.realConsoleError;
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
removeOverride({log = true, info = true, warn = true, error = false} = {})
|
|
643
|
-
{
|
|
644
|
-
if (log)
|
|
645
|
-
{
|
|
646
|
-
console.log = this.realConsoleLog;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
if (info)
|
|
650
|
-
{
|
|
651
|
-
console.info = this.realConsoleInfo;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
if (warn)
|
|
655
|
-
{
|
|
656
|
-
console.warn = this.realConsoleWarn;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
if (error)
|
|
660
|
-
{
|
|
661
|
-
this.removeOverrideError();
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
info(...args)
|
|
667
|
-
{
|
|
668
|
-
return this.log(...args)
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
warn(...args)
|
|
672
|
-
{
|
|
673
|
-
return this.log(...args)
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
alert(...args)
|
|
677
|
-
{
|
|
678
|
-
if (this.isNode())
|
|
679
|
-
{
|
|
680
|
-
return this.log(...args)
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
const message = args.join(" | ");
|
|
684
|
-
|
|
685
|
-
alert(message);
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
assert(condition, expected = true, ...args)
|
|
689
|
-
{
|
|
690
|
-
let result;
|
|
691
|
-
|
|
692
|
-
try
|
|
693
|
-
{
|
|
694
|
-
if (typeof condition === 'function')
|
|
695
|
-
{
|
|
696
|
-
result = condition(...args);
|
|
697
|
-
if (result !== expected)
|
|
698
|
-
{
|
|
699
|
-
this.error(`Asset failed`);
|
|
700
|
-
return false
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
if (this.options.showPassingTests)
|
|
704
|
-
{
|
|
705
|
-
this.log(`SUCCESS: Assert passed`);
|
|
706
|
-
}
|
|
707
|
-
return true
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
if (condition !== expected)
|
|
711
|
-
{
|
|
712
|
-
this.error(`Assert failed`);
|
|
713
|
-
return false
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
if (this.options.showPassingTests)
|
|
717
|
-
{
|
|
718
|
-
this.log(`SUCCESS: Assert passed`);
|
|
719
|
-
}
|
|
720
|
-
return true
|
|
721
|
-
}
|
|
722
|
-
catch (e)
|
|
723
|
-
{
|
|
724
|
-
this.error(`Unexpected error in assert`);
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
return false;
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
const anaLogger = new AnaLogger();
|
|
733
|
-
|
|
734
|
-
export { anaLogger };
|
|
1
|
+
function rgbHex(e,t,o,s){var r=(e+(s||"")).toString().includes("%");if("string"==typeof e?[e,t,o,s]=e.match(/(0?\.?\d{1,3})%?\b/g).map(e=>Number(e)):void 0!==s&&(s=Number.parseFloat(s)),"number"!=typeof e||"number"!=typeof t||"number"!=typeof o||255<e||255<t||255<o)throw new TypeError("Expected three numbers below 256");if("number"==typeof s){if(!r&&0<=s&&s<=1)s=Math.round(255*s);else{if(!(r&&0<=s&&s<=100))throw new TypeError(`Expected alpha value (${s}) as a fraction or percentage`);s=Math.round(255*s/100)}s=(256|s).toString(16).slice(1)}else s="";return(o|t<<8|e<<16|1<<24).toString(16).slice(1)+s}const constants={COLOR_TABLE:["#d2466e","#FFA07A","#FF7F50","#FF6347","#FFE4B5","#ADFF2F","#808000","#40E0D0","#1E90FF","#EE82EE","#708090","#DEB887","#FE642E","#210B61","#088A4B","#5E610B","#FA8258","#088A68","#B40431"],SYSTEM:{BROWSER:"BROWSER",NODE:"NODE"}},COLOR_TABLE=constants.COLOR_TABLE,SYSTEM=constants.SYSTEM,chalk=null,EOL=`
|
|
2
|
+
`;class AnaLogger{system="";logIndex=0;logCounter=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";keepLog=!1;logHistory=[];$containers=null;options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};originalFormatFunction;constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),this.originalFormatFunction=this.format,this.errorTargetHandler=this.onError.bind(this),this.errorUserTargetHandler=this.onErrorForUserTarget.bind(this),this.setOptions(this.options),this.realConsoleLog=console.log,this.realConsoleInfo=console.info,this.realConsoleWarn=console.warn,this.realConsoleError=console.error,this.ALIGN=AnaLogger.ALIGN,this.ENVIRONMENT_TYPE=AnaLogger.ENVIRONMENT_TYPE}keepLogHistory(){this.keepLog=!0}releaseLogHistory(){this.keepLog=!1}resetLogHistory(){this.logHistory=[]}getLogHistory(e=!0,t=EOL){const o=JSON.parse(JSON.stringify(this.logHistory.slice(0)));return e?o.join(t):o}isNode(){return this.system===SYSTEM.NODE}isBrowser(){return!this.isNode()}setOptions({contextLenMax:e=10,idLenMax:t=5,lidLenMax:o=5,symbolLenMax:s=2,messageLenMax:r=60,hideLog:i=!1,hideError:n=!1,hideHookMessage:a=!1,showPassingTests:h=!0,logToDom:l=void 0,logToFile:g=void 0,silent:c=!1}={}){this.options.contextLenMax=e,this.options.idLenMax=t,this.options.lidLenMax=o,this.options.messageLenMax=r,this.options.symbolLenMax=s,this.options.hideLog=!!i,this.options.hideError=!!n,this.options.hideHookMessage=!!a,this.options.showPassingTests=!!h,void 0!==l&&(this.options.logToDom=l||"#analogger"),void 0!==g&&(this.isBrowser()||(this.options.logToFile=g||"./analogger.log",this.options.logToFile=require("path").resolve(this.options.logToFile),this.logFile=require("fs").createWriteStream(this.options.logToFile,{flags:"w"}),this.EOL=require("os").EOL)),c&&(this.options.hideLog=!0,this.options.hideHookMessage=!0,this.options.silent=!0)}getOptions(){return this.options}truncateMessage(e="",{fit:t=0,align:o=AnaLogger.ALIGN.LEFT}){return e=""+e,t&&e.length>=t+2&&(e=e.substring(0,t-3)+"..."),e=o===AnaLogger.ALIGN.LEFT?e.padEnd(t+1," "):e.padStart(t+1," ")}onBuildLog({contextName:e,message:t="",lid:o="",symbol:s=""}={}){const r=new Date;var i=("0"+r.getHours()).slice(-2)+":"+("0"+r.getMinutes()).slice(-2)+":"+("0"+r.getSeconds()).slice(-2),i=this.truncateMessage(i,{fit:7});return e=this.truncateMessage(e,{fit:this.options.contextLenMax,align:AnaLogger.ALIGN.RIGHT}),o=this.truncateMessage(o,{fit:this.options.lidLenMax}),t=this.truncateMessage(t,{fit:this.options.messageLenMax}),`[${i}] ${e}: (${o}) ${s=this.truncateMessage(s,{fit:this.options.symbolLenMax})} `+t}onErrorForUserTarget(e,...t){this.errorUserTargetHandler(e,...t)}onError(e,...t){e.target===this.targets.USER&&this.onErrorForUserTarget(e,...t)}onDisplayLog(...e){this.log(...e)}onDisplayError(...e){this.error(...e)}setLogFormat(e){if("function"!=typeof e)return console.error("Invalid parameter for setFormat. It is expecting a function or method."),!1;this.format=e.bind(this)}resetLogFormatter(){this.format=this.originalFormatFunction}setErrorHandler(e){this.errorTargetHandler=e.bind(this)}setErrorHandlerForUserTarget(e){this.errorUserTargetHandler=e.bind(this)}isContextValid(e){return"object"==typeof e&&!Array.isArray(e)&&null!==e&&(e.hasOwnProperty("contextName")&&e.hasOwnProperty("target"))}generateDefaultContext(){const e={name:"DEFAULT",contextName:"DEFAULT",target:"ALL",symbol:"⚡"};return e.id=this.logIndex++,e.color=COLOR_TABLE[1],e}generateNewContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[this.indexColor++%(COLOR_TABLE.length-3)+2],e.symbol="",e}generateErrorContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[0],e.symbol="v",e.error=!0,e}#allegeProperties(e){let t=e;e=this.generateNewContext();return t=Object.assign({},e,t),-1<t.color.toLowerCase().indexOf("rgb")?t.color="#"+rgbHex(t.color):t.color.indexOf("#"),t}setContexts(o){const e=Object.keys(o);o.DEFAULT=this.contexts.DEFAULT=this.generateDefaultContext(),o.ERROR=this.contexts.ERROR=this.generateErrorContext(),e.forEach(e=>{const t=o[e]||{};t.contextName=e,t.name=e,this.contexts[e]=this.#allegeProperties(t),o[e]=this.contexts[e]})}setTargets(e={}){this.targets=Object.assign({},e,{ALL:"ALL",USER:"USER"})}setActiveTarget(e){this.activeTarget=e}isTargetAllowed(e){return!e||!this.activeTarget||(e===this.targets.ALL||this.activeTarget===e)}setColumns(e,t,o){let s=0;for(var r in t)if("name"!==r){var i=t[r];const n=document.createElement("span");n.classList.add("analogger-col","analogger-col-"+r,"analogger-col-"+s),++s,n.textContent=i,e.append(n)}const n=document.createElement("span");n.classList.add("analogger-col","analogger-col-text","analogger-col-"+s),n.textContent=o,e.append(n)}writeLogToDom(o,s){this.$containers=this.$containers||document.querySelectorAll(this.options.logToDom);for(let t=0;t<this.$containers.length;++t){const r=this.$containers[t];let e=r.querySelector(".analogger-view");e||(e=document.createElement("div"),e.classList.add("analogger-view"),r.append(e));const i=document.createElement("div");i.classList.add("to-esm-line"),i.style.color=o.color,i.setAttribute("data-log-counter",this.logCounter),i.setAttribute("data-log-index",this.logIndex),this.setColumns(i,o,s),e.append(i)}}writeLogToFile(e){this.logFile.write(e+this.EOL)}processOutput(o={}){try{if(!this.isTargetAllowed(o.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var s=e.join(" | ");let t="";var r=this.format({...o,message:s});if(++this.logCounter,this.isBrowser()?(o.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,this.options.logToDom&&this.writeLogToDom(o,r),t="%c"+r):(o.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,t=chalk.hex(o.color)(r),this.options.logToFile&&this.writeLogToFile(r)),this.keepLog&&this.logHistory.push(t),this.options.hideLog)return;this.isBrowser()?this.realConsoleLog(t,"color: "+o.color):this.realConsoleLog(t),this.errorTargetHandler(o,e)}catch(e){console.error("AnaLogger:",e.message)}}isExtendedOptionsPassed(e){return"object"==typeof e&&(e.hasOwnProperty("context")||e.hasOwnProperty("target"))}convertToContext(e,t){t=t||this.generateDefaultContext();let o=e=e||t;if(e.context&&"object"==typeof e.context){const s=Object.assign({},e);delete s.context,o=Object.assign({},e.context,s)}return o=Object.assign({},t,o),delete o.context,o}log(e,...t){if(!this.isExtendedOptionsPassed(e))return o=this.generateDefaultContext(),void this.processOutput.apply(this,[o,e,...t]);var o=this.convertToContext(e);this.processOutput.apply(this,[o,...t])}error(e,...t){if(!this.options.hideError){if(!this.isExtendedOptionsPassed(e))return o=this.generateErrorContext(),void this.processOutput.apply(this,[o,e,...t]);var o=this.generateErrorContext(),o=this.convertToContext(e,o),s=Array.prototype.slice.call(arguments);this.log(o,...s)}}overrideError(){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.error"),console.error=this.onDisplayError.bind(this)}overrideConsole({log:e=!0,info:t=!0,warn:o=!0,error:s=!1}={}){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.log"),e&&(console.log=this.onDisplayLog.bind(this)),t&&(console.info=this.onDisplayLog.bind(this)),o&&(console.warn=this.onDisplayLog.bind(this)),s&&this.overrideError()}removeOverrideError(){console.warn=this.realConsoleError}removeOverride({log:e=!0,info:t=!0,warn:o=!0,error:s=!1}={}){e&&(console.log=this.realConsoleLog),t&&(console.info=this.realConsoleInfo),o&&(console.warn=this.realConsoleWarn),s&&this.removeOverrideError()}info(...e){return this.log(...e)}warn(...e){return this.log(...e)}alert(...e){if(this.isNode())return this.log(...e);e=e.join(" | ");alert(e)}assert(e,t=!0,...o){try{return"function"==typeof e?e(...o)!==t?(this.error("Asset failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0):e!==t?(this.error("Assert failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0)}catch(e){this.error("Unexpected error in assert")}return!1}}const anaLogger=new AnaLogger;export{anaLogger};
|
package/dist/index.css
CHANGED
|
@@ -1,47 +1,83 @@
|
|
|
1
|
-
.analogger {
|
|
2
|
-
background-color: #462210;
|
|
3
|
-
border: 4px solid #16122a;
|
|
4
|
-
box-shadow: 3px 4px 9px 0px rgba(0, 0, 0, 0.75);
|
|
5
|
-
height: 320px;
|
|
6
|
-
overflow: hidden;
|
|
7
|
-
margin: 1rem;
|
|
8
|
-
padding: 13px 0 0 0;
|
|
9
|
-
width: 80%;
|
|
10
|
-
max-height: 300px;
|
|
11
|
-
}
|
|
12
|
-
.analogger .analogger-view {
|
|
13
|
-
background-color: #0c0c0c;
|
|
14
|
-
box-sizing: border-box;
|
|
15
|
-
color: antiquewhite;
|
|
16
|
-
display: block;
|
|
17
|
-
font-family: sans-serif;
|
|
18
|
-
font-size: 12px;
|
|
19
|
-
line-height: 30px;
|
|
20
|
-
height: calc(100% - 20px);
|
|
21
|
-
margin: 20px 0 0 0;
|
|
22
|
-
padding: 12px;
|
|
23
|
-
overflow: auto;
|
|
24
|
-
position: relative;
|
|
25
|
-
width: 100%;
|
|
26
|
-
}
|
|
27
|
-
.analogger .analogger-view
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
1
|
+
.analogger {
|
|
2
|
+
background-color: #462210;
|
|
3
|
+
border: 4px solid #16122a;
|
|
4
|
+
box-shadow: 3px 4px 9px 0px rgba(0, 0, 0, 0.75);
|
|
5
|
+
height: 320px;
|
|
6
|
+
overflow: hidden;
|
|
7
|
+
margin: 1rem;
|
|
8
|
+
padding: 13px 0 0 0;
|
|
9
|
+
width: 80%;
|
|
10
|
+
max-height: 300px;
|
|
11
|
+
}
|
|
12
|
+
.analogger .analogger-view {
|
|
13
|
+
background-color: #0c0c0c;
|
|
14
|
+
box-sizing: border-box;
|
|
15
|
+
color: antiquewhite;
|
|
16
|
+
display: block;
|
|
17
|
+
font-family: sans-serif;
|
|
18
|
+
font-size: 12px;
|
|
19
|
+
line-height: 30px;
|
|
20
|
+
height: calc(100% - 20px);
|
|
21
|
+
margin: 20px 0 0 0;
|
|
22
|
+
padding: 12px;
|
|
23
|
+
overflow: auto;
|
|
24
|
+
position: relative;
|
|
25
|
+
width: 100%;
|
|
26
|
+
}
|
|
27
|
+
.analogger .analogger-view .to-esm-line {
|
|
28
|
+
display: block;
|
|
29
|
+
overflow: hidden;
|
|
30
|
+
white-space: nowrap;
|
|
31
|
+
}
|
|
32
|
+
.analogger .analogger-view .to-esm-line .analogger-col {
|
|
33
|
+
display: inline-block;
|
|
34
|
+
overflow: hidden;
|
|
35
|
+
padding: 0 4px;
|
|
36
|
+
text-overflow: ellipsis;
|
|
37
|
+
white-space: nowrap;
|
|
38
|
+
}
|
|
39
|
+
.analogger .analogger-view .to-esm-line .analogger-col:nth-child(1) {
|
|
40
|
+
width: 60px;
|
|
41
|
+
}
|
|
42
|
+
.analogger .analogger-view .to-esm-line .analogger-col:nth-child(2) {
|
|
43
|
+
width: 40px;
|
|
44
|
+
}
|
|
45
|
+
.analogger .analogger-view .to-esm-line .analogger-col:nth-child(3) {
|
|
46
|
+
width: 20px;
|
|
47
|
+
}
|
|
48
|
+
.analogger .analogger-view .to-esm-line .analogger-col:nth-child(4) {
|
|
49
|
+
width: 16px;
|
|
50
|
+
}
|
|
51
|
+
.analogger .analogger-view .to-esm-line .analogger-col:nth-child(5) {
|
|
52
|
+
width: 60px;
|
|
53
|
+
}
|
|
54
|
+
.analogger .analogger-view .to-esm-line .analogger-col:nth-child(6) {
|
|
55
|
+
width: 70px;
|
|
56
|
+
}
|
|
57
|
+
.analogger .analogger-view .to-esm-line .analogger-col:nth-child(7) {
|
|
58
|
+
width: auto;
|
|
59
|
+
}
|
|
60
|
+
.analogger .analogger-view .to-esm-line .analogger-col-text {
|
|
61
|
+
min-width: 200px;
|
|
62
|
+
}
|
|
63
|
+
.analogger .analogger-view::-webkit-scrollbar-thumb {
|
|
64
|
+
border: 5px solid transparent;
|
|
65
|
+
border-radius: 100px;
|
|
66
|
+
background-color: #514b6e;
|
|
67
|
+
background-clip: content-box;
|
|
68
|
+
}
|
|
69
|
+
.analogger .analogger-view::-webkit-scrollbar {
|
|
70
|
+
width: 20px;
|
|
71
|
+
}
|
|
72
|
+
.analogger .analogger-view::-webkit-scrollbar-track {
|
|
73
|
+
background-color: #382525;
|
|
74
|
+
border-radius: 100px;
|
|
75
|
+
}
|
|
76
|
+
.analogger .analogger-view::-webkit-scrollbar-thumb {
|
|
77
|
+
border-radius: 100px;
|
|
78
|
+
border: 4px solid transparent;
|
|
79
|
+
background-clip: content-box;
|
|
80
|
+
background-color: #9f6c53;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "analogger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Js Logger",
|
|
5
5
|
"main": "dist/index-cjs.min.cjs",
|
|
6
6
|
"module": "dist/index-esm.min.mjs",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"rollup-plugin-uglify": "^6.0.4",
|
|
69
69
|
"semantic-release": "^19.0.2",
|
|
70
70
|
"sinon": "^13.0.1",
|
|
71
|
-
"to-esm": "^1.6.
|
|
71
|
+
"to-esm": "^1.6.8"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"chalk": "^5.0.0",
|
package/.eslintrc.cjs
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
env: {
|
|
3
|
-
browser: true,
|
|
4
|
-
node: true
|
|
5
|
-
},
|
|
6
|
-
parser: "@babel/eslint-parser",
|
|
7
|
-
parserOptions: {
|
|
8
|
-
"ecmaVersion": 2020
|
|
9
|
-
},
|
|
10
|
-
rules: {
|
|
11
|
-
"comma-dangle": 0,
|
|
12
|
-
"comma-style": ["error", "last"],
|
|
13
|
-
"no-bitwise": "off",
|
|
14
|
-
"no-console": "off",
|
|
15
|
-
"no-nested-ternary": "off",
|
|
16
|
-
"no-plusplus": "off",
|
|
17
|
-
"prefer-const": "off",
|
|
18
|
-
"spaced-comment": "off",
|
|
19
|
-
"no-debugger": "error",
|
|
20
|
-
"quotes": ["error", "double"],
|
|
21
|
-
"semi": 2,
|
|
22
|
-
"no-unused-vars": "error",
|
|
23
|
-
"max-len": ["error", {code: 200}]
|
|
24
|
-
}
|
|
25
|
-
};
|
package/.nycrc
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"lines": 100,
|
|
3
|
-
"statements": 100,
|
|
4
|
-
"functions": 100,
|
|
5
|
-
"branches": 50,
|
|
6
|
-
"reporter": [
|
|
7
|
-
"json",
|
|
8
|
-
"text-summary"
|
|
9
|
-
],
|
|
10
|
-
"exclude": [
|
|
11
|
-
"coverage/**",
|
|
12
|
-
"docs/**",
|
|
13
|
-
"test{,s}/**",
|
|
14
|
-
"test{,-*}.{js,cjs,mjs,ts}",
|
|
15
|
-
"lib/browser/**",
|
|
16
|
-
"package-scripts.js",
|
|
17
|
-
"scripts/**"
|
|
18
|
-
],
|
|
19
|
-
"check-coverage": true
|
|
20
|
-
}
|
package/.releaserc
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"branches": ["test", "main", "next"],
|
|
3
|
-
"dryRun": false,
|
|
4
|
-
"ci": true,
|
|
5
|
-
"debug": true,
|
|
6
|
-
"plugins": [
|
|
7
|
-
["@semantic-release/commit-analyzer",
|
|
8
|
-
{
|
|
9
|
-
"preset": "angular",
|
|
10
|
-
"releaseRules": [
|
|
11
|
-
{"type": "docs", "scope":"README", "release": "patch"},
|
|
12
|
-
{"type": "refactor", "release": "patch"},
|
|
13
|
-
{"type": "style", "release": "patch"}
|
|
14
|
-
],
|
|
15
|
-
"parserOpts": {
|
|
16
|
-
"noteKeywords": ["BREAKING CHANGE", "BREAKING CHANGES"]
|
|
17
|
-
}
|
|
18
|
-
}],
|
|
19
|
-
"@semantic-release/release-notes-generator",
|
|
20
|
-
"@semantic-release/changelog",
|
|
21
|
-
"@semantic-release/npm",
|
|
22
|
-
["@semantic-release/git", {
|
|
23
|
-
"assets": ["package.json", "CHANGELOG.md"],
|
|
24
|
-
"message": "AnaLogger Release: ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
|
25
|
-
}]
|
|
26
|
-
]
|
|
27
|
-
}
|
package/ci.md
DELETED
|
Binary file
|
package/todo.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
// Log to server
|