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

Split codetracer.nim into multiple files and remove orphaned functions #24

Merged
merged 3 commits into from
Mar 10, 2025
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
19 changes: 19 additions & 0 deletions docs/experimental-documentation/Advanced/EnvironmentVariables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CodeTracer exposes a number of environment variables that you can use to override some of its behaviours:

1. `CODETRACER_ELECTRON_ARGS` - adds arguments for launching Electron. Useful for debugging production builds
1. `CODETRACER_WRAPPER_PID` - overrides the process ID of the `ct` CodeTracer wrapper
1. `CODETRACER_CALLTRACE_MODE` - changes the calltrace mode
1. `CODETRACER_RECORD_CORE` - this does nothing as it is only related to the unreleased system backend

## CodeTracer Shell
These are generally not functional right now, since they affect CodeTracer Shell, which is currently not implemented:

1. `CODETRACER_SHELL_BASH_LOG_FILE` - overrides the log file
1. `CODETRACER_SHELL_ID` - overrides the shell ID
1. `CODETRACER_SESSION_ID` - overrides the CodeTracer Shell session ID so that the current commands affect a previous shell session
1. `CODETRACER_SHELL_REPORT_FILE` - overrides the report file of CodeTracer Shell
1. `CODETRACER_SHELL_USE_SCRIPT` - ?
1. `CODETRACER_SHELL_RECORDS_OUTPUT` - ?
1. `CODETRACER_SHELL_EXPORT` - ?
1. `CODETRACER_SHELL_CLEANUP_OUTPUT_FOLDER` - ?
1. `CODETRACER_SHELL_SOCKET` and `CODETRACER_SHELL_ADDRESS` - they override the socket location and address respectively
1 change: 1 addition & 0 deletions docs/experimental-documentation/_Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- Limitations
- [Troubleshooting](https://dev-docs.codetracer.com/Introduction/Troubleshooting)
- Advanced topics
- [Environment variables](https://dev-docs.codetracer.com/Advanced/EnvironmentVariables)
- Custom patches
- Dotfiles
- Misc
Expand Down
9 changes: 9 additions & 0 deletions src/ct/cli/help.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import
std/[ osproc ],
../../common/[paths]

proc displayHelp*: void =
# echo "help: TODO"
let process = startProcess(codetracerExe, args = @["--help"], options = {poParentStreams})
let code = waitForExit(process)
quit(code)
80 changes: 80 additions & 0 deletions src/ct/cli/interactive_replay.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import
std/[strutils, strformat, sequtils, algorithm, rdstdin],
../../common/[ trace_index, types, lang ],
../trace/[ run, storage_and_import ],
../utilities/[ env ],
../codetracerconf,
json_serialization

const
TRACE_CMD_COLUMN_WIDTH = 70
TRACE_WORKDIR_COLUMN_WIDTH = 40

func limitColumnLeft(text: string, width: int): string =
if text.len > width:
".." & text[text.len - (width - 2) .. ^1]
else:
text


func limitColumnRight(text: string, width: int): string =
if text.len > width:
text[0 .. (width - 2) - 1] & ".."
else:
text


func traceInText*(trace: Trace): string =
let displayCmd = limitColumnRight(trace.program & " " & trace.args.join(" "), TRACE_CMD_COLUMN_WIDTH)
let displayWorkdir = limitColumnLeft("ran in " & trace.workdir, TRACE_WORKDIR_COLUMN_WIDTH)
let idColumn = fmt"{trace.id}."
alignLeft(idColumn, 5) & " | " & alignLeft(displayCmd, TRACE_CMD_COLUMN_WIDTH) & " | " &
alignLeft(displayWorkdir, TRACE_WORKDIR_COLUMN_WIDTH) & " | " &
alignLeft(toName(trace.lang), 15) & " | " & alignLeft(trace.date, 15)


func tracesInText*(traces: seq[Trace]): string =
traces.reversed.mapIt(traceInText(it)).join("\n")


func tracesInJson*(traces: seq[Trace]): string =
Json.encode(traces)


proc interactiveReplayMenu*(command: StartupCommand) =
let recordCore = envLoadRecordCore()
# ordered by id
# returns the newest(biggest id) first
let traces = trace_index.all(test=false)
let limitedTraces = if traces.len > 10:
traces[0 ..< 10]
else:
traces

echo "Select a trace to replay, entering its id:"
echo ""

for trace in limitedTraces:
echo traceInText(trace)

if traces.len > 10:
echo "..(older traces not shown)"

echo ""

while true:
let raw = readLineFromStdin("replay: ")
try:
let traceId = raw.parseInt
let trace = trace_index.find(traceId, test=false)
if not trace.isNil:
if command != StartupCommand.upload:
discard runRecordedTrace(trace, test=false, recordCore=recordCore)
else:
uploadTrace(trace)
break
else:
echo fmt"trace with id {traceId} not found in local codetracer db, please try again"
except:
echo "error: ", getCurrentExceptionMsg()
echo "please try again"
49 changes: 49 additions & 0 deletions src/ct/cli/list.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import
../../common/trace_index,
logging,
interactive_replay

type
ListFormat = enum FormatText, FormatJson
ListTarget {.pure.} = enum Local, Remote


proc parseListFormat(arg: string): ListFormat =
if arg == "text":
FormatText
elif arg == "json":
FormatJson
else:
errorMessage "error: expected --format text/json"
quit(1)


proc parseListTarget(arg: string): ListTarget =
if arg == "local":
ListTarget.Local
elif arg == "remote":
ListTarget.Remote
else:
errorMessage "error: expected local or remote"
quit(1)


proc listLocalTraces(format: ListFormat) =
let traces = trace_index.all(test=false)
case format:
of FormatText:
echo tracesInText(traces)
of FormatJson:
echo tracesInJson(traces)


proc listCommand*(rawTarget: string, rawFormat: string) =
# list [local/remote (default local)] [--format text/json (default text)]
let target = parseListTarget(rawTarget)
let format = parseListFormat(rawFormat)
case target:
of ListTarget.Local:
listLocalTraces(format)
of ListTarget.Remote:
echo "error: unsupported currently!"
# listRemoteTraces(format)
7 changes: 7 additions & 0 deletions src/ct/cli/logging.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import std/[ strformat ]

template errorMessage*(message: string) =
echo message

proc notSupportedCommand*(commandName: string) =
echo fmt"{commandName} not supported with this backend"
Loading