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

Add option to append timestamp to generated trace filename #914

Merged
merged 2 commits into from
May 2, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions cli/cli_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ traceProgram(trace::API api,
const char *output,
int verbose,
bool debug,
bool mhook)
bool mhook,
bool timestamp)
{
const char *wrapperFilename;
std::vector<const char *> args;
Expand Down Expand Up @@ -163,6 +164,9 @@ traceProgram(trace::API api,
if (mhook) {
args.push_back("-m");
}
if (timestamp) {
args.push_back("-t");
}
for (int i = 1; i < verbose; ++i) {
args.push_back("-v");
}
Expand Down Expand Up @@ -265,6 +269,8 @@ traceProgram(trace::API api,

if (output) {
os::setEnvironment("TRACE_FILE", output);
} else if (timestamp) {
os::setEnvironment("TRACE_TIMESTAMP", "1");
}

for (char * const * arg = argv; *arg; ++arg) {
Expand Down Expand Up @@ -342,6 +348,8 @@ usage(void)
" default is `gl`\n"
" -o, --output=TRACE specify output trace file;\n"
" default is `PROGRAM.trace`\n"
" -t, --timestamp append timestamp to output trace filename;\n"
" ignored if --output argument is specified\n"
#ifdef TRACE_VARIABLE
" -d, --debug run inside debugger (gdb/lldb)\n"
#endif
Expand All @@ -362,6 +370,7 @@ longOptions[] = {
{ "output", required_argument, 0, 'o' },
{ "debug", no_argument, 0, 'd' },
{ "mhook", no_argument, 0, 'm' },
{ "timestamp", no_argument, 0, 't' },
{ 0, 0, 0, 0 }
};

Expand All @@ -373,6 +382,7 @@ command(int argc, char *argv[])
const char *output = NULL;
bool debug = false;
bool mhook = false;
bool timestamp = false;

int opt;
while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
Expand Down Expand Up @@ -420,6 +430,9 @@ command(int argc, char *argv[])
case 'm':
mhook = true;
break;
case 't':
timestamp = true;
break;
default:
std::cerr << "error: unexpected option `" << (char)opt << "`\n";
usage();
Expand All @@ -434,7 +447,7 @@ command(int argc, char *argv[])
}

assert(argv[argc] == 0);
return traceProgram(api, argv + optind, output, verbose, debug, mhook);
return traceProgram(api, argv + optind, output, verbose, debug, mhook, timestamp);
}

const Command trace_command = {
Expand Down
2 changes: 1 addition & 1 deletion lib/trace/trace_option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ boolOption(const char *option, bool default_) {
strcmp(option, "false") == 0) {
return false;
}
if (strcmp(option, "0") == 0 ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

if (strcmp(option, "1") == 0 ||
strcmp(option, "yes") == 0 ||
strcmp(option, "true") == 0) {
return true;
Expand Down
12 changes: 10 additions & 2 deletions lib/trace/trace_writer_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "os_thread.hpp"
#include "os_string.hpp"
#include "os_version.hpp"
#include "trace_option.hpp"
#include "trace_ostream.hpp"
#include "trace_writer_local.hpp"
#include "trace_format.hpp"
Expand Down Expand Up @@ -111,6 +112,7 @@ LocalWriter::open(void) {
lpFileName = getenv("TRACE_FILE");
if (!lpFileName) {
static unsigned dwCounter = 0;
char suffix[17] = "";

os::String process = os::getProcessName();
#ifdef _WIN32
Expand All @@ -130,13 +132,19 @@ LocalWriter::open(void) {
#endif
prefix.join(process);

const char *lpTimestamp = getenv("TRACE_TIMESTAMP");
if (lpTimestamp && boolOption(lpTimestamp)) {
std::time_t time = std::time({});
strftime(suffix, sizeof(suffix), ".%Y%m%dT%H%M%S", std::localtime(&time));
}

for (;;) {
FILE *file;

if (dwCounter)
szFileName = os::String::format("%s.%u.trace", prefix.str(), dwCounter);
szFileName = os::String::format("%s.%s%u.trace", prefix.str(), suffix, dwCounter);
else
szFileName = os::String::format("%s.trace", prefix.str());
szFileName = os::String::format("%s%s.trace", prefix.str(), suffix);

lpFileName = szFileName;
file = fopen(lpFileName, "rb");
Expand Down