Support logging to file (#558)

* Support logging to file

* Log the entire config and fix build error

* Downgrade log level for "starting codex node" config output

* bump ethers to prevent nonce gaps

* fix tests
This commit is contained in:
Eric 2023-11-09 16:35:55 +11:00 committed by GitHub
parent c0bec2f899
commit 7d4ea878d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 9 deletions

View File

@ -129,7 +129,7 @@ proc bootstrapInteractions(
return (client, host, validator) return (client, host, validator)
proc start*(s: CodexServer) {.async.} = proc start*(s: CodexServer) {.async.} =
notice "Starting codex node" trace "Starting codex node", config = $s.config
await s.repoStore.start() await s.repoStore.start()
s.maintenance.start() s.maintenance.start()

View File

@ -28,8 +28,11 @@ import pkg/metrics
import pkg/metrics/chronos_httpserver import pkg/metrics/chronos_httpserver
import pkg/stew/shims/net as stewnet import pkg/stew/shims/net as stewnet
import pkg/stew/shims/parseutils import pkg/stew/shims/parseutils
import pkg/stew/byteutils
import pkg/libp2p import pkg/libp2p
import pkg/ethers import pkg/ethers
import pkg/questionable
import pkg/questionable/results
import ./discovery import ./discovery
import ./stores import ./stores
@ -260,6 +263,13 @@ type
hidden hidden
.}: int .}: int
logFile* {.
desc: "Logs to file"
defaultValue: string.none
name: "log-file"
hidden
.}: Option[string]
of initNode: of initNode:
discard discard
@ -445,9 +455,10 @@ proc updateLogLevel*(logLevel: string) {.upraises: [ValueError].} =
warn "Unrecognized logging topic", topic = topicName warn "Unrecognized logging topic", topic = topicName
proc setupLogging*(conf: CodexConf) = proc setupLogging*(conf: CodexConf) =
when defaultChroniclesStream.outputs.type.arity != 2: when defaultChroniclesStream.outputs.type.arity != 3:
warn "Logging configuration options not enabled in the current build" warn "Logging configuration options not enabled in the current build"
else: else:
var logFile: ?IoHandle
proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard
proc writeAndFlush(f: File, msg: LogOutputStr) = proc writeAndFlush(f: File, msg: LogOutputStr) =
try: try:
@ -462,6 +473,25 @@ proc setupLogging*(conf: CodexConf) =
proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) = proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) =
writeAndFlush(stdout, stripAnsi(msg)) writeAndFlush(stdout, stripAnsi(msg))
proc fileFlush(logLevel: LogLevel, msg: LogOutputStr) =
if file =? logFile:
if error =? file.writeFile(stripAnsi(msg).toBytes).errorOption:
error "failed to write to log file", errorCode = $error
defaultChroniclesStream.outputs[2].writer = noOutput
if logFilePath =? conf.logFile and logFilePath.len > 0:
let logFileHandle = openFile(
logFilePath,
{OpenFlags.Write, OpenFlags.Create, OpenFlags.Truncate}
)
if logFileHandle.isErr:
error "failed to open log file",
path = logFilePath,
errorCode = $logFileHandle.error
else:
logFile = logFileHandle.option
defaultChroniclesStream.outputs[2].writer = fileFlush
defaultChroniclesStream.outputs[1].writer = noOutput defaultChroniclesStream.outputs[1].writer = noOutput
let writer = let writer =

View File

@ -111,7 +111,7 @@ switch("define", "libp2p_pki_schemes=secp256k1")
#TODO this infects everything in this folder, ideally it would only #TODO this infects everything in this folder, ideally it would only
# apply to codex.nim, but since codex.nims is used for other purpose # apply to codex.nim, but since codex.nims is used for other purpose
# we can't use it. And codex.cfg doesn't work # we can't use it. And codex.cfg doesn't work
switch("define", "chronicles_sinks=textlines[dynamic],json[dynamic]") switch("define", "chronicles_sinks=textlines[dynamic],json[dynamic],textlines[dynamic]")
# begin Nimble config (version 1) # begin Nimble config (version 1)
when system.fileExists("nimble.paths"): when system.fileExists("nimble.paths"):

View File

@ -68,7 +68,8 @@ ethersuite "Marketplace contracts":
switchAccount(host) switchAccount(host)
await waitUntilProofRequired(slotId) await waitUntilProofRequired(slotId)
let missingPeriod = periodicity.periodOf(await provider.currentTime()) let missingPeriod = periodicity.periodOf(await provider.currentTime())
await provider.advanceTime(periodicity.seconds) let endOfPeriod = periodicity.periodEnd(missingPeriod)
await provider.advanceTimeTo(endOfPeriod + 1)
switchAccount(client) switchAccount(client)
await marketplace.markProofAsMissing(slotId, missingPeriod) await marketplace.markProofAsMissing(slotId, missingPeriod)
@ -77,7 +78,7 @@ ethersuite "Marketplace contracts":
let address = await host.getAddress() let address = await host.getAddress()
await startContract() await startContract()
let requestEnd = await marketplace.requestEnd(request.id) let requestEnd = await marketplace.requestEnd(request.id)
await provider.advanceTimeTo(requestEnd.u256) await provider.advanceTimeTo(requestEnd.u256 + 1)
let startBalance = await token.balanceOf(address) let startBalance = await token.balanceOf(address)
await marketplace.freeSlot(slotId) await marketplace.freeSlot(slotId)
let endBalance = await token.balanceOf(address) let endBalance = await token.balanceOf(address)

View File

@ -70,7 +70,7 @@ ethersuite "On-Chain Market":
test "supports withdrawing of funds": test "supports withdrawing of funds":
await market.requestStorage(request) await market.requestStorage(request)
await provider.advanceTimeTo(request.expiry) await provider.advanceTimeTo(request.expiry + 1)
await market.withdrawFunds(request.id) await market.withdrawFunds(request.id)
test "supports request subscriptions": test "supports request subscriptions":
@ -213,7 +213,7 @@ ethersuite "On-Chain Market":
receivedIds.add(id) receivedIds.add(id)
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled) let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)
await provider.advanceTimeTo(request.expiry) await provider.advanceTimeTo(request.expiry + 1)
await market.withdrawFunds(request.id) await market.withdrawFunds(request.id)
check receivedIds == @[request.id] check receivedIds == @[request.id]
await subscription.unsubscribe() await subscription.unsubscribe()
@ -252,7 +252,7 @@ ethersuite "On-Chain Market":
receivedIds.add(requestId) receivedIds.add(requestId)
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled) let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)
await provider.advanceTimeTo(request.expiry) # shares expiry with otherRequest await provider.advanceTimeTo(request.expiry + 1) # shares expiry with otherRequest
await market.withdrawFunds(otherRequest.id) await market.withdrawFunds(otherRequest.id)
check receivedIds.len == 0 check receivedIds.len == 0
await market.withdrawFunds(request.id) await market.withdrawFunds(request.id)

2
vendor/nim-ethers vendored

@ -1 +1 @@
Subproject commit 620b402a7d33385ae8e2cb5677e9f6dce9724acb Subproject commit 2428b756d6fdee15017e76ec6077e890ebfd7bf6