Skip to content

Commit 4a0093c

Browse files
committed
8294362: UL: Replace the internal usage of manual buffers with stringStream in LogSelection
Reviewed-by: dholmes, rehn
1 parent fef68bb commit 4a0093c

File tree

6 files changed

+34
-76
lines changed

6 files changed

+34
-76
lines changed

src/hotspot/share/logging/logOutput.cpp

+5-27
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
#include "runtime/mutexLocker.hpp"
3333
#include "runtime/os.hpp"
3434

35-
LogOutput::~LogOutput() {
36-
os::free(_config_string);
37-
}
38-
3935
void LogOutput::describe(outputStream *out) {
4036
out->print("%s ", name());
4137
out->print_raw(config_string()); // raw printed because length might exceed O_BUFLEN
@@ -56,34 +52,16 @@ void LogOutput::describe(outputStream *out) {
5652
}
5753

5854
void LogOutput::set_config_string(const char* string) {
59-
os::free(_config_string);
60-
_config_string = os::strdup(string, mtLogging);
61-
_config_string_buffer_size = strlen(_config_string) + 1;
55+
_config_string.reset();
56+
_config_string.print_raw(string);
6257
}
6358

6459
void LogOutput::add_to_config_string(const LogSelection& selection) {
65-
if (_config_string_buffer_size < InitialConfigBufferSize) {
66-
_config_string_buffer_size = InitialConfigBufferSize;
67-
_config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging);
68-
}
69-
70-
size_t offset = strlen(_config_string);
71-
if (offset > 0) {
60+
if (_config_string.size() > 0) {
7261
// Add commas in-between tag and level combinations in the config string
73-
_config_string[offset++] = ',';
62+
_config_string.print_raw(",");
7463
}
75-
76-
for (;;) {
77-
int ret = selection.describe(_config_string + offset,
78-
_config_string_buffer_size - offset);
79-
if (ret == -1) {
80-
// Double the buffer size and retry
81-
_config_string_buffer_size *= 2;
82-
_config_string = REALLOC_C_HEAP_ARRAY(char, _config_string, _config_string_buffer_size, mtLogging);
83-
continue;
84-
}
85-
break;
86-
};
64+
selection.describe_on(&_config_string);
8765
}
8866

8967

src/hotspot/share/logging/logOutput.hpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "logging/logMessageBuffer.hpp"
3030
#include "memory/allocation.hpp"
3131
#include "utilities/globalDefinitions.hpp"
32+
#include "utilities/ostream.hpp"
3233

3334
class LogDecorations;
3435
class LogMessageBuffer;
@@ -43,24 +44,21 @@ class LogOutput : public CHeapObj<mtLogging> {
4344
friend class LogConfiguration;
4445

4546
private:
46-
static const size_t InitialConfigBufferSize = 256;
47-
4847
// Track if the output has been reconfigured dynamically during runtime.
4948
// The status is set each time the configuration of the output is modified,
5049
// and is reset once after logging initialization is complete.
5150
// This is only used during logging of the configuration.
5251
bool _reconfigured;
5352

54-
char* _config_string;
55-
size_t _config_string_buffer_size;
53+
stringStream _config_string;
5654

5755
// Adds the log selection to the config description (e.g. "tag1+tag2*=level").
5856
void add_to_config_string(const LogSelection& selection);
5957

6058
protected:
6159
LogDecorators _decorators;
6260

63-
// Replaces the current config description with the given string.
61+
// Replaces the current config description with a copy of the given string.
6462
void set_config_string(const char* string);
6563

6664
// Update the config string for this output to reflect its current configuration
@@ -80,13 +78,13 @@ class LogOutput : public CHeapObj<mtLogging> {
8078
}
8179

8280
const char* config_string() const {
83-
return _config_string;
81+
return _config_string.base();
8482
}
8583

86-
LogOutput() : _reconfigured(false), _config_string(NULL), _config_string_buffer_size(0) {
84+
LogOutput() : _reconfigured(false), _config_string(){
8785
}
8886

89-
virtual ~LogOutput();
87+
virtual ~LogOutput() {};
9088

9189
// If the output can be rotated, trigger a forced rotation, otherwise do nothing.
9290
// Log outputs with rotation capabilities should override this.

src/hotspot/share/logging/logSelection.cpp

+8-27
Original file line numberDiff line numberDiff line change
@@ -202,36 +202,18 @@ size_t LogSelection::tag_sets_selected() const {
202202
return _tag_sets_selected;
203203
}
204204

205-
int LogSelection::describe_tags(char* buf, size_t bufsize) const {
206-
int tot_written = 0;
205+
void LogSelection::describe_tags_on(outputStream* out) const {
207206
for (size_t i = 0; i < _ntags; i++) {
208-
int written = jio_snprintf(buf + tot_written, bufsize - tot_written,
209-
"%s%s", (i == 0 ? "" : "+"), LogTag::name(_tags[i]));
210-
if (written == -1) {
211-
return written;
212-
}
213-
tot_written += written;
207+
out->print("%s%s", (i == 0 ? "" : "+"), LogTag::name(_tags[i]));
214208
}
215-
216209
if (_wildcard) {
217-
int written = jio_snprintf(buf + tot_written, bufsize - tot_written, "*");
218-
if (written == -1) {
219-
return written;
220-
}
221-
tot_written += written;
210+
out->print("*");
222211
}
223-
return tot_written;
224212
}
225213

226-
int LogSelection::describe(char* buf, size_t bufsize) const {
227-
int tot_written = describe_tags(buf, bufsize);
228-
229-
int written = jio_snprintf(buf + tot_written, bufsize - tot_written, "=%s", LogLevel::name(_level));
230-
if (written == -1) {
231-
return -1;
232-
}
233-
tot_written += written;
234-
return tot_written;
214+
void LogSelection::describe_on(outputStream* out) const {
215+
describe_tags_on(out);
216+
out->print("=%s", LogLevel::name(_level));
235217
}
236218

237219
double LogSelection::similarity(const LogSelection& other) const {
@@ -345,8 +327,7 @@ void LogSelection::suggest_similar_matching(outputStream* out) const {
345327

346328
out->print("Did you mean any of the following?");
347329
for (size_t i = 0; i < nsuggestions; i++) {
348-
char buf[128];
349-
suggestions[i].describe_tags(buf, sizeof(buf));
350-
out->print(" %s", buf);
330+
out->print(" ");
331+
suggestions[i].describe_tags_on(out);
351332
}
352333
}

src/hotspot/share/logging/logSelection.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class LogSelection : public StackObj {
6161
bool selects(const LogTagSet& ts) const;
6262
bool consists_of(const LogTagType tags[LogTag::MaxTags]) const;
6363

64-
int describe_tags(char* buf, size_t bufsize) const;
65-
int describe(char* buf, size_t bufsize) const;
64+
void describe_tags_on(outputStream* out) const;
65+
void describe_on(outputStream* out) const;
6666

6767
// List similar selections that matches existing tag sets on the given outputstream
6868
void suggest_similar_matching(outputStream* out) const;

src/hotspot/share/logging/logSelectionList.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ bool LogSelectionList::verify_selections(outputStream* out) const {
4242
out->print("No tag set matches selection:");
4343
valid = false;
4444

45-
char buf[256];
46-
_selections[i].describe_tags(buf, sizeof(buf));
47-
out->print(" %s. ", buf);
45+
out->print(" ");
46+
_selections[i].describe_tags_on(out);
47+
out->print(". ");
4848

4949
_selections[i].suggest_similar_matching(out);
5050
out->cr();

test/hotspot/gtest/logging/test_logSelection.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "logging/logSelection.hpp"
2828
#include "logging/logTagSet.hpp"
2929
#include "utilities/globalDefinitions.hpp"
30+
#include "utilities/ostream.hpp"
3031
#include "logTestUtils.inline.hpp"
3132
#include "unittest.hpp"
3233

@@ -41,9 +42,9 @@ void PrintTo(const LogSelection& sel, ::std::ostream* os) {
4142
*os << "LogSelection::Invalid";
4243
return;
4344
}
44-
char buf[256];
45-
sel.describe(buf, sizeof(buf));
46-
*os << buf;
45+
stringStream ss;
46+
sel.describe_on(&ss);
47+
*os << ss.freeze();
4748
}
4849

4950
TEST(LogSelection, sanity) {
@@ -199,19 +200,19 @@ TEST(LogSelection, consists_of) {
199200
}
200201

201202
TEST(LogSelection, describe_tags) {
202-
char buf[256];
203+
stringStream ss;
203204
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
204205
LogSelection selection(tags, true, LogLevel::Off);
205-
selection.describe_tags(buf, sizeof(buf));
206-
EXPECT_STREQ("logging+test*", buf);
206+
selection.describe_tags_on(&ss);
207+
EXPECT_STREQ("logging+test*", ss.freeze());
207208
}
208209

209210
TEST(LogSelection, describe) {
210-
char buf[256];
211+
stringStream ss;
211212
LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };
212213
LogSelection selection(tags, true, LogLevel::Off);
213-
selection.describe(buf, sizeof(buf));
214-
EXPECT_STREQ("logging+test*=off", buf);
214+
selection.describe_on(&ss);
215+
EXPECT_STREQ("logging+test*=off", ss.freeze());
215216
}
216217

217218
#endif

0 commit comments

Comments
 (0)