Skip to content

Commit a5abe7b

Browse files
rfvirgilshuahkh
authored andcommitted
kunit: string-stream: Add option to make all lines end with newline
Add an optional feature to string_stream that will append a newline to any added string that does not already end with a newline. The purpose of this is so that string_stream can be used to collect log lines. This is enabled/disabled by calling string_stream_set_append_newlines(). Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com> Reviewed-by: Rae Moar <rmoar@google.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 4551cac commit a5abe7b

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/kunit/string-stream.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,46 @@ int string_stream_vadd(struct string_stream *stream,
4444
va_list args)
4545
{
4646
struct string_stream_fragment *frag_container;
47-
int len;
47+
int buf_len, result_len;
4848
va_list args_for_counting;
4949

5050
/* Make a copy because `vsnprintf` could change it */
5151
va_copy(args_for_counting, args);
5252

5353
/* Evaluate length of formatted string */
54-
len = vsnprintf(NULL, 0, fmt, args_for_counting);
54+
buf_len = vsnprintf(NULL, 0, fmt, args_for_counting);
5555

5656
va_end(args_for_counting);
5757

58-
if (len == 0)
58+
if (buf_len == 0)
5959
return 0;
6060

61+
/* Reserve one extra for possible appended newline. */
62+
if (stream->append_newlines)
63+
buf_len++;
64+
6165
/* Need space for null byte. */
62-
len++;
66+
buf_len++;
6367

6468
frag_container = alloc_string_stream_fragment(stream->test,
65-
len,
69+
buf_len,
6670
stream->gfp);
6771
if (IS_ERR(frag_container))
6872
return PTR_ERR(frag_container);
6973

70-
len = vsnprintf(frag_container->fragment, len, fmt, args);
74+
if (stream->append_newlines) {
75+
/* Don't include reserved newline byte in writeable length. */
76+
result_len = vsnprintf(frag_container->fragment, buf_len - 1, fmt, args);
77+
78+
/* Append newline if necessary. */
79+
if (frag_container->fragment[result_len - 1] != '\n')
80+
result_len = strlcat(frag_container->fragment, "\n", buf_len);
81+
} else {
82+
result_len = vsnprintf(frag_container->fragment, buf_len, fmt, args);
83+
}
84+
7185
spin_lock(&stream->lock);
72-
stream->length += len;
86+
stream->length += result_len;
7387
list_add_tail(&frag_container->node, &stream->fragments);
7488
spin_unlock(&stream->lock);
7589

lib/kunit/string-stream.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct string_stream {
2525
spinlock_t lock;
2626
struct kunit *test;
2727
gfp_t gfp;
28+
bool append_newlines;
2829
};
2930

3031
struct kunit;
@@ -47,4 +48,10 @@ bool string_stream_is_empty(struct string_stream *stream);
4748

4849
void string_stream_destroy(struct string_stream *stream);
4950

51+
static inline void string_stream_set_append_newlines(struct string_stream *stream,
52+
bool append_newlines)
53+
{
54+
stream->append_newlines = append_newlines;
55+
}
56+
5057
#endif /* _KUNIT_STRING_STREAM_H */

0 commit comments

Comments
 (0)