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 { | |
08284556 AM |
26 | |
27 | private Boolean isRoot; | |
501f6777 CB |
28 | |
29 | public LTTngLogAppender(Boolean isRoot) { | |
30 | super(); | |
08284556 | 31 | this.isRoot = isRoot; |
501f6777 CB |
32 | try { |
33 | System.loadLibrary("lttng-ust-log4j-jni"); //$NON-NLS-1$ | |
34 | } catch (SecurityException e) { | |
35 | e.printStackTrace(); | |
36 | } catch (UnsatisfiedLinkError e) { | |
37 | e.printStackTrace(); | |
38 | } catch (NullPointerException e) { | |
39 | /* Should never happen */ | |
40 | e.printStackTrace(); | |
41 | } | |
42 | } | |
43 | ||
44 | public Boolean isRoot() { | |
08284556 | 45 | return this.isRoot; |
501f6777 CB |
46 | } |
47 | ||
48 | @Override | |
49 | protected void append(LoggingEvent event) { | |
50 | int line; | |
51 | ||
52 | /* | |
53 | * The line number returned from LocationInformation is a | |
54 | * string. At least try to convert to a proper int. | |
55 | */ | |
56 | try { | |
57 | String lineString = event.getLocationInformation().getLineNumber(); | |
58 | line = Integer.parseInt(lineString); | |
59 | } catch (NumberFormatException n) { | |
60 | line = -1; | |
61 | } | |
62 | ||
08284556 | 63 | if (this.isRoot) { |
501f6777 | 64 | tracepointS(event.getRenderedMessage(), |
08284556 AM |
65 | event.getLoggerName(), |
66 | event.getLocationInformation().getClassName(), | |
67 | event.getLocationInformation().getMethodName(), | |
68 | event.getLocationInformation().getFileName(), | |
69 | line, | |
70 | event.getTimeStamp(), | |
71 | event.getLevel().toInt(), | |
72 | event.getThreadName()); | |
501f6777 CB |
73 | } else { |
74 | tracepointU(event.getRenderedMessage(), | |
08284556 AM |
75 | event.getLoggerName(), |
76 | event.getLocationInformation().getClassName(), | |
77 | event.getLocationInformation().getMethodName(), | |
78 | event.getLocationInformation().getFileName(), | |
79 | line, | |
80 | event.getTimeStamp(), | |
81 | event.getLevel().toInt(), | |
82 | event.getThreadName()); | |
501f6777 CB |
83 | } |
84 | } | |
85 | ||
86 | @Override | |
87 | public void close() {} | |
88 | ||
89 | @Override | |
90 | public boolean requiresLayout() { | |
91 | return false; | |
92 | } | |
93 | ||
94 | /* Use for a user session daemon. */ | |
95 | private native void tracepointU(String msg, | |
96 | String logger_name, | |
97 | String class_name, | |
98 | String method_name, | |
99 | String file_name, | |
100 | int line_number, | |
101 | long timestamp, | |
102 | int loglevel, | |
103 | String thread_name); | |
104 | ||
105 | /* Use for a root session daemon. */ | |
106 | private native void tracepointS(String msg, | |
107 | String logger_name, | |
108 | String class_name, | |
109 | String method_name, | |
110 | String file_name, | |
111 | int line_number, | |
112 | long timestamp, | |
113 | int loglevel, | |
114 | String thread_name); | |
115 | } |