Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TimedLogger |
|
| 2.0;2 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | * See the License for the specific language governing permissions and | |
16 | * limitations under the License. | |
17 | */ | |
18 | ||
19 | package org.apache.giraph.utils; | |
20 | ||
21 | import org.apache.log4j.Logger; | |
22 | ||
23 | /** | |
24 | * Print log messages only if the time is met. Thread-safe. | |
25 | */ | |
26 | public class TimedLogger { | |
27 | /** Last time printed */ | |
28 | 0 | private volatile long lastPrint = System.currentTimeMillis(); |
29 | /** Minimum interval of time to wait before printing */ | |
30 | private final int msecs; | |
31 | /** Logger */ | |
32 | private final Logger log; | |
33 | ||
34 | /** | |
35 | * Constructor of the timed logger | |
36 | * | |
37 | * @param msecs Msecs to wait before printing again | |
38 | * @param log Logger to print to | |
39 | */ | |
40 | 0 | public TimedLogger(int msecs, Logger log) { |
41 | 0 | this.msecs = msecs; |
42 | 0 | this.log = log; |
43 | 0 | } |
44 | ||
45 | /** | |
46 | * Print to the info log level if the minimum waiting time was reached. | |
47 | * | |
48 | * @param msg Message to print | |
49 | */ | |
50 | public void info(String msg) { | |
51 | 0 | if (isPrintable()) { |
52 | 0 | log.info(msg); |
53 | } | |
54 | 0 | } |
55 | ||
56 | /** | |
57 | * Is the log message printable (minimum interval met)? | |
58 | * | |
59 | * @return True if the message is printable | |
60 | */ | |
61 | public boolean isPrintable() { | |
62 | 0 | if (System.currentTimeMillis() > lastPrint + msecs) { |
63 | 0 | lastPrint = System.currentTimeMillis(); |
64 | 0 | return true; |
65 | } | |
66 | ||
67 | 0 | return false; |
68 | } | |
69 | } |