Commit | Line | Data |
---|---|---|
501f6777 CB |
1 | /* |
2 | * Copyright (C) 2014 - Christian Babeux <christian.babeux@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
11 | * for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public License | |
14 | * along with this library; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
18 | package org.lttng.ust.agent.log4j; | |
19 | ||
20 | import java.lang.String; | |
21 | ||
22 | import org.apache.log4j.AppenderSkeleton; | |
23 | import org.apache.log4j.spi.LoggingEvent; | |
24 | ||
25 | class LTTngLogAppender extends AppenderSkeleton { | |
26 | private Boolean is_root; | |
27 | ||
28 | public LTTngLogAppender(Boolean isRoot) { | |
29 | super(); | |
30 | this.is_root = isRoot; | |
31 | try { | |
32 | System.loadLibrary("lttng-ust-log4j-jni"); //$NON-NLS-1$ | |
33 | } catch (SecurityException e) { | |
34 | e.printStackTrace(); | |
35 | } catch (UnsatisfiedLinkError e) { | |
36 | e.printStackTrace(); | |
37 | } catch (NullPointerException e) { | |
38 | /* Should never happen */ | |
39 | e.printStackTrace(); | |
40 | } | |
41 | } | |
42 | ||
43 | public Boolean isRoot() { | |
44 | return this.is_root; | |
45 | } | |
46 | ||
47 | @Override | |
48 | protected void append(LoggingEvent event) { | |
49 | int line; | |
50 | ||
51 | /* | |
52 | * The line number returned from LocationInformation is a | |
53 | * string. At least try to convert to a proper int. | |
54 | */ | |
55 | try { | |
56 | String lineString = event.getLocationInformation().getLineNumber(); | |
57 | line = Integer.parseInt(lineString); | |
58 | } catch (NumberFormatException n) { | |
59 | line = -1; | |
60 | } | |
61 | ||
62 | if (this.is_root) { | |
63 | tracepointS(event.getRenderedMessage(), | |
64 | event.getLoggerName(), | |
65 | event.getLocationInformation().getClassName(), | |
66 | event.getLocationInformation().getMethodName(), | |
67 | event.getLocationInformation().getFileName(), | |
68 | line, | |
69 | event.getTimeStamp(), | |
70 | event.getLevel().toInt(), | |
71 | event.getThreadName()); | |
72 | } else { | |
73 | tracepointU(event.getRenderedMessage(), | |
74 | event.getLoggerName(), | |
75 | event.getLocationInformation().getClassName(), | |
76 | event.getLocationInformation().getMethodName(), | |
77 | event.getLocationInformation().getFileName(), | |
78 | line, | |
79 | event.getTimeStamp(), | |
80 | event.getLevel().toInt(), | |
81 | event.getThreadName()); | |
82 | } | |
83 | } | |
84 | ||
85 | @Override | |
86 | public void close() {} | |
87 | ||
88 | @Override | |
89 | public boolean requiresLayout() { | |
90 | return false; | |
91 | } | |
92 | ||
93 | /* Use for a user session daemon. */ | |
94 | private native void tracepointU(String msg, | |
95 | String logger_name, | |
96 | String class_name, | |
97 | String method_name, | |
98 | String file_name, | |
99 | int line_number, | |
100 | long timestamp, | |
101 | int loglevel, | |
102 | String thread_name); | |
103 | ||
104 | /* Use for a root session daemon. */ | |
105 | private native void tracepointS(String msg, | |
106 | String logger_name, | |
107 | String class_name, | |
108 | String method_name, | |
109 | String file_name, | |
110 | int line_number, | |
111 | long timestamp, | |
112 | int loglevel, | |
113 | String thread_name); | |
114 | } |