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.io.StringWriter;
035import java.text.DateFormat;
036import java.text.SimpleDateFormat;
037import java.util.Date;
038import java.util.logging.Handler;
039import java.util.logging.Level;
040import java.util.logging.LogRecord;
041
042public class LogHandler extends Handler {
043
044        private static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // ISO 8601
045        private final boolean writeToFile;
046        private final PrintWriter fileWriter;
047
048        public LogHandler(boolean writeToFile) {
049                boolean toFile = writeToFile;
050                PrintWriter out = null;
051
052                if (toFile) {
053                        try {
054                                File log = new File("teamspeak.log");
055                                if (!log.exists()) {
056                                        log.createNewFile();
057                                }
058                                out = new PrintWriter(new BufferedWriter(new FileWriter(log, true)), true);
059                        } catch (IOException io) {
060                                System.err.println("Could not set log handler! Using System.out instead");
061                                io.printStackTrace();
062                                toFile = false;
063                        }
064                }
065
066                this.writeToFile = toFile;
067                this.fileWriter = out;
068        }
069
070        @Override
071        public void close() {
072                if (writeToFile) {
073                        fileWriter.flush();
074                        fileWriter.close();
075                }
076        }
077
078        @Override
079        public void flush() {
080                if (writeToFile) {
081                        fileWriter.flush();
082                }
083        }
084
085        @Override
086        public void publish(LogRecord record) {
087                StringBuilder logMessage = new StringBuilder();
088
089                // Start the log message with a timestamp
090                logMessage.append("[").append(format.format(new Date())).append("] ");
091
092                // Include the severity if we're dealing with a warning or an error
093                if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
094                        logMessage.append("[").append(record.getLevel()).append("] ");
095                }
096
097                logMessage.append(record.getMessage());
098
099                Throwable t = record.getThrown();
100                if (t != null) {
101                        logMessage.append("\n");
102                        StringWriter stackTrace = new StringWriter();
103                        t.printStackTrace(new PrintWriter(stackTrace));
104                        logMessage.append(stackTrace.getBuffer());
105                }
106
107                // Write to System.out and, if enabled, to a log file
108                System.out.println(logMessage);
109                if (writeToFile) {
110                        fileWriter.println(logMessage);
111                }
112        }
113}