Skip to content

Instantly share code, notes, and snippets.

@JoshOrndorff
Created April 9, 2019 16:08
Show Gist options
  • Save JoshOrndorff/2ba119c6105bf7867542bec8eded1144 to your computer and use it in GitHub Desktop.
Save JoshOrndorff/2ba119c6105bf7867542bec8eded1144 to your computer and use it in GitHub Desktop.
Logging Patterns
new fancyLogFactory in {
contract fancyLogFactory (return) = {
new
fancyLog,
logCh
in {
// Give the new log instance back to the user. A new unforgeable
// name is created each time, so you won't need to choose a new
// public name in your client-side code each time. However you,
// will need to know the unforgeable id of fancyLog. It isn't
// top-level, so it can't be preview with the grpc service,so you'll
// have to print it directly
stdout!(*fancyLog) |
// Give the caller back a capability to log stuff
return!(fancyLog) |
// Initialize the log channel with an empty log
// The tuple is a boolean whether _every_ log message
// has reported success, and a list of messages logged so far
logCh!((true, [])) |
// To log new data, just call the contract.
// The data should be boolean indicating success, and a message.
contract fancyLog(@(success, message)) = {
// Grab the old log from the cell
for ( @(oldSuccess, messages) <- logCh) {
logCh!((oldSuccess and success, messages ++ [message]))
}
} |
// A way to reset the log to prevent tuplespace pollution.
// This method never _needs_ to be used.
contract @(fancyLog, "clear")(_) = {
for (_ <- logCh) {
logCh!((true, []))
}
}
}
}
}
new
stdout(`rho:io:stdout`),
logger // Potential Gotcha. Artur originally called this variable log.
// I've used that name for another purpose.
in {
new
rl(`rho:registry:lookup`),
RevVaultCh,
vaultCh
in {
rl!(`rho:id:1o93uitkrjfubh43jt19owanuezhntag5wh74c6ur5feuotpi73q8z`, *RevVaultCh) |
for (@(_, RevVault) <- RevVaultCh) {
logger!(("0.create_genesis_vault.rho")) |
match "%REV_ADDR" {
revAddress => {
logger!(("Using RevAddress:", revAddress, "to obtain a `deployer's-revAddress-bound` vault")) |
// so far, this call is not secured. This will be changed. Most likely, the method will disappear.
@RevVault!("findOrCreateGenesisVault", revAddress, 9000, *vaultCh ) |
// most Vault methods return an `Either[String, A] = (false, String) \/ (true, A)`
for (@(true, vault) <- vaultCh) {
logger!(("Genesis vault created")) |
new balanceCh in {
@vault!("balance", *balanceCh) |
for (@balance <- balanceCh) {
logger!(("Genesis vault balance is", balance))
}
}
}
}
}
} |
// Artur's version of the log contract.
contract logger(@data) = {
@"DEMO"!(data) | stdout!(data)
} |
// Ed and Joshy's version of the log contract.
// It uses the cell pattern where logCh is the cell.
new logCh in {
// logCh will accumulate log messages. It starts with an empty list
logCh!([]) |
contract logger(@data) = {
// Grab the old log off from the cell
for ( @oldLog <- logCh) {
// Write the updated version back
logCh!(oldLog ++ [data])
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment