1cc1dc7a3Sopenharmony_ci// SPDX-License-Identifier: Apache-2.0
2cc1dc7a3Sopenharmony_ci// ----------------------------------------------------------------------------
3cc1dc7a3Sopenharmony_ci// Copyright 2021-2022 Arm Limited
4cc1dc7a3Sopenharmony_ci//
5cc1dc7a3Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); you may not
6cc1dc7a3Sopenharmony_ci// use this file except in compliance with the License. You may obtain a copy
7cc1dc7a3Sopenharmony_ci// of the License at:
8cc1dc7a3Sopenharmony_ci//
9cc1dc7a3Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
10cc1dc7a3Sopenharmony_ci//
11cc1dc7a3Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
12cc1dc7a3Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13cc1dc7a3Sopenharmony_ci// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14cc1dc7a3Sopenharmony_ci// License for the specific language governing permissions and limitations
15cc1dc7a3Sopenharmony_ci// under the License.
16cc1dc7a3Sopenharmony_ci// ----------------------------------------------------------------------------
17cc1dc7a3Sopenharmony_ci
18cc1dc7a3Sopenharmony_ci/**
19cc1dc7a3Sopenharmony_ci * @brief This module provides a set of diagnostic tracing utilities.
20cc1dc7a3Sopenharmony_ci *
21cc1dc7a3Sopenharmony_ci * Overview
22cc1dc7a3Sopenharmony_ci * ========
23cc1dc7a3Sopenharmony_ci *
24cc1dc7a3Sopenharmony_ci * The built-in diagnostic trace tool generates a hierarchical JSON tree structure. The tree
25cc1dc7a3Sopenharmony_ci * hierarchy contains three levels:
26cc1dc7a3Sopenharmony_ci *
27cc1dc7a3Sopenharmony_ci *    - block
28cc1dc7a3Sopenharmony_ci *        - pass
29cc1dc7a3Sopenharmony_ci *           - candidate
30cc1dc7a3Sopenharmony_ci *
31cc1dc7a3Sopenharmony_ci * One block node exists for each compressed block in the image. One pass node exists for each major
32cc1dc7a3Sopenharmony_ci * pass (N partition, M planes, O components) applied to a block. One candidate node exists for each
33cc1dc7a3Sopenharmony_ci * encoding candidate trialed for a pass.
34cc1dc7a3Sopenharmony_ci *
35cc1dc7a3Sopenharmony_ci * Each node contains both the hierarchy but also a number of attributes which explain the behavior.
36cc1dc7a3Sopenharmony_ci * For example, the block node contains the block coordinates in the image, the pass explains the
37cc1dc7a3Sopenharmony_ci * pass configuration, and the candidate will explain the candidate encoding such as weight
38cc1dc7a3Sopenharmony_ci * decimation, refinement error, etc.
39cc1dc7a3Sopenharmony_ci *
40cc1dc7a3Sopenharmony_ci * Trace Nodes are designed as scope-managed C++ objects with stack-like push/pop behavior.
41cc1dc7a3Sopenharmony_ci * Constructing a trace node on the stack will automatically add it to the current node as a child,
42cc1dc7a3Sopenharmony_ci * and then make it the current node. Destroying the current node will pop the stack and set the
43cc1dc7a3Sopenharmony_ci * parent to the current node. This provides a robust mechanism for ensuring reliable nesting in the
44cc1dc7a3Sopenharmony_ci * tree structure.
45cc1dc7a3Sopenharmony_ci *
46cc1dc7a3Sopenharmony_ci * A set of utility macros are provided to add attribute annotations to the current trace node.
47cc1dc7a3Sopenharmony_ci *
48cc1dc7a3Sopenharmony_ci * Usage
49cc1dc7a3Sopenharmony_ci * =====
50cc1dc7a3Sopenharmony_ci *
51cc1dc7a3Sopenharmony_ci * Create Trace Nodes on the stack using the @c TRACE_NODE() macro. This will compile-out completely
52cc1dc7a3Sopenharmony_ci * in builds with diagnostics disabled.
53cc1dc7a3Sopenharmony_ci *
54cc1dc7a3Sopenharmony_ci * Add annotations to the current trace node using the @c trace_add_data() macro. This will
55cc1dc7a3Sopenharmony_ci * similarly compile out completely in builds with diagnostics disabled.
56cc1dc7a3Sopenharmony_ci *
57cc1dc7a3Sopenharmony_ci * If you need to add additional code to support diagnostics-only behavior wrap
58cc1dc7a3Sopenharmony_ci * it in preprocessor guards:
59cc1dc7a3Sopenharmony_ci *
60cc1dc7a3Sopenharmony_ci *     #if defined(ASTCENC_DIAGNOSTICS)
61cc1dc7a3Sopenharmony_ci *     #endif
62cc1dc7a3Sopenharmony_ci */
63cc1dc7a3Sopenharmony_ci
64cc1dc7a3Sopenharmony_ci#ifndef ASTCENC_DIAGNOSTIC_TRACE_INCLUDED
65cc1dc7a3Sopenharmony_ci#define ASTCENC_DIAGNOSTIC_TRACE_INCLUDED
66cc1dc7a3Sopenharmony_ci
67cc1dc7a3Sopenharmony_ci#if defined(ASTCENC_DIAGNOSTICS)
68cc1dc7a3Sopenharmony_ci
69cc1dc7a3Sopenharmony_ci#include <iostream>
70cc1dc7a3Sopenharmony_ci#include <fstream>
71cc1dc7a3Sopenharmony_ci#include <vector>
72cc1dc7a3Sopenharmony_ci
73cc1dc7a3Sopenharmony_ci/**
74cc1dc7a3Sopenharmony_ci * @brief Class representing a single node in the trace hierarchy.
75cc1dc7a3Sopenharmony_ci */
76cc1dc7a3Sopenharmony_ciclass TraceNode
77cc1dc7a3Sopenharmony_ci{
78cc1dc7a3Sopenharmony_cipublic:
79cc1dc7a3Sopenharmony_ci	/**
80cc1dc7a3Sopenharmony_ci	 * @brief Construct a new node.
81cc1dc7a3Sopenharmony_ci	 *
82cc1dc7a3Sopenharmony_ci	 * Constructing a node will push to the the top of the stack, automatically making it a child of
83cc1dc7a3Sopenharmony_ci	 * the current node, and then setting it to become the current node.
84cc1dc7a3Sopenharmony_ci	 *
85cc1dc7a3Sopenharmony_ci	 * @param format   The format template for the node name.
86cc1dc7a3Sopenharmony_ci	 * @param ...      The format parameters.
87cc1dc7a3Sopenharmony_ci	 */
88cc1dc7a3Sopenharmony_ci	TraceNode(const char* format, ...);
89cc1dc7a3Sopenharmony_ci
90cc1dc7a3Sopenharmony_ci	/**
91cc1dc7a3Sopenharmony_ci	 * @brief Add an attribute to this node.
92cc1dc7a3Sopenharmony_ci	 *
93cc1dc7a3Sopenharmony_ci	 * Note that no quoting is applied to the @c value, so if quoting is needed it must be done by
94cc1dc7a3Sopenharmony_ci	 * the caller.
95cc1dc7a3Sopenharmony_ci	 *
96cc1dc7a3Sopenharmony_ci	 * @param type    The type of the attribute.
97cc1dc7a3Sopenharmony_ci	 * @param key     The key of the attribute.
98cc1dc7a3Sopenharmony_ci	 * @param value   The value of the attribute.
99cc1dc7a3Sopenharmony_ci	 */
100cc1dc7a3Sopenharmony_ci	void add_attrib(std::string type, std::string key, std::string value);
101cc1dc7a3Sopenharmony_ci
102cc1dc7a3Sopenharmony_ci	/**
103cc1dc7a3Sopenharmony_ci	 * @brief Destroy this node.
104cc1dc7a3Sopenharmony_ci	 *
105cc1dc7a3Sopenharmony_ci	 * Destroying a node will pop it from the top of the stack, making its parent the current node.
106cc1dc7a3Sopenharmony_ci	 * It is invalid behavior to destroy a node that is not the current node; usage must conform to
107cc1dc7a3Sopenharmony_ci	 * stack push-pop semantics.
108cc1dc7a3Sopenharmony_ci	 */
109cc1dc7a3Sopenharmony_ci	~TraceNode();
110cc1dc7a3Sopenharmony_ci
111cc1dc7a3Sopenharmony_ci	/**
112cc1dc7a3Sopenharmony_ci	 * @brief The number of attributes and child nodes in this node.
113cc1dc7a3Sopenharmony_ci	 */
114cc1dc7a3Sopenharmony_ci	unsigned int m_attrib_count { 0 };
115cc1dc7a3Sopenharmony_ci};
116cc1dc7a3Sopenharmony_ci
117cc1dc7a3Sopenharmony_ci/**
118cc1dc7a3Sopenharmony_ci * @brief Class representing the trace log file being written.
119cc1dc7a3Sopenharmony_ci */
120cc1dc7a3Sopenharmony_ciclass TraceLog
121cc1dc7a3Sopenharmony_ci{
122cc1dc7a3Sopenharmony_cipublic:
123cc1dc7a3Sopenharmony_ci	/**
124cc1dc7a3Sopenharmony_ci	 * @brief Create a new trace log.
125cc1dc7a3Sopenharmony_ci	 *
126cc1dc7a3Sopenharmony_ci	 * The trace log is global; there can be only one at a time.
127cc1dc7a3Sopenharmony_ci	 *
128cc1dc7a3Sopenharmony_ci	 * @param file_name   The name of the file to write.
129cc1dc7a3Sopenharmony_ci	 */
130cc1dc7a3Sopenharmony_ci	TraceLog(const char* file_name);
131cc1dc7a3Sopenharmony_ci
132cc1dc7a3Sopenharmony_ci	/**
133cc1dc7a3Sopenharmony_ci	 * @brief Detroy the trace log.
134cc1dc7a3Sopenharmony_ci	 *
135cc1dc7a3Sopenharmony_ci	 * Trace logs MUST be cleanly destroyed to ensure the file gets written.
136cc1dc7a3Sopenharmony_ci	 */
137cc1dc7a3Sopenharmony_ci	~TraceLog();
138cc1dc7a3Sopenharmony_ci
139cc1dc7a3Sopenharmony_ci	/**
140cc1dc7a3Sopenharmony_ci	 * @brief Get the current child node.
141cc1dc7a3Sopenharmony_ci	 *
142cc1dc7a3Sopenharmony_ci	 * @return The current leaf node.
143cc1dc7a3Sopenharmony_ci	 */
144cc1dc7a3Sopenharmony_ci	TraceNode* get_current_leaf();
145cc1dc7a3Sopenharmony_ci
146cc1dc7a3Sopenharmony_ci	/**
147cc1dc7a3Sopenharmony_ci	 * @brief Get the stack depth of the current child node.
148cc1dc7a3Sopenharmony_ci	 *
149cc1dc7a3Sopenharmony_ci	 * @return The current leaf node stack depth.
150cc1dc7a3Sopenharmony_ci	 */
151cc1dc7a3Sopenharmony_ci	size_t get_depth();
152cc1dc7a3Sopenharmony_ci
153cc1dc7a3Sopenharmony_ci	/**
154cc1dc7a3Sopenharmony_ci	 * @brief The file stream to write to.
155cc1dc7a3Sopenharmony_ci	 */
156cc1dc7a3Sopenharmony_ci	std::ofstream m_file;
157cc1dc7a3Sopenharmony_ci
158cc1dc7a3Sopenharmony_ci	/**
159cc1dc7a3Sopenharmony_ci	 * @brief The stack of nodes (newest at the back).
160cc1dc7a3Sopenharmony_ci	 */
161cc1dc7a3Sopenharmony_ci	std::vector<TraceNode*> m_stack;
162cc1dc7a3Sopenharmony_ci
163cc1dc7a3Sopenharmony_ciprivate:
164cc1dc7a3Sopenharmony_ci	/**
165cc1dc7a3Sopenharmony_ci	 * @brief The root node in the JSON file.
166cc1dc7a3Sopenharmony_ci	 */
167cc1dc7a3Sopenharmony_ci	TraceNode* m_root;
168cc1dc7a3Sopenharmony_ci};
169cc1dc7a3Sopenharmony_ci
170cc1dc7a3Sopenharmony_ci/**
171cc1dc7a3Sopenharmony_ci * @brief Utility macro to create a trace node on the stack.
172cc1dc7a3Sopenharmony_ci *
173cc1dc7a3Sopenharmony_ci * @param name     The variable name to use.
174cc1dc7a3Sopenharmony_ci * @param ...      The name template and format parameters.
175cc1dc7a3Sopenharmony_ci */
176cc1dc7a3Sopenharmony_ci#define TRACE_NODE(name, ...) TraceNode name(__VA_ARGS__);
177cc1dc7a3Sopenharmony_ci
178cc1dc7a3Sopenharmony_ci/**
179cc1dc7a3Sopenharmony_ci * @brief Add a string annotation to the current node.
180cc1dc7a3Sopenharmony_ci *
181cc1dc7a3Sopenharmony_ci * @param key      The name of the attribute.
182cc1dc7a3Sopenharmony_ci * @param format   The format template for the attribute value.
183cc1dc7a3Sopenharmony_ci * @param ...      The format parameters.
184cc1dc7a3Sopenharmony_ci */
185cc1dc7a3Sopenharmony_civoid trace_add_data(const char* key, const char* format, ...);
186cc1dc7a3Sopenharmony_ci
187cc1dc7a3Sopenharmony_ci/**
188cc1dc7a3Sopenharmony_ci * @brief Add a float annotation to the current node.
189cc1dc7a3Sopenharmony_ci *
190cc1dc7a3Sopenharmony_ci * @param key     The name of the attribute.
191cc1dc7a3Sopenharmony_ci * @param value   The value of the attribute.
192cc1dc7a3Sopenharmony_ci */
193cc1dc7a3Sopenharmony_civoid trace_add_data(const char* key, float value);
194cc1dc7a3Sopenharmony_ci
195cc1dc7a3Sopenharmony_ci/**
196cc1dc7a3Sopenharmony_ci * @brief Add an integer annotation to the current node.
197cc1dc7a3Sopenharmony_ci *
198cc1dc7a3Sopenharmony_ci * @param key     The name of the attribute.
199cc1dc7a3Sopenharmony_ci * @param value   The value of the attribute.
200cc1dc7a3Sopenharmony_ci */
201cc1dc7a3Sopenharmony_civoid trace_add_data(const char* key, int value);
202cc1dc7a3Sopenharmony_ci
203cc1dc7a3Sopenharmony_ci/**
204cc1dc7a3Sopenharmony_ci * @brief Add an unsigned integer annotation to the current node.
205cc1dc7a3Sopenharmony_ci *
206cc1dc7a3Sopenharmony_ci * @param key     The name of the attribute.
207cc1dc7a3Sopenharmony_ci * @param value   The value of the attribute.
208cc1dc7a3Sopenharmony_ci */
209cc1dc7a3Sopenharmony_civoid trace_add_data(const char* key, unsigned int value);
210cc1dc7a3Sopenharmony_ci
211cc1dc7a3Sopenharmony_ci#else
212cc1dc7a3Sopenharmony_ci
213cc1dc7a3Sopenharmony_ci#define TRACE_NODE(name, ...)
214cc1dc7a3Sopenharmony_ci
215cc1dc7a3Sopenharmony_ci#define trace_add_data(...)
216cc1dc7a3Sopenharmony_ci
217cc1dc7a3Sopenharmony_ci#endif
218cc1dc7a3Sopenharmony_ci
219cc1dc7a3Sopenharmony_ci#endif
220