Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support logging to file #558

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codex/codex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ proc bootstrapInteractions(
return (client, host, validator)

proc start*(s: CodexServer) {.async.} =
notice "Starting codex node"
trace "Starting codex node", config = $s.config

await s.repoStore.start()
s.maintenance.start()
Expand Down
32 changes: 31 additions & 1 deletion codex/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import pkg/metrics
import pkg/metrics/chronos_httpserver
import pkg/stew/shims/net as stewnet
import pkg/stew/shims/parseutils
import pkg/stew/byteutils
import pkg/libp2p
import pkg/ethers
import pkg/questionable
import pkg/questionable/results

import ./discovery
import ./stores
Expand Down Expand Up @@ -260,6 +263,13 @@ type
hidden
.}: int

logFile* {.
desc: "Logs to file"
defaultValue: string.none
name: "log-file"
hidden
.}: Option[string]

of initNode:
discard

Expand Down Expand Up @@ -445,9 +455,10 @@ proc updateLogLevel*(logLevel: string) {.upraises: [ValueError].} =
warn "Unrecognized logging topic", topic = topicName

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"
else:
var logFile: ?IoHandle
proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard
proc writeAndFlush(f: File, msg: LogOutputStr) =
try:
Expand All @@ -462,6 +473,25 @@ proc setupLogging*(conf: CodexConf) =
proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) =
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

let writer =
Expand Down
2 changes: 1 addition & 1 deletion config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ switch("define", "libp2p_pki_schemes=secp256k1")
#TODO this infects everything in this folder, ideally it would only
# apply to codex.nim, but since codex.nims is used for other purpose
# 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)
when system.fileExists("nimble.paths"):
Expand Down
5 changes: 3 additions & 2 deletions tests/contracts/testContracts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ ethersuite "Marketplace contracts":
switchAccount(host)
await waitUntilProofRequired(slotId)
let missingPeriod = periodicity.periodOf(await provider.currentTime())
await provider.advanceTime(periodicity.seconds)
let endOfPeriod = periodicity.periodEnd(missingPeriod)
await provider.advanceTimeTo(endOfPeriod + 1)
switchAccount(client)
await marketplace.markProofAsMissing(slotId, missingPeriod)

Expand All @@ -77,7 +78,7 @@ ethersuite "Marketplace contracts":
let address = await host.getAddress()
await startContract()
let requestEnd = await marketplace.requestEnd(request.id)
await provider.advanceTimeTo(requestEnd.u256)
await provider.advanceTimeTo(requestEnd.u256 + 1)
let startBalance = await token.balanceOf(address)
await marketplace.freeSlot(slotId)
let endBalance = await token.balanceOf(address)
Expand Down
6 changes: 3 additions & 3 deletions tests/contracts/testMarket.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ethersuite "On-Chain Market":

test "supports withdrawing of funds":
await market.requestStorage(request)
await provider.advanceTimeTo(request.expiry)
await provider.advanceTimeTo(request.expiry + 1)
await market.withdrawFunds(request.id)

test "supports request subscriptions":
Expand Down Expand Up @@ -213,7 +213,7 @@ ethersuite "On-Chain Market":
receivedIds.add(id)
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)

await provider.advanceTimeTo(request.expiry)
await provider.advanceTimeTo(request.expiry + 1)
await market.withdrawFunds(request.id)
check receivedIds == @[request.id]
await subscription.unsubscribe()
Expand Down Expand Up @@ -252,7 +252,7 @@ ethersuite "On-Chain Market":
receivedIds.add(requestId)

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)
check receivedIds.len == 0
await market.withdrawFunds(request.id)
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-ethers