001package com.github.theholywaffle.teamspeak3.log;
002
003/*
004 * #%L
005 * TeamSpeak 3 Java API
006 * %%
007 * Copyright (C) 2014 Bert De Geyter
008 * %%
009 * Permission is hereby granted, free of charge, to any person obtaining a copy
010 * of this software and associated documentation files (the "Software"), to deal
011 * in the Software without restriction, including without limitation the rights
012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
013 * copies of the Software, and to permit persons to whom the Software is
014 * furnished to do so, subject to the following conditions:
015 * 
016 * The above copyright notice and this permission notice shall be included in
017 * all copies or substantial portions of the Software.
018 * 
019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
025 * THE SOFTWARE.
026 * #L%
027 */
028
029import java.io.BufferedWriter;
030import java.io.File;
031import java.io.FileWriter;
032import java.io.IOException;
033import java.io.PrintWriter;
034import java.text.DateFormat;
035import java.text.SimpleDateFormat;
036import java.util.Date;
037import java.util.logging.Handler;
038import java.util.logging.Level;
039import java.util.logging.LogRecord;
040
041public class LogHandler extends Handler {
042
043        private static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // ISO 8601
044        private final boolean writeToFile;
045        private final PrintWriter fileWriter;
046
047        public LogHandler(boolean writeToFile) {
048                boolean toFile = writeToFile;
049                PrintWriter out = null;
050
051                if (toFile) {
052                        try {
053                                File log = new File("teamspeak.log");
054                                if (!log.exists()) {
055                                        log.createNewFile();
056                                }
057                                out = new PrintWriter(new BufferedWriter(new FileWriter(log, true)), true);
058                        } catch (IOException io) {
059                                System.err.println("Could not set log handler! Using System.out instead");
060                                io.printStackTrace();
061                                toFile = false;
062                        }
063                }
064
065                this.writeToFile = toFile;
066                this.fileWriter = out;
067        }
068
069        @Override
070        public void close() {
071                if (writeToFile) {
072                        fileWriter.flush();
073                        fileWriter.close();
074                }
075        }
076
077        @Override
078        public void flush() {
079                if (writeToFile) {
080                        fileWriter.flush();
081                }
082        }
083
084        @Override
085        public void publish(LogRecord record) {
086                StringBuilder logMessage = new StringBuilder();
087
088                // Start the log message with a timestamp
089                logMessage.append("[").append(format.format(new Date())).append("] ");
090
091                // Include the severity if we're dealing with a warning or an error
092                if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
093                        logMessage.append("[").append(record.getLevel()).append("] ");
094                }
095
096                logMessage.append(record.getMessage());
097
098                // Write to System.out and, if enabled, to a log file
099                System.out.println(logMessage);
100                if (writeToFile) {
101                        fileWriter.println(logMessage);
102                }
103        }
104}