Skip to content

Commit

Permalink
Adds support for logging trace logs to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalevan authored and Randy Farmer committed Jan 24, 2017
1 parent 1d2befe commit b789ae5
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions ServerCore/java/org/elkoserver/util/trace/TraceLog.java
Expand Up @@ -30,6 +30,9 @@ class TraceLog implements TraceMessageAcceptor {
/** Flag controlling whether a log file should be written at all. */
private boolean amWriteEnabled = false;

/** Flag controlling whether log messages should be sent to stderr. */
private boolean logToStderr = false;

/** Log file size above which the file will be rolled over, in chars. */
private long myMaxSize = STARTING_LOG_SIZE_THRESHOLD;

Expand Down Expand Up @@ -103,20 +106,24 @@ public synchronized void accept(TraceMessage message) {
private void outputMessage(TraceMessage message) {
message.stringify(myStringBuffer);
String output = myStringBuffer.toString();
myCurrent.stream.println(output);
/* Note: there's little point in checking for an output error. We
can't put the trace in the log, and there's little chance the user
would see it in the trace buffer. So we ignore it, with regret. */

myCurrentSize += output.length() + LINE_SEPARATOR_LENGTH;
if (myCurrentSize > myMaxSize) {
rolloverLogFile("This log is full.");
} else if (myNextRolloverTime != 0 &&
myNextRolloverTime < message.timestamp()) {
do {
myNextRolloverTime += myRolloverFrequency;
} while (myNextRolloverTime < message.timestamp());
rolloverLogFile("The time has come for a new log file.");
if (logToStderr) {
System.err.println(output);
} else {
myCurrent.stream.println(output);
/* Note: there's little point in checking for an output error. We
can't put the trace in the log, and there's little chance the user
would see it in the trace buffer. So we ignore it, with regret. */

myCurrentSize += output.length() + LINE_SEPARATOR_LENGTH;
if (myCurrentSize > myMaxSize) {
rolloverLogFile("This log is full.");
} else if (myNextRolloverTime != 0 &&
myNextRolloverTime < message.timestamp()) {
do {
myNextRolloverTime += myRolloverFrequency;
} while (myNextRolloverTime < message.timestamp());
rolloverLogFile("The time has come for a new log file.");
}
}
}

Expand Down Expand Up @@ -294,6 +301,13 @@ private void changeTag(String value) {
myPending.setTag(value);
}

/**
* Sets the tracing log to output to stderr if the user so desires.
*/
private void changeLogToStderr(String value) {
logToStderr = Boolean.parseBoolean(value);
}

/**
* The meaning of changeWrite is complicated. Here are the cases when it's
* used to turn writing ON.
Expand Down Expand Up @@ -510,6 +524,8 @@ public synchronized boolean setConfiguration(String name, String value) {
changeRollover(value);
} else if (name.equalsIgnoreCase("versions")) {
changeVersionFileHandling(value);
} else if (name.equalsIgnoreCase("stderr")) {
changeLogToStderr(value);
} else if (name.equalsIgnoreCase("reopen")) {
reopen(value);
} else {
Expand Down

0 comments on commit b789ae5

Please sign in to comment.