Path: blob/master/thirdparty/astcenc/astcenc_diagnostic_trace.h
9896 views
// SPDX-License-Identifier: Apache-2.01// ----------------------------------------------------------------------------2// Copyright 2021-2022 Arm Limited3//4// Licensed under the Apache License, Version 2.0 (the "License"); you may not5// use this file except in compliance with the License. You may obtain a copy6// of the License at:7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13// License for the specific language governing permissions and limitations14// under the License.15// ----------------------------------------------------------------------------1617/**18* @brief This module provides a set of diagnostic tracing utilities.19*20* Overview21* ========22*23* The built-in diagnostic trace tool generates a hierarchical JSON tree structure. The tree24* hierarchy contains three levels:25*26* - block27* - pass28* - candidate29*30* One block node exists for each compressed block in the image. One pass node exists for each major31* pass (N partition, M planes, O components) applied to a block. One candidate node exists for each32* encoding candidate trialed for a pass.33*34* Each node contains both the hierarchy but also a number of attributes which explain the behavior.35* For example, the block node contains the block coordinates in the image, the pass explains the36* pass configuration, and the candidate will explain the candidate encoding such as weight37* decimation, refinement error, etc.38*39* Trace Nodes are designed as scope-managed C++ objects with stack-like push/pop behavior.40* Constructing a trace node on the stack will automatically add it to the current node as a child,41* and then make it the current node. Destroying the current node will pop the stack and set the42* parent to the current node. This provides a robust mechanism for ensuring reliable nesting in the43* tree structure.44*45* A set of utility macros are provided to add attribute annotations to the current trace node.46*47* Usage48* =====49*50* Create Trace Nodes on the stack using the @c TRACE_NODE() macro. This will compile-out completely51* in builds with diagnostics disabled.52*53* Add annotations to the current trace node using the @c trace_add_data() macro. This will54* similarly compile out completely in builds with diagnostics disabled.55*56* If you need to add additional code to support diagnostics-only behavior wrap57* it in preprocessor guards:58*59* #if defined(ASTCENC_DIAGNOSTICS)60* #endif61*/6263#ifndef ASTCENC_DIAGNOSTIC_TRACE_INCLUDED64#define ASTCENC_DIAGNOSTIC_TRACE_INCLUDED6566#if defined(ASTCENC_DIAGNOSTICS)6768#include <iostream>69#include <fstream>70#include <vector>7172/**73* @brief Class representing a single node in the trace hierarchy.74*/75class TraceNode76{77public:78/**79* @brief Construct a new node.80*81* Constructing a node will push to the the top of the stack, automatically making it a child of82* the current node, and then setting it to become the current node.83*84* @param format The format template for the node name.85* @param ... The format parameters.86*/87TraceNode(const char* format, ...);8889/**90* @brief Add an attribute to this node.91*92* Note that no quoting is applied to the @c value, so if quoting is needed it must be done by93* the caller.94*95* @param type The type of the attribute.96* @param key The key of the attribute.97* @param value The value of the attribute.98*/99void add_attrib(std::string type, std::string key, std::string value);100101/**102* @brief Destroy this node.103*104* Destroying a node will pop it from the top of the stack, making its parent the current node.105* It is invalid behavior to destroy a node that is not the current node; usage must conform to106* stack push-pop semantics.107*/108~TraceNode();109110/**111* @brief The number of attributes and child nodes in this node.112*/113unsigned int m_attrib_count { 0 };114};115116/**117* @brief Class representing the trace log file being written.118*/119class TraceLog120{121public:122/**123* @brief Create a new trace log.124*125* The trace log is global; there can be only one at a time.126*127* @param file_name The name of the file to write.128*/129TraceLog(const char* file_name);130131/**132* @brief Detroy the trace log.133*134* Trace logs MUST be cleanly destroyed to ensure the file gets written.135*/136~TraceLog();137138/**139* @brief Get the current child node.140*141* @return The current leaf node.142*/143TraceNode* get_current_leaf();144145/**146* @brief Get the stack depth of the current child node.147*148* @return The current leaf node stack depth.149*/150size_t get_depth();151152/**153* @brief The file stream to write to.154*/155std::ofstream m_file;156157/**158* @brief The stack of nodes (newest at the back).159*/160std::vector<TraceNode*> m_stack;161162private:163/**164* @brief The root node in the JSON file.165*/166TraceNode* m_root;167};168169/**170* @brief Utility macro to create a trace node on the stack.171*172* @param name The variable name to use.173* @param ... The name template and format parameters.174*/175#define TRACE_NODE(name, ...) TraceNode name(__VA_ARGS__);176177/**178* @brief Add a string annotation to the current node.179*180* @param key The name of the attribute.181* @param format The format template for the attribute value.182* @param ... The format parameters.183*/184void trace_add_data(const char* key, const char* format, ...);185186/**187* @brief Add a float annotation to the current node.188*189* @param key The name of the attribute.190* @param value The value of the attribute.191*/192void trace_add_data(const char* key, float value);193194/**195* @brief Add an integer annotation to the current node.196*197* @param key The name of the attribute.198* @param value The value of the attribute.199*/200void trace_add_data(const char* key, int value);201202/**203* @brief Add an unsigned integer annotation to the current node.204*205* @param key The name of the attribute.206* @param value The value of the attribute.207*/208void trace_add_data(const char* key, unsigned int value);209210#else211212#define TRACE_NODE(name, ...)213214#define trace_add_data(...)215216#endif217218#endif219220221