Skip to content

Commit

Permalink
uftrace: Improve logfile creation location
Browse files Browse the repository at this point in the history
Ensure that when using "--logfile=<name>", the logfile is created
inside the uftrace.data directory.

Additionally, when using both "--host" and "--logfile" options
simultaneously on the client, the logfile in the uftrace.data
directory is sent to the host.

Fixed: namhyung#831

Signed-off-by: Rihyeon Kim <[email protected]>
  • Loading branch information
dlgus8648 committed Sep 12, 2024
1 parent f3b92bb commit 05e2610
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
13 changes: 8 additions & 5 deletions cmds/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -1423,12 +1423,15 @@ static void send_event_file(int sock, const char *dirname)
send_trace_metadata(sock, dirname, "events.txt");
}

static void send_log_file(int sock, const char *logfile)
static void send_log_file(int sock, const char *dirname, const char *logfile)
{
if (access(logfile, F_OK) != 0)
char *logfile_path = NULL;
xasprintf(&logfile_path, "%s/%s", dirname, logfile);

if (access(logfile_path, F_OK) != 0)
return;

send_trace_metadata(sock, NULL, (char *)logfile);
send_trace_metadata(sock, dirname, (char *)logfile);
}

static void update_session_maps(struct uftrace_opts *opts)
Expand Down Expand Up @@ -2070,7 +2073,7 @@ static void write_symbol_files(struct writer_data *wd, struct uftrace_opts *opts
if (opts->event)
send_event_file(sock, opts->dirname);
if (opts->logfile)
send_log_file(sock, opts->logfile);
send_log_file(sock, opts->dirname, opts->logfile);

send_trace_end(sock);
close(sock);
Expand Down Expand Up @@ -2275,7 +2278,7 @@ int command_record(int argc, char *argv[], struct uftrace_opts *opts)
check_perf_event(opts);

if (!opts->nop) {
if (create_directory(opts->dirname) < 0)
if (!opts->logfile && (create_directory(opts->dirname) < 0))
return -1;

xasprintf(&channel, "%s/%s", opts->dirname, ".channel");
Expand Down
11 changes: 10 additions & 1 deletion uftrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,16 @@ int main(int argc, char *argv[])
debug = 1;

if (opts.logfile) {
logfp = fopen(opts.logfile, "a");
char *logfile_path = NULL;
if (create_directory(opts.dirname) < 0) {
ret = -1;
goto cleanup;
}

xasprintf(&logfile_path, "%s/%s", opts.dirname, opts.logfile);
logfp = fopen(logfile_path, "a");
free(logfile_path);

if (logfp == NULL) {
logfp = stderr;
pr_err("cannot open log file");
Expand Down

0 comments on commit 05e2610

Please sign in to comment.