2019-10-19 01:08:22 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "flog.h"
|
|
|
|
#include "parser.h"
|
|
|
|
|
2019-12-16 11:56:30 +00:00
|
|
|
static const wcstring VAR_fish_trace = L"fish_trace";
|
|
|
|
|
2019-10-19 01:08:22 +00:00
|
|
|
bool trace_enabled(const parser_t &parser) {
|
2020-03-13 20:59:10 +00:00
|
|
|
const auto &ld = parser.libdata();
|
2019-10-19 01:08:22 +00:00
|
|
|
if (ld.suppress_fish_trace) return false;
|
|
|
|
// TODO: this variable lookup is somewhat expensive, consider how to make this cheaper.
|
2019-12-16 11:56:30 +00:00
|
|
|
return !parser.vars().get(VAR_fish_trace).missing_or_empty();
|
2019-10-19 01:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trace an "argv": a list of arguments where the first is the command.
|
|
|
|
void trace_argv(const parser_t &parser, const wchar_t *command, const wcstring_list_t &argv) {
|
|
|
|
// Format into a string to prevent interleaving with flog in other threads.
|
|
|
|
// Add the + prefix.
|
2019-12-22 23:07:41 +00:00
|
|
|
wcstring trace_text(parser.blocks().size(), '+');
|
2019-10-19 01:08:22 +00:00
|
|
|
|
|
|
|
if (command && command[0]) {
|
|
|
|
trace_text.push_back(L' ');
|
|
|
|
trace_text.append(command);
|
|
|
|
}
|
|
|
|
for (const wcstring &arg : argv) {
|
|
|
|
trace_text.push_back(L' ');
|
|
|
|
trace_text.append(escape_string(arg, ESCAPE_ALL));
|
|
|
|
}
|
|
|
|
trace_text.push_back('\n');
|
|
|
|
log_extra_to_flog_file(trace_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace_if_enabled(const parser_t &parser, const wchar_t *command, const wcstring_list_t &argv) {
|
|
|
|
if (trace_enabled(parser)) trace_argv(parser, command, argv);
|
|
|
|
}
|