Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,17 @@ private static void reportError(String message, LogData data) {
System.err.flush();
}

@SuppressWarnings({"GoodTime", "JavaUtilDate", "SimpleDateFormat"}) // JDK7, no Joda-Time.
@SuppressWarnings({"GoodTime", "JavaUtilDate", "SimpleDateFormat"}) // Android, no Joda-Time.
private static String formatTimestampIso8601(LogData data) {
// Sadly in JDK7, we don't have access to java.time and can't depend on things like Joda-Time.
// Sadly, external Android users might not have access to java.time.
// Also, we can't depend on things like Joda-Time.
Date timestamp = new Date(NANOSECONDS.toMillis(data.getTimestampNanos()));
// Use the system timezone here since we don't know how logger backends want to format dates.
// The only alternative is to always use UTC, but that may cause confusion to some users, and
// if users really want UTC, they can set that as the system timezone.
//
// ISO format from https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
// ISO format from
// https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/text/SimpleDateFormat.html.
// Note that ending with "SSSXXX" would be more correct, but Android does not support this until
// v24+ (https://developer.android.com/reference/java/text/SimpleDateFormat.html).
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.flogger.util.Checks.checkArgument;
import static com.google.common.flogger.util.Checks.checkNotNull;
import static java.lang.Math.floorMod;

import java.util.HashMap;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -179,12 +180,7 @@ public static final LogPerBucketingStrategy<Object> byHashCode(final int maxBuck
@Override
protected Object apply(Object key) {
// Modulo can return -ve values and we want a value in the range (0 <= modulo < maxBuckets).
// Note: Math.floorMod() is Java 8, so cannot be used here (yet) otherwise we would just do:
// return Math.floorMod(key.hashCode(), maxBuckets) - 128;
int modulo = key.hashCode() % maxBuckets;
// Can only be -ve if the hashcode was negative, and if so (-maxBuckets < modulo < 0).
// The following adds maxBuckets if modulo was negative, or zero (saves a branch).
modulo += (modulo >> 31) & maxBuckets;
int modulo = floorMod(key.hashCode(), maxBuckets);
// Subtract 128 from the modulo in order to take full advantage of the promised Integer
// cache in the JVM (ensuring up to 256 cached values). From java.lang.Integer#valueOf():
// ""This method will always cache values in the range -128 to 127 ...""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
*/
public abstract class ScopedLoggingContext {
/** A logging context which must be closed in the reverse order to which it was created. */
// If Flogger is bumped to JDK 1.7, this should be switched to AutoCloseable.
// TODO(cpovirk): Switch to AutoCloseable now that we require new enough versions of JDK+Android?
public interface LoggingContextCloseable extends Closeable {
// Overridden to remove the throws clause allowing simple try-with-resources use.
@Override
Expand Down Expand Up @@ -260,8 +260,8 @@ public final <R> R callUnchecked(Callable<R> c) {
/**
* Installs a new context based on the state of the builder. The caller is <em>required</em> to
* invoke {@link LoggingContextCloseable#close() close()} on the returned instances in the
* reverse order to which they were obtained. For JDK 1.7 and above, this is best achieved by
* using a try-with-resources construction in the calling code.
* reverse order to which they were obtained. This is best achieved by using a
* try-with-resources construction in the calling code.
*
* <pre>{@code
* try (LoggingContextCloseable ctx = ScopedLoggingContext.getInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
/* See LogContextTest.java for the vast majority of tests related to base logging behaviour. */
@RunWith(JUnit4.class)
public final class AbstractLoggerTest {
// Matches ISO 8601 date/time format.
// See: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
// Matches ISO 8601 date/time format. See:
// https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/text/SimpleDateFormat.html
private static final Pattern ISO_TIMESTAMP_PREFIX =
Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}\\:\\d{2}\\.\\d{3}[-+]\\d{4}: .*");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private abstract static class ScopedReference<T> {
return value.get();
}

// Note: If we could use Java 1.8 runtime libraries, this would just be "accumulateAndGet()",
// but gRPC is Java 1.7 compatible: https://github.com/grpc/grpc-java/blob/master/README.md
// Note: If we could use Android 24 libraries, this would just be "accumulateAndGet()",
// but gRPC supports Android 21: https://github.com/grpc/grpc-java/blob/master/README.md
final void mergeFrom(@Nullable T delta) {
if (delta != null) {
T current;
Expand Down