log.jsp 6.76 KB
Newer Older
1 2 3 4 5
<%--
  -	$RCSfile$
  -	$Revision$
  -	$Date$
  -
6
  - Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
  -
8 9 10 11 12 13 14 15 16 17 18 19
  - Licensed under the Apache License, Version 2.0 (the "License");
  - you may not use this file except in compliance with the License.
  - You may obtain a copy of the License at
  -
  -     http://www.apache.org/licenses/LICENSE-2.0
  -
  - Unless required by applicable law or agreed to in writing, software
  - distributed under the License is distributed on an "AS IS" BASIS,
  - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - See the License for the specific language governing permissions and
  - limitations under the License.

20 21 22 23 24 25 26 27 28 29 30 31
--%>

<%@ page import="java.io.*,
                 java.text.SimpleDateFormat,
                 java.util.Date,
                 java.text.ParseException,
                 org.jivesoftware.util.ParamUtils,
                 org.jivesoftware.util.Log,
                 org.jivesoftware.util.StringUtils"
    errorPage="error.jsp"
%>

32
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
33 34 35 36 37
<jsp:useBean id="pageinfo" scope="request" class="org.jivesoftware.admin.AdminPageBean" />

<%!
    static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd kk:mm:ss");

38
    private static String parseDate(String input) {
39 40 41 42 43 44 45 46 47 48
        if (input == null || "".equals(input)) {
            return input;
        }
        if (input.length() < 19) {
            return input;
        }
        String d = input.substring(0,19);
        // try to parse it
        try {
            StringBuffer buf = new StringBuffer(input.length());
49 50 51 52 53
            synchronized (formatter) {
                Date date = formatter.parse(d);
                buf.append("<span class=\"date\" title=\"").append(formatter.format(date))
                        .append("\">");
            }
54 55 56 57 58 59 60 61 62
            buf.append(d).append("</span>");
            buf.append(input.substring(19,input.length()));
            return buf.toString();
        }
        catch (ParseException pe) {
            return input;
        }
    }

63
    private static String hilite(String input) {
64 65 66 67 68 69 70 71
        if (input == null || "".equals(input)) {
            return input;
        }
        if (input.indexOf("org.jivesoftware.") > -1) {
            StringBuffer buf = new StringBuffer();
            buf.append("<span class=\"hilite\">").append(input).append("</span>");
            return buf.toString();
        }
Matt Tucker's avatar
Matt Tucker committed
72
        else if (input.trim().startsWith("---") && input.trim().endsWith("---")) {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
            StringBuffer buf = new StringBuffer();
            buf.append("<span class=\"hilite-marker\">").append(input).append("</span>");
            return buf.toString();
        }
        return input;
    }
%>

<%
    // Get parameters
    String log = ParamUtils.getParameter(request,"log");
    String numLinesParam = ParamUtils.getParameter(request,"lines");
    int numLines = ParamUtils.getIntParameter(request,"lines",50);
    String mode = ParamUtils.getParameter(request,"mode");

88 89 90 91 92
    // Only allow requests for valid log file names.
    if (!("debug".equals(log) || "warn".equals(log) || "info".equals(log) || "error".equals(log))) {
        log = null;
    }

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    // Set defaults
    if (log == null) {
        log = "error";
    }
    if (mode == null) {
        mode = "asc";
    }
    if (numLinesParam == null) {
        numLinesParam = "50";
    }

    // Other vars
    File logDir = new File(Log.getLogDirectory());
    String filename = log + ".log";
    File logFile = new File(logDir, filename);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    
    String lines[] = new String[0];
    int start = 0;
    try {
	    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "UTF-8"));
	    String line;
	    int totalNumLines = 0;
	    while ((line=in.readLine()) != null) {
	        totalNumLines++;
	    }
	    in.close();
	    // adjust the 'numLines' var to match totalNumLines if 'all' was passed in:
	    if ("All".equals(numLinesParam)) {
	        numLines = totalNumLines;
	    }
	    lines = new String[numLines];
	    in = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "UTF-8"));
	    // skip lines
	    start = totalNumLines - numLines;
	    if (start < 0) { start = 0; }
	    for (int i=0; i<start; i++) {
	        in.readLine();
	    }
	    int i = 0;
	    if ("asc".equals(mode)) {
	        while ((line=in.readLine()) != null && i<numLines) {
	            line = StringUtils.escapeHTMLTags(line);
	            line = parseDate(line);
	            line = hilite(line);
	            lines[i] = line;
	            i++;
	        }
	    }
	    else {
	        int end = lines.length-1;
	        while ((line=in.readLine()) != null && i<numLines) {
	            line = StringUtils.escapeHTMLTags(line);
	            line = parseDate(line);
	            line = hilite(line);
	            lines[end-i] = line;
	            i++;
	        }
	    }
	    numLines = start + i;
    } catch (FileNotFoundException ex) {
    	Log.info("Could not open (log)file.", ex);
154 155 156 157 158 159
    }
%>

<html>
<head>
    <title><%= log %></title>
160
    <meta name="decorator" content="none"/>
161 162 163 164 165
    <style type="text/css">
    .log TABLE {
        border : 1px #ccc solid;
    }
    .log TH {
166
        font-family : verdana, arial, sans-serif;
167 168 169 170 171 172 173 174 175 176 177 178 179 180
        font-weight : bold;
        font-size : 8pt;
    }
    .log TR TH {
        background-color : #ddd;
        border-bottom : 1px #ccc solid;
        padding-left : 2px;
        padding-right : 2px;
        text-align : left;
    }
    .log .head-num {
        border-right : 1px #ccc solid;
    }
    .log TD {
181
        font-family : courier new,monospace;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        font-size : 9pt;
        background-color : #ffe;
    }
    .log .num {
        width : 1%;
        background-color : #eee !important;
        border-right : 1px #ccc solid;
        padding-left : 2px;
        padding-right : 2px;
    }
    .log .line {
        padding-left : 10px;
    }
    .hilite {
        color : #900;
    }
    .hilite-marker {
        background-color : #ff0;
        color : #000;
        font-weight : bold;
    }
    </style>
</head>
<body>

<div class="log">
<table cellpadding="1" cellspacing="0" border="0" width="100%">
<tr>
210
    <th class="head-num"><fmt:message key="log.line" /></th>
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    <th>&nbsp;</th>
</tr>
<tr>
    <td width="1%" nowrap class="num">
        <%  if ("asc".equals(mode)) { %>
            <%  for (int j=start+1; j<=numLines; j++) { %>
                <%= j %><br>
            <%  } %>
        <%  } else { %>
            <%  for(int j=numLines; j>=start+1; j--) { %>
                <%= j %><br>
            <%  } %>
        <%  } %>
    </td>
    <td width="99%" class="line">
226 227
        <% for (String line1 : lines) {
            if (line1 != null) {
228
        %>
229 230 231
        <nobr><%= line1 %>
        </nobr>
        <br>
232

233 234
        <% }
        }
235 236 237 238 239 240 241 242
        %>
    </td>
</tr>
</table>
</div>

</body>
</html>