• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.internal.widget.remotecompose.core.operations.layout.utils;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 
21 import java.util.ArrayList;
22 
23 /** Internal utility debug class */
24 public class DebugLog {
25 
26     public static final boolean DEBUG_LAYOUT_ON = false;
27 
28     public static class Node {
29         @Nullable public Node parent;
30         @NonNull public String name;
31         @NonNull public String endString;
32         @NonNull public ArrayList<Node> list = new ArrayList<>();
33 
Node(@ullable Node parent, @NonNull String name)34         public Node(@Nullable Node parent, @NonNull String name) {
35             this.parent = parent;
36             this.name = name;
37             this.endString = name + " DONE";
38             if (parent != null) {
39                 parent.add(this);
40             }
41         }
42 
43         /**
44          * Add a node to the current node
45          *
46          * @param node
47          */
add(@onNull Node node)48         public void add(@NonNull Node node) {
49             list.add(node);
50         }
51     }
52 
53     public static class LogNode extends Node {
LogNode(@ullable Node parent, @NonNull String name)54         public LogNode(@Nullable Node parent, @NonNull String name) {
55             super(parent, name);
56         }
57     }
58 
59     @NonNull public static Node node = new Node(null, "Root");
60     @NonNull public static Node currentNode = node;
61 
62     /** clear the current logging */
clear()63     public static void clear() {
64         node = new Node(null, "Root");
65         currentNode = node;
66     }
67 
68     /**
69      * start a node
70      *
71      * @param valueSupplier
72      */
s(@onNull StringValueSupplier valueSupplier)73     public static void s(@NonNull StringValueSupplier valueSupplier) {
74         if (DEBUG_LAYOUT_ON) {
75             currentNode = new Node(currentNode, valueSupplier.getString());
76         }
77     }
78 
79     /**
80      * arbitrary log statement
81      *
82      * @param valueSupplier
83      */
log(@onNull StringValueSupplier valueSupplier)84     public static void log(@NonNull StringValueSupplier valueSupplier) {
85         if (DEBUG_LAYOUT_ON) {
86             new LogNode(currentNode, valueSupplier.getString());
87         }
88     }
89 
90     /** end a node */
e()91     public static void e() {
92         if (DEBUG_LAYOUT_ON) {
93             if (currentNode.parent != null) {
94                 currentNode = currentNode.parent;
95             } else {
96                 currentNode = node;
97             }
98         }
99     }
100 
101     /**
102      * end a node
103      *
104      * @param valueSupplier
105      */
e(@onNull StringValueSupplier valueSupplier)106     public static void e(@NonNull StringValueSupplier valueSupplier) {
107         if (DEBUG_LAYOUT_ON) {
108             currentNode.endString = valueSupplier.getString();
109             if (currentNode.parent != null) {
110                 currentNode = currentNode.parent;
111             } else {
112                 currentNode = node;
113             }
114         }
115     }
116 
117     /**
118      * print a given node
119      *
120      * @param indent
121      * @param node
122      * @param builder
123      */
printNode(int indent, @NonNull Node node, @NonNull StringBuilder builder)124     public static void printNode(int indent, @NonNull Node node, @NonNull StringBuilder builder) {
125         if (DEBUG_LAYOUT_ON) {
126             StringBuilder indentationBuilder = new StringBuilder();
127             for (int i = 0; i < indent; i++) {
128                 indentationBuilder.append("| ");
129             }
130             String indentation = indentationBuilder.toString();
131 
132             if (node.list.size() > 0) {
133                 builder.append(indentation).append(node.name).append("\n");
134                 for (Node c : node.list) {
135                     printNode(indent + 1, c, builder);
136                 }
137                 builder.append(indentation).append(node.endString).append("\n");
138             } else {
139                 if (node instanceof LogNode) {
140                     builder.append(indentation).append("     ").append(node.name).append("\n");
141                 } else {
142                     builder.append(indentation)
143                             .append("-- ")
144                             .append(node.name)
145                             .append(" : ")
146                             .append(node.endString)
147                             .append("\n");
148                 }
149             }
150         }
151     }
152 
153     /** Output the captured log to System.out */
display()154     public static void display() {
155         if (DEBUG_LAYOUT_ON) {
156             StringBuilder builder = new StringBuilder();
157             printNode(0, node, builder);
158             System.out.println("\n" + builder.toString());
159         }
160     }
161 }
162