Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
60437397
authored
Jul 13, 2022
by
Rohit Singh
Browse files
Options
_('Browse Files')
Download
Plain Diff
Merge pull request #10185 from TiVo:p-custom-logger
PiperOrigin-RevId: 460689252
parents
e0b46ece
40bb0eeb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
130 additions
and
26 deletions
library/common/src/main/java/com/google/android/exoplayer2/util/Log.java
library/common/src/main/java/com/google/android/exoplayer2/util/Log.java
View file @
60437397
...
@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.util;
...
@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.util;
import
static
java
.
lang
.
annotation
.
ElementType
.
TYPE_USE
;
import
static
java
.
lang
.
annotation
.
ElementType
.
TYPE_USE
;
import
android.text.TextUtils
;
import
android.text.TextUtils
;
import
androidx.annotation.GuardedBy
;
import
androidx.annotation.IntDef
;
import
androidx.annotation.IntDef
;
import
androidx.annotation.Nullable
;
import
androidx.annotation.Nullable
;
import
androidx.annotation.Size
;
import
androidx.annotation.Size
;
...
@@ -28,7 +29,10 @@ import java.lang.annotation.Target;
...
@@ -28,7 +29,10 @@ import java.lang.annotation.Target;
import
java.net.UnknownHostException
;
import
java.net.UnknownHostException
;
import
org.checkerframework.dataflow.qual.Pure
;
import
org.checkerframework.dataflow.qual.Pure
;
/** Wrapper around {@link android.util.Log} which allows to set the log level. */
/**
* Wrapper around {@link android.util.Log} which allows to set the log level and to specify a custom
* log output.
*/
public
final
class
Log
{
public
final
class
Log
{
/**
/**
...
@@ -51,15 +55,89 @@ public final class Log {
...
@@ -51,15 +55,89 @@ public final class Log {
/** Log level to disable all logging. */
/** Log level to disable all logging. */
public
static
final
int
LOG_LEVEL_OFF
=
Integer
.
MAX_VALUE
;
public
static
final
int
LOG_LEVEL_OFF
=
Integer
.
MAX_VALUE
;
/**
* Interface for a logger that can output messages with a tag.
*
* <p>Use {@link #DEFAULT} to output to {@link android.util.Log}.
*/
public
interface
Logger
{
/** The default instance logging to {@link android.util.Log}. */
Logger
DEFAULT
=
new
Logger
()
{
@Override
public
void
d
(
String
tag
,
String
message
)
{
android
.
util
.
Log
.
d
(
tag
,
message
);
}
@Override
public
void
i
(
String
tag
,
String
message
)
{
android
.
util
.
Log
.
i
(
tag
,
message
);
}
@Override
public
void
w
(
String
tag
,
String
message
)
{
android
.
util
.
Log
.
w
(
tag
,
message
);
}
@Override
public
void
e
(
String
tag
,
String
message
)
{
android
.
util
.
Log
.
e
(
tag
,
message
);
}
};
/**
* Logs a debug-level message.
*
* @param tag The tag of the message.
* @param message The message.
*/
void
d
(
String
tag
,
String
message
);
/**
* Logs an information-level message.
*
* @param tag The tag of the message.
* @param message The message.
*/
void
i
(
String
tag
,
String
message
);
/**
* Logs a warning-level message.
*
* @param tag The tag of the message.
* @param message The message.
*/
void
w
(
String
tag
,
String
message
);
/**
* Logs an error-level message.
*
* @param tag The tag of the message.
* @param message The message.
*/
void
e
(
String
tag
,
String
message
);
}
private
static
final
Object
lock
=
new
Object
();
@GuardedBy
(
"lock"
)
private
static
int
logLevel
=
LOG_LEVEL_ALL
;
private
static
int
logLevel
=
LOG_LEVEL_ALL
;
@GuardedBy
(
"lock"
)
private
static
boolean
logStackTraces
=
true
;
private
static
boolean
logStackTraces
=
true
;
@GuardedBy
(
"lock"
)
private
static
Logger
logger
=
Logger
.
DEFAULT
;
private
Log
()
{}
private
Log
()
{}
/** Returns current {@link LogLevel} for ExoPlayer logcat logging. */
/** Returns current {@link LogLevel} for ExoPlayer logcat logging. */
@Pure
@Pure
public
static
@LogLevel
int
getLogLevel
()
{
public
static
@LogLevel
int
getLogLevel
()
{
return
logLevel
;
synchronized
(
lock
)
{
return
logLevel
;
}
}
}
/**
/**
...
@@ -68,7 +146,9 @@ public final class Log {
...
@@ -68,7 +146,9 @@ public final class Log {
* @param logLevel The new {@link LogLevel}.
* @param logLevel The new {@link LogLevel}.
*/
*/
public
static
void
setLogLevel
(
@LogLevel
int
logLevel
)
{
public
static
void
setLogLevel
(
@LogLevel
int
logLevel
)
{
Log
.
logLevel
=
logLevel
;
synchronized
(
lock
)
{
Log
.
logLevel
=
logLevel
;
}
}
}
/**
/**
...
@@ -78,7 +158,20 @@ public final class Log {
...
@@ -78,7 +158,20 @@ public final class Log {
* @param logStackTraces Whether stack traces will be logged.
* @param logStackTraces Whether stack traces will be logged.
*/
*/
public
static
void
setLogStackTraces
(
boolean
logStackTraces
)
{
public
static
void
setLogStackTraces
(
boolean
logStackTraces
)
{
Log
.
logStackTraces
=
logStackTraces
;
synchronized
(
lock
)
{
Log
.
logStackTraces
=
logStackTraces
;
}
}
/**
* Sets a custom {@link Logger} as the output.
*
* @param logger The {@link Logger}.
*/
public
static
void
setLogger
(
Logger
logger
)
{
synchronized
(
lock
)
{
Log
.
logger
=
logger
;
}
}
}
/**
/**
...
@@ -86,8 +179,10 @@ public final class Log {
...
@@ -86,8 +179,10 @@ public final class Log {
*/
*/
@Pure
@Pure
public
static
void
d
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
public
static
void
d
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
if
(
logLevel
==
LOG_LEVEL_ALL
)
{
synchronized
(
lock
)
{
android
.
util
.
Log
.
d
(
tag
,
message
);
if
(
logLevel
==
LOG_LEVEL_ALL
)
{
logger
.
d
(
tag
,
message
);
}
}
}
}
}
...
@@ -104,8 +199,10 @@ public final class Log {
...
@@ -104,8 +199,10 @@ public final class Log {
*/
*/
@Pure
@Pure
public
static
void
i
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
public
static
void
i
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
if
(
logLevel
<=
LOG_LEVEL_INFO
)
{
synchronized
(
lock
)
{
android
.
util
.
Log
.
i
(
tag
,
message
);
if
(
logLevel
<=
LOG_LEVEL_INFO
)
{
logger
.
i
(
tag
,
message
);
}
}
}
}
}
...
@@ -122,8 +219,10 @@ public final class Log {
...
@@ -122,8 +219,10 @@ public final class Log {
*/
*/
@Pure
@Pure
public
static
void
w
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
public
static
void
w
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
if
(
logLevel
<=
LOG_LEVEL_WARNING
)
{
synchronized
(
lock
)
{
android
.
util
.
Log
.
w
(
tag
,
message
);
if
(
logLevel
<=
LOG_LEVEL_WARNING
)
{
logger
.
w
(
tag
,
message
);
}
}
}
}
}
...
@@ -140,8 +239,10 @@ public final class Log {
...
@@ -140,8 +239,10 @@ public final class Log {
*/
*/
@Pure
@Pure
public
static
void
e
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
public
static
void
e
(
@Size
(
max
=
23
)
String
tag
,
String
message
)
{
if
(
logLevel
<=
LOG_LEVEL_ERROR
)
{
synchronized
(
lock
)
{
android
.
util
.
Log
.
e
(
tag
,
message
);
if
(
logLevel
<=
LOG_LEVEL_ERROR
)
{
logger
.
e
(
tag
,
message
);
}
}
}
}
}
...
@@ -167,20 +268,23 @@ public final class Log {
...
@@ -167,20 +268,23 @@ public final class Log {
@Nullable
@Nullable
@Pure
@Pure
public
static
String
getThrowableString
(
@Nullable
Throwable
throwable
)
{
public
static
String
getThrowableString
(
@Nullable
Throwable
throwable
)
{
if
(
throwable
==
null
)
{
synchronized
(
lock
)
{
return
null
;
if
(
throwable
==
null
)
{
}
else
if
(
isCausedByUnknownHostException
(
throwable
))
{
return
null
;
// UnknownHostException implies the device doesn't have network connectivity.
}
else
if
(
isCausedByUnknownHostException
(
throwable
))
{
// UnknownHostException.getMessage() may return a string that's more verbose than desired for
// UnknownHostException implies the device doesn't have network connectivity.
// logging an expected failure mode. Conversely, android.util.Log.getStackTraceString has
// UnknownHostException.getMessage() may return a string that's more verbose than desired
// special handling to return the empty string, which can result in logging that doesn't
// for
// indicate the failure mode at all. Hence we special case this exception to always return a
// logging an expected failure mode. Conversely, android.util.Log.getStackTraceString has
// concise but useful message.
// special handling to return the empty string, which can result in logging that doesn't
return
"UnknownHostException (no network)"
;
// indicate the failure mode at all. Hence we special case this exception to always return a
}
else
if
(!
logStackTraces
)
{
// concise but useful message.
return
throwable
.
getMessage
();
return
"UnknownHostException (no network)"
;
}
else
{
}
else
if
(!
logStackTraces
)
{
return
android
.
util
.
Log
.
getStackTraceString
(
throwable
).
trim
().
replace
(
"\t"
,
" "
);
return
throwable
.
getMessage
();
}
else
{
return
android
.
util
.
Log
.
getStackTraceString
(
throwable
).
trim
().
replace
(
"\t"
,
" "
);
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment