souffle  2.0.2-371-g6315b36
DebugReport.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file reports/DebugReport.h
12  *
13  * Defines classes for creating HTML reports of debugging information.
14  *
15  ***********************************************************************/
16 #pragma once
17 
18 #include <cstdint>
19 #include <ostream>
20 #include <stack>
21 #include <string>
22 #include <string_view>
23 #include <utility>
24 #include <vector>
25 
26 namespace souffle {
27 
28 /**
29  * Class representing a section of a HTML report.
30  * Consists of a unique identifier, a title, a number of subsections,
31  * and the HTML code for the body of the section.
32  */
33 class DebugReportSection {
34 public:
35  DebugReportSection(std::string id, std::string title, std::string body)
36  : id(generateUniqueID(std::move(id))), title(std::move(title)), body(std::move(body)) {}
37 
39  std::string id, std::string title, std::vector<DebugReportSection> subsections, std::string body)
40  : id(generateUniqueID(std::move(id))), title(std::move(title)),
41  subsections(std::move(subsections)), body(std::move(body)) {}
42 
43  /**
44  * Outputs the HTML code for the index to the given stream,
45  * consisting of a link to the section body followed by a list of
46  * the indices for each subsection.
47  */
48  void printIndex(std::ostream& out) const;
49 
50  /**
51  * Outputs the HTML code for the title header to the given stream.
52  */
53  void printTitle(std::ostream& out) const;
54 
55  /**
56  * Outputs the HTML code for the content of the section to the given
57  * stream, consisting of the title header, the body text, followed
58  * by the content for each subsection.
59  */
60  void printContent(std::ostream& out) const;
61 
62  bool hasSubsections() const {
63  return !subsections.empty();
64  }
65 
66 private:
67  std::string id;
68  std::string title;
69  std::vector<DebugReportSection> subsections;
70  std::string body;
71 
72  static std::string generateUniqueID(std::string id) {
73  static int count = 0;
74  return std::move(id) + std::to_string(count++);
75  }
76 };
77 
78 /**
79  * Class representing a HTML report, consisting of a list of sections.
80  */
81 class DebugReport {
82 public:
83  ~DebugReport();
84 
85  void flush();
86 
88  auto& buf = currentSubsections.empty() ? sections : currentSubsections.top();
89  buf.emplace_back(std::move(section));
90  }
91 
92  void addSection(std::string id, std::string title, std::string_view code);
93  void addCodeSection(std::string id, std::string title, std::string_view language, std::string_view prev,
94  std::string_view curr);
95 
96  void startSection() {
97  currentSubsections.emplace();
98  }
99 
100  void endSection(std::string currentSectionName, std::string currentSectionTitle);
101 
102  /**
103  * Outputs a complete HTML document to the given stream,
104  * consisting of an index of all of the sections of the report,
105  * followed by the content of each section.
106  */
107  void print(std::ostream& out) const;
108 
109  /**
110  * Generate a debug report section for code (preserving formatting), with the given id and title.
111  */
112  static DebugReportSection getCodeSection(const std::string& id, std::string title, std::string code);
113 
114  friend std::ostream& operator<<(std::ostream& out, const DebugReport& report) {
115  report.print(out);
116  return out;
117  }
118 
119 private:
120  std::vector<DebugReportSection> sections;
121  std::stack<std::vector<DebugReportSection>> currentSubsections;
122  uint32_t nextUniqueId = 0; // used for generating unique HTML `id` tags
123 
124  bool empty() const {
125  return sections.empty();
126  }
127 };
128 
129 } // end of namespace souffle
souffle::DebugReportSection::generateUniqueID
static std::string generateUniqueID(std::string id)
Definition: DebugReport.h:84
souffle::DebugReport::sections
std::vector< DebugReportSection > sections
Definition: DebugReport.h:126
souffle::DebugReportSection
Class representing a section of a HTML report.
Definition: DebugReport.h:39
souffle::DebugReportSection::hasSubsections
bool hasSubsections() const
Definition: DebugReport.h:74
souffle::id
A functor representing the identity function for a generic type T.
Definition: StreamUtil.h:136
souffle::DebugReportSection::printTitle
void printTitle(std::ostream &out) const
Outputs the HTML code for the title header to the given stream.
souffle::DebugReport::nextUniqueId
uint32_t nextUniqueId
Definition: DebugReport.h:128
souffle::DebugReportSection::printIndex
void printIndex(std::ostream &out) const
Outputs the HTML code for the index to the given stream, consisting of a link to the section body fol...
souffle::DebugReport::flush
void flush()
souffle::DebugReport::addSection
void addSection(DebugReportSection section)
Definition: DebugReport.h:93
souffle::DebugReportSection::id
std::string id
Definition: DebugReport.h:79
souffle::DebugReport::startSection
void startSection()
Definition: DebugReport.h:102
souffle::DebugReport::print
void print(std::ostream &out) const
Outputs a complete HTML document to the given stream, consisting of an index of all of the sections o...
souffle::DebugReport::operator<<
friend std::ostream & operator<<(std::ostream &out, const DebugReport &report)
Definition: DebugReport.h:120
souffle::DebugReportSection::printContent
void printContent(std::ostream &out) const
Outputs the HTML code for the content of the section to the given stream, consisting of the title hea...
souffle::DebugReport::addCodeSection
void addCodeSection(std::string id, std::string title, std::string_view language, std::string_view prev, std::string_view curr)
souffle::DebugReport
Class representing a HTML report, consisting of a list of sections.
Definition: DebugReport.h:87
souffle::DebugReportSection::body
std::string body
Definition: DebugReport.h:82
souffle::DebugReport::empty
bool empty() const
Definition: DebugReport.h:130
souffle::test::count
int count(const C &c)
Definition: table_test.cpp:40
souffle::DebugReport::~DebugReport
~DebugReport()
souffle::DebugReportSection::DebugReportSection
DebugReportSection(std::string id, std::string title, std::string body)
Definition: DebugReport.h:47
souffle::DebugReport::getCodeSection
static DebugReportSection getCodeSection(const std::string &id, std::string title, std::string code)
Generate a debug report section for code (preserving formatting), with the given id and title.
souffle::DebugReport::endSection
void endSection(std::string currentSectionName, std::string currentSectionTitle)
std
Definition: Brie.h:3053
souffle::DebugReportSection::title
std::string title
Definition: DebugReport.h:80
souffle
Definition: AggregateOp.h:25
souffle::DebugReport::currentSubsections
std::stack< std::vector< DebugReportSection > > currentSubsections
Definition: DebugReport.h:127
souffle::DebugReportSection::subsections
std::vector< DebugReportSection > subsections
Definition: DebugReport.h:81