astcenc_diagnostic_trace.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2021-2023 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 <cmath>
  25. #include <limits>
  26. #include <string>
  27. #include "astcenc_diagnostic_trace.h"
  28. /** @brief The global trace logger. */
  29. static TraceLog* g_TraceLog = nullptr;
  30. /** @brief The JSON indentation level. */
  31. static const size_t g_trace_indent = 2;
  32. TraceLog::TraceLog(
  33. const char* file_name):
  34. m_file(file_name, std::ofstream::out | std::ofstream::binary)
  35. {
  36. assert(!g_TraceLog);
  37. g_TraceLog = this;
  38. m_root = new TraceNode("root");
  39. }
  40. /* See header for documentation. */
  41. TraceNode* TraceLog::get_current_leaf()
  42. {
  43. if (m_stack.size())
  44. {
  45. return m_stack.back();
  46. }
  47. return nullptr;
  48. }
  49. /* See header for documentation. */
  50. size_t TraceLog::get_depth()
  51. {
  52. return m_stack.size();
  53. }
  54. /* See header for documentation. */
  55. TraceLog::~TraceLog()
  56. {
  57. assert(g_TraceLog == this);
  58. delete m_root;
  59. g_TraceLog = nullptr;
  60. }
  61. /* See header for documentation. */
  62. TraceNode::TraceNode(
  63. const char* format,
  64. ...
  65. ) {
  66. // Format the name string
  67. constexpr size_t bufsz = 256;
  68. char buffer[bufsz];
  69. va_list args;
  70. va_start (args, format);
  71. vsnprintf (buffer, bufsz, format, args);
  72. va_end (args);
  73. // Guarantee there is a nul terminator
  74. buffer[bufsz - 1] = 0;
  75. // Generate the node
  76. TraceNode* parent = g_TraceLog->get_current_leaf();
  77. size_t depth = g_TraceLog->get_depth();
  78. g_TraceLog->m_stack.push_back(this);
  79. bool comma = parent && parent->m_attrib_count;
  80. auto& out = g_TraceLog->m_file;
  81. if (parent)
  82. {
  83. parent->m_attrib_count++;
  84. }
  85. if (comma)
  86. {
  87. out << ',';
  88. }
  89. if (depth)
  90. {
  91. out << '\n';
  92. }
  93. size_t out_indent = (depth * 2) * g_trace_indent;
  94. size_t in_indent = (depth * 2 + 1) * g_trace_indent;
  95. std::string out_indents("");
  96. if (out_indent)
  97. {
  98. out_indents = std::string(out_indent, ' ');
  99. }
  100. std::string in_indents(in_indent, ' ');
  101. out << out_indents << "[ \"node\", \"" << buffer << "\",\n";
  102. out << in_indents << "[";
  103. }
  104. /* See header for documentation. */
  105. void TraceNode::add_attrib(
  106. std::string type,
  107. std::string key,
  108. std::string value
  109. ) {
  110. (void)type;
  111. size_t depth = g_TraceLog->get_depth();
  112. size_t indent = (depth * 2) * g_trace_indent;
  113. auto& out = g_TraceLog->m_file;
  114. bool comma = m_attrib_count;
  115. m_attrib_count++;
  116. if (comma)
  117. {
  118. out << ',';
  119. }
  120. out << '\n';
  121. out << std::string(indent, ' ') << "[ "
  122. << "\"" << key << "\", "
  123. << value << " ]";
  124. }
  125. /* See header for documentation. */
  126. TraceNode::~TraceNode()
  127. {
  128. g_TraceLog->m_stack.pop_back();
  129. auto& out = g_TraceLog->m_file;
  130. size_t depth = g_TraceLog->get_depth();
  131. size_t out_indent = (depth * 2) * g_trace_indent;
  132. size_t in_indent = (depth * 2 + 1) * g_trace_indent;
  133. std::string out_indents("");
  134. if (out_indent)
  135. {
  136. out_indents = std::string(out_indent, ' ');
  137. }
  138. std::string in_indents(in_indent, ' ');
  139. if (m_attrib_count)
  140. {
  141. out << "\n" << in_indents;
  142. }
  143. out << "]\n";
  144. out << out_indents << "]";
  145. }
  146. /* See header for documentation. */
  147. void trace_add_data(
  148. const char* key,
  149. const char* format,
  150. ...
  151. ) {
  152. constexpr size_t bufsz = 256;
  153. char buffer[bufsz];
  154. va_list args;
  155. va_start (args, format);
  156. vsnprintf (buffer, bufsz, format, args);
  157. va_end (args);
  158. // Guarantee there is a nul terminator
  159. buffer[bufsz - 1] = 0;
  160. std::string value = "\"" + std::string(buffer) + "\"";
  161. TraceNode* node = g_TraceLog->get_current_leaf();
  162. node->add_attrib("str", key, value);
  163. }
  164. /* See header for documentation. */
  165. void trace_add_data(
  166. const char* key,
  167. float value
  168. ) {
  169. // Turn infinities into parseable values
  170. if (std::isinf(value))
  171. {
  172. if (value > 0.0f)
  173. {
  174. value = std::numeric_limits<float>::max();
  175. }
  176. else
  177. {
  178. value = -std::numeric_limits<float>::max();
  179. }
  180. }
  181. char buffer[256];
  182. sprintf(buffer, "%.20g", (double)value);
  183. TraceNode* node = g_TraceLog->get_current_leaf();
  184. node->add_attrib("float", key, buffer);
  185. }
  186. /* See header for documentation. */
  187. void trace_add_data(
  188. const char* key,
  189. int value
  190. ) {
  191. TraceNode* node = g_TraceLog->get_current_leaf();
  192. node->add_attrib("int", key, std::to_string(value));
  193. }
  194. /* See header for documentation. */
  195. void trace_add_data(
  196. const char* key,
  197. unsigned int value
  198. ) {
  199. TraceNode* node = g_TraceLog->get_current_leaf();
  200. node->add_attrib("int", key, std::to_string(value));
  201. }
  202. #endif