001    /*
002     * Copyright (C) 2006-2010 Enrique Lara (k957@68k.org)
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006      * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package net.sf.jmorse.logging;
018    
019    import net.sf.jmorse.AudioMorseWriter;
020    
021    import java.util.logging.ErrorManager;
022    import java.util.logging.Formatter;
023    import java.util.logging.Handler;
024    import java.util.logging.Level;
025    import java.util.logging.LogManager;
026    import java.util.logging.LogRecord;
027    
028    public class AudioMorseHandler extends Handler {
029        AudioMorseWriter morse = new AudioMorseWriter();
030    
031    // --------------------------------------------------- Properties
032        public int getVolume() {
033            return morse.getVolume();
034        }
035    
036        public int getTone() {
037            return morse.getTone();
038        }
039    
040        public int getWpm() {
041            return morse.getWpm();
042        }
043    
044        public void setVolume(int volume) {
045            morse.setVolume(volume);
046        }
047    
048        public void setTone(int tone) {
049            morse.setTone(tone);
050        }
051    
052        public void setWpm(int wpm) {
053            morse.setWpm(wpm);
054        }
055    
056        public AudioMorseHandler() {
057            setFormatter(new PassThroughFormatter());
058            configure();
059        }
060    
061        class PassThroughFormatter extends Formatter {
062            public String format(LogRecord record) {
063                return super.formatMessage(record);
064            }
065        }
066    
067        public void configure() {
068            LogManager manager = LogManager.getLogManager();
069    
070            String cname = AudioMorseHandler.class.getName();
071    
072            int wpm = getIntProperty(manager, cname + ".wpm", getWpm());
073            int volume = getIntProperty(manager, cname + ".volume", getVolume());
074            int tone = getIntProperty(manager, cname + ".tone", getTone());
075    
076            String level = manager.getProperty(cname + ".level");
077    
078            if (isNotEmpty(level)) {
079                setLevel(Level.parse(level.trim()));
080            }
081    
082            setWpm(wpm);
083            setVolume(volume);
084            setTone(tone);
085        }
086    
087        private int getIntProperty(LogManager manager, String var, int defValue) {
088            int rval = defValue;
089            String sValue = manager.getProperty(var);
090    
091            if (isNotEmpty(sValue)) {
092                rval = Integer.parseInt(sValue);
093            }
094    
095            return rval;
096        }
097    
098        /**
099         * @param sValue
100         * @return
101         */
102        private boolean isNotEmpty(String sValue) {
103            return (sValue != null) && (sValue.trim().length() > 0);
104        }
105    
106        public synchronized void publish(LogRecord record) {
107            if (!isLoggable(record)) {
108                return;
109            }
110    
111            String msg;
112    
113            try {
114                msg = getFormatter().format(record);
115            } catch (Exception ex) {
116                // We don't want to throw an exception here, but we
117                // report the exception to any registered ErrorManager.
118                reportError(null, ex, ErrorManager.FORMAT_FAILURE);
119    
120                return;
121            }
122    
123            try {
124    //                      System.out.println("MSG:" + msg);
125                morse.write(msg);
126            } catch (Exception ex) {
127                // We don't want to throw an exception here, but we
128                // report the exception to any registered ErrorManager.
129                reportError(null, ex, ErrorManager.WRITE_FAILURE);
130    
131                return;
132            }
133        }
134    
135        public void close() throws SecurityException {
136            //close
137        }
138    
139        public void flush() {
140            //flush
141        }
142    }