analogger 1.1.4 → 1.2.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.
@@ -0,0 +1,10 @@
1
+ name: Version
2
+ description: 'Version builds automatically'
3
+ author: 'thimpat'
4
+ runs:
5
+ using: composite
6
+ steps:
7
+ - name: Welcome
8
+ shell: powershell
9
+ run: |
10
+ echo Welcome
@@ -0,0 +1,27 @@
1
+ name: Versioning AnaLogger
2
+ env:
3
+ ANALOGGER_APP_NAME: "release/analogger"
4
+ on:
5
+ push:
6
+ branches:
7
+ - main
8
+ - ci
9
+ paths-ignore:
10
+ - "**.md"
11
+ - ".vscode/**"
12
+ jobs:
13
+ set-version:
14
+ runs-on: [self-hosted]
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - name: Check out Git repository
18
+ uses: thimpat/analogger/.github/actions/checkout@ci
19
+ - name: Install dependencies
20
+ shell: powershell
21
+ run: npm install
22
+ - name: Upgrade version
23
+ shell: powershell
24
+ run: |
25
+ npx semantic-release
26
+ - name: Versioning successful
27
+ run: echo "Success"
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  Analogger is a very simple logger for both Node and the Browser.
3
- It is a library using both CJS and ESM.
3
+ It is a library using both CJS and ESM.
4
4
  It serves as a packaging example of **hybrid (CJS/ESM) module**.
5
5
 
6
6
  ## Installation
@@ -45,7 +45,7 @@ import {anaLogger} from "analogger"
45
45
 
46
46
  ### log() / info() / warn() / error()
47
47
 
48
- Display a message in the terminal or the inspector. Depending on where the process is running.
48
+ Display a message in the terminal or the inspector, depending on where the process is running.
49
49
 
50
50
  ```javascript
51
51
  anaLogger.log(`I'am some log`);
@@ -61,7 +61,7 @@ anaLogger.error(`I'am some log`);
61
61
  anaLogger.alert(`I'am some log`);
62
62
  ```
63
63
 
64
- Display the browser native message box if ran from it, otherwise displays the message in the terminal.
64
+ Display the browser native message box if run from it; otherwise, it displays the message in the terminal.
65
65
 
66
66
  <br/>
67
67
 
@@ -76,8 +76,8 @@ console.log(`Log After override`);
76
76
 
77
77
  Override console.log, console.info and console.warn. If you already have many console.log running in your system,
78
78
  it allows hiding them all in one go.
79
- In this example, the terminal (or inspector) will not show the message "Log After override". All following messages
80
- either.
79
+ In this example, the terminal (or inspector) will not show the message "Log After override". All following messages
80
+ either.
81
81
 
82
82
  <br/>
83
83
 
@@ -87,6 +87,12 @@ Same as above, but for errors (console.error)
87
87
 
88
88
  <br/>
89
89
 
90
+ ### removeOverride() | removeOverrideError()
91
+
92
+ Remove overridden console methods
93
+
94
+ <br/>
95
+
90
96
  ### setContexts()
91
97
 
92
98
  #### Contexts
@@ -97,18 +103,18 @@ A context allows grouping the logs by functionality by assigning them some colou
97
103
  ##### Examples
98
104
 
99
105
  ```javascript
100
- const LOG_CONTEXT = {STANDARD: null, TEST: {color: "#B18904"}, C1: null, C2: null, C3: null, DEFAULT: {}}
106
+ const LOG_CONTEXTS = {STANDARD: null, TEST: {color: "#B18904"}, C1: null, C2: null, C3: null, DEFAULT: {}}
101
107
  const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"};
102
108
 
103
- anaLogger.setContexts(LOG_CONTEXT);
109
+ anaLogger.setContexts(LOG_CONTEXTS);
104
110
 
105
- anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
106
- anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
107
- anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
111
+ anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C1`);
112
+ anaLogger.log(LOG_CONTEXTS.C2, `Test Log example C2`);
113
+ anaLogger.log(LOG_CONTEXTS.C3, `Test Log example C3`);
108
114
  ```
109
115
 
110
- See LOG_CONTEXT.C1 in this example to categorise the functionality we want to monitor.
111
- For instance, LOG_CONTEXT.INVESTIGATING_TIMER_EFFECT could be a to display output related to something that has to
116
+ See LOG_CONTEXTS.C1 in this example to categorise the functionality we want to monitor.
117
+ For instance, LOG_CONTEXTS.INVESTIGATING_TIMER_EFFECT could display output related to something that has to
112
118
  do with a timer.
113
119
 
114
120
  The "Testing log 2" log will not show up in the console or the terminal.
@@ -128,29 +134,25 @@ The "Testing log 2" log will not show up in the console or the terminal.
128
134
 
129
135
  #### Targets
130
136
 
131
- Targets allow to define some log categories. They can be developpers, roles, etc.
137
+ Targets allow defining some log categories. For example, they can be developers, roles, etc.
132
138
  setActiveTarget() allows hiding logs from other devs or roles.
133
139
 
134
140
  ##### Examples
135
141
 
136
142
  ```javascript
137
- anaLogger.setActiveTarget(LOG_TARGETS.DEV1);
138
- console.log({target: LOG_CONTEXT.DEV1}, `Testing log 1`)
139
- console.log({target: LOG_CONTEXT.DEV2}, `Testing log 2`)
140
- console.log({context: LOG_CONTEXT.DEV3}, `Testing log 3`)
141
- console.log(`Testing log 4`)
142
- ```
143
+ const LOG_CONTEXTS = {STANDARD: null, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
144
+ const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"};
143
145
 
146
+ anaLogger.setContexts(LOG_CONTEXTS);
147
+ anaLogger.setActiveTarget(LOG_TARGETS.DEV1); // <- You are DEV1
144
148
 
145
- ```javascript
146
- const LOG_CONTEXT = {STANDARD: null, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
147
- const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"};
149
+ console.log({target: LOG_TARGETS.DEV1}, `Testing log 1`); // You will see this
150
+ console.log({target: LOG_TARGETS.DEV2}, `Testing log 2`); // You will not see this
151
+ console.log({context: LOG_CONTEXTS.STANDARD}, `Testing log 3`); // You will see this
152
+ console.log(`Testing log 4`); // You will see this. No context = LOG_CONTEXTS.ALL
148
153
 
149
- anaLogger.setContexts(LOG_CONTEXT);
150
154
 
151
- anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
152
- anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
153
- anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
155
+ anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C1`); // You will see this
154
156
  ```
155
157
 
156
158
  <br/><br/>
@@ -158,9 +160,12 @@ anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
158
160
  ### assert()
159
161
 
160
162
  You can set some tests directly in the code. It serves as early feedback.
161
- It is useful to guarantee that the code is running straight away, rather than waiting on the CI to send its feedback.
163
+ It is helpful to guarantee that the code is running straight away rather than waiting on the CI to send its feedback.
162
164
 
163
165
 
164
166
  ```javascript
165
- anaLogger.asset((a, b)=> a === b, true)
166
- ```
167
+ anaLogger.assert(1 === 1)
168
+ anaLogger.assert(1 === 2)
169
+ anaLogger.assert(()=>true, true)
170
+ anaLogger.assert((a, b)=> a === b, true, 2, 2)
171
+ ```
package/ci.md ADDED
Binary file
package/package.json CHANGED
@@ -1,50 +1,60 @@
1
- {
2
- "name": "analogger",
3
- "version": "1.1.4",
4
- "description": "Js Logger",
5
- "main": "dist/index-cjs.min.cjs",
6
- "module": "dist/index-esm.min.mjs",
7
- "type": "module",
8
- "exports": {
9
- ".": {
10
- "require": "./dist/index-cjs.min.cjs",
11
- "import": "./dist/index-esm.min.mjs"
12
- }
13
- },
14
- "scripts": {
15
- "convert-cjs:esm": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --output=src/esm/ --input=\"example/**/*.cjs\" --output=example/esm/ --config=\".toesm.cjs\"",
16
- "convert-cjs:browser": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --input=\"example/**/*.cjs\" --output=src/esm-browser/ --output=example/esm-browser/ --config=\".toesm.cjs\" --solvedep",
17
- "bundle:prod:cjs": "rollup --config rollup-cjs.config.js",
18
- "bundle:prod:esm": "rollup --config rollup-esm.config.js",
19
- "bundle:prod": "npm run convert-cjs:esm && npm run convert-cjs:browser && npm run bundle:prod:cjs && npm run bundle:prod:esm",
20
- "demo": "npm run bundle:prod && node example/cjs/demo.cjs"
21
- },
22
- "author": "Patrice Thimothee",
23
- "license": "MIT",
24
- "homepage": "https://github.com/thimpat/analogger/blob/main/README.md",
25
- "repository": {
26
- "type": "git",
27
- "url": "https://github.com/thimpat/analogger.git"
28
- },
29
- "devDependencies": {
30
- "@babel/core": "^7.17.0",
31
- "@babel/eslint-parser": "^7.17.0",
32
- "@babel/preset-env": "^7.16.11",
33
- "@rollup/plugin-commonjs": "^21.0.1",
34
- "@rollup/plugin-node-resolve": "^13.1.3",
35
- "babel-eslint": "^10.1.0",
36
- "eslint": "^8.8.0",
37
- "rollup": "^2.67.0",
38
- "rollup-plugin-delete": "^2.0.0",
39
- "rollup-plugin-uglify": "^6.0.4",
40
- "to-esm": "^1.6.2"
41
- },
42
- "dependencies": {
43
- "chalk": "^5.0.0",
44
- "chalk-cjs": "npm:chalk@^4.1.2",
45
- "color-convert": "^2.0.1",
46
- "color-convert-cjs": "npm:color-convert@^2.0.1",
47
- "rgb-hex": "^4.0.0",
48
- "rgb-hex-cjs": "npm:rgb-hex@^3.0.0"
49
- }
50
- }
1
+ {
2
+ "name": "analogger",
3
+ "version": "1.2.0",
4
+ "description": "Js Logger",
5
+ "main": "dist/index-cjs.min.cjs",
6
+ "module": "dist/index-esm.min.mjs",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "require": "./dist/index-cjs.min.cjs",
11
+ "import": "./dist/index-esm.min.mjs"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "convert-cjs:esm": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --output=src/esm/ --input=\"example/**/*.cjs\" --output=example/esm/ --config=\".toesm.cjs\"",
16
+ "convert-cjs:browser": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --input=\"example/**/*.cjs\" --output=src/esm-browser/ --output=example/esm-browser/ --config=\".toesm.cjs\" --solvedep",
17
+ "bundle:prod:cjs": "rollup --config rollup-cjs.config.js",
18
+ "bundle:prod:esm": "rollup --config rollup-esm.config.js",
19
+ "bundle:prod": "npm run convert-cjs:esm && npm run convert-cjs:browser && npm run bundle:prod:cjs && npm run bundle:prod:esm",
20
+ "demo": "npm run bundle:prod && node example/cjs/demo.cjs"
21
+ },
22
+ "author": "Patrice Thimothee",
23
+ "license": "MIT",
24
+ "homepage": "https://github.com/thimpat/analogger/blob/main/README.md",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/thimpat/analogger.git"
28
+ },
29
+ "release": {
30
+ "branches": [
31
+ "main",
32
+ {
33
+ "name": "ci"
34
+ },
35
+ "master"
36
+ ]
37
+ },
38
+ "devDependencies": {
39
+ "@babel/core": "^7.17.0",
40
+ "@babel/eslint-parser": "^7.17.0",
41
+ "@babel/preset-env": "^7.16.11",
42
+ "@rollup/plugin-commonjs": "^21.0.1",
43
+ "@rollup/plugin-node-resolve": "^13.1.3",
44
+ "babel-eslint": "^10.1.0",
45
+ "eslint": "^8.8.0",
46
+ "rollup": "^2.67.0",
47
+ "rollup-plugin-delete": "^2.0.0",
48
+ "rollup-plugin-uglify": "^6.0.4",
49
+ "semantic-release": "^19.0.2",
50
+ "to-esm": "^1.6.2"
51
+ },
52
+ "dependencies": {
53
+ "chalk": "^5.0.0",
54
+ "chalk-cjs": "npm:chalk@^4.1.2",
55
+ "color-convert": "^2.0.1",
56
+ "color-convert-cjs": "npm:color-convert@^2.0.1",
57
+ "rgb-hex": "^4.0.0",
58
+ "rgb-hex-cjs": "npm:rgb-hex@^3.0.0"
59
+ }
60
+ }
@@ -1 +0,0 @@
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;class AnaLogger{system="";logIndex=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),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}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:n=!1,hideError:i=!1,hideHookMessage:a=!1,showPassingTests:g=!0,silent:l=!1}={}){this.options.contextLenMax=e,this.options.idLenMax=t,this.options.lidLenMax=o,this.options.messageLenMax=s,this.options.symbolLenMax=r,this.options.hideLog=!!n,this.options.hideError=!!i,this.options.hideHookMessage=!!a,this.options.showPassingTests=!!g,l&&(this.options.hideLog=!0,this.options.hideHookMessage=!0)}truncateMessage(e="",{fit:t=0,align:o=AnaLogger.ALIGN.LEFT}){try{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," ")}catch(e){console.error("AnaLogger:",e)}}onBuildLog({contextName:e,message:t="",lid:o="",symbol:r=""}={}){const s=new Date;var n=("0"+s.getHours()).slice(-2)+":"+("0"+s.getMinutes()).slice(-2)+":"+("0"+s.getSeconds()).slice(-2),n=this.truncateMessage(n,{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}),`[${n}] ${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)}setErrorHandler(e){this.errorTargetHandler=e.bind(this)}setErrorHandlerForUserTarget(e){this.errorUserTargetHandler=e.bind(this)}isContext(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();if(t=t||{contextName:"DEFAULT"},Array.isArray(t))throw new Error(`AnaLogger: Cannot convert Array [${JSON.stringify(t)}] to context`);if(("string"==typeof t||t instanceof String)&&(t={contextName:t}),"object"!=typeof t)throw new Error(`AnaLogger: Cannot convert Unknown [${JSON.stringify(t)}] to context`);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}enableContexts(e){this.contexts.forEach(e=>{})}getActiveLogContexts(){}isTargetAllowed(e){return!e||!this.activeTarget||(e===this.targets.ALL||this.activeTarget===e)}processLog(t={}){try{if(this.options.hideLog)return;if(!this.isTargetAllowed(t.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var o=e.join(" | "),r=this.format({...t,message:o});this.isBrowser()?(t.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,this.realConsoleLog("%c"+r,"color: "+t.color)):(t.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,this.realConsoleLog(chalk.hex(t.color)(r))),this.errorTargetHandler(t,e)}catch(e){console.error("AnaLogger:",e.message)}}isExtendedOptionsPassed(e){return"object"==typeof e&&(e.hasOwnProperty("context")||e.hasOwnProperty("target"))}log(e,...t){var o;if(!this.isExtendedOptionsPassed(e))return o=this.generateDefaultContext(),void this.processLog.apply(this,[o,e,...t]);let r=e;if("object"==typeof e.context){const s=Object.assign({},e);delete s.context,r=Object.assign({},e.context,s)}r.hasOwnProperty("context")&&(r=Object.assign({},this.generateDefaultContext(),r),delete r.context),this.processLog.apply(this,[r,...t])}error(e,...t){if(!this.options.hideError){var o=this.generateErrorContext();if(this.isExtendedOptionsPassed(e))return e=Object.assign({},o,e),this.log(e,...t);e=Array.prototype.slice.call(arguments);this.log(o,...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()}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){try{if("function"==typeof e)return e()!==t?void this.error("Asset failed"):void(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"));if(e!==t)return void this.error("Asset failed");this.options.showPassingTests&&this.log("SUCCESS: Assert passed")}catch(e){this.error("Unexpected error in assert")}}}var anaLogger_1=anaLogger.anaLogger=new AnaLogger;exports.anaLogger=anaLogger_1,exports.default=anaLogger;
@@ -1 +0,0 @@
1
- function rgbHex(e,t,o,r){var s=(e+(r||"")).toString().includes("%");if("string"==typeof e?[e,t,o,r]=e.match(/(0?\.?\d{1,3})%?\b/g).map(e=>Number(e)):void 0!==r&&(r=Number.parseFloat(r)),"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 r){if(!s&&0<=r&&r<=1)r=Math.round(255*r);else{if(!(s&&0<=r&&r<=100))throw new TypeError(`Expected alpha value (${r}) as a fraction or percentage`);r=Math.round(255*r/100)}r=(256|r).toString(16).slice(1)}else r="";return(o|t<<8|e<<16|1<<24).toString(16).slice(1)+r}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;class AnaLogger{system="";logIndex=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),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}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:n=!1,hideError:i=!1,hideHookMessage:a=!1,showPassingTests:g=!0,silent:h=!1}={}){this.options.contextLenMax=e,this.options.idLenMax=t,this.options.lidLenMax=o,this.options.messageLenMax=s,this.options.symbolLenMax=r,this.options.hideLog=!!n,this.options.hideError=!!i,this.options.hideHookMessage=!!a,this.options.showPassingTests=!!g,h&&(this.options.hideLog=!0,this.options.hideHookMessage=!0)}truncateMessage(e="",{fit:t=0,align:o=AnaLogger.ALIGN.LEFT}){try{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," ")}catch(e){console.error("AnaLogger:",e)}}onBuildLog({contextName:e,message:t="",lid:o="",symbol:r=""}={}){const s=new Date;var n=("0"+s.getHours()).slice(-2)+":"+("0"+s.getMinutes()).slice(-2)+":"+("0"+s.getSeconds()).slice(-2),n=this.truncateMessage(n,{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}),`[${n}] ${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)}setErrorHandler(e){this.errorTargetHandler=e.bind(this)}setErrorHandlerForUserTarget(e){this.errorUserTargetHandler=e.bind(this)}isContext(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();if(t=t||{contextName:"DEFAULT"},Array.isArray(t))throw new Error(`AnaLogger: Cannot convert Array [${JSON.stringify(t)}] to context`);if(("string"==typeof t||t instanceof String)&&(t={contextName:t}),"object"!=typeof t)throw new Error(`AnaLogger: Cannot convert Unknown [${JSON.stringify(t)}] to context`);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}enableContexts(e){this.contexts.forEach(e=>{})}getActiveLogContexts(){}isTargetAllowed(e){return!e||!this.activeTarget||(e===this.targets.ALL||this.activeTarget===e)}processLog(t={}){try{if(this.options.hideLog)return;if(!this.isTargetAllowed(t.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var o=e.join(" | "),r=this.format({...t,message:o});this.isBrowser()?(t.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,this.realConsoleLog("%c"+r,"color: "+t.color)):(t.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,this.realConsoleLog(chalk.hex(t.color)(r))),this.errorTargetHandler(t,e)}catch(e){console.error("AnaLogger:",e.message)}}isExtendedOptionsPassed(e){return"object"==typeof e&&(e.hasOwnProperty("context")||e.hasOwnProperty("target"))}log(e,...t){var o;if(!this.isExtendedOptionsPassed(e))return o=this.generateDefaultContext(),void this.processLog.apply(this,[o,e,...t]);let r=e;if("object"==typeof e.context){const s=Object.assign({},e);delete s.context,r=Object.assign({},e.context,s)}r.hasOwnProperty("context")&&(r=Object.assign({},this.generateDefaultContext(),r),delete r.context),this.processLog.apply(this,[r,...t])}error(e,...t){if(!this.options.hideError){var o=this.generateErrorContext();if(this.isExtendedOptionsPassed(e))return e=Object.assign({},o,e),this.log(e,...t);var r=Array.prototype.slice.call(arguments);this.log(o,...r)}}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()}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){try{if("function"==typeof e)return e()!==t?void this.error("Asset failed"):void(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"));if(e!==t)return void this.error("Asset failed");this.options.showPassingTests&&this.log("SUCCESS: Assert passed")}catch(e){this.error("Unexpected error in assert")}}}const anaLogger=new AnaLogger;export{anaLogger};