blob: 1d9feceb5a09f1cada63a8688d1e5d860cf1999e [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SRC_TRACE_PROCESSOR_SHELL_RESULT_FORMATTER_H_
#define SRC_TRACE_PROCESSOR_SHELL_RESULT_FORMATTER_H_
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "perfetto/trace_processor/basic_types.h"
namespace perfetto::trace_processor {
enum class ResultFormat {
// Quoted CSV: the historical, machine-parseable format.
kCsv,
// Compact markdown table without alignment padding.
kMarkdown,
// Aligned plain-text table for terminals, printed a page at a time.
kTable,
};
struct ResultFormatOptions {
ResultFormat format = ResultFormat::kCsv;
// 0 means "no limit" for all the options below.
// A result set with more rows or bytes than this is cut: markdown prints its
// first and last rows with a marker row in between, CSV its first rows.
uint64_t max_rows = 0;
uint64_t max_bytes = 0;
// Cells with more characters than this are cut with a visible marker.
uint64_t max_cell_chars = 0;
// Once a result set is over the limits above, at most this many further
// rows are read before the statement is abandoned.
uint64_t max_rows_past_limit = 0;
// kTable only: rows printed before asking whether to print more.
uint64_t page_rows = 0;
};
// Prints a single result set. Rows are passed to AddRow() while the statement
// is iterated and the result is printed by Finish(), so that time spent
// printing is not attributed to query execution.
class ResultFormatter {
public:
virtual ~ResultFormatter();
// Adds a row with one cell per column. Returns false once enough rows have
// been seen that the caller should stop iterating the statement.
virtual bool AddRow(const SqlValue* cells) = 0;
// Prints the result set. Called exactly once, after the last row.
virtual void Finish() = 0;
// When AddRow() first waited for the user, if it did. Time after that is
// not query execution.
virtual std::optional<std::chrono::steady_clock::time_point> FirstPromptTime()
const;
};
// The number of UTF-8 code points in |s|, used as its display width.
size_t CountChars(std::string_view s);
// Cuts |s| to at most |max_chars| UTF-8 code points (no limit if 0), never in
// the middle of a character. Returns the number of code points dropped.
uint64_t CutToChars(std::string_view* s, uint64_t max_chars);
// The marker appended to a cell from which CutToChars() dropped characters.
std::string CutMarker(uint64_t dropped_chars);
// Appends |s| to |out| with line breaks, tabs and every character in
// |extra_escapes| escaped with a backslash.
void AppendEscaped(std::string_view s,
std::string_view extra_escapes,
std::string* out);
// Formats |value| for people to read: NULL as "NULL", doubles in their
// shortest exact form, bytes as "<N bytes>" and strings cut to |max_chars|
// (no limit if 0) and escaped by AppendEscaped().
std::string FormatCellForDisplay(const SqlValue& value,
uint64_t max_chars,
std::string_view extra_escapes);
// Markdown options with the limits used unless overridden by flags.
ResultFormatOptions DefaultMarkdownOptions();
// Table options for the interactive shell, paging every |page_rows| rows.
ResultFormatOptions DefaultTableOptions(uint64_t page_rows);
std::unique_ptr<ResultFormatter> CreateResultFormatter(
const ResultFormatOptions& options,
std::vector<std::string> column_names,
FILE* output);
} // namespace perfetto::trace_processor
#endif // SRC_TRACE_PROCESSOR_SHELL_RESULT_FORMATTER_H_