TraceHandler.java 14.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xfa.ut.trace;

import com.adobe.xfa.ut.MsgFormatPos;
import com.adobe.xfa.ut.ResourceLoader;
import com.adobe.xfa.ut.StringUtils;
import com.adobe.xfa.ut.trace.GlobalTraceStore;
import com.adobe.xfa.ut.trace.Trace;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLongArray;

public final class TraceHandler {
    private static volatile boolean gbTimingTraceEnabled;
    private static final AtomicLongArray mTimingData;
    private static volatile BufferedWriter gScriptLog;
    private static BufferedWriter gTimingLog;
    private static boolean gbTimingLogNonZeroOnly;
    private static final AtomicInteger mnThreadIdPool;
    private static final ThreadLocal<TraceHandler> mTraceHandlerTLS;
    private final int mnThreadId;
    private int mnLastCheckState = 0;
    private String msLastCheckName = "";
    private int mnMaxTraceNameLen;
    private final Map<String, Integer> mTraceLevels = new HashMap<String, Integer>();
    private final Map<String, Trace> mTraces = new HashMap<String, Trace>();
    private Formatter mFormatter;
    private Object mUserData = null;
    private AmendMsgCallback mAmendMsgCallback = null;

    public static void timingTraceEnabled(boolean bEnabled) {
        gbTimingTraceEnabled = bEnabled;
    }

    public static boolean timingTraceEnabled() {
        return gbTimingTraceEnabled;
    }

    public static boolean scriptLoggingEnabled() {
        return gScriptLog != null;
    }

    public static void setAmendMsgCallback(AmendMsgCallback callback, Object userData) {
        TraceHandler traceHandler = TraceHandler.getTraceHandler();
        if (traceHandler != null) {
            traceHandler.mAmendMsgCallback = callback;
            traceHandler.mUserData = userData;
        }
    }

    public static void resetTiming() {
        for (int i = 0; i < mTimingData.length(); ++i) {
            mTimingData.set(i, 0);
        }
    }

    public static void resetTiming(TimingType eType) {
        mTimingData.set(eType.ordinal(), 0);
    }

    public static void initializeTiming() {
        TraceHandler.resetTiming();
        TraceHandler.parseTraceControlFile();
    }

    public static synchronized void reportScriptInfo(String sMsg) {
        TraceHandler.parseTraceControlFile();
        if (gScriptLog == null) {
            return;
        }
        try {
            gScriptLog.write(sMsg);
            gScriptLog.write(10);
        }
        catch (IOException ex) {
            // empty catch block
        }
    }

    private static void outputTime(String prefix, TimingType eType) throws IOException {
        long milliseconds = TraceHandler.getTimerMilliseconds(eType);
        if (milliseconds == 0 && gbTimingLogNonZeroOnly) {
            return;
        }
        gTimingLog.write(prefix);
        gTimingLog.write(Long.toString(milliseconds));
        gTimingLog.write(10);
    }

    public static synchronized void reportTimingResults(boolean bClearStoppedTimers) {
        TraceHandler.parseTraceControlFile();
        if (gTimingLog == null) {
            return;
        }
        try {
            gTimingLog.write("Timing results (ms):\n");
            TraceHandler.outputTime("XFAPA_MERGE_TIMING:       ", TimingType.XFAPA_MERGE_TIMING);
            TraceHandler.outputTime("XFAPA_LAYOUT_TIMING:      ", TimingType.XFAPA_LAYOUT_TIMING);
            TraceHandler.outputTime("XFAPA_RENDER_TIMING:      ", TimingType.XFAPA_RENDER_TIMING);
            TraceHandler.outputTime("XFAPA_MERGE_CALC_TIMING:  ", TimingType.XFAPA_MERGE_CALC_TIMING);
            TraceHandler.outputTime("XFAPA_LAYOUT_CALC_TIMING: ", TimingType.XFAPA_LAYOUT_CALC_TIMING);
            TraceHandler.outputTime("XFA_MERGE_ONLY_TIMING:    ", TimingType.XFA_MERGE_ONLY_TIMING);
            TraceHandler.outputTime("XFA_LAYOUT_ONLY_TIMING:   ", TimingType.XFA_LAYOUT_ONLY_TIMING);
            TraceHandler.outputTime("XFA_SCRIPTS_ONLY_TIMING:  ", TimingType.XFA_SCRIPTS_ONLY_TIMING);
            TraceHandler.outputTime("XFA_OPEN_TIMING:          ", TimingType.XFA_OPEN_TIMING);
            gTimingLog.write("Timing results complete.\n\n");
            gTimingLog.flush();
            if (bClearStoppedTimers) {
                TraceHandler.resetTiming();
            }
        }
        catch (IOException ex) {
            // empty catch block
        }
    }

    public static long getTimerMilliseconds(TimingType eType) {
        return mTimingData.get(eType.ordinal()) / 1000000;
    }

    static void addTimerNanos(TimingType eType, long nanos) {
        mTimingData.addAndGet(eType.ordinal(), nanos);
    }

    public TraceHandler(int nThreadId) {
        this.mnThreadId = nThreadId;
        this.mnMaxTraceNameLen = 0;
    }

    public static TraceHandler getTraceHandler() {
        return mTraceHandlerTLS.get();
    }

    public static void registerTraceHandler(TraceHandler traceHandler) {
        if (traceHandler != null) {
            mTraceHandlerTLS.set(traceHandler);
        }
    }

    public static void unregisterTraceHandler() {
        mTraceHandlerTLS.set(null);
    }

    public void activate(Trace trace, int nLevel) {
        if (trace == null) {
            return;
        }
        this.activate(trace.getName(), nLevel);
    }

    public void activate(String sName, int nLevel) {
        if (sName.equals("timing")) {
            TraceHandler.timingTraceEnabled(nLevel > 0);
            TraceHandler.resetTiming();
        }
        this.msLastCheckName = sName;
        this.mnLastCheckState = 0;
        if (!this.mTraceLevels.containsKey(this.msLastCheckName)) {
            GlobalTraceStore globalTraceStore = GlobalTraceStore.getStore();
            globalTraceStore.ensureTraceExists(this.msLastCheckName);
            for (Trace t : globalTraceStore.getTraceSections()) {
                this.addTrace(t);
            }
        }
        this.mTraceLevels.put(this.msLastCheckName, nLevel);
        this.mnLastCheckState = nLevel;
        Trace trace = this.mTraces.get(this.msLastCheckName);
        GlobalTraceStore.getStore().enable();
        if (trace != null) {
            trace.enable();
        }
    }

    public void activate(String sNamesAndLevels) {
        StringTokenizer tokenizer = new StringTokenizer(sNamesAndLevels);
        while (tokenizer.hasMoreTokens()) {
            String sName;
            String token = tokenizer.nextToken();
            int colon = token.indexOf(58);
            int nLevel = 0;
            if (colon == -1) {
                sName = token.trim();
            } else {
                sName = token.substring(0, colon).trim();
                String sLevel = token.substring(colon + 1);
                try {
                    nLevel = Integer.parseInt(sLevel);
                }
                catch (NumberFormatException ex) {
                    // empty catch block
                }
            }
            this.activate(sName, nLevel);
        }
    }

    public void deactivate(String sName) {
        this.msLastCheckName = sName;
        this.mnLastCheckState = 0;
        if (this.mTraceLevels.containsKey(sName)) {
            this.mTraceLevels.put(sName, 0);
        }
    }

    String dumpAvailable(boolean bIncludeHelp) {
        StringBuilder sRetValue = new StringBuilder();
        for (Trace trace : this.mTraces.values()) {
            if (sRetValue.length() > 0) {
                sRetValue.append('\n');
            }
            if (bIncludeHelp) {
                sRetValue.append('\t');
            }
            sRetValue.append(trace.getName());
            if (!bIncludeHelp) continue;
            sRetValue.append(" - ");
            sRetValue.append(trace.getHelpId());
        }
        return sRetValue.toString();
    }

    boolean isActive(String sName, int nLevel) {
        if (!this.msLastCheckName.equals(sName)) {
            this.msLastCheckName = sName;
            Integer value = this.mTraceLevels.get(this.msLastCheckName);
            int n = this.mnLastCheckState = value == null ? 0 : value;
        }
        if (this.mnLastCheckState == 0 || this.mnLastCheckState < nLevel) {
            return false;
        }
        return true;
    }

    void trace(Trace oTrace, int nLevel, int nId, String sMsg) {
        if (oTrace != null && oTrace.isEnabled(nLevel)) {
            this.printTrace(oTrace.getName(), nId, sMsg);
        }
    }

    void trace(String sName, int nLevel, int nId, String sMsg) {
        if (this.isActive(sName, nLevel)) {
            this.printTrace(sName, nId, sMsg);
        }
    }

    void trace(String sName, int nLevel, MsgFormatPos oMsg) {
        if (this.isActive(sName, nLevel)) {
            this.printTrace(sName, oMsg.resId(), oMsg.toString());
        }
    }

    void trace(Trace oTrace, int nLevel, MsgFormatPos oMsg) {
        if (oTrace != null && oTrace.isEnabled(nLevel)) {
            this.printTrace(oTrace.getName(), oMsg.resId(), oMsg.toString());
        }
    }

    public int getThreadId() {
        return this.mnThreadId;
    }

    private void addTrace(Trace trace) {
        if (trace.getName().length() > this.mnMaxTraceNameLen) {
            this.mnMaxTraceNameLen = trace.getName().length();
        }
        this.mTraceLevels.put(trace.getName(), 0);
        this.mTraces.put(trace.getName(), trace);
    }

    private String amendMsg(int nAmendId) {
        if (this.mAmendMsgCallback != null) {
            return this.mAmendMsgCallback.amendMessage(nAmendId, this.mUserData);
        }
        return "";
    }

    private void printTrace(String sName, int nId, String sMsg) {
        if (this.mFormatter == null) {
            this.mFormatter = new Formatter(System.out, Locale.US);
        }
        this.mFormatter.format("%-" + this.mnMaxTraceNameLen + "s trace: mid=%-3u tid=%-3u msg: %s\n", this.mnMaxTraceNameLen, sName, nId, this.mnThreadId, sMsg);
        int nAmendId = nId;
        String sAmendId = this.amendMsg(nAmendId);
        if (!StringUtils.isEmpty(sAmendId)) {
            this.mFormatter.format("   trace: mid=%-3u tid=%-3u msg: %s\n", nAmendId, this.mnThreadId, sAmendId);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private static synchronized void parseTraceControlFile() {
        String sTraceControlFileName = ResourceLoader.loadProperty("xtg.trace.control.file");
        if (sTraceControlFileName.length() == 0) {
            return;
        }
        gbTimingTraceEnabled = true;
        if (gTimingLog != null) {
            try {
                gTimingLog.close();
            }
            catch (IOException ex) {
                // empty catch block
            }
            gTimingLog = null;
        }
        if (gScriptLog != null) {
            try {
                gScriptLog.close();
            }
            catch (IOException ex) {
                // empty catch block
            }
            gScriptLog = null;
        }
        gbTimingLogNonZeroOnly = false;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(sTraceControlFileName));
            block19 : do {
                do {
                    String line;
                    try {
                        line = reader.readLine();
                    }
                    catch (IOException ex) {
                        return;
                    }
                    if (line == null) return;
                    int index = line.indexOf("timinglog=");
                    if (index != -1) {
                        String timingLog = line.substring(index + "timinglog=".length());
                        if (gTimingLog != null) continue;
                        try {
                            gTimingLog = new BufferedWriter(new FileWriter(timingLog, true));
                            continue block19;
                        }
                        catch (IOException ex) {
                            continue;
                        }
                    }
                    index = line.indexOf("nonZeroOnly=");
                    if (index != -1) {
                        String timingLogNonZeroOnly = line.substring(index + "nonZeroOnly=".length());
                        gbTimingLogNonZeroOnly = Integer.parseInt(timingLogNonZeroOnly) == 1;
                        continue;
                    }
                    index = line.indexOf("scriptlog=");
                    if (index == -1) continue;
                    String scriptLog = line.substring(index + "scriptlog=".length());
                    if (gScriptLog != null) continue;
                    try {
                        gScriptLog = new BufferedWriter(new FileWriter(scriptLog, true));
                        continue block19;
                    }
                    catch (IOException ex) {
                        continue;
                    }
                    break;
                } while (true);
            } while (true);
            finally {
                try {
                    reader.close();
                }
                catch (IOException ex) {}
            }
        }
        catch (FileNotFoundException ex) {
            // empty catch block
        }
    }

    public boolean isTimerActive(TimingType eType) {
        return mTimingData.get(eType.ordinal()) != 0;
    }

    static {
        mTimingData = new AtomicLongArray(TimingType.values().length);
        mnThreadIdPool = new AtomicInteger();
        mTraceHandlerTLS = new ThreadLocal<TraceHandler>(){

            @Override
            protected TraceHandler initialValue() {
                return new TraceHandler(mnThreadIdPool.getAndIncrement());
            }
        };
    }

    public static interface AmendMsgCallback {
        public String amendMessage(int var1, Object var2);
    }

    public static enum TimingType {
        XFAPA_MERGE_TIMING,
        XFAPA_LAYOUT_TIMING,
        XFAPA_RENDER_TIMING,
        XFAPA_MERGE_CALC_TIMING,
        XFAPA_LAYOUT_CALC_TIMING,
        XFAPA_LAYOUT_FONTSERVICE_TIMING,
        XMLFORM_INIT_TIMING,
        XMLFORM_INIT_BATCH_TIMING,
        XMLFORM_RENDER_TIMING,
        XMLFORM_IMPORT_TIMING,
        XMLFORM_POSTPROCESS_TIMING,
        CORETECH_INIT_TIMING,
        XFA_MERGE_ONLY_TIMING,
        XFA_LAYOUT_ONLY_TIMING,
        XFA_SCRIPTS_ONLY_TIMING,
        UT_TESTCASE,
        APP_TOTALTIME,
        XFA_OPEN_TIMING,
        USER1_TIMING,
        USER2_TIMING;
        

        private TimingType() {
        }
    }

}