Clover Coverage Report - Kuali Student 1.3.0-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Apr 28 2011 05:03:32 EDT
../../../../../../../img/srcFileCovDistChart0.png 2% of files have more coverage
48   168   28   2
8   122   0.58   24
24     1.17  
1    
 
  Logger       Line # 31 48 0% 28 80 0% 0.0
 
No Tests
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.common.ui.client.logging;
17   
18    import java.util.ArrayList;
19    import java.util.HashMap;
20    import java.util.List;
21    import java.util.Map;
22   
23    import org.kuali.student.common.ui.client.application.KSAsyncCallback;
24    import org.kuali.student.common.ui.client.service.LogRpcService;
25    import org.kuali.student.common.ui.client.service.LogRpcServiceAsync;
26   
27    import com.google.gwt.core.client.GWT;
28    import com.google.gwt.user.client.Command;
29    import com.google.gwt.user.client.DeferredCommand;
30   
 
31    public class Logger {
32    /**
33    * Default log level is LogLevel.WARN
34    */
35    static LogLevel logLevel = LogLevel.WARN;
36    static LogBuffer buffer = new LogBuffer();
37    static Map<String, String> clientContextInfo = new HashMap<String, String>();
38    static int maxBufferSize = Integer.MAX_VALUE;
39    /**
40    * Sets the maximum buffer size, resizing buffer if necessary.
41    * Can be an expensive operation, avoid frequent use.
42    * @param maxSize
43    */
 
44  0 toggle public static void setMaxBufferSize(int maxSize) {
45  0 LogBuffer tmp = new LogBuffer(maxSize);
46  0 for (LogMessage m : buffer.getLogMessages()) {
47  0 tmp.add(m);
48    }
49  0 buffer = tmp;
50    }
51   
 
52  0 toggle public static void setLogLevel(LogLevel level) {
53  0 logLevel = level;
54    }
 
55  0 toggle public static LogLevel getLogLevel() {
56  0 return logLevel;
57    }
 
58  0 toggle public static void log(LogMessage message) {
59  0 if (message.getLogLevel().getLevel() >= logLevel.getLevel()) {
60  0 buffer.add(message);
61    }
62    }
63   
 
64  0 toggle public static void log(LogLevel level, String message) {
65  0 if (level.getLevel() >= logLevel.getLevel()) {
66  0 buffer.add(new LogMessage(level, message, (Throwable) null));
67    }
68    }
69   
 
70  0 toggle public static void log(LogLevel level, String message, Throwable error) {
71  0 if (level.getLevel() >= logLevel.getLevel()) {
72  0 buffer.add(new LogMessage(level, message, error));
73    }
74    }
75   
 
76  0 toggle public static void debug(String message) {
77  0 log(LogLevel.DEBUG, message);
78    }
 
79  0 toggle public static void debug(String message, Throwable error) {
80  0 log(LogLevel.DEBUG, message, error);
81    }
82   
 
83  0 toggle public static void info(String message) {
84  0 log(LogLevel.INFO, message);
85    }
 
86  0 toggle public static void info(String message, Throwable error) {
87  0 log(LogLevel.INFO, message, error);
88    }
89   
 
90  0 toggle public static void warn(String message) {
91  0 log(LogLevel.WARN, message);
92    }
 
93  0 toggle public static void warn(String message, Throwable error) {
94  0 log(LogLevel.WARN, message, error);
95    }
96   
 
97  0 toggle public static void error(String message) {
98  0 log(LogLevel.ERROR, message);
99    }
 
100  0 toggle public static void error(String message, Throwable error) {
101  0 log(LogLevel.ERROR, message, error);
102    }
103   
 
104  0 toggle public static void fatal(String message) {
105  0 log(LogLevel.FATAL, message);
106    }
 
107  0 toggle public static void fatal(String message, Throwable error) {
108  0 log(LogLevel.FATAL, message, error);
109    }
110   
 
111  0 toggle public static void reset() {
112  0 buffer = new LogBuffer(maxBufferSize);
113    }
114   
115   
116   
 
117  0 toggle public static Map<String, String> getClientContextInfo() {
118  0 return clientContextInfo;
119    }
120   
 
121  0 toggle public static void sendLogs() {
122  0 final List<LogMessage> messages = new ArrayList<LogMessage>(buffer.getLogMessages());
123  0 final Map<String, String> context = new HashMap<String, String>(clientContextInfo);
124  0 reset();
125  0 DeferredCommand.addCommand(new Command() {
 
126  0 toggle public void execute() {
127  0 String log = formatLog(messages);
128  0 LogRpcServiceAsync logService = (LogRpcServiceAsync) GWT .create(LogRpcService.class);
129   
130  0 logService.sendLog(context, log, new KSAsyncCallback<Boolean>() {
 
131  0 toggle public void handleFailure(Throwable caught) {
132  0 GWT.log("sendLog Failed", caught);
133    //throw new LogFailedException(caught);
134    }
135   
 
136  0 toggle public void onSuccess(Boolean result) {
137    // do nothing
138  0 GWT.log("sendLog OK", null);
139    }
140    });
141    }
142    });
143    }
144   
 
145  0 toggle private static String formatLog(List<LogMessage> messages) {
146  0 StringBuilder s = new StringBuilder();
147  0 for (LogMessage lm : messages) {
148  0 s.append(lm.getLogLevel().toString());
149  0 s.append(":\t");
150  0 s.append(lm.getMessage());
151  0 Throwable t = lm.getError();
152  0 if (t != null) {
153  0 appendStackTrace(t, s);
154    }
155    }
156  0 return s.toString();
157    }
158   
 
159  0 toggle private static void appendStackTrace(Throwable t, StringBuilder s) {
160  0 s.append(t.toString());
161  0 s.append(": at\n");
162  0 StackTraceElement[] stack = t.getStackTrace();
163  0 for (StackTraceElement frame : stack) {
164  0 s.append(frame.toString());
165  0 s.append("\n");
166    }
167    }
168    }