Add a Log4j 2.x Java agent
[lttng-ust.git] / liblttng-ust-java-agent / jni / log4j / lttng_ust_log4j.c
CommitLineData
43e5396b 1/*
8ab5c06b 2 * Copyright (C) 2016 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
43e5396b
DG
3 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
1ddceb36 20#define _LGPL_SOURCE
6e7bc9e0 21#include "org_lttng_ust_agent_log4j_LttngLog4jApi.h"
43e5396b 22
6e8e8cb8 23#include "lttng_ust_log4j_tp.h"
8ab5c06b 24#include "../common/lttng_ust_context.h"
43e5396b 25
9aabed2d 26/*
8ab5c06b 27 * Deprecated function from before the context information was passed.
9aabed2d 28 */
6e7bc9e0 29JNIEXPORT void JNICALL Java_org_lttng_ust_agent_log4j_LttngLog4jApi_tracepoint(JNIEnv *env,
43e5396b
DG
30 jobject jobj,
31 jstring msg,
32 jstring logger_name,
33 jstring class_name,
34 jstring method_name,
501f6777
CB
35 jstring file_name,
36 jint line_number,
37 jlong timestamp,
38 jint loglevel,
39 jstring thread_name)
43e5396b
DG
40{
41 jboolean iscopy;
42 const char *msg_cstr = (*env)->GetStringUTFChars(env, msg, &iscopy);
43 const char *logger_name_cstr = (*env)->GetStringUTFChars(env, logger_name, &iscopy);
44 const char *class_name_cstr = (*env)->GetStringUTFChars(env, class_name, &iscopy);
45 const char *method_name_cstr = (*env)->GetStringUTFChars(env, method_name, &iscopy);
501f6777
CB
46 const char *file_name_cstr = (*env)->GetStringUTFChars(env, file_name, &iscopy);
47 const char *thread_name_cstr = (*env)->GetStringUTFChars(env, thread_name, &iscopy);
43e5396b 48
8286ff50 49 tracepoint(lttng_log4j, event, msg_cstr, logger_name_cstr,
501f6777
CB
50 class_name_cstr, method_name_cstr, file_name_cstr,
51 line_number, timestamp, loglevel, thread_name_cstr);
9aabed2d
DG
52
53 (*env)->ReleaseStringUTFChars(env, msg, msg_cstr);
54 (*env)->ReleaseStringUTFChars(env, logger_name, logger_name_cstr);
55 (*env)->ReleaseStringUTFChars(env, class_name, class_name_cstr);
56 (*env)->ReleaseStringUTFChars(env, method_name, method_name_cstr);
501f6777
CB
57 (*env)->ReleaseStringUTFChars(env, file_name, file_name_cstr);
58 (*env)->ReleaseStringUTFChars(env, thread_name, thread_name_cstr);
9aabed2d
DG
59}
60
8ab5c06b
AM
61/*
62 * Tracepoint used by Java applications using the log4j handler.
63 */
64JNIEXPORT void JNICALL Java_org_lttng_ust_agent_log4j_LttngLog4jApi_tracepointWithContext(JNIEnv *env,
65 jobject jobj,
66 jstring msg,
67 jstring logger_name,
68 jstring class_name,
69 jstring method_name,
70 jstring file_name,
71 jint line_number,
72 jlong timestamp,
73 jint loglevel,
74 jstring thread_name,
b1ca4c5f
AM
75 jbyteArray context_info_entries,
76 jbyteArray context_info_strings)
8ab5c06b
AM
77{
78 jboolean iscopy;
79 const char *msg_cstr = (*env)->GetStringUTFChars(env, msg, &iscopy);
80 const char *logger_name_cstr = (*env)->GetStringUTFChars(env, logger_name, &iscopy);
81 const char *class_name_cstr = (*env)->GetStringUTFChars(env, class_name, &iscopy);
82 const char *method_name_cstr = (*env)->GetStringUTFChars(env, method_name, &iscopy);
83 const char *file_name_cstr = (*env)->GetStringUTFChars(env, file_name, &iscopy);
84 const char *thread_name_cstr = (*env)->GetStringUTFChars(env, thread_name, &iscopy);
b1ca4c5f
AM
85 signed char *context_info_entries_array;
86 signed char *context_info_strings_array;
8ab5c06b
AM
87
88 /*
89 * Write these to the TLS variables, so that the UST callbacks in
90 * lttng_ust_context.c can access them.
91 */
b1ca4c5f
AM
92 context_info_entries_array = (*env)->GetByteArrayElements(env, context_info_entries, &iscopy);
93 lttng_ust_context_info_tls.ctx_entries = (struct lttng_ust_jni_ctx_entry *) context_info_entries_array;
94 lttng_ust_context_info_tls.ctx_entries_len = (*env)->GetArrayLength(env, context_info_entries);
95 context_info_strings_array = (*env)->GetByteArrayElements(env, context_info_strings, &iscopy);
96 lttng_ust_context_info_tls.ctx_strings = context_info_strings_array;
97 lttng_ust_context_info_tls.ctx_strings_len = (*env)->GetArrayLength(env, context_info_strings);
8ab5c06b
AM
98
99 tracepoint(lttng_log4j, event, msg_cstr, logger_name_cstr,
100 class_name_cstr, method_name_cstr, file_name_cstr,
101 line_number, timestamp, loglevel, thread_name_cstr);
102
b1ca4c5f
AM
103 lttng_ust_context_info_tls.ctx_entries = NULL;
104 lttng_ust_context_info_tls.ctx_entries_len = 0;
105 lttng_ust_context_info_tls.ctx_strings = NULL;
106 lttng_ust_context_info_tls.ctx_strings_len = 0;
8ab5c06b
AM
107 (*env)->ReleaseStringUTFChars(env, msg, msg_cstr);
108 (*env)->ReleaseStringUTFChars(env, logger_name, logger_name_cstr);
109 (*env)->ReleaseStringUTFChars(env, class_name, class_name_cstr);
110 (*env)->ReleaseStringUTFChars(env, method_name, method_name_cstr);
111 (*env)->ReleaseStringUTFChars(env, file_name, file_name_cstr);
112 (*env)->ReleaseStringUTFChars(env, thread_name, thread_name_cstr);
b1ca4c5f
AM
113 (*env)->ReleaseByteArrayElements(env, context_info_entries, context_info_entries_array, 0);
114 (*env)->ReleaseByteArrayElements(env, context_info_strings, context_info_strings_array, 0);
8ab5c06b 115}
This page took 0.048676 seconds and 5 git commands to generate.