xref: /aosp_15_r20/external/jazzer-api/src/main/java/com/code_intelligence/jazzer/utils/Log.java (revision 33edd6723662ea34453766bfdca85dbfdd5342b8)
1 /*
2  * Copyright 2023 Code Intelligence GmbH
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 
17 package com.code_intelligence.jazzer.utils;
18 
19 import java.io.PrintStream;
20 
21 /**
22  * Provides static functions that should be used for any kind of output (structured or unstructured)
23  * emitted by the fuzzer.
24  *
25  * <p>Output is printed to {@link System#err} and {@link System#out} until {@link
26  * Log#fixOutErr(PrintStream, PrintStream)} is called, which locks in the {@link PrintStream}s to be
27  * used from there on.
28  */
29 public class Log {
30   // Don't use directly, always use getOut() and getErr() instead - when these fields haven't been
31   // set yet, we want to resolve them dynamically as System.out and System.err, which may change
32   // over the course of the fuzzer's lifetime.
33   private static PrintStream fixedOut;
34   private static PrintStream fixedErr;
35 
36   /**
37    * The {@link PrintStream}s to use for all output from this call on.
38    */
fixOutErr(PrintStream out, PrintStream err)39   public static void fixOutErr(PrintStream out, PrintStream err) {
40     if (out == null) {
41       throw new IllegalArgumentException("out must not be null");
42     }
43     if (err == null) {
44       throw new IllegalArgumentException("err must not be null");
45     }
46     Log.fixedOut = out;
47     Log.fixedErr = err;
48   }
49 
println(String message)50   public static void println(String message) {
51     getErr().println(message);
52   }
53 
structuredOutput(String output)54   public static void structuredOutput(String output) {
55     getOut().println(output);
56   }
57 
info(String message)58   public static void info(String message) {
59     println("INFO: ", message, null);
60   }
61 
warn(String message)62   public static void warn(String message) {
63     warn(message, null);
64   }
65 
warn(String message, Throwable t)66   public static void warn(String message, Throwable t) {
67     println("WARN: ", message, t);
68   }
69 
error(String message)70   public static void error(String message) {
71     error(message, null);
72   }
73 
error(Throwable t)74   public static void error(Throwable t) {
75     error(null, t);
76   }
77 
error(String message, Throwable t)78   public static void error(String message, Throwable t) {
79     println("ERROR: ", message, t);
80   }
81 
finding(Throwable t)82   public static void finding(Throwable t) {
83     println("\n== Java Exception: ", null, t);
84   }
85 
println(String prefix, String message, Throwable t)86   private static void println(String prefix, String message, Throwable t) {
87     PrintStream err = getErr();
88     err.print(prefix);
89     if (message != null) {
90       err.println(message + (t != null ? ":" : ""));
91     }
92     if (t != null) {
93       t.printStackTrace(err);
94     }
95   }
96 
getOut()97   private static PrintStream getOut() {
98     return fixedOut != null ? fixedOut : System.out;
99   }
100 
getErr()101   private static PrintStream getErr() {
102     return fixedErr != null ? fixedErr : System.err;
103   }
104 }
105