Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3cef71ce31b315afcc9bb972dbf36986b491c4ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2009, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Red Hat Inc. - Bug 460967
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.repository.helpers;

import java.util.*;
import java.util.Map.Entry;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.eclipse.equinox.internal.p2.repository.Activator;
import org.eclipse.osgi.service.debug.DebugOptions;

public class DebugHelper {
	public static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$

	public static final boolean DEBUG_REPOSITORY_CREDENTIALS;
	public static final boolean DEBUG_REPOSITORY_TRANSPORT;

	static {
		DebugOptions options = ServiceHelper.getService(Activator.getContext(), DebugOptions.class);
		if (options != null) {
			DEBUG_REPOSITORY_CREDENTIALS = options.getBooleanOption(Activator.ID + "/credentials/debug", false); //$NON-NLS-1$
			DEBUG_REPOSITORY_TRANSPORT = options.getBooleanOption(Activator.ID + "/transport/debug", false); //$NON-NLS-1$
		} else {
			DEBUG_REPOSITORY_CREDENTIALS = false;
			DEBUG_REPOSITORY_TRANSPORT = false;
		}
	}

	public static void debug(String name, String message) {
		StringBuilder buffer = new StringBuilder();
		buffer.append("["); //$NON-NLS-1$
		buffer.append(Activator.ID + "-" + name); //$NON-NLS-1$
		buffer.append("] "); //$NON-NLS-1$
		buffer.append(new Date(System.currentTimeMillis()));
		buffer.append(" - ["); //$NON-NLS-1$
		buffer.append(Thread.currentThread().getName());
		buffer.append("] " + LINE_SEPARATOR); //$NON-NLS-1$
		buffer.append(message);
		System.out.println(buffer.toString());
	}

	public static void debug(String name, String message, Object[] keyValueArray) {
		if (keyValueArray == null || keyValueArray.length == 0)
			debug(name, message);
		else {
			Map<Object, Object> params = new LinkedHashMap<>(keyValueArray.length / 2);
			for (int i = 0; i < keyValueArray.length; i += 2)
				params.put(keyValueArray[i], keyValueArray[i + 1]);
			StringBuilder buffer = new StringBuilder();
			buffer.append(message);
			buffer.append(formatMap(params, true, true));
			debug(name, buffer.toString());
		}

		StringBuilder buffer = new StringBuilder();
		buffer.append("["); //$NON-NLS-1$
		buffer.append(Activator.ID + "-" + name); //$NON-NLS-1$
		buffer.append("] "); //$NON-NLS-1$
		buffer.append(new Date(System.currentTimeMillis()));
		buffer.append(" - ["); //$NON-NLS-1$
		buffer.append(Thread.currentThread().getName());
		buffer.append("] " + LINE_SEPARATOR); //$NON-NLS-1$
		buffer.append(message);
		System.out.println(buffer.toString());
	}

	public static String formatArray(Object[] array, boolean toString, boolean newLines) {
		if (array == null || array.length == 0)
			return "[]"; //$NON-NLS-1$

		StringBuilder buffer = new StringBuilder();
		buffer.append('[');
		int i = 0;
		for (;;) {
			if (toString)
				buffer.append(array[i].toString());
			else
				buffer.append(array[i].getClass().getName());
			i++;
			if (i == array.length)
				break;
			buffer.append(',');
			if (newLines)
				buffer.append(DebugHelper.LINE_SEPARATOR);
			else
				buffer.append(' ');
		}
		buffer.append(']');
		return buffer.toString();
	}

	public static String formatMap(Map<?, ?> map, boolean toString, boolean newLines) {
		if (map == null || map.size() == 0)
			return "[]"; //$NON-NLS-1$

		StringBuilder buffer = new StringBuilder();
		buffer.append('[');
		for (Entry<?, ?> e : map.entrySet()) {
			buffer.append(e.getKey());
			buffer.append('=');
			if (toString)
				buffer.append(e.getValue().toString());
			else
				buffer.append(e.getValue().getClass().getName());

			buffer.append(',');
			if (newLines) {
				buffer.append(DebugHelper.LINE_SEPARATOR);
				buffer.append("    "); //$NON-NLS-1$
			} else
				buffer.append(' ');
		}
		buffer.append(']');
		return buffer.toString();
	}

}

Back to the top