<%--
  -	$RCSfile$
  -	$Revision$
  -	$Date$
  -
  - Copyright (C) 2004 Jive Software. All rights reserved.
  -
  - This software is published under the terms of the GNU Public License (GPL),
  - a copy of which is included in this distribution.
--%>

<%@ page import="java.io.*,
                 org.jivesoftware.util.*,
                 java.text.*,
                 org.jivesoftware.util.log.Logger,
                 org.jivesoftware.messenger.auth.UnauthorizedException,
                 org.jivesoftware.messenger.JiveGlobals,
                 org.jivesoftware.messenger.user.*,
                 java.util.*,
                 org.jivesoftware.admin.AdminPageBean"
%>

<%@ taglib uri="core" prefix="c"%>

<jsp:useBean id="pageinfo" scope="request" class="org.jivesoftware.admin.AdminPageBean" />

<jsp:useBean id="admin" class="org.jivesoftware.util.WebManager"  />
<% admin.init(request, response, session, application, out ); %>

<%!
    static final String ERROR = "error";
    static final String INFO = "info";
    static final String WARN = "warn";
    static final String DEBUG = "debug";
    static final String DEFAULT = ERROR;

    static final String ASCENDING = "asc";
    static final String DESCENDING = "desc";

    static final String[] LINES = {"50","100","250","500"};

    static final String[] REFRESHES = {"None","10","30","60","90"};

    private static HashMap parseCookie(Cookie cookie) {
        if (cookie == null || cookie.getValue() == null) {
            HashMap empty = new HashMap();
            return empty;
        }
        StringTokenizer tokenizer = new StringTokenizer(cookie.getValue(),"&");
        HashMap valueMap = new HashMap();
        while (tokenizer.hasMoreTokens()) {
            String tok = tokenizer.nextToken();
            int pos = tok.indexOf("=");
            if (pos > 0) {
                String name = tok.substring(0,pos);
                String value = tok.substring(pos+1,tok.length());
                valueMap.put(name,value);
            }
        }
        return valueMap;
    }

    private static void saveCookie(HttpServletResponse response, HashMap cookie) {
        StringBuffer buf = new StringBuffer();
        for (Iterator iter=cookie.keySet().iterator(); iter.hasNext();) {
            String name = (String)iter.next();
            String value = (String)cookie.get(name);
            buf.append(name).append("=").append(value);
            if (iter.hasNext()) {
                buf.append("&");
            }
        }
        Cookie newCookie = new Cookie("jiveforums.admin.logviewer",buf.toString());
        newCookie.setPath("/");
        newCookie.setMaxAge(60*60*24*30); // one month
        response.addCookie(newCookie);
    }

    private static HashMap getLogUpdate(HttpServletRequest request, HttpServletResponse response,
            File logDir)
    {
        // Get the cookie associated with the log files
        HashMap cookie = parseCookie(CookieUtils.getCookie(request,"jiveforums.admin.logviewer"));
        String[] logs = {"error", "info", "warn", "debug"};
        HashMap newCookie = new HashMap();
        HashMap updates = new HashMap();
        for (int i=0; i<logs.length; i++) {
            // Check for the value in the cookie:
            String key = logs[i] + ".size";
            long savedSize = 0L;
            if (cookie.containsKey(key)) {
                try {
                    savedSize = Long.parseLong((String)cookie.get(key));
                }
                catch (NumberFormatException nfe) {}
            }
            // Update the size in the Map:
            File logFile = new File(logDir, logs[i] + ".log");
            long currentSize = logFile.length();
            newCookie.put(key, ""+currentSize);
            if (currentSize != savedSize) {
                updates.put(logs[i], "true");
            }
        }
        saveCookie(response, newCookie);
        return updates;
    }
%>

<%
    // Get parameters
    String log = ParamUtils.getParameter(request, "log");
    String numLinesParam = ParamUtils.getParameter(request,"lines");
    int numLines = ParamUtils.getIntParameter(request,"lines",50);
    int refresh = ParamUtils.getIntParameter(request,"refresh",10);
    String refreshParam = ParamUtils.getParameter(request,"refresh");
    String mode = ParamUtils.getParameter(request,"mode");
    boolean clearLog = ParamUtils.getBooleanParameter(request,"clearLog");
    boolean markLog = ParamUtils.getBooleanParameter(request,"markLog");
    boolean saveLog = ParamUtils.getBooleanParameter(request,"saveLog");
    boolean emailLog = ParamUtils.getBooleanParameter(request,"emailLog");
    boolean debugEnabled = ParamUtils.getBooleanParameter(request,"debugEnabled");
    boolean wasDebugEnabled = ParamUtils.getBooleanParameter(request,"wasDebugEnabled");

    // Enable/disable debugging
    if (request.getParameter("wasDebugEnabled") != null && wasDebugEnabled != debugEnabled) {
        Log.setDebugEnabled(debugEnabled);
        response.sendRedirect("logviewer.jsp?log=debug");
        return;
    }

    debugEnabled = Log.isDebugEnabled();
    User pageUser = admin.getUser();

    if (clearLog && log != null) {
        if ("error".equals(log)) {
            Log.rotateErrorLogFile();
        }
        else if ("warn".equals(log)) {
            Log.rotateWarnLogFile();
        }
        else if ("info".equals(log)) {
            Log.rotateInfoLogFile();
        }
        else if ("debug".equals(log)) {
            Log.rotateDebugLogFile();
        }
        response.sendRedirect("logviewer.jsp?log=" + log);
        return;
    }
    else if (markLog && log != null) {
        if ("error".equals(log)) {
            Log.markErrorLogFile(pageUser);
        }
        else if ("warn".equals(log)) {
            Log.markWarnLogFile(pageUser);
        }
        else if ("info".equals(log)) {
            Log.markInfoLogFile(pageUser);
        }
        else if ("debug".equals(log)) {
            Log.markDebugLogFile(pageUser);
        }
        response.sendRedirect("logviewer.jsp?log=" + log);
        return;
    }
    else if (saveLog && log != null) {
        saveLog = false;
        response.sendRedirect(request.getContextPath() + "/servlet/JiveServlet/?log=" + log);
        return;
    }
    else if (emailLog && log != null) {
        response.sendRedirect("emaillog.jsp?log=" + log);
        return;
    }

    // Set defaults
    if (log == null) {
        log = DEFAULT;
    }
    if (mode == null) {
        mode = ASCENDING;
    }
    if (numLinesParam == null) {
        numLinesParam = "50";
    }

    // Other vars
    File logDir = new File(Log.getLogDirectory());
    String filename = log + ".log";
    File logFile = new File(logDir, filename);

    // Determine if any of the log files contents have been updated:
    HashMap newlogs = getLogUpdate(request, response, logDir);
%>

<%  // Title of this page and breadcrumbs
    String title = "Log Viewer";
    pageinfo.setTitle(title);
    pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "main.jsp"));
    pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(title, "logviewer.jsp?log=" + log));
    pageinfo.setPageID("server-logs");
%>
<jsp:include page="top.jsp" flush="true" />
<jsp:include page="title.jsp" flush="true" />

<%  if (refreshParam != null && !"None".equals(refreshParam)) { %>

    <meta http-equiv="refresh" content="<%= refresh %>">

<%  } %>

<div id="logviewer">

<style type="text/css">
SELECT, INPUT {
    font-family : verdana, arial;
    font-size : 8pt;
}
.date {
    color : #00f;
    border-width : 0px 0px 1px 0px;
    border-style : dotted;
    border-color : #00f;
}
.buttons TD {
    padding : 3px;
}
.buttons .icon-label {
    padding-right : 1em;
}
.log-info {
    border-width : 0px 1px 1px 1px;
    border-color : #ccc;
    border-style : solid;
}
IFRAME {
    border : 1px #666 solid;
}
</style>

<form action="logviewer.jsp" name="logViewer" method="get">
<input type="hidden" name="log" value="<%= log %>">

<div class="jive-tabs">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tbody>
    <tr>
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("error".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=error"
            >Error</a>
            <span class="new">
            <%= ((newlogs.containsKey("error"))?"*":"") %>
            </span>
        </td>
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("warn".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=warn"
            >Warn</a>
            <span class="new">
            <%= ((newlogs.containsKey("warn"))?"*":"") %>
            </span>
        </td>
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("info".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=info"
            >Info</a>
            <span class="new">
            <%= ((newlogs.containsKey("info"))?"*":"") %>
            </span>
        </td>
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("debug".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=debug"
            >Debug</a>
            <span class="new">
            <%= ((newlogs.containsKey("debug"))?"*":"") %>
            </span>
        </td>
        <td class="jive-stretch" width="92%" align="right" nowrap>
            &nbsp;
        </td>
    </tr>
</tbody>
</table>

<%  ByteFormat byteFormatter = new ByteFormat();
    Date lastMod = new Date(logFile.lastModified());
    DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
%>

<div class="log-info">
<table cellpadding="6" cellspacing="0" border="0" width="100%">
<tbody>
    <tr>
        <td>
            <table cellpadding="3" cellspacing="0" border="0" width="100%">
            <tr>
                <td nowrap>Log File:</td>
                <td nowrap><b><%= logFile.getName() %></b> (<%= byteFormatter.format(logFile.length()) %>)</td>
                <td width="96%" rowspan="3">&nbsp;</td>
                <td nowrap>Order:</td>
                <td nowrap>
                    <input type="radio" name="mode" value="asc"<%= ("asc".equals(mode)?" checked":"") %>
                     onclick="this.form.submit();" id="rb01"
                     ><label for="rb01">Normal</label>
                    <input type="radio" name="mode" value="desc"<%= ("desc".equals(mode)?" checked":"") %>
                     onclick="this.form.submit();" id="rb02"
                     ><label for="rb02">Reverse</label>
                </td>
            </tr>
            <tr>
                <td nowrap>Last Modified:</td>
                <td nowrap>
                    <span><%= dateFormatter.format(lastMod) %></span>
                </td>
                <td nowrap>Lines:</td>
                <td nowrap>
                    <select name="lines" size="1"
                     onchange="this.form.submit();">
                        <%  for (int j=0; j<LINES.length; j++) {
                                String selected = (LINES[j].equals(numLinesParam))?" selected":"";
                        %>
                            <option value="<%= LINES[j] %>"<%= selected %>><%= LINES[j] %>

                        <%  } %>
                            <option value="All"<%= (("All".equals(numLinesParam))?" selected":"") %>
                             >All
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <script language="JavaScript" type="text/javascript">
                        <!--
                        function setLog(log) {
                            document.logViewer.clearLog.value = 'false';
                            document.logViewer.markLog.value = 'false';
                            document.logViewer.saveLog.value = 'false';
                            document.logViewer.emailLog.value = 'false';

                            var t = eval("document.logViewer." + log);
                            t.value = 'true';
                        }
                        // -->
                    </script>
                    <input type="hidden" name="clearLog" value="false">
                    <input type="hidden" name="markLog" value="false">
                    <input type="hidden" name="saveLog" value="false">
                    <input type="hidden" name="emailLog" value="false">
                    <div class="buttons">
                    <table cellpadding="0" cellspacing="0" border="0">
                    <tbody>
                        <tr>
                            <td class="icon">
                                <a href="#" onclick="if (confirm('Are you sure you want to clear this log file?')) {setLog('clearLog'); document.logViewer.submit(); return true;} else { return false; }"><img src="images/delete-16x16.gif" border="0" alt="Clear Log"></a>
                            </td>
                            <td class="icon-label">
                                <a href="#" onclick="if (confirm('Are you sure you want to clear this log file?')) {setLog('clearLog'); document.logViewer.submit(); return true;} else { return false; }"
                                 >Clear</a>
                            </td>
                            <td class="icon">
                                <a href="#" onclick="setLog('markLog'); document.logViewer.submit(); return true;"><img src="images/mark-16x16.gif" border="0" alt="Mark Log"></a>
                            </td>
                            <td class="icon-label">
                                <a href="#" onclick="setLog('markLog'); document.logViewer.submit(); return true;"
                                 >Mark</a>
                            </td>
                        </tr>
                    </tbody>
                    </table>
                    </div>
                </td>
                <td nowrap>Refresh:</td>
                <td nowrap>
                    <select size="1" name="refresh" onchange="this.form.submit();">
                    <%  for (int j=0; j<REFRESHES.length; j++) {
                            String selected = REFRESHES[j].equals(refreshParam)?" selected":"";
                    %>
                        <option value="<%= REFRESHES[j] %>"<%= selected %>><%= REFRESHES[j] %>

                    <%  } %>
                    </select>
                    (seconds)
                </td>
            </tr>

            <%  if ("debug".equals(log)) { %>

                <tr>
                    <td colspan="5">

                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                        <tr>
                            <td width="1%" nowrap>
                                Debug Log: &nbsp;
                            </td>
                            <td width="1%">
                                <input type="radio" name="debugEnabled" value="true"<%= ((debugEnabled) ? " checked" : "") %> id="de01">
                            </td>
                            <td width="1%" nowrap>
                                <label for="de01">Enabled</label> &nbsp;
                            </td>
                            <td width="1%">
                                <input type="radio" name="debugEnabled" value="false"<%= ((!debugEnabled) ? " checked" : "") %> id="de02">
                            </td>
                            <td width="1%" nowrap>
                                <label for="de02">Disabled</label> &nbsp;
                            </td>
                            <td width="1%">
                                <input type="hidden" name="wasDebugEnabled" value="<%= debugEnabled %>">
                                <input type="submit" name="" value="Update">
                            </td>
                            <td width="94%">&nbsp;</td>
                        </tr>
                        </table>
                    </td>
                </tr>

            <%  } %>

            </table>
            </div>
        </td>
    </tr>
</tbody>
</table>
</div>

<br>

<span class="jive-description" style="color:#666;">
Log dir: <%= JiveGlobals.getMessengerHome() %><%= File.separator %>logs
</span>

<br><br>

<iframe src="log.jsp?log=<%= log %>&mode=<%= mode %>&lines=<%= ("All".equals(numLinesParam) ? "All" : String.valueOf(numLines)) %>"
    frameborder="0" height="400" width="100%" marginheight="0" marginwidth="0" scrolling="auto"></iframe>

</form>

</div>

<jsp:include page="bottom.jsp" flush="true" />