From 6e9364ab50082fba74fd0a0a733993d49075756e Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 14 Dec 2020 19:25:55 +0100 Subject: [PATCH] fish_indent: Change --debug-level to --debug with flog categories The "debug-level" flag makes little sense since we have no more debug *levels* left. --- doc_src/cmds/fish_indent.rst | 4 +++- share/completions/fish_indent.fish | 3 ++- src/fish_indent.cpp | 31 +++++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/doc_src/cmds/fish_indent.rst b/doc_src/cmds/fish_indent.rst index 7572d6358..fa5a23eb8 100644 --- a/doc_src/cmds/fish_indent.rst +++ b/doc_src/cmds/fish_indent.rst @@ -30,6 +30,8 @@ The following options are available: - ``--html`` outputs HTML, which supports syntax highlighting if the appropriate CSS is defined. The CSS class names are the same as the variable names, such as ``fish_color_command``. -- ``-d`` or ``--debug-level=DEBUG_LEVEL`` enables debug output and specifies a verbosity level. Defaults to 0. +- ``-d`` or ``--debug=DEBUG_CATEGORIES`` enable debug output and specify a pattern for matching debug categories. See :ref:`Debugging ` in :ref:`fish(1) ` for details. + +- ``-o`` or ``--debug-output=DEBUG_FILE`` specify a file path to receive the debug output, including categories and ``fish_trace``. The default is stderr. - ``--dump-parse-tree`` dumps information about the parsed statements to stderr. This is likely to be of interest only to people working on the fish source code. diff --git a/share/completions/fish_indent.fish b/share/completions/fish_indent.fish index 17deb50e9..50cbf0532 100644 --- a/share/completions/fish_indent.fish +++ b/share/completions/fish_indent.fish @@ -4,6 +4,7 @@ complete -c fish_indent -s i -l no-indent -d 'Do not indent output, only reforma complete -c fish_indent -l ansi -d 'Colorize the output using ANSI escape sequences' complete -c fish_indent -l html -d 'Output in HTML format' complete -c fish_indent -s w -l write -d 'Write to file' -complete -c fish_indent -s d -l debug-level -x -d 'Enable debug at specified verbosity level' +complete -c fish_indent -s d -l debug -x -d 'Enable debug at specified verbosity level' +complete -c fish_indent -s o -l debug-output -d "Where to direct debug output to" -rF complete -c fish_indent -s D -l debug-stack-frames -x -d 'Specify how many stack frames to display in debug messages' complete -c fish_indent -l dump-parse-tree -d 'Dump information about parsed statements to stderr' diff --git a/src/fish_indent.cpp b/src/fish_indent.cpp index faa4729c6..cdbca5db2 100644 --- a/src/fish_indent.cpp +++ b/src/fish_indent.cpp @@ -858,9 +858,12 @@ int main(int argc, char *argv[]) { } output_type = output_type_plain_text; const char *output_location = ""; bool do_indent = true; + // File path for debug output. + std::string debug_output; const char *short_opts = "+d:hvwicD:"; - const struct option long_opts[] = {{"debug-level", required_argument, nullptr, 'd'}, + const struct option long_opts[] = {{"debug", required_argument, nullptr, 'd'}, + {"debug-output", required_argument, nullptr, 'o'}, {"debug-stack-frames", required_argument, nullptr, 'D'}, {"dump-parse-tree", no_argument, nullptr, 'P'}, {"no-indent", no_argument, nullptr, 'i'}, @@ -922,8 +925,12 @@ int main(int argc, char *argv[]) { if (tmp >= 0 && tmp <= 10 && !*end && !errno) { debug_level = static_cast(tmp); } else { - std::fwprintf(stderr, _(L"Invalid value '%s' for debug-level flag"), optarg); - exit(1); + activate_flog_categories_by_pattern(str2wcstring(optarg)); + } + for (auto cat : get_flog_categories()) { + if (cat->enabled) { + printf("Debug enabled for category: %ls\n", cat->name); + } } break; } @@ -932,6 +939,10 @@ int main(int argc, char *argv[]) { // Either remove it or make it work with FLOG. break; } + case 'o': { + debug_output = optarg; + break; + } default: { // We assume getopt_long() has already emitted a diagnostic msg. exit(1); @@ -942,6 +953,20 @@ int main(int argc, char *argv[]) { argc -= optind; argv += optind; + // Direct any debug output right away. + FILE *debug_output_file = nullptr; + if (!debug_output.empty()) { + debug_output_file = fopen(debug_output.c_str(), "w"); + if (!debug_output_file) { + fprintf(stderr, "Could not open file %s\n", debug_output.c_str()); + perror("fopen"); + exit(-1); + } + set_cloexec(fileno(debug_output_file)); + setlinebuf(debug_output_file); + set_flog_output_file(debug_output_file); + } + int retval = 0; wcstring src;