aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/code_intelligence/jazzer/utils/Log.java
blob: bccd3a32d84180a31a417e8a19b23b92693a76fc (plain)
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
/*
 * Copyright 2023 Code Intelligence GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.code_intelligence.jazzer.utils;

import java.io.PrintStream;

/**
 * Provides static functions that should be used for any kind of output (structured or unstructured)
 * emitted by the fuzzer.
 *
 * <p>Output is printed to {@link System#err} and {@link System#out} until {@link
 * Log#fixOutErr(PrintStream, PrintStream)} is called, which locks in the {@link PrintStream}s to be
 * used from there on.
 */
public class Log {
  // Don't use directly, always use getOut() and getErr() instead - when these fields haven't been
  // set yet, we want to resolve them dynamically as System.out and System.err, which may change
  // over the course of the fuzzer's lifetime.
  private static PrintStream fixedOut;
  private static PrintStream fixedErr;

  /**
   * The {@link PrintStream}s to use for all output from this call on.
   */
  public static void fixOutErr(PrintStream out, PrintStream err) {
    if (out == null) {
      throw new IllegalArgumentException("out must not be null");
    }
    if (err == null) {
      throw new IllegalArgumentException("err must not be null");
    }
    Log.fixedOut = out;
    Log.fixedErr = err;
  }

  public static void println(String message) {
    getErr().println(message);
  }

  public static void structuredOutput(String output) {
    getOut().println(output);
  }

  public static void info(String message) {
    println("INFO: ", message, null);
  }

  public static void warn(String message) {
    warn(message, null);
  }

  public static void warn(String message, Throwable t) {
    println("WARN: ", message, t);
  }

  public static void error(String message) {
    error(message, null);
  }

  public static void error(Throwable t) {
    error(null, t);
  }

  public static void error(String message, Throwable t) {
    println("ERROR: ", message, t);
  }

  public static void finding(Throwable t) {
    println("\n== Java Exception: ", null, t);
  }

  private static void println(String prefix, String message, Throwable t) {
    PrintStream err = getErr();
    err.print(prefix);
    if (message != null) {
      err.println(message + (t != null ? ":" : ""));
    }
    if (t != null) {
      t.printStackTrace(err);
    }
  }

  private static PrintStream getOut() {
    return fixedOut != null ? fixedOut : System.out;
  }

  private static PrintStream getErr() {
    return fixedErr != null ? fixedErr : System.err;
  }
}