Skip to content

Commit

Permalink
v2.0.0-beta.12:
Browse files Browse the repository at this point in the history
* Upgraded formatting to kurly v2 which supports static pipes
* Implemented outputs as Kurly formats
* Include kurly in ulog as standard
* Added colors
* Added alignments
* Lazy loading
  • Loading branch information
Download committed Feb 4, 2021
1 parent 0a82290 commit 1e072ba
Show file tree
Hide file tree
Showing 77 changed files with 1,510 additions and 738 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/dist
/node_modules
/ulog.min.js
/ulog.lazy.min.js
/.nyc_output
/test.min.js
/full.min.js
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Stijn de Witt
Copyright (c) 2021 Stijn de Witt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
584 changes: 332 additions & 252 deletions README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var ulog = module.exports = require('./core')
ulog.use([
require('./mods/config'),
])
79 changes: 64 additions & 15 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,74 @@ var path = require('path')

var gzipSize = require('gzip-size')
// be cool and use ulog to print the logging in the build of ulog :)
var log = require('./')('ulog:build')
var ulog = require('./')
var log = ulog('ulog:build')

var [ processName, script, command, ...args ] = process.argv
var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
var v = pkg.version

;(function(){
var data = fs.readFileSync(path.resolve(__dirname, pkg.unpkg), 'utf8')
var file = path.resolve(__dirname, pkg.unpkg)
log('Reading ' + file)
data = fs.readFileSync(file, 'utf8')
log('Estimating gzipped size')
var gzip = (gzipSize.sync(data) / 1024).toFixed(1)
log.info(`Built ${pkg.unpkg} (~${gzip} kB minified and gzipped)`)
data = fs.readFileSync(path.resolve(__dirname, 'full.min.js'), 'utf8')
var fullzip = (gzipSize.sync(data) / 1024).toFixed(1)
log.info(`Built full.min.js (~${fullzip} kB minified and gzipped)`)
var readme = fs.readFileSync('README.md', 'utf-8')
readme = readme.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?/g, `ulog@${v}`)
readme = readme.replace(/\<sub\>\<sup\>v\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?/g, `<sub\><sup\>v${v}`)
readme = readme.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?\) \(~\d\.\dkB/g, `ulog@${v}) (~${gzip}kB`)
readme = readme.replace(/just 2.7kB/g, `just ${gzip}kB`)
readme = readme.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?\/full\.min\.js\) \(~\d\.\dkB/g, `ulog@${v}/full.min.js) (~${fullzip}kB`)
fs.writeFileSync('README.md', readme, 'utf8')
log.info(`Updated README.md`)
log.info(`${pkg.unpkg} (~${(data.length / 1024).toFixed(1)} kB, ${gzip} kB gzipped)`)

file = path.resolve(__dirname, 'ulog.lazy.min.js')
log('Reading ' + file)
data = fs.readFileSync(file, 'utf8')
log('Estimating gzipped size')
var lazy = (gzipSize.sync(data) / 1024).toFixed(1)
log.info(`ulog.lazy.min.js (~${(data.length / 1024).toFixed(1)} kB, ${lazy} kB gzipped)`)

file = path.resolve(__dirname, 'full.min.js')
log('Reading ' + file)
data = fs.readFileSync(file, 'utf8')
log('Estimating gzipped size')
var full = (gzipSize.sync(data) / 1024).toFixed(1)
log.info(`full.min.js (~${(data.length / 1024).toFixed(1)} kB, ${full} kB gzipped)`)

file = path.resolve(__dirname, 'README.md')
log(`Reading ${file}`)
data = fs.readFileSync(file, 'utf-8')
log(`Updating version to ${v} and gzip size to ${gzip}...`)
data = data.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?/g, `ulog@${v}`)
data = data.replace(/\<sub\>\<sup\>v\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?/g, `<sub\><sup\>v${v}`)
data = data.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?\/ulog.min.js\) \(~\d\.\dkB/g, `ulog@${v}/ulog.min.js) (~${gzip}kB`)
data = data.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?\/ulog.lazy.min.js\) \(~\d\.\dkB/g, `ulog@${v}/ulog.lazy.min.js) (~${lazy}kB`)
data = data.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?\/full.min.js\) \(~\d\.\dkB/g, `ulog@${v}/full.min.js) (~${full}kB`)
log(`Writing ${file}`)
fs.writeFileSync(file, data, 'utf8')
log.info(`README.md (~${(data.length / 1024).toFixed(1)} kB)`)

ulog('a:one').debug('A debug message')
ulog('a:two').debug('A debug message')
ulog('a:three').debug('A debug message')
ulog('a:four').debug('A debug message')
ulog('a:five').debug('A debug message')
ulog('b:one').log('A log message')
ulog('b:two').log('A log message')
ulog('b:three').log('A log message')
ulog('b:four').log('A log message')
ulog('b:five').log('A log message')
ulog('c:one').info('An info message')
ulog('c:two').info('An info message')
ulog('c:three').info('An info message')
ulog('c:four').info('An info message')
ulog('c:five').info('An info message')
ulog('d:one').warn('A warn message')
ulog('d:two').warn('A warn message')
ulog('d:three').warn('A warn message')
ulog('d:four').warn('A warn message')
ulog('d:five').warn('A warn message')
ulog('e:one').error('An error message')
ulog('e:two').error('An error message')
ulog('e:three').error('An error message')
ulog('e:four').error('An error message')
ulog('e:five').error('An error message')
ulog('ulog').error('An error message')
setTimeout(function(){
log('A delayed message')
}, 1250)
})()
12 changes: 6 additions & 6 deletions core/grab.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var merge = require('./merge')

module.exports = function(ulog, name, result) {
ulog.mods.reduce(function(r,item) {
if (Array.isArray(r) && (name in item)) {
r.push(item[name])
ulog.mods.reduce(function(r,mod){
if (Array.isArray(r) && (name in mod)) {
r.push(mod[name])
} else {
for (var o in item[name]) {
r[o] = item[name][o]
}
merge(r, mod[name])
}
return r
}, result)
Expand Down
13 changes: 7 additions & 6 deletions core/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
var ulog = require('anylogger')
var grab = require('./grab')

// ulog.levels.none = 0
// ulog.levels.all = 9007199254740991 // Number.MAX_SAFE_INTEGER
// var ext = ulog.ext // save for later
var ext = ulog.ext

/**
* `ulog.ext(logger) => logger`
Expand All @@ -19,9 +17,12 @@ var grab = require('./grab')
*/
ulog.ext = function(logger) {
if (logger) {
// ext(logger) // create default methods
ext(logger)
grab(ulog, 'ext', []).map(function(ext){
ext.call(ulog, logger) // call hook on registered mods
ext.call(ulog, logger)
})
grab(ulog, 'after', []).map(function(ext){
ext.call(ulog, logger)
})
return logger
} else {
Expand Down Expand Up @@ -69,7 +70,7 @@ ulog.use = function(mod) {
if (Array.isArray(mod)) {
return mod.reduce(function(r,mod){return r + ulog.use(mod)}, 0)
}
// handle mod being a single mod
// // handle mod being a single mod
var result = ulog.mods.indexOf(mod) === -1 ? 1 : 0
if (result) {
if (mod.use) {
Expand Down
14 changes: 14 additions & 0 deletions core/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var merge = module.exports = function(result, obj) {
for (var o in obj) {
if ((typeof obj[o] == 'object') && (Object.getPrototypeOf(obj[o]) === Object.prototype)) {
if (! (o in result)) result[o] = {}
if ((typeof result[o] == 'object') && (Object.getPrototypeOf(obj[o]) === Object.prototype)) {
merge(result[o], obj[o])
} else {
result[o] = obj[o]
}
} else {
result[o] = obj[o]
}
}
}
118 changes: 118 additions & 0 deletions core/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"lvl name:22 date:yy/mm/dd perf"

"console file:./log.output url:https://deze-auto-kopen.nl component:( with nested components )"


function parse(str) {
var tag, result = []
if (str || (str === '')) {
while (tag = nextTag(str)) {
var before = str.substring(0, tag.index)
if (before) result.push(before)
result.push({
name: tag.name,
text: tag.text,
ast: parse(tag.text)
})
str = str.substring(tag.end + 1)
}
if (str) result.push(str)
}
return result
}

function nextTag(str) {
var match = str.match(/\{[_a-zA-Z][_a-zA-Z0-9]*([^_a-zA-Z0-9].*)?\}/)
var result
if (match) {
var name = match[1] ? match[0].substring(1, match[0].indexOf(match[1])) : match[0].substring(1, match[0].indexOf('}'))
result = { name: name, index: match.index, end: -1, text: '' }
// loop through the string, parsing it as we go through it
var esc = false
var open=1 // we already found one open brace
for (var i=match.index+name.length+1; i<str.length; i++) {
var token = str[i]
if (esc) {
token = (token == 'n') ? '\n' :
(token == 't') ? '\t' :
(token == '{') ||
(token == '}') ||
(token == '\\') ? token :
'\\' + token // unrecognized escape sequence is ignored
}
else {
if (token === '{') {
open++
}
if (token === '}') {
open--
if (!open) {
result.end = i
break
}
}
if (token === '\\') {
esc = true
continue
}
if (!result.text && (token.search(/\s/)===0)) {
continue
}
}
result.text += token
esc = false
}
}
return result
}

/**
* Compiles an abstract syntax tree into a function
*
* @param {Array<String|Object>} ast An abstract syntax tree created with `parse`
* @param {Object} tags An object of tags keyed by tag name
* @param {Function} parent Optionally, a compiled parent function for the ast
*
* @returns An array, possibly empty but never null or undefined.
*/
function compile(ast, tags, parent) {
if (process.env.NODE_ENV != 'production') {
log.debug('compile', ast, tags, parent)
if ((ast === undefined) || (ast === null)) throw new Error('parameter `ast` is required')
if (! Array.isArray(ast)) throw new Error('parameter `ast` must be an array')
if ((tags === undefined) || (tags === null)) throw new Error('parameter `tags` is required')
if (typeof tags != 'object') throw new Error('parameter `tags` must be an object')
}

// recursively compile the ast
var nodes = ast.map(function(n){
return typeof n == 'string'
? n :
compile(n.ast, tags,
tags[n.name] ? tags[n.name](n) :
tags['*'] ? tags['*'](n) :
undefined
)
})

// create the result function
var result = function(rec) {
// clone rec into res
var res = {}
for (k in rec) res[k] = rec[k]
// get the result children
res.children = nodes.reduce(function(r, n){
if (typeof n == 'function') n = n(rec)
r.push.apply(r, Array.isArray(n) ? n : [n])
return r
}, [])
// invoke parent if we have it
return parent ? parent(res) : res.children
}
if (process.env.NODE_ENV != 'production') {
log('compile', ast, tags, parent, '=>', '[Function]')
}
return result;
}


13 changes: 0 additions & 13 deletions example.js

This file was deleted.

9 changes: 0 additions & 9 deletions full.bundle.js

This file was deleted.

9 changes: 0 additions & 9 deletions full.js

This file was deleted.

18 changes: 18 additions & 0 deletions mods/align/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// var grab = require('../../core/grab')
// var palette = require('./utils').palette
// var levels = require('./utils').levels

var boolean = require('../props/boolean')

module.exports = {
use: [
require('../props'),
],

settings: {
align: {
config: 'log_align',
prop: boolean()
},
},
}
25 changes: 25 additions & 0 deletions mods/align/utils.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var ZWSP = '​'; // zero-width space
var firefox = require('../colors/utils').firefox

module.exports = {
// alignment depends on color format specifiers in the browser
hasAlign: require('../colors/utils').hasColor,

specifier: {
error: '%c%s%c%s',
warn: '%c%s%c%s',
info: '%c%s%c%s',
log: '%c%s%c%s',
debug: '%c%s%c%s',
trace: '%c%s%c%s',
},

args: {
error: ['padding-left:0px', ZWSP, 'padding-left:0px', ZWSP],
warn: ['padding-left:' + (firefox ? '12' : '0') + 'px', ZWSP, 'padding-left:0px', ZWSP],
info: ['padding-left:' + (firefox ? '12' : '10') + 'px', ZWSP, 'padding-left:0px', ZWSP],
log: ['padding-left:' + (firefox ? '12' : '10') + 'px', ZWSP, 'padding-left:0px', ZWSP],
debug: ['padding-left:' + (firefox ? '12' : '10') + 'px', ZWSP, 'padding-left:0px', ZWSP],
trace: ['padding-left:0px', ZWSP, 'padding-left:0px', ZWSP],
},
}
7 changes: 7 additions & 0 deletions mods/align/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var console = require('../channels/console')
module.exports = {
hasAlign: function(output){return output === console},
specifier: {
trace: '\n'
}
}
1 change: 1 addition & 0 deletions mods/channels/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (typeof console != 'undefined') && console
Loading

0 comments on commit 1e072ba

Please sign in to comment.