implemented logging with the debug logging utility (read the updated docs)
This commit is contained in:
parent
e1a160b894
commit
d5cd9d94d5
21
README.md
21
README.md
@ -225,6 +225,27 @@ The promise returns an instance of Y. We denote it with a lower case `y`.
|
|||||||
* y.db.userId :: String
|
* y.db.userId :: String
|
||||||
* The used user id for this client. **Never overwrite this**
|
* The used user id for this client. **Never overwrite this**
|
||||||
|
|
||||||
|
### Logging
|
||||||
|
Yjs uses [debug](https://github.com/visionmedia/debug) for logging. The flag
|
||||||
|
`y*` enables logging for all y-* components. You can selectively remove
|
||||||
|
components you are not interested in: E.g. The flag `y*,-y:connector-message`
|
||||||
|
will not log the long `y:connector-message` messages.
|
||||||
|
|
||||||
|
##### Enable logging in Node.js
|
||||||
|
```sh
|
||||||
|
DEBUG=y* node app.js
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove the colors in order to log to a file:
|
||||||
|
```sh
|
||||||
|
DEBUG_COLORS=0 DEBUG=y* node app.js > log
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Enable logging in the browser
|
||||||
|
```js
|
||||||
|
localStorage.debug = 'y*'
|
||||||
|
```
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
### 12.0.0
|
### 12.0.0
|
||||||
|
@ -43,6 +43,8 @@ module.exports = function (Y/* :any */) {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error("Role must be either 'master' or 'slave'!")
|
throw new Error("Role must be either 'master' or 'slave'!")
|
||||||
}
|
}
|
||||||
|
this.log = Y.debug('y:connector')
|
||||||
|
this.logMessage = Y.debug('y:connector-message')
|
||||||
this.y.db.forwardAppliedOperations = opts.forwardAppliedOperations || false
|
this.y.db.forwardAppliedOperations = opts.forwardAppliedOperations || false
|
||||||
this.role = opts.role
|
this.role = opts.role
|
||||||
this.connections = {}
|
this.connections = {}
|
||||||
@ -73,8 +75,10 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
reconnect () {
|
reconnect () {
|
||||||
|
this.log('reconnecting..')
|
||||||
}
|
}
|
||||||
disconnect () {
|
disconnect () {
|
||||||
|
this.log('discronnecting..')
|
||||||
this.connections = {}
|
this.connections = {}
|
||||||
this.isSynced = false
|
this.isSynced = false
|
||||||
this.currentSyncTarget = null
|
this.currentSyncTarget = null
|
||||||
@ -84,7 +88,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
return this.y.db.stopGarbageCollector()
|
return this.y.db.stopGarbageCollector()
|
||||||
}
|
}
|
||||||
repair () {
|
repair () {
|
||||||
console.info('Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues')
|
this.log('Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues')
|
||||||
for (var name in this.connections) {
|
for (var name in this.connections) {
|
||||||
this.connections[name].isSynced = false
|
this.connections[name].isSynced = false
|
||||||
}
|
}
|
||||||
@ -95,6 +99,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
setUserId (userId) {
|
setUserId (userId) {
|
||||||
if (this.userId == null) {
|
if (this.userId == null) {
|
||||||
|
this.log('Set userId to "%s"', userId)
|
||||||
this.userId = userId
|
this.userId = userId
|
||||||
return this.y.db.setUserId(userId)
|
return this.y.db.setUserId(userId)
|
||||||
} else {
|
} else {
|
||||||
@ -109,6 +114,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
userLeft (user) {
|
userLeft (user) {
|
||||||
if (this.connections[user] != null) {
|
if (this.connections[user] != null) {
|
||||||
|
this.log('User left: %s', user)
|
||||||
delete this.connections[user]
|
delete this.connections[user]
|
||||||
if (user === this.currentSyncTarget) {
|
if (user === this.currentSyncTarget) {
|
||||||
this.currentSyncTarget = null
|
this.currentSyncTarget = null
|
||||||
@ -132,6 +138,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
if (this.connections[user] != null) {
|
if (this.connections[user] != null) {
|
||||||
throw new Error('This user already joined!')
|
throw new Error('This user already joined!')
|
||||||
}
|
}
|
||||||
|
this.log('User joined: %s', user)
|
||||||
this.connections[user] = {
|
this.connections[user] = {
|
||||||
isSynced: false,
|
isSynced: false,
|
||||||
role: role
|
role: role
|
||||||
@ -200,9 +207,12 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
send (uid, message) {
|
send (uid, message) {
|
||||||
if (this.debug) {
|
this.log('Send \'%s\' to %s', message.type, uid)
|
||||||
console.log(`send ${this.userId} -> ${uid}: ${message.type}`, message) // eslint-disable-line
|
this.logMessage('Message: %j', message)
|
||||||
}
|
}
|
||||||
|
broadcast (message) {
|
||||||
|
this.log('Broadcast \'%s\'', message.type)
|
||||||
|
this.logMessage('Message: %j', message)
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Buffer operations, and broadcast them when ready.
|
Buffer operations, and broadcast them when ready.
|
||||||
@ -239,11 +249,10 @@ module.exports = function (Y/* :any */) {
|
|||||||
if (sender === this.userId) {
|
if (sender === this.userId) {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
if (this.debug) {
|
this.log('Receive \'%s\' from %s', message.type, sender)
|
||||||
console.log(`receive ${sender} -> ${this.userId}: ${message.type}`, JSON.parse(JSON.stringify(message))) // eslint-disable-line
|
this.logMessage('Message: %j', message)
|
||||||
}
|
|
||||||
if (message.protocolVersion != null && message.protocolVersion !== this.protocolVersion) {
|
if (message.protocolVersion != null && message.protocolVersion !== this.protocolVersion) {
|
||||||
console.error(
|
this.log(
|
||||||
`You tried to sync with a yjs instance that has a different protocol version
|
`You tried to sync with a yjs instance that has a different protocol version
|
||||||
(You: ${this.protocolVersion}, Client: ${message.protocolVersion}).
|
(You: ${this.protocolVersion}, Client: ${message.protocolVersion}).
|
||||||
The sync was stopped. You need to upgrade your dependencies (especially Yjs & the Connector)!
|
The sync was stopped. You need to upgrade your dependencies (especially Yjs & the Connector)!
|
||||||
|
3
src/y.js
3
src/y.js
@ -8,6 +8,9 @@ require('./Struct.js')(Y)
|
|||||||
require('./Utils.js')(Y)
|
require('./Utils.js')(Y)
|
||||||
require('./Connectors/Test.js')(Y)
|
require('./Connectors/Test.js')(Y)
|
||||||
|
|
||||||
|
Y.debug = require('debug')
|
||||||
|
// Y.debug.log = console.log.bind(console)
|
||||||
|
|
||||||
var requiringModules = {}
|
var requiringModules = {}
|
||||||
|
|
||||||
module.exports = Y
|
module.exports = Y
|
||||||
|
Loading…
x
Reference in New Issue
Block a user