Commit | Line | Data |
---|---|---|
464c4756 MJ |
1 | /* |
2 | * SPDX-License-Identifier: LGPL-2.1-only | |
3 | * | |
4 | * Copyright (C) 2016-2022 EfficiOS Inc. | |
5 | * Copyright (C) 2016 Alexandre Montplaisir <alexmonthy@efficios.com> | |
6 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | */ | |
8 | ||
9 | #define _LGPL_SOURCE | |
10 | #include "org_lttng_ust_agent_log4j2_LttngLog4j2Api.h" | |
11 | #include "lttng_ust_log4j_tp.h" | |
12 | #include "../common/lttng_ust_context.h" | |
13 | ||
14 | /* | |
15 | * Those are an exact map from the class org.apache.log4j.Level. | |
16 | */ | |
17 | enum loglevel_log4j1 { | |
18 | LOGLEVEL_LOG4J1_OFF = INT32_MAX, | |
19 | LOGLEVEL_LOG4J1_FATAL = 50000, | |
20 | LOGLEVEL_LOG4J1_ERROR = 40000, | |
21 | LOGLEVEL_LOG4J1_WARN = 30000, | |
22 | LOGLEVEL_LOG4J1_INFO = 20000, | |
23 | LOGLEVEL_LOG4J1_DEBUG = 10000, | |
24 | LOGLEVEL_LOG4J1_TRACE = 5000, | |
25 | LOGLEVEL_LOG4J1_ALL = INT32_MIN, | |
26 | }; | |
27 | ||
28 | /* | |
29 | * Those are an exact map from the class | |
30 | * org.apache.logging.log4j.spi.StandardLevel. | |
31 | */ | |
32 | enum loglevel_log4j2 { | |
33 | LOGLEVEL_LOG4J2_OFF = 0, | |
34 | LOGLEVEL_LOG4J2_FATAL = 100, | |
35 | LOGLEVEL_LOG4J2_ERROR = 200, | |
36 | LOGLEVEL_LOG4J2_WARN = 300, | |
37 | LOGLEVEL_LOG4J2_INFO = 400, | |
38 | LOGLEVEL_LOG4J2_DEBUG = 500, | |
39 | LOGLEVEL_LOG4J2_TRACE = 600, | |
40 | LOGLEVEL_LOG4J2_ALL = INT32_MAX, | |
41 | }; | |
42 | ||
43 | /* | |
44 | * The integer values of the loglevels has obviously changed in log4j2, | |
45 | * translate them to the values of log4j1 since they are exposed in the API of | |
46 | * lttng-tools. | |
47 | * | |
48 | * Custom loglevels might pose a problem when using ranges. | |
49 | */ | |
50 | static jint loglevel_2x_to_1x(jint loglevel) | |
51 | { | |
52 | switch (loglevel) { | |
53 | case LOGLEVEL_LOG4J2_OFF: | |
54 | return LOGLEVEL_LOG4J1_OFF; | |
55 | case LOGLEVEL_LOG4J2_FATAL: | |
56 | return LOGLEVEL_LOG4J1_FATAL; | |
57 | case LOGLEVEL_LOG4J2_ERROR: | |
58 | return LOGLEVEL_LOG4J1_ERROR; | |
59 | case LOGLEVEL_LOG4J2_WARN: | |
60 | return LOGLEVEL_LOG4J1_WARN; | |
61 | case LOGLEVEL_LOG4J2_INFO: | |
62 | return LOGLEVEL_LOG4J1_INFO; | |
63 | case LOGLEVEL_LOG4J2_DEBUG: | |
64 | return LOGLEVEL_LOG4J1_DEBUG; | |
65 | case LOGLEVEL_LOG4J2_TRACE: | |
66 | return LOGLEVEL_LOG4J1_TRACE; | |
67 | case LOGLEVEL_LOG4J2_ALL: | |
68 | return LOGLEVEL_LOG4J1_ALL; | |
69 | default: | |
70 | /* Handle custom loglevels. */ | |
71 | return loglevel; | |
72 | } | |
73 | } | |
74 | ||
75 | /* | |
76 | * Tracepoint used by Java applications using the log4j 2.x handler. | |
77 | */ | |
78 | JNIEXPORT void JNICALL Java_org_lttng_ust_agent_log4j2_LttngLog4j2Api_tracepointWithContext(JNIEnv *env, | |
79 | jobject jobj __attribute__((unused)), | |
80 | jstring message, | |
81 | jstring loggerName, | |
82 | jstring className, | |
83 | jstring methodName, | |
84 | jstring fileName, | |
85 | jint lineNumber, | |
86 | jlong timeStamp, | |
87 | jint logLevel, | |
88 | jstring threadName, | |
89 | jbyteArray context_info_entries, | |
90 | jbyteArray context_info_strings) | |
91 | { | |
92 | jboolean iscopy; | |
93 | const char *msg_cstr = (*env)->GetStringUTFChars(env, message, &iscopy); | |
94 | const char *logger_name_cstr = (*env)->GetStringUTFChars(env, loggerName, &iscopy); | |
95 | const char *class_name_cstr = (*env)->GetStringUTFChars(env, className, &iscopy); | |
96 | const char *method_name_cstr = (*env)->GetStringUTFChars(env, methodName, &iscopy); | |
97 | const char *file_name_cstr = (*env)->GetStringUTFChars(env, fileName, &iscopy); | |
98 | const char *thread_name_cstr = (*env)->GetStringUTFChars(env, threadName, &iscopy); | |
99 | signed char *context_info_entries_array; | |
100 | signed char *context_info_strings_array; | |
101 | ||
102 | /* | |
103 | * Write these to the TLS variables, so that the UST callbacks in | |
104 | * lttng_ust_context.c can access them. | |
105 | */ | |
106 | context_info_entries_array = (*env)->GetByteArrayElements(env, context_info_entries, &iscopy); | |
107 | lttng_ust_context_info_tls.ctx_entries = (struct lttng_ust_jni_ctx_entry *) context_info_entries_array; | |
108 | lttng_ust_context_info_tls.ctx_entries_len = (*env)->GetArrayLength(env, context_info_entries); | |
109 | context_info_strings_array = (*env)->GetByteArrayElements(env, context_info_strings, &iscopy); | |
110 | lttng_ust_context_info_tls.ctx_strings = context_info_strings_array; | |
111 | lttng_ust_context_info_tls.ctx_strings_len = (*env)->GetArrayLength(env, context_info_strings); | |
112 | ||
113 | lttng_ust_tracepoint(lttng_log4j, event, msg_cstr, logger_name_cstr, | |
114 | class_name_cstr, method_name_cstr, file_name_cstr, | |
115 | lineNumber, timeStamp, loglevel_2x_to_1x(logLevel), thread_name_cstr); | |
116 | ||
117 | lttng_ust_context_info_tls.ctx_entries = NULL; | |
118 | lttng_ust_context_info_tls.ctx_entries_len = 0; | |
119 | lttng_ust_context_info_tls.ctx_strings = NULL; | |
120 | lttng_ust_context_info_tls.ctx_strings_len = 0; | |
121 | (*env)->ReleaseStringUTFChars(env, message, msg_cstr); | |
122 | (*env)->ReleaseStringUTFChars(env, loggerName, logger_name_cstr); | |
123 | (*env)->ReleaseStringUTFChars(env, className, class_name_cstr); | |
124 | (*env)->ReleaseStringUTFChars(env, methodName, method_name_cstr); | |
125 | (*env)->ReleaseStringUTFChars(env, fileName, file_name_cstr); | |
126 | (*env)->ReleaseStringUTFChars(env, threadName, thread_name_cstr); | |
127 | (*env)->ReleaseByteArrayElements(env, context_info_entries, context_info_entries_array, 0); | |
128 | (*env)->ReleaseByteArrayElements(env, context_info_strings, context_info_strings_array, 0); | |
129 | } |