View Javadoc

1   /*
2    * This file is part of AceLogger.
3    * 
4    * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU
5    * Lesser General Public License as published by the Free Software Foundation, either version 3 of
6    * the License, or (at your option) any later version.
7    * 
8    * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10   * Lesser General Public License for more details.
11   * 
12   * You should have received a copy of the GNU Lesser General Public License along with AceLogger.
13   * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
14   */
15  package net.sourceforge.acelogger.location.gatherer;
16  
17  import net.sourceforge.acelogger.location.LogEventLocation;
18  import net.sourceforge.acelogger.location.resolver.CodeFrameResolver;
19  
20  /**
21   * An implementation of LocationGatherer that discovers only the file and line of the caller.
22   * 
23   * @author Zardi (https://sourceforge.net/users/daniel_zardi)
24   * @version 1.0.0
25   * @since 1.0.0
26   */
27  public class FileAndLineLocationGatherer extends BaseLocationGatherer {
28  
29  	/**
30  	 * Constructs a FileAndLineLocationGatherer with the supplied identifier and a
31  	 * UserCodeFrameResolver as it's resolver.
32  	 * 
33  	 * @param identifier
34  	 *            A string that identifies this gatherer.
35  	 * @since 1.0.0
36  	 */
37  	public FileAndLineLocationGatherer(String identifier) {
38  		super(identifier);
39  	}
40  
41  	/**
42  	 * Constructs a FileAndLineLocationGatherer with the supplied identifier and resolver.
43  	 * 
44  	 * @param identifier
45  	 *            A string that identifies this gatherer.
46  	 * @param frameResolver
47  	 *            The resolver that should be used to generate caller information.
48  	 * @since 1.0.0
49  	 */
50  	public FileAndLineLocationGatherer(String identifier, CodeFrameResolver frameResolver) {
51  		super(identifier, frameResolver);
52  	}
53  
54  	/** {@inheritDoc} */
55  	public LogEventLocation getCallLocation(Thread currentThread) {
56  		LogEventLocation location = new LogEventLocation();
57  		StackTraceElement userCodeFrame = getUserCodeFrame(currentThread);
58  		int lineNumber = userCodeFrame.getLineNumber();
59  		if (lineNumber > 0) {
60  			location.setLineNumber(lineNumber);
61  		}
62  		String fileName = userCodeFrame.getFileName();
63  		if (fileName == null) {
64  			fileName = "";
65  		}
66  		location.setFileName(fileName);
67  		location.setThreadName(currentThread.getName());
68  		return location;
69  	}
70  
71  }