Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

status-logger

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

status-logger - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

example.js

82

index.js

@@ -1,72 +0,26 @@

var singleLineLog = require('single-line-log')
var differ = require('ansi-diff-stream')
module.exports = function (opts) {
module.exports = function (messageGroups, opts) {
if (!messageGroups || !Array.isArray(messageGroups)) return new Error('Message list required')
if (!Array.isArray(messageGroups[0])) messageGroups = [messageGroups]
if (!opts) opts = {}
var logger = getLogger(opts)
var messageQueue = []
var statusLines = []
var statusLastLine = ''
var diff = differ()
if (!opts.debug && !opts.quiet) diff.pipe(process.stdout)
return {
message: message,
status: status,
print: print
}
function message (msg) {
messageQueue.push(msg)
}
function status (msg, lineNum) {
if (typeof lineNum === 'undefined') statusLines = [msg]
else if (lineNum === -1) statusLastLine = msg
else if (lineNum < statusLines.length) statusLines[lineNum] = msg
else statusLines.push(msg)
}
function print () {
logger.stdout() // Clear old stdout before printing messages
while (true) {
if (messageQueue.length === 0) break
logger.log(messageQueue[0])
messageQueue.shift()
groups: messageGroups,
print: function () {
var msg = ''
var prevGroup = false
messageGroups.forEach(function (messages) {
if (!messages.length) return
if (prevGroup) msg += '\n'
msg += messages.join('\n')
prevGroup = true
})
if (opts.debug) console.log(msg)
else if (!opts.quiet) diff.write(msg)
}
if (statusLines.length || statusLastLine.length) {
var msg = statusLines.join('\n')
msg += '\n' + statusLastLine
logger.stdout(msg)
}
}
}
function getLogger (opts) {
if (opts.quiet) {
return {
stderr: logQuiet,
stdout: logQuiet,
log: logQuiet,
error: logQuiet
}
}
if (opts.debug) {
return {
stderr: console.error.bind(console),
stdout: console.log.bind(console),
log: console.error.bind(console),
error: console.log.bind(console)
}
}
return {
stderr: singleLineLog.stderr,
stdout: singleLineLog.stdout,
log: console.log.bind(console),
error: console.error.bind(console)
}
}
function logQuiet () {
// do nothing
}
{
"name": "status-logger",
"version": "2.0.1",
"version": "3.0.0",
"description": "print updating progress to console and stdout",

@@ -10,5 +10,5 @@ "main": "index.js",

"author": "Joe Hand <joe@joeahand.com> (http://joeahand.com/)",
"license": "ISC",
"license": "MIT",
"dependencies": {
"single-line-log": "^1.1.1"
"ansi-diff-stream": "^1.1.1"
},

@@ -15,0 +15,0 @@ "devDependencies": {},

# Status Logger
Log a progress status while also queuing messages.
Send groups of messages, update the messages, and print them to stdout via [ansi-diff-stream](https://github.com/mafintosh/ansi-diff-stream).
* Messages are added to queue and printed above status lines.
* Status can be multi-line, specify line with each message.
Each message group is an array of lines to print. Lines and groups can be updated and printed. Groups are printed in order with a line in between them.
## Example
## Usage
```js
var outputLines = []
var log = statusLogger([outputLines])
setInterval(function () {
log.print()
}, 100)
```javascript
var statusLogger = require('status-logger')
var sec = 0
outputLines.push('I am starting.')
setInterval(function () {
sec++
outputLines[1] = 'I am progressing & overwriting this line. Seconds = ' + sec
}, 1000)
```
var log = statusLogger(opts)
See `example.js` for full example with multiple message groups.
log.message('Send a regular console log message') // queue message for next interval
## Installation
log.status('Print to stdout line 0', 0) // print to stdout line 0 in next interval
log.status('Print to stdout line 1', 1)
```
npm install status-logger
```
var percentage = 0
log.status('Percentage' + percentage, 2)
## API
// update percentage & will print updated info with interval
### `var log = statusLogger(messageGroups, opts)`
log.status('Last line', -1) // print to last status line
log.status('Overwrite line 1', 1)
`messageGroups` is an array groups with of lines to print. They will be printed in order.
setInterval(function () {
log.print() // print on interval
}, 500)
```
### Options
### `log.print()`: print status + messages
* `quiet`: do not print anything
* `debug`: print everything to console.log or console.error
### `log.message(msg)`: log a message
### `log.print()`: print messages from all groups
### `log.status(msg, lineNum)`: print to stdout line
### `log.groups`: array of message groups
## Options
* `quiet`: do not print anything
* `debug`: print everything to console.log or console.error
## License
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc