1 /*
 2  * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.asmtools.jdis;
24 
25 import org.openjdk.asmtools.asmutils.HexUtils;
26 
27 import java.util.Arrays;
28 import java.util.stream.Collectors;
29 
30 import static java.lang.String.format;
31 
32 public class TraceUtils {
33 
34     public static String prefixString = "\t";
35 
36     public static void trace(String s) {
37         if (!(Options.OptionObject()).debug()) {
38             return;
39         }
40         System.out.print(s);
41     }
42 
43     public static void trace(int prefixLength, String s) {
44         if (!(Options.OptionObject()).debug()) {
45             return;
46         }
47         System.out.print((prefixLength > 0) ? new String(new char[prefixLength]).replace("\0", prefixString) + s : s);
48     }
49 
50     public static void traceln(String s) {
51         if (!(Options.OptionObject()).debug()) {
52             return;
53         }
54         System.out.println(s);
55     }
56 
57     public static void traceln(String... lines) {
58         if (!(Options.OptionObject()).debug()) {
59             return;
60         }
61 
62         if (lines.length == 0) {
63             System.out.println();
64         } else {
65             for (String s : lines) {
66                 System.out.println(s);
67             }
68         }
69     }
70 
71     public static void traceln(int prefixLength, String s) {
72         if (!(Options.OptionObject()).debug()) {
73             return;
74         }
75         System.out.println((prefixLength > 0) ? new String(new char[prefixLength]).replace("\0", prefixString) + s : s);
76     }
77 
78     public static void traceln(int prefixLength, String... lines) {
79         if (!(Options.OptionObject()).debug()) {
80             return;
81         }
82         if (lines.length == 0) {
83             System.out.println();
84         } else {
85             String prefix = (prefixLength > 0) ? new String(new char[prefixLength]).replace("\0", prefixString) : "";
86             for (String s : lines) {
87                 System.out.println(prefix + s);
88             }
89         }
90     }
91 
92     public static String mapToHexString(int[] array) {
93         return format("%d %s",
94                 array.length,
95                 Arrays.stream(array).
96                         mapToObj(val -> HexUtils.toHex(val)).
97                         collect(Collectors.joining(" ")));
98     }
99 }