-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new Debug API with redaction.
Implement emittingSingleLine TextFormat printer option. PiperOrigin-RevId: 633672722
- Loading branch information
1 parent
5a91707
commit bb68eb2
Showing
5 changed files
with
371 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
java/core/src/main/java/com/google/protobuf/DebugFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.google.protobuf; | ||
|
||
import com.google.protobuf.Descriptors.FieldDescriptor; | ||
|
||
/** | ||
* Provides an explicit API for unstable, redacting debug output suitable for debug logging. This | ||
* implementation is based on TextFormat, but should not be parsed. | ||
*/ | ||
public final class DebugFormat { | ||
|
||
private final boolean isSingleLine; | ||
|
||
private DebugFormat(boolean singleLine) { | ||
isSingleLine = singleLine; | ||
} | ||
|
||
public static DebugFormat singleLine() { | ||
return new DebugFormat(true); | ||
} | ||
|
||
public static DebugFormat multiline() { | ||
return new DebugFormat(false); | ||
} | ||
|
||
public String toString(MessageOrBuilder message) { | ||
return TextFormat.printer() | ||
.emittingSingleLine(this.isSingleLine) | ||
.enablingSafeDebugFormat(true) | ||
.printToString(message); | ||
} | ||
|
||
public String toString(FieldDescriptor field, Object value) { | ||
return TextFormat.printer() | ||
.emittingSingleLine(this.isSingleLine) | ||
.enablingSafeDebugFormat(true) | ||
.printFieldToString(field, value); | ||
} | ||
|
||
public String toString(UnknownFieldSet fields) { | ||
return TextFormat.printer() | ||
.emittingSingleLine(this.isSingleLine) | ||
.enablingSafeDebugFormat(true) | ||
.printToString(fields); | ||
} | ||
|
||
public Object lazyToString(MessageOrBuilder message) { | ||
return new LazyDebugOutput(message, this); | ||
} | ||
|
||
public Object lazyToString(UnknownFieldSet fields) { | ||
return new LazyDebugOutput(fields, this); | ||
} | ||
|
||
private static class LazyDebugOutput { | ||
private final MessageOrBuilder message; | ||
private final UnknownFieldSet fields; | ||
private final DebugFormat format; | ||
|
||
LazyDebugOutput(MessageOrBuilder message, DebugFormat format) { | ||
this.message = message; | ||
this.fields = null; | ||
this.format = format; | ||
} | ||
|
||
LazyDebugOutput(UnknownFieldSet fields, DebugFormat format) { | ||
this.message = null; | ||
this.fields = fields; | ||
this.format = format; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (message != null) { | ||
return format.toString(message); | ||
} | ||
return format.toString(fields); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.