markv_logger.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_COMP_MARKV_LOGGER_H_
  15. #define SOURCE_COMP_MARKV_LOGGER_H_
  16. #include "source/comp/markv.h"
  17. namespace spvtools {
  18. namespace comp {
  19. class MarkvLogger {
  20. public:
  21. MarkvLogger(MarkvLogConsumer log_consumer, MarkvDebugConsumer debug_consumer)
  22. : log_consumer_(log_consumer), debug_consumer_(debug_consumer) {}
  23. void AppendText(const std::string& str) {
  24. Append(str);
  25. use_delimiter_ = false;
  26. }
  27. void AppendTextNewLine(const std::string& str) {
  28. Append(str);
  29. Append("\n");
  30. use_delimiter_ = false;
  31. }
  32. void AppendBitSequence(const std::string& str) {
  33. if (debug_consumer_) instruction_bits_ << str;
  34. if (use_delimiter_) Append("-");
  35. Append(str);
  36. use_delimiter_ = true;
  37. }
  38. void AppendWhitespaces(size_t num) {
  39. Append(std::string(num, ' '));
  40. use_delimiter_ = false;
  41. }
  42. void NewLine() {
  43. Append("\n");
  44. use_delimiter_ = false;
  45. }
  46. bool DebugInstruction(const spv_parsed_instruction_t& inst) {
  47. bool result = true;
  48. if (debug_consumer_) {
  49. result = debug_consumer_(
  50. std::vector<uint32_t>(inst.words, inst.words + inst.num_words),
  51. instruction_bits_.str(), instruction_comment_.str());
  52. instruction_bits_.str(std::string());
  53. instruction_comment_.str(std::string());
  54. }
  55. return result;
  56. }
  57. private:
  58. MarkvLogger(const MarkvLogger&) = delete;
  59. MarkvLogger(MarkvLogger&&) = delete;
  60. MarkvLogger& operator=(const MarkvLogger&) = delete;
  61. MarkvLogger& operator=(MarkvLogger&&) = delete;
  62. void Append(const std::string& str) {
  63. if (log_consumer_) log_consumer_(str);
  64. if (debug_consumer_) instruction_comment_ << str;
  65. }
  66. MarkvLogConsumer log_consumer_;
  67. MarkvDebugConsumer debug_consumer_;
  68. std::stringstream instruction_bits_;
  69. std::stringstream instruction_comment_;
  70. // If true a delimiter will be appended before the next bit sequence.
  71. // Used to generate outputs like: 1100-0 1110-1-1100-1-1111-0 110-0.
  72. bool use_delimiter_ = false;
  73. };
  74. } // namespace comp
  75. } // namespace spvtools
  76. #endif // SOURCE_COMP_MARKV_LOGGER_H_