public final class ProgressLogger extends Object
This class provides a simple way to log progress information about long-lasting activities.
To use this class, you first create a new instance by passing a
Log4J logger, a Priority
and a time interval in millisecond (you can use constants such as ONE_MINUTE
).
Information will be logged about the current state of affairs no more often than the given
time interval. The output of the logger depends on
the items name (the name that will be used to denote counted
items), which can be changed at any time.
To log the progress of an activity, you call start(CharSequence)
at the beginning, which will
display the given string. Then, each time you want to mark progress, you call update()
or lightUpdate()
.
The latter methods increase the item counter, and will log progress information if enough time
has passed since the last log (and if the counter is a multiple of 2log2Modulus
, in the case of lightUpdate()
).
When the activity is over, you call stop()
. At that point, the method toString()
returns
information about the internal state of the logger (elapsed time, number of items per second) that
can be printed or otherwise processed. If update()
has never been called, you will just
get the elapsed time. By calling done()
instead of stop, this information will be logged for you.
Additionally, by setting the expected amount of updates before
calling start()
you can get some estimations on the completion time.
After you finished a run of the progress logger,
you can change its attributes and call start()
again
to measure another activity.
A typical call sequence to a progress logger is as follows:
ProgressLogger pl = new ProgressLogger( logger, ProgressLogger.ONE_MINUTE ); pl.start("Smashing pumpkins..."); ... activity on pumpkins that calls update() on each pumpkin ... pl.done();
A more flexible behaviour can be obtained at the end of the
process by calling stop()
:
ProgressLogger pl = new ProgressLogger( logger, ProgressLogger.ONE_MINUTE, "pumpkins" ); pl.start("Smashing pumpkins..."); ... activity on pumpkins that calls update() on each pumpkin ... pl.stop( "Really done!" ); pl.logger.log( pl.priority, pm );
Should you need to display additional information, you can set the field info
to any
object: it will be printed just after the timing (and possibly memory) information.
Note that the Log4J logger and
priority are available via the public fields logger
and
priority
: this makes it possible to pass around a progress logger and log additional information on
the same logging stream. The priority is initialised to Level.INFO
, but it can be set at any time.
Modifier and Type | Field and Description |
---|---|
long |
count
|
static long |
DEFAULT_LOG_INTERVAL |
int |
DEFAULT_LOG2_MODULUS
The default
log2Modulus for lightUpdate() . |
boolean |
displayFreeMemory
Whether to display the free memory at each progress log (default: false).
|
long |
expectedUpdates
The number of expected calls to
update() (used to compute the percentages, ignored if negative). |
Object |
info
If non-
null , this object will be printed after the timinig information. |
String |
itemsName
The name of several counted items.
|
int |
log2Modulus
If nonzero, calls to
lightUpdate() will cause a call to
System.currentTimeMillis() only if the current value of count
is a multiple of 2 raised to this power. |
org.apache.log4j.Logger |
logger
The logger used by this progress logger.
|
long |
logInterval
The time interval for a new log in milliseconds.
|
int |
modulus
Deprecated.
Replaced by
log2Modulus , which avoids the very slow modulus operator at each lightUpdate() call. |
static long |
ONE_HOUR |
static long |
ONE_MINUTE |
static long |
ONE_SECOND |
org.apache.log4j.Level |
priority
The priority used by this progress logger.
|
static long |
TEN_MINUTES |
static long |
TEN_SECONDS |
Constructor and Description |
---|
ProgressLogger()
Creates a new progress logger using items as items name and logging every
DEFAULT_LOG_INTERVAL milliseconds with
to the root logger. |
ProgressLogger(org.apache.log4j.Logger logger)
Creates a new progress logger using items as items name and logging every
DEFAULT_LOG_INTERVAL milliseconds. |
ProgressLogger(org.apache.log4j.Logger logger,
long logInterval)
Creates a new progress logger using items as items name.
|
ProgressLogger(org.apache.log4j.Logger logger,
long logInterval,
String itemsName)
Creates a new progress logger.
|
ProgressLogger(org.apache.log4j.Logger logger,
String itemsName)
Creates a new progress logger logging every
DEFAULT_LOG_INTERVAL milliseconds. |
Modifier and Type | Method and Description |
---|---|
void |
done()
Completes a run of this progress logger, logging Completed. and the logger itself.
|
void |
lightUpdate()
Updates the progress logger in a lightweight fashion.
|
long |
millis()
|
void |
start()
Starts the progress logger, resetting the count.
|
void |
start(CharSequence message)
Starts the progress logger, displaying a message and resetting the count.
|
void |
stop()
Stops the progress logger.
|
void |
stop(CharSequence message)
Stops the progress logger, displaying a message.
|
String |
toString()
Converts the data stored in this progress logger to a string.
|
void |
update()
Updates the progress logger.
|
public static final long ONE_SECOND
public static final long TEN_SECONDS
public static final long ONE_MINUTE
public static final long TEN_MINUTES
public static final long ONE_HOUR
public static final long DEFAULT_LOG_INTERVAL
public final int DEFAULT_LOG2_MODULUS
log2Modulus
for lightUpdate()
.public final org.apache.log4j.Logger logger
public org.apache.log4j.Level priority
public long logInterval
@Deprecated public int modulus
log2Modulus
, which avoids the very slow modulus operator at each lightUpdate()
call.public int log2Modulus
lightUpdate()
will cause a call to
System.currentTimeMillis()
only if the current value of count
is a multiple of 2 raised to this power.public Object info
null
, this object will be printed after the timinig information.public long count
public long expectedUpdates
update()
(used to compute the percentages, ignored if negative).public String itemsName
public boolean displayFreeMemory
public ProgressLogger()
DEFAULT_LOG_INTERVAL
milliseconds with
to the root logger.public ProgressLogger(org.apache.log4j.Logger logger)
DEFAULT_LOG_INTERVAL
milliseconds.logger
- the logger to which messages will be sent.public ProgressLogger(org.apache.log4j.Logger logger, String itemsName)
DEFAULT_LOG_INTERVAL
milliseconds.logger
- the logger to which messages will be sent.itemsName
- a plural name denoting the counted items.public ProgressLogger(org.apache.log4j.Logger logger, long logInterval)
logger
- the logger to which messages will be sent.logInterval
- the logging interval in milliseconds.public ProgressLogger(org.apache.log4j.Logger logger, long logInterval, String itemsName)
logger
- the logger to which messages will be sent.logInterval
- the logging interval in milliseconds.itemsName
- a plural name denoting the counted items.public void update()
This call updates the progress logger internal count. If enough time has passed since the last log, information will be logged.
This method is kept intentionally short (it delegates most of the work to an internal
private method) so to suggest inlining. However, it performs a call to System.currentTimeMillis()
that takes microseconds (not nanoseconds). If you plan on calling this method more than a
few thousands times per second, you should use lightUpdate()
.
public final void lightUpdate()
This call updates the progress logger internal counter as update()
. However,
it will actually call System.currentTimeMillis()
only if the new count
is a multiple of 2log2Modulus
. This mechanism makes it possible to reduce the number of
calls to System.currentTimeMillis()
arbitrarily.
This method is useful when the operations being counted take less than a few microseconds.
update()
public void start(CharSequence message)
message
- the message to display.public void start()
public void stop(CharSequence message)
This method will also mark expectedUpdates
as invalid,
to avoid erroneous reuses of previous values.
message
- the message to display.public void stop()
public void done()
public long millis()
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.