001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
003    //
004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
006    // either version 3 of the License, or (at your option) any later version.
007    //
008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
010    // PURPOSE. See the GNU Lesser General Public License for more details.
011    //
012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
013    // Java Packages. If not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.echocomp;
016    
017    import java.io.DataOutputStream;
018    import java.io.File;
019    import java.io.FileOutputStream;
020    import java.io.IOException;
021    import java.util.Calendar;
022    
023    /**
024     * This class is used to log the events on the server.
025     * 
026     * @author Tobias Kuhn
027     */
028    public class Logger {
029            
030            private final String fileName;
031            private String username;
032            private final int sessionID;
033            
034            /**
035             * Creates a new logger instance for the given file, user name, and session id.
036             * 
037             * @param fileName The name of the log file.
038             * @param username The user name.
039             * @param sessionID The session id.
040             */
041            public Logger(String fileName, String username, int sessionID) {
042                    this.fileName = fileName;
043                    this.username = username;
044                    this.sessionID = sessionID;
045            }
046            
047            /**
048             * Creates a new logger instance for the given file and session id.
049             * 
050             * @param fileName The name of the log file.
051             * @param sessionID The session id.
052             */
053            public Logger(String fileName, int sessionID) {
054                    this(fileName, "", sessionID);
055            }
056            
057            /**
058             * Sets the user name.
059             * 
060             * @param username The user name.
061             */
062            public void setUsername(String username) {
063                    this.username = username;
064            }
065            
066            /**
067             * Writes a log entry into the log file of the respective ontology.
068             * 
069             * @param type The type of the log entry.
070             * @param text The text of the log entry.
071             */
072            public void log(String type, String text) {
073                    log(fileName, username, sessionID, type, text);
074            }
075            
076            /**
077             * Writes a log entry into the specified log file.
078             * 
079             * @param fileName The name of the log file.
080             * @param userName The user name.
081             * @param sessionID The session id.
082             * @param type The type of the log entry.
083             * @param text The text of the log entry.
084             */
085            public static void log(String fileName, String userName, int sessionID, String type, String text) {
086                    Calendar c = Calendar.getInstance();
087                    long timestamp = System.currentTimeMillis();
088                    c.setTimeInMillis(timestamp);
089                    String year = c.get(Calendar.YEAR) + "";
090                    String month = makeString(c.get(Calendar.MONTH)+1, 2);
091                    String day = makeString(c.get(Calendar.DAY_OF_MONTH), 2);
092                    String hour = makeString(c.get(Calendar.HOUR_OF_DAY), 2);
093                    String min = makeString(c.get(Calendar.MINUTE), 2);
094                    String sec = makeString(c.get(Calendar.SECOND), 2);
095                    String millis = makeString(c.get(Calendar.MILLISECOND), 3);
096                    String dateTime = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + millis;
097                    String session = makeString(sessionID, 4);
098                    if (userName == null || userName.equals("")) {
099                            userName = "";
100                    } else {
101                            userName = " '" + userName + "'";
102                    }
103                    try {
104                            if (!(new File("logs")).exists()) (new File("logs")).mkdir();
105                            DataOutputStream out = new DataOutputStream(new FileOutputStream("logs/" + fileName + ".log", true));
106                            out.writeBytes(timestamp + " (" + dateTime + ") [" + session + "]" + userName + " [" + type + "] " + text.replaceAll("\\n", "~n") + "\n");
107                            out.flush();
108                            out.close();
109                    } catch (IOException ex) {
110                            ex.printStackTrace();
111                    }
112            }
113            
114            private static String makeString(int value, int size) {
115                    String s = value + "";
116                    while (s.length() < size) {
117                            s = "0" + s;
118                    }
119                    return s;
120            }
121    
122    }