[sal~src:11] Creation of log actor
- From: therealbean@kenai.com
- To: commits@sal.kenai.com
- Subject: [sal~src:11] Creation of log actor
- Date: Sat, 21 Nov 2009 07:40:16 +0000
Project: sal
Repository: src
Revision: 11
Author: therealbean
Date: 2009-11-21 07:40:13 UTC
Link:
Log Message:
------------
Creation of log actor
Revisions:
----------
11
Added Paths:
------------
org/pz/sal/commonActors/log.java
Diffs:
------
Index: org/pz/sal/commonActors/log.java
===================================================================
--- org/pz/sal/commonActors/log.java (revision 0)
+++ org/pz/sal/commonActors/log.java (revision 11)
@@ -0,0 +1,83 @@
+/***
+ * Sal: Simple Actor Library (available free at sal.pz.org)
+ *
+ * Sal is (c) Copyright 2009 Pacific Data Works LLC. All Rights Reserved.
+ * Licensed under Apache License 2.0
(http://www.apache.org/licenses/LICENSE-2.0.html)
+ */
+
+package org.pz.sal.commonActors;
+
+import org.pz.sal.*;
+
+import java.io.*;
+
+/**
+ * Simple actor that logs strings to a log file or stdout
+ *
+ * @author alb
+ */
+public class log extends Actor
+{
+ private File logfile = null;
+ private BufferedWriter writer = null;
+
+ public log( final String assignedName )
+ {
+ super( assignedName );
+ }
+
+ @Override
+ public void processMessage( final Object o )
+ {
+ assert( o != null );
+
+ String msg;
+
+ if( o instanceof String ) {
+ processString( (String) o );
+ }
+ else
+ if ( o instanceof File ) {
+ setupLogfile( (File) o );
+
+ }
+ }
+
+ private void setupLogfile( final File f )
+ {
+ try {
+ writer = new BufferedWriter(new FileWriter( logfile ));
+ }
+ catch( IOException ioe ) {
+ logfile = null;
+ }
+ }
+
+ private void processString( final String string )
+ {
+ if( string.isEmpty() ) {
+ return;
+ }
+
+ if( string.equals( Commands.SHUTDOWN )) {
+ super.shutdown();
+ return;
+ }
+
+ if( logfile == null ) {
+ System.out.println( string );
+ }
+ else {
+ try {
+ writer.write( string + "\n" );
+ }
+ catch( IOException ioe ) {
+ logfile = null;
+ processString( "Error writing to logfile. Closing logfile."
);
+ processString( string ); //>>>> need to close file
+ }
+ }
+ }
+}
+
+
|
[sal~src:11] Creation of log actor |
therealbean | 11/21/2009 |





