76 set_name(test_name);
77 remove(name());
78 }
79
80 ~TestLogFile() {
81 remove(name());
82 }
83
84 const char* name() {
85 return file_name;
86 }
87 };
88
89 class TestLogSavedConfig {
90 private:
91 char* _saved_config;
92 char* _new_output;
93 Log(logging) _log;
94 public:
95 TestLogSavedConfig(const char* apply_output = NULL, const char* apply_setting = NULL) : _new_output(0) {
96 _saved_config = os::strdup_check_oom(LogOutput::Stdout->config_string());
97 bool success = LogConfiguration::parse_log_arguments("stdout", "all=off", NULL, NULL, _log.error_stream());
98 assert(success, "test unable to turn all off");
99
100 if (apply_output) {
101 _new_output = os::strdup_check_oom(apply_output);
102 bool success = LogConfiguration::parse_log_arguments(_new_output, apply_setting, NULL, NULL, _log.error_stream());
103 assert(success, "test unable to apply test log configuration");
104 }
105 }
106
107 ~TestLogSavedConfig() {
108 if (_new_output) {
109 bool success = LogConfiguration::parse_log_arguments(_new_output, "all=off", NULL, NULL, _log.error_stream());
110 assert(success, "test unable to turn all off");
111 os::free(_new_output);
112 }
113
114 bool success = LogConfiguration::parse_log_arguments("stdout", _saved_config, NULL, NULL, _log.error_stream());
115 assert(success, "test unable to restore log configuration");
116 os::free(_saved_config);
117 }
118 };
119
120 void Test_configure_stdout() {
121 ResourceMark rm;
122 LogOutput* stdoutput = LogOutput::Stdout;
123 TestLogSavedConfig tlsc;
124
125 // Enable 'logging=info', verifying it has been set
126 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(logging));
127 assert_str_eq("logging=info,", stdoutput->config_string());
137 LogConfiguration::configure_stdout(LogLevel::Trace, false, LOG_TAGS(gc));
138 assert_char_in('+', stdoutput->config_string());
139 assert(log_is_enabled(Trace, gc), "logging was not properly enabled");
140
141 // Disable 'gc*' and 'logging', verifying all logging is properly disabled
142 LogConfiguration::configure_stdout(LogLevel::Off, false, LOG_TAGS(gc));
143 LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(logging));
144 assert_str_eq("all=off", stdoutput->config_string());
145 }
146
147 static int Test_logconfiguration_subscribe_triggered = 0;
148
149 static void Test_logconfiguration_subscribe_helper() {
150 Test_logconfiguration_subscribe_triggered++;
151 }
152
153 void Test_logconfiguration_subscribe() {
154 ResourceMark rm;
155 Log(logging) log;
156
157 TestLogSavedConfig log_cfg("stdout", "logging+test=trace");
158
159 LogConfiguration::register_update_listener(&Test_logconfiguration_subscribe_helper);
160
161 LogConfiguration::parse_log_arguments("stdout", "logging=trace", NULL, NULL, log.error_stream());
162 assert(Test_logconfiguration_subscribe_triggered == 1, "subscription not triggered (1)");
163
164 LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(gc));
165 assert(Test_logconfiguration_subscribe_triggered == 2, "subscription not triggered (2)");
166
167 LogConfiguration::disable_logging();
168 assert(Test_logconfiguration_subscribe_triggered == 3, "subscription not triggered (3)");
169
170 // We need to renable stderr error logging since "disable_logging" disable it all.
171 // TestLogSavedConfig log_cfg will only renable stdout for us.
172 LogConfiguration::parse_log_arguments("stderr", "all=warning", NULL, NULL, log.error_stream());
173 assert(Test_logconfiguration_subscribe_triggered == 4, "subscription not triggered (3)");
174 }
175
176 #define LOG_PREFIX_STR "THE_PREFIX "
177 #define LOG_LINE_STR "a log line"
248 LogTagType tag = ts->tag(i);
249 if (!other->contains(tag)) {
250 equal = false;
251 break;
252 }
253 }
254 // Since tagsets are implemented using template arguments, using both of
255 // the (logically equivalent) tagsets (t1, t2) and (t2, t1) somewhere will
256 // instantiate two different LogTagSetMappings. This causes multiple
257 // tagset instances to be created for the same logical set. We want to
258 // avoid this to save time, memory and prevent any confusion around it.
259 if (equal) {
260 char other_name[512];
261 other->label(other_name, sizeof(other_name), ",");
262 assert(false, "duplicate LogTagSets found: '%s' vs '%s' "
263 "(tags must always be specified in the same order for each tagset)",
264 ts_name, other_name);
265 }
266 }
267 }
268 }
269
270 #endif // PRODUCT
|
76 set_name(test_name);
77 remove(name());
78 }
79
80 ~TestLogFile() {
81 remove(name());
82 }
83
84 const char* name() {
85 return file_name;
86 }
87 };
88
89 class TestLogSavedConfig {
90 private:
91 char* _saved_config;
92 char* _new_output;
93 Log(logging) _log;
94 public:
95 TestLogSavedConfig(const char* apply_output = NULL, const char* apply_setting = NULL) : _new_output(0) {
96 ResourceMark rm;
97 _saved_config = os::strdup_check_oom(LogOutput::Stdout->config_string());
98 bool success = LogConfiguration::parse_log_arguments("stdout", "all=off", NULL, NULL, _log.error_stream());
99 assert(success, "test unable to turn all off");
100
101 if (apply_output) {
102 _new_output = os::strdup_check_oom(apply_output);
103 bool success = LogConfiguration::parse_log_arguments(_new_output, apply_setting, NULL, NULL, _log.error_stream());
104 assert(success, "test unable to apply test log configuration");
105 }
106 }
107
108 ~TestLogSavedConfig() {
109 ResourceMark rm;
110 if (_new_output) {
111 bool success = LogConfiguration::parse_log_arguments(_new_output, "all=off", NULL, NULL, _log.error_stream());
112 assert(success, "test unable to turn all off");
113 os::free(_new_output);
114 }
115
116 bool success = LogConfiguration::parse_log_arguments("stdout", _saved_config, NULL, NULL, _log.error_stream());
117 assert(success, "test unable to restore log configuration");
118 os::free(_saved_config);
119 }
120 };
121
122 void Test_configure_stdout() {
123 ResourceMark rm;
124 LogOutput* stdoutput = LogOutput::Stdout;
125 TestLogSavedConfig tlsc;
126
127 // Enable 'logging=info', verifying it has been set
128 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(logging));
129 assert_str_eq("logging=info,", stdoutput->config_string());
139 LogConfiguration::configure_stdout(LogLevel::Trace, false, LOG_TAGS(gc));
140 assert_char_in('+', stdoutput->config_string());
141 assert(log_is_enabled(Trace, gc), "logging was not properly enabled");
142
143 // Disable 'gc*' and 'logging', verifying all logging is properly disabled
144 LogConfiguration::configure_stdout(LogLevel::Off, false, LOG_TAGS(gc));
145 LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(logging));
146 assert_str_eq("all=off", stdoutput->config_string());
147 }
148
149 static int Test_logconfiguration_subscribe_triggered = 0;
150
151 static void Test_logconfiguration_subscribe_helper() {
152 Test_logconfiguration_subscribe_triggered++;
153 }
154
155 void Test_logconfiguration_subscribe() {
156 ResourceMark rm;
157 Log(logging) log;
158
159 TestLogSavedConfig log_cfg("stdout", "logging*=trace");
160
161 LogConfiguration::register_update_listener(&Test_logconfiguration_subscribe_helper);
162
163 LogConfiguration::parse_log_arguments("stdout", "logging=trace", NULL, NULL, log.error_stream());
164 assert(Test_logconfiguration_subscribe_triggered == 1, "subscription not triggered (1)");
165
166 LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(gc));
167 assert(Test_logconfiguration_subscribe_triggered == 2, "subscription not triggered (2)");
168
169 LogConfiguration::disable_logging();
170 assert(Test_logconfiguration_subscribe_triggered == 3, "subscription not triggered (3)");
171
172 // We need to renable stderr error logging since "disable_logging" disable it all.
173 // TestLogSavedConfig log_cfg will only renable stdout for us.
174 LogConfiguration::parse_log_arguments("stderr", "all=warning", NULL, NULL, log.error_stream());
175 assert(Test_logconfiguration_subscribe_triggered == 4, "subscription not triggered (3)");
176 }
177
178 #define LOG_PREFIX_STR "THE_PREFIX "
179 #define LOG_LINE_STR "a log line"
250 LogTagType tag = ts->tag(i);
251 if (!other->contains(tag)) {
252 equal = false;
253 break;
254 }
255 }
256 // Since tagsets are implemented using template arguments, using both of
257 // the (logically equivalent) tagsets (t1, t2) and (t2, t1) somewhere will
258 // instantiate two different LogTagSetMappings. This causes multiple
259 // tagset instances to be created for the same logical set. We want to
260 // avoid this to save time, memory and prevent any confusion around it.
261 if (equal) {
262 char other_name[512];
263 other->label(other_name, sizeof(other_name), ",");
264 assert(false, "duplicate LogTagSets found: '%s' vs '%s' "
265 "(tags must always be specified in the same order for each tagset)",
266 ts_name, other_name);
267 }
268 }
269 }
270 }
271
272 #define Test_logtarget_string_literal "First line"
273
274
275 static void Test_logtarget_on() {
276 TestLogFile log_file("log_target");
277 TestLogSavedConfig tlsc(log_file.name(), "gc=debug");
278
279 LogTarget(Debug, gc) log;
280
281 assert(log.is_enabled(), "assert");
282
283 // Log the line and expect it to be available in the output file.
284 log.print(Test_logtarget_string_literal);
285
286 FILE* fp = fopen(log_file.name(), "r");
287 assert(fp, "File read error");
288
289 char output[256 /* Large enough buffer */];
290 if (fgets(output, sizeof(output), fp) != NULL) {
291 assert(strstr(output, Test_logtarget_string_literal) != NULL, "log line missing");
292 }
293 fclose(fp);
294 }
295
296 static void Test_logtarget_off() {
297 TestLogFile log_file("log_target");
298 TestLogSavedConfig tlsc(log_file.name(), "gc=info");
299
300 LogTarget(Debug, gc) log;
301
302 // The log level for gc is info, so this log instance shouldn't be enabled.
303 assert(!log.is_enabled(), "assert");
304
305 // Try to log, but expect this to be filtered out.
306 log.print(Test_logtarget_string_literal);
307
308 FILE* fp = fopen(log_file.name(), "r");
309 assert(fp, "File read error");
310
311 char output[256 /* Large enough buffer */];
312 if (fgets(output, sizeof(output), fp) != NULL) {
313 assert(strstr(output, Test_logtarget_string_literal) == NULL, "log line not missing");
314 }
315 fclose(fp);
316 }
317
318 void Test_logtarget() {
319 Test_logtarget_on();
320 Test_logtarget_off();
321 }
322
323 #endif // PRODUCT
|