Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/systemtests/TestLoggingHelper.java

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.qpid.proton.systemtests;
20
21 import java.nio.ByteBuffer;
22 import java.util.logging.Logger;
23
24 /**
25 * Provides functions for Proton tests to produce readable logging.
26 * Uses terminal colours if system property {@value #PROTON_TEST_TERMINAL_COLOURS_PROPERTY}
27 * is true
28 */
29 public class TestLoggingHelper
30 {
31 public static final String PROTON_TEST_TERMINAL_COLOURS_PROPERTY = "proton.test.terminal.colours";
32
33 private static final String COLOUR_RESET;
34 private static final String SERVER_COLOUR;
35 private static final String CLIENT_COLOUR;
36 private static final String BOLD;
37
38 static
39 {
40 if(Boolean.getBoolean(PROTON_TEST_TERMINAL_COLOURS_PROPERTY))
41 {
42 BOLD = "\033[1m";
43 SERVER_COLOUR = "\033[34m"; // blue
44 CLIENT_COLOUR = "\033[32m"; // green
45 COLOUR_RESET = "\033[0m";
46 }
47 else
48 {
49 BOLD = SERVER_COLOUR = CLIENT_COLOUR = COLOUR_RESET = "";
50 }
51 }
52
53 public static final String CLIENT_PREFIX = CLIENT_COLOUR + "CLIENT" + COLOUR_RESET;
54 public static final String SERVER_PREFIX = SERVER_COLOUR + "SERVER" + COLOUR_RESET;
55 public static final String MESSAGE_PREFIX = "MESSAGE";
56
57
58 private final BinaryFormatter _binaryFormatter = new BinaryFormatter();
59 private Logger _logger;
60
61 public TestLoggingHelper(Logger logger)
62 {
63 _logger = logger;
64 }
65
66 public void prettyPrint(String prefix, byte[] bytes)
67 {
68 _logger.fine(prefix + " " + bytes.length + " byte(s) " + _binaryFormatter.format(bytes));
69 }
70
71 /**
72 * Note that ByteBuffer is assumed to be readable. Its state is unchanged by this operation.
73 */
74 public void prettyPrint(String prefix, ByteBuffer buf)
75 {
76 byte[] bytes = new byte[buf.remaining()];
77 buf.duplicate().get(bytes);
78 prettyPrint(prefix, bytes);
79 }
80
81 public static String bold(String string)
82 {
83 return BOLD + string + TestLoggingHelper.COLOUR_RESET;
84 }
85
86
87 }