astcenc_diagnostic_trace.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2021-2022 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // of the License at:
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. /**
  18. * @brief Functions for the library entrypoint.
  19. */
  20. #if defined(ASTCENC_DIAGNOSTICS)
  21. #include <cassert>
  22. #include <cstdarg>
  23. #include <cstdio>
  24. #include <string>
  25. #include "astcenc_diagnostic_trace.h"
  26. /** @brief The global trace logger. */
  27. static TraceLog* g_TraceLog = nullptr;
  28. /** @brief The JSON indentation level. */
  29. static const size_t g_trace_indent = 2;
  30. TraceLog::TraceLog(
  31. const char* file_name):
  32. m_file(file_name, std::ofstream::out | std::ofstream::binary)
  33. {
  34. assert(!g_TraceLog);
  35. g_TraceLog = this;
  36. m_root = new TraceNode("root");
  37. }
  38. /* See header for documentation. */
  39. TraceNode* TraceLog::get_current_leaf()
  40. {
  41. if (m_stack.size())
  42. {
  43. return m_stack.back();
  44. }
  45. return nullptr;
  46. }
  47. /* See header for documentation. */
  48. size_t TraceLog::get_depth()
  49. {
  50. return m_stack.size();
  51. }
  52. /* See header for documentation. */
  53. TraceLog::~TraceLog()
  54. {
  55. assert(g_TraceLog == this);
  56. delete m_root;
  57. g_TraceLog = nullptr;
  58. }
  59. /* See header for documentation. */
  60. TraceNode::TraceNode(
  61. const char* format,
  62. ...
  63. ) {
  64. // Format the name string
  65. constexpr size_t bufsz = 256;
  66. char buffer[bufsz];
  67. va_list args;
  68. va_start (args, format);
  69. vsnprintf (buffer, bufsz, format, args);
  70. va_end (args);
  71. // Guarantee there is a nul terminator
  72. buffer[bufsz - 1] = 0;
  73. // Generate the node
  74. TraceNode* parent = g_TraceLog->get_current_leaf();
  75. size_t depth = g_TraceLog->get_depth();
  76. g_TraceLog->m_stack.push_back(this);
  77. bool comma = parent && parent->m_attrib_count;
  78. auto& out = g_TraceLog->m_file;
  79. if (parent)
  80. {
  81. parent->m_attrib_count++;
  82. }
  83. if (comma)
  84. {
  85. out << ',';
  86. }
  87. if (depth)
  88. {
  89. out << '\n';
  90. }
  91. size_t out_indent = (depth * 2) * g_trace_indent;
  92. size_t in_indent = (depth * 2 + 1) * g_trace_indent;
  93. std::string out_indents("");
  94. if (out_indent)
  95. {
  96. out_indents = std::string(out_indent, ' ');
  97. }
  98. std::string in_indents(in_indent, ' ');
  99. out << out_indents << "[ \"node\", \"" << buffer << "\",\n";
  100. out << in_indents << "[";
  101. }
  102. /* See header for documentation. */
  103. void TraceNode::add_attrib(
  104. std::string type,
  105. std::string key,
  106. std::string value
  107. ) {
  108. (void)type;
  109. size_t depth = g_TraceLog->get_depth();
  110. size_t indent = (depth * 2) * g_trace_indent;
  111. auto& out = g_TraceLog->m_file;
  112. bool comma = m_attrib_count;
  113. m_attrib_count++;
  114. if (comma)
  115. {
  116. out << ',';
  117. }
  118. out << '\n';
  119. out << std::string(indent, ' ') << "[ "
  120. << "\"" << key << "\", "
  121. << value << " ]";
  122. }
  123. /* See header for documentation. */
  124. TraceNode::~TraceNode()
  125. {
  126. g_TraceLog->m_stack.pop_back();
  127. auto& out = g_TraceLog->m_file;
  128. size_t depth = g_TraceLog->get_depth();
  129. size_t out_indent = (depth * 2) * g_trace_indent;
  130. size_t in_indent = (depth * 2 + 1) * g_trace_indent;
  131. std::string out_indents("");
  132. if (out_indent)
  133. {
  134. out_indents = std::string(out_indent, ' ');
  135. }
  136. std::string in_indents(in_indent, ' ');
  137. if (m_attrib_count)
  138. {
  139. out << "\n" << in_indents;
  140. }
  141. out << "]\n";
  142. out << out_indents << "]";
  143. }
  144. /* See header for documentation. */
  145. void trace_add_data(
  146. const char* key,
  147. const char* format,
  148. ...
  149. ) {
  150. constexpr size_t bufsz = 256;
  151. char buffer[bufsz];
  152. va_list args;
  153. va_start (args, format);
  154. vsnprintf (buffer, bufsz, format, args);
  155. va_end (args);
  156. // Guarantee there is a nul terminator
  157. buffer[bufsz - 1] = 0;
  158. std::string value = "\"" + std::string(buffer) + "\"";
  159. TraceNode* node = g_TraceLog->get_current_leaf();
  160. node->add_attrib("str", key, value);
  161. }
  162. /* See header for documentation. */
  163. void trace_add_data(
  164. const char* key,
  165. float value
  166. ) {
  167. char buffer[256];
  168. sprintf(buffer, "%.20g", (double)value);
  169. TraceNode* node = g_TraceLog->get_current_leaf();
  170. node->add_attrib("float", key, buffer);
  171. }
  172. /* See header for documentation. */
  173. void trace_add_data(
  174. const char* key,
  175. int value
  176. ) {
  177. TraceNode* node = g_TraceLog->get_current_leaf();
  178. node->add_attrib("int", key, std::to_string(value));
  179. }
  180. /* See header for documentation. */
  181. void trace_add_data(
  182. const char* key,
  183. unsigned int value
  184. ) {
  185. TraceNode* node = g_TraceLog->get_current_leaf();
  186. node->add_attrib("int", key, std::to_string(value));
  187. }
  188. #endif