Commit | Line | Data |
---|---|---|
43c692e7 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 | ||
784d97f6 MJ |
43 | /* |
44 | * Convert a custom Log4j 2.x loglevel to its equivalent 1.x standard loglevel. | |
45 | */ | |
46 | static jint loglevel_custom_2x_to_standard_1x(jint loglevel) | |
47 | { | |
48 | if (loglevel <= LOGLEVEL_LOG4J2_OFF) { | |
49 | return LOGLEVEL_LOG4J1_OFF; | |
50 | } else if (loglevel <= LOGLEVEL_LOG4J2_FATAL) { | |
51 | return LOGLEVEL_LOG4J1_FATAL; | |
52 | } else if (loglevel <= LOGLEVEL_LOG4J2_ERROR) { | |
53 | return LOGLEVEL_LOG4J1_ERROR; | |
54 | } else if (loglevel <= LOGLEVEL_LOG4J2_WARN) { | |
55 | return LOGLEVEL_LOG4J1_WARN; | |
56 | } else if (loglevel <= LOGLEVEL_LOG4J2_INFO) { | |
57 | return LOGLEVEL_LOG4J1_INFO; | |
58 | } else if (loglevel <= LOGLEVEL_LOG4J2_DEBUG) { | |
59 | return LOGLEVEL_LOG4J1_DEBUG; | |
60 | } else if (loglevel <= LOGLEVEL_LOG4J2_TRACE) { | |
61 | return LOGLEVEL_LOG4J1_TRACE; | |
62 | } else { | |
63 | return LOGLEVEL_LOG4J1_ALL; | |
64 | } | |
65 | } | |
66 | ||
43c692e7 MJ |
67 | /* |
68 | * The integer values of the loglevels has obviously changed in log4j2, | |
69 | * translate them to the values of log4j1 since they are exposed in the API of | |
70 | * lttng-tools. | |
43c692e7 MJ |
71 | */ |
72 | static jint loglevel_2x_to_1x(jint loglevel) | |
73 | { | |
74 | switch (loglevel) { | |
75 | case LOGLEVEL_LOG4J2_OFF: | |
76 | return LOGLEVEL_LOG4J1_OFF; | |
77 | case LOGLEVEL_LOG4J2_FATAL: | |
78 | return LOGLEVEL_LOG4J1_FATAL; | |
79 | case LOGLEVEL_LOG4J2_ERROR: | |
80 | return LOGLEVEL_LOG4J1_ERROR; | |
81 | case LOGLEVEL_LOG4J2_WARN: | |
82 | return LOGLEVEL_LOG4J1_WARN; | |
83 | case LOGLEVEL_LOG4J2_INFO: | |
84 | return LOGLEVEL_LOG4J1_INFO; | |
85 | case LOGLEVEL_LOG4J2_DEBUG: | |
86 | return LOGLEVEL_LOG4J1_DEBUG; | |
87 | case LOGLEVEL_LOG4J2_TRACE: | |
88 | return LOGLEVEL_LOG4J1_TRACE; | |
89 | case LOGLEVEL_LOG4J2_ALL: | |
90 | return LOGLEVEL_LOG4J1_ALL; | |
91 | default: | |
92 | /* Handle custom loglevels. */ | |
784d97f6 | 93 | return loglevel_custom_2x_to_standard_1x(loglevel); |
43c692e7 MJ |
94 | } |
95 | } | |
96 | ||
97 | /* | |
98 | * Tracepoint used by Java applications using the log4j 2.x handler. | |
99 | */ | |
100 | JNIEXPORT void JNICALL Java_org_lttng_ust_agent_log4j2_LttngLog4j2Api_tracepointWithContext(JNIEnv *env, | |
101 | jobject jobj __attribute__((unused)), | |
102 | jstring message, | |
103 | jstring loggerName, | |
104 | jstring className, | |
105 | jstring methodName, | |
106 | jstring fileName, | |
107 | jint lineNumber, | |
108 | jlong timeStamp, | |
109 | jint logLevel, | |
110 | jstring threadName, | |
111 | jbyteArray context_info_entries, | |
112 | jbyteArray context_info_strings) | |
113 | { | |
114 | jboolean iscopy; | |
115 | const char *msg_cstr = (*env)->GetStringUTFChars(env, message, &iscopy); | |
116 | const char *logger_name_cstr = (*env)->GetStringUTFChars(env, loggerName, &iscopy); | |
117 | const char *class_name_cstr = (*env)->GetStringUTFChars(env, className, &iscopy); | |
118 | const char *method_name_cstr = (*env)->GetStringUTFChars(env, methodName, &iscopy); | |
119 | const char *file_name_cstr = (*env)->GetStringUTFChars(env, fileName, &iscopy); | |
120 | const char *thread_name_cstr = (*env)->GetStringUTFChars(env, threadName, &iscopy); | |
121 | signed char *context_info_entries_array; | |
122 | signed char *context_info_strings_array; | |
123 | ||
124 | /* | |
125 | * Write these to the TLS variables, so that the UST callbacks in | |
126 | * lttng_ust_context.c can access them. | |
127 | */ | |
128 | context_info_entries_array = (*env)->GetByteArrayElements(env, context_info_entries, &iscopy); | |
129 | lttng_ust_context_info_tls.ctx_entries = (struct lttng_ust_jni_ctx_entry *) context_info_entries_array; | |
130 | lttng_ust_context_info_tls.ctx_entries_len = (*env)->GetArrayLength(env, context_info_entries); | |
131 | context_info_strings_array = (*env)->GetByteArrayElements(env, context_info_strings, &iscopy); | |
132 | lttng_ust_context_info_tls.ctx_strings = context_info_strings_array; | |
133 | lttng_ust_context_info_tls.ctx_strings_len = (*env)->GetArrayLength(env, context_info_strings); | |
134 | ||
135 | lttng_ust_tracepoint(lttng_log4j, event, msg_cstr, logger_name_cstr, | |
136 | class_name_cstr, method_name_cstr, file_name_cstr, | |
137 | lineNumber, timeStamp, loglevel_2x_to_1x(logLevel), thread_name_cstr); | |
138 | ||
139 | lttng_ust_context_info_tls.ctx_entries = NULL; | |
140 | lttng_ust_context_info_tls.ctx_entries_len = 0; | |
141 | lttng_ust_context_info_tls.ctx_strings = NULL; | |
142 | lttng_ust_context_info_tls.ctx_strings_len = 0; | |
143 | (*env)->ReleaseStringUTFChars(env, message, msg_cstr); | |
144 | (*env)->ReleaseStringUTFChars(env, loggerName, logger_name_cstr); | |
145 | (*env)->ReleaseStringUTFChars(env, className, class_name_cstr); | |
146 | (*env)->ReleaseStringUTFChars(env, methodName, method_name_cstr); | |
147 | (*env)->ReleaseStringUTFChars(env, fileName, file_name_cstr); | |
148 | (*env)->ReleaseStringUTFChars(env, threadName, thread_name_cstr); | |
149 | (*env)->ReleaseByteArrayElements(env, context_info_entries, context_info_entries_array, 0); | |
150 | (*env)->ReleaseByteArrayElements(env, context_info_strings, context_info_strings_array, 0); | |
151 | } |