OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 Rev 174
Line 23... Line 23...
23
import java.awt.FlowLayout;
23
import java.awt.FlowLayout;
24
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionEvent;
25
import java.awt.event.ActionListener;
25
import java.awt.event.ActionListener;
26
import java.awt.event.MouseAdapter;
26
import java.awt.event.MouseAdapter;
27
import java.awt.event.MouseEvent;
27
import java.awt.event.MouseEvent;
-
 
28
import java.io.PrintStream;
28
import java.sql.Connection;
29
import java.sql.Connection;
29
import java.sql.PreparedStatement;
30
import java.sql.PreparedStatement;
30
import java.text.DateFormat;
31
import java.text.DateFormat;
31
import java.text.DecimalFormat;
32
import java.text.DecimalFormat;
32
import java.text.SimpleDateFormat;
33
import java.text.SimpleDateFormat;
Line 58... Line 59...
58
 
59
 
59
    private static final Color BG_PINK = new Color(254, 240, 240);
60
    private static final Color BG_PINK = new Color(254, 240, 240);
60
    private static final String ACTIVER_LA_CAPTURE = "Enable monitoring";
61
    private static final String ACTIVER_LA_CAPTURE = "Enable monitoring";
61
    private static final String DESACTIVER_LA_CAPTURE = "Disable monitoring";
62
    private static final String DESACTIVER_LA_CAPTURE = "Disable monitoring";
62
    private static Vector<SQLRequestLog> list = new Vector<SQLRequestLog>(500);
63
    private static Vector<SQLRequestLog> list = new Vector<SQLRequestLog>(500);
-
 
64
    @GuardedBy("this")
63
    private static boolean enabled;
65
    private static boolean enabled;
-
 
66
 
-
 
67
    public static synchronized void setEnabled(boolean enable) {
-
 
68
        enabled = enable;
-
 
69
    }
-
 
70
 
-
 
71
    public static synchronized boolean toggleEnabled() {
-
 
72
        final boolean newVal = !isEnabled();
-
 
73
        setEnabled(newVal);
-
 
74
        return newVal;
-
 
75
    }
-
 
76
 
-
 
77
    public static synchronized boolean isEnabled() {
-
 
78
        return enabled;
-
 
79
    }
-
 
80
 
64
    private String query;
81
    private String query;
65
    private String comment;
82
    private String comment;
66
    private long startAsMs;
83
    private long startAsMs;
67
    private final long startTime, afterCache, afterQueryInfo, afterExecute, afterHandle, endTime;
84
    private final long startTime, afterCache, afterQueryInfo, afterExecute, afterHandle, endTime;
68
    private String stack;
85
    private String stack;
Line 83... Line 100...
83
    private static final String format(final Object nano) {
100
    private static final String format(final Object nano) {
84
        final long l = ((Number) nano).longValue();
101
        final long l = ((Number) nano).longValue();
85
        return l == 0 ? "" : dformat.format(l / 1000000D) + " ms";
102
        return l == 0 ? "" : dformat.format(l / 1000000D) + " ms";
86
    }
103
    }
87
 
104
 
88
    public static void setEnabled(boolean enable) {
-
 
89
        enabled = enable;
-
 
90
    }
-
 
91
 
-
 
92
    public SQLRequestLog(String query, String comment, int connectionId, long starAtMs, String ex, boolean inSwing, long startTime, long afterCache, long afterQueryInfo, long afterExecute,
105
    public SQLRequestLog(String query, String comment, int connectionId, long starAtMs, String ex, boolean inSwing, long startTime, long afterCache, long afterQueryInfo, long afterExecute,
93
            long afterHandle, long endTime, int count) {
106
            long afterHandle, long endTime, int count) {
94
        this.query = query;
107
        this.query = query;
95
        this.comment = comment;
108
        this.comment = comment;
96
        this.connectionId = connectionId;
109
        this.connectionId = connectionId;
Line 114... Line 127...
114
    static long total_count = 0;
127
    static long total_count = 0;
115
 
128
 
116
    public static void log(String query, String comment, int connectionId, long starAtMs, long startTime, long afterCache, long afterQueryInfo, long afterExecute, long afterHandle, long endTime,
129
    public static void log(String query, String comment, int connectionId, long starAtMs, long startTime, long afterCache, long afterQueryInfo, long afterExecute, long afterHandle, long endTime,
117
            int count) {
130
            int count) {
118
 
131
 
119
        if (enabled) {
132
        if (isEnabled()) {
120
            if (list.size() < 50000) {
133
            if (list.size() < 50000) {
121
                final String ex = ExceptionUtils.getStackTrace(new Exception());
134
                final String ex = ExceptionUtils.getStackTrace(new Exception());
122
 
135
 
123
                list.add(new SQLRequestLog(query, comment, connectionId, starAtMs, ex, SwingUtilities.isEventDispatchThread(), startTime, afterCache, afterQueryInfo, afterExecute, afterHandle,
136
                list.add(new SQLRequestLog(query, comment, connectionId, starAtMs, ex, SwingUtilities.isEventDispatchThread(), startTime, afterCache, afterQueryInfo, afterExecute, afterHandle,
124
                        endTime, count));
137
                        endTime, count));
Line 133... Line 146...
133
        log(query, comment, 0, starAtMs, startTime, startTime, startTime, startTime, startTime, startTime, 0);
146
        log(query, comment, 0, starAtMs, startTime, startTime, startTime, startTime, startTime, startTime, 0);
134
    }
147
    }
135
 
148
 
136
    public static void log(PreparedStatement pStmt, String comment, long timeMs, long startTime, long afterCache, long afterQueryInfo, long afterExecute, long afterHandle, long endTime) {
149
    public static void log(PreparedStatement pStmt, String comment, long timeMs, long startTime, long afterCache, long afterQueryInfo, long afterExecute, long afterHandle, long endTime) {
137
        // only call potentially expensive and/or exceptions throwing methods if necessary
150
        // only call potentially expensive and/or exceptions throwing methods if necessary
138
        if (enabled) {
151
        if (isEnabled()) {
139
            try {
152
            try {
140
                log(pStmt.toString(), comment, pStmt.getConnection(), timeMs, startTime, afterCache, afterQueryInfo, afterExecute, afterHandle, endTime, 0);
153
                log(pStmt.toString(), comment, pStmt.getConnection(), timeMs, startTime, afterCache, afterQueryInfo, afterExecute, afterHandle, endTime, 0);
141
            } catch (Exception e) {
154
            } catch (Exception e) {
142
                // never propagate exceptions
155
                // never propagate exceptions
143
                Log.get().log(Level.WARNING, "Couldn't log " + pStmt, e);
156
                Log.get().log(Level.WARNING, "Couldn't log " + pStmt, e);
Line 431... Line 444...
431
        JPanel p = new JPanel(new BorderLayout());
444
        JPanel p = new JPanel(new BorderLayout());
432
 
445
 
433
        JPanel bar = new JPanel(new FlowLayout());
446
        JPanel bar = new JPanel(new FlowLayout());
434
 
447
 
435
        final JButton b0 = new JButton();
448
        final JButton b0 = new JButton();
436
        if (enabled) {
449
        if (isEnabled()) {
437
            b0.setText(DESACTIVER_LA_CAPTURE);
450
            b0.setText(DESACTIVER_LA_CAPTURE);
438
        } else {
451
        } else {
439
            b0.setText(ACTIVER_LA_CAPTURE);
452
            b0.setText(ACTIVER_LA_CAPTURE);
440
        }
453
        }
441
        b0.addActionListener(new ActionListener() {
454
        b0.addActionListener(new ActionListener() {
442
            @Override
455
            @Override
443
            public void actionPerformed(ActionEvent e) {
456
            public void actionPerformed(ActionEvent e) {                
444
                if (enabled) {
457
                if (!toggleEnabled()) {
445
                    enabled = false;
-
 
446
                    b0.setText(ACTIVER_LA_CAPTURE);
458
                    b0.setText(ACTIVER_LA_CAPTURE);
447
                } else {
459
                } else {
448
                    enabled = true;
-
 
449
                    b0.setText(DESACTIVER_LA_CAPTURE);
460
                    b0.setText(DESACTIVER_LA_CAPTURE);
450
                }
461
                }
451
 
462
 
452
            }
463
            }
453
        });
464
        });
Line 618... Line 629...
618
                l.isHighlighted = l.getQuery().equals(req);
629
                l.isHighlighted = l.getQuery().equals(req);
619
            }
630
            }
620
            model.fireTableRowsUpdated(0, model.getRowCount() - 1);
631
            model.fireTableRowsUpdated(0, model.getRowCount() - 1);
621
        }
632
        }
622
    }
633
    }
-
 
634
 
-
 
635
    public static void dump(PrintStream out) {
-
 
636
        final DecimalFormat dformat = new DecimalFormat("##0.00");
-
 
637
        for (SQLRequestLog log : list) {
-
 
638
            out.print(dformat.format(log.getDurationTotalNano() / 1000000D) + " ms ");
-
 
639
            out.print("\t");
-
 
640
            out.print(log.getQuery());
-
 
641
            out.println();
-
 
642
        }
-
 
643
 
-
 
644
    }
623
}
645
}