]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/logging/TimeLogger.java
Prevent NPEs in ViewUtils layout loading code
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / logging / TimeLogger.java
1 package org.simantics.utils.logging;
2
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5
6 public class TimeLogger {
7     public static final boolean TIME_LOGGING_ENABLED = false;
8     
9     private static final NumberFormat TIME_FORMAT = new DecimalFormat("0.000");
10     
11     private static long BEGIN_TIME = System.nanoTime();
12     
13     private static void printCurrentTime() {
14         double time = (System.nanoTime() - BEGIN_TIME) * 1e-9;
15         System.out.print("[");
16         System.out.print(TIME_FORMAT.format(time));
17         System.out.print(" s] ");
18     }
19     
20     public static void resetTimeAndLog(String message) {
21         if(TIME_LOGGING_ENABLED) {
22             BEGIN_TIME = System.nanoTime();
23             printCurrentTime();
24             System.out.print(message);
25             System.out.print(" ");
26             for(int i=0;i<140-message.length();++i)
27                 System.out.print("=");
28             System.out.println();
29         }
30     }
31     
32     public static void resetTimeAndLog(Class<?> clazz, String method) {
33         if(TIME_LOGGING_ENABLED)
34             resetTimeAndLog(clazz.getSimpleName() + "." + method);
35     }
36     
37     public static void resetTime() {
38         if(TIME_LOGGING_ENABLED)
39             BEGIN_TIME = System.nanoTime();
40     }
41     
42     public static void log(String message) {
43         if(TIME_LOGGING_ENABLED) {
44             printCurrentTime();
45             System.out.println(message);
46         }
47     }
48     
49     public static void log(Class<?> clazz, String method) {
50         if(TIME_LOGGING_ENABLED)
51             log(clazz.getSimpleName() + "." + method);
52     }
53 }