2022-08-21 14:51:33 -07:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2019-10-18 18:08:22 -07:00
|
|
|
|
|
|
|
#include "trace.h"
|
|
|
|
|
2022-08-20 23:14:48 -07:00
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
|
2019-10-18 18:08:22 -07:00
|
|
|
#include "common.h"
|
|
|
|
#include "flog.h"
|
|
|
|
#include "parser.h"
|
|
|
|
|
2021-10-28 19:38:41 +02:00
|
|
|
static bool do_trace = false;
|
|
|
|
|
2021-11-08 11:36:01 -08:00
|
|
|
void trace_set_enabled(bool do_enable) { do_trace = do_enable; }
|
2019-12-16 12:56:30 +01:00
|
|
|
|
2019-10-18 18:08:22 -07:00
|
|
|
bool trace_enabled(const parser_t &parser) {
|
2020-03-13 13:59:10 -07:00
|
|
|
const auto &ld = parser.libdata();
|
2019-10-18 18:08:22 -07:00
|
|
|
if (ld.suppress_fish_trace) return false;
|
2021-10-28 19:38:41 +02:00
|
|
|
return do_trace;
|
2019-10-18 18:08:22 -07: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.
|
2020-12-09 20:51:07 +01:00
|
|
|
wcstring trace_text(parser.blocks().size() - 1, L'-');
|
|
|
|
trace_text.push_back(L'>');
|
2019-10-18 18:08:22 -07:00
|
|
|
|
|
|
|
if (command && command[0]) {
|
|
|
|
trace_text.push_back(L' ');
|
|
|
|
trace_text.append(command);
|
|
|
|
}
|
|
|
|
for (const wcstring &arg : argv) {
|
|
|
|
trace_text.push_back(L' ');
|
2022-07-25 16:25:04 +02:00
|
|
|
trace_text.append(escape_string(arg));
|
2019-10-18 18:08:22 -07:00
|
|
|
}
|
2020-12-09 20:51:07 +01:00
|
|
|
trace_text.push_back(L'\n');
|
2019-10-18 18:08:22 -07:00
|
|
|
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);
|
|
|
|
}
|