Commit 702a4ef0 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Add tabstops to printbufs

Now, when outputting to printbufs, we can set tabstops and left or right
justify text to them - this is to be used by the userspace 'bcachefs fs
usage' command.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
parent f61816d0
...@@ -1061,7 +1061,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb, ...@@ -1061,7 +1061,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb,
pr_buf(out, "Device: %u", i); pr_buf(out, "Device: %u", i);
pr_newline(out); pr_newline(out);
printbuf_indent_push(out, 2); pr_indent_push(out, 2);
pr_buf(out, "UUID: "); pr_buf(out, "UUID: ");
pr_uuid(out, m->uuid.b); pr_uuid(out, m->uuid.b);
...@@ -1129,7 +1129,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb, ...@@ -1129,7 +1129,7 @@ static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb,
BCH_MEMBER_DISCARD(m)); BCH_MEMBER_DISCARD(m));
pr_newline(out); pr_newline(out);
printbuf_indent_pop(out, 2); pr_indent_pop(out, 2);
} }
} }
...@@ -1471,9 +1471,9 @@ void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb, ...@@ -1471,9 +1471,9 @@ void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
pr_newline(out); pr_newline(out);
if (ops && ops->to_text) { if (ops && ops->to_text) {
printbuf_indent_push(out, 2); pr_indent_push(out, 2);
bch2_sb_field_ops[type]->to_text(out, sb, f); bch2_sb_field_ops[type]->to_text(out, sb, f);
printbuf_indent_pop(out, 2); pr_indent_pop(out, 2);
} }
} }
...@@ -1656,9 +1656,9 @@ void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb, ...@@ -1656,9 +1656,9 @@ void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
pr_newline(out); pr_newline(out);
pr_buf(out, "layout:"); pr_buf(out, "layout:");
pr_newline(out); pr_newline(out);
printbuf_indent_push(out, 2); pr_indent_push(out, 2);
bch2_sb_layout_to_text(out, &sb->layout); bch2_sb_layout_to_text(out, &sb->layout);
printbuf_indent_pop(out, 2); pr_indent_pop(out, 2);
} }
vstruct_for_each(sb, f) vstruct_for_each(sb, f)
......
...@@ -117,17 +117,11 @@ void bch2_hprint(struct printbuf *buf, s64 v) ...@@ -117,17 +117,11 @@ void bch2_hprint(struct printbuf *buf, s64 v)
if (u && t && v < 100 && v > -100) if (u && t && v < 100 && v > -100)
pr_buf(buf, ".%i", t / 103); pr_buf(buf, ".%i", t / 103);
if (u) if (u)
pr_buf(buf, "%c", si_units[u]); pr_char(buf, si_units[u]);
} }
void bch2_pr_units(struct printbuf *out, s64 raw, s64 bytes) void bch2_pr_units(struct printbuf *out, s64 raw, s64 bytes)
{ {
if (raw < 0) {
pr_buf(out, "-");
raw = -raw;
bytes = -bytes;
}
switch (out->units) { switch (out->units) {
case PRINTBUF_UNITS_RAW: case PRINTBUF_UNITS_RAW:
pr_buf(out, "%llu", raw); pr_buf(out, "%llu", raw);
......
...@@ -244,8 +244,12 @@ enum printbuf_units { ...@@ -244,8 +244,12 @@ enum printbuf_units {
struct printbuf { struct printbuf {
char *pos; char *pos;
char *end; char *end;
char *last_newline;
char *last_field;
unsigned indent; unsigned indent;
enum printbuf_units units; enum printbuf_units units;
unsigned tabstop;
unsigned tabstops[4];
}; };
static inline size_t printbuf_remaining(struct printbuf *buf) static inline size_t printbuf_remaining(struct printbuf *buf)
...@@ -253,29 +257,49 @@ static inline size_t printbuf_remaining(struct printbuf *buf) ...@@ -253,29 +257,49 @@ static inline size_t printbuf_remaining(struct printbuf *buf)
return buf->end - buf->pos; return buf->end - buf->pos;
} }
static inline size_t printbuf_linelen(struct printbuf *buf)
{
return buf->pos - buf->last_newline;
}
#define _PBUF(_buf, _len) \ #define _PBUF(_buf, _len) \
((struct printbuf) { \ ((struct printbuf) { \
.pos = _buf, \ .pos = _buf, \
.end = _buf + _len, \ .end = _buf + _len, \
.last_newline = _buf, \
.last_field = _buf, \
}) })
#define PBUF(_buf) _PBUF(_buf, sizeof(_buf)) #define PBUF(_buf) _PBUF(_buf, sizeof(_buf))
#define pr_buf(_out, ...) \ #define pr_buf(_out, ...) \
do { \ do { \
(_out)->pos += scnprintf((_out)->pos, printbuf_remaining(_out), \ (_out)->pos += scnprintf((_out)->pos, printbuf_remaining(_out), \
__VA_ARGS__); \ __VA_ARGS__); \
} while (0) } while (0)
static inline void printbuf_indent_push(struct printbuf *buf, unsigned spaces) static inline void pr_char(struct printbuf *out, char c)
{
if (printbuf_remaining(out) > 1) {
*out->pos = c;
out->pos++;
}
}
static inline void pr_indent_push(struct printbuf *buf, unsigned spaces)
{ {
buf->indent += spaces; buf->indent += spaces;
while (spaces--) while (spaces--)
pr_buf(buf, " "); pr_char(buf, ' ');
} }
static inline void printbuf_indent_pop(struct printbuf *buf, unsigned spaces) static inline void pr_indent_pop(struct printbuf *buf, unsigned spaces)
{ {
if (buf->last_newline + buf->indent == buf->pos) {
buf->pos -= spaces;
buf->pos = '\0';
}
buf->indent -= spaces; buf->indent -= spaces;
} }
...@@ -283,14 +307,60 @@ static inline void pr_newline(struct printbuf *buf) ...@@ -283,14 +307,60 @@ static inline void pr_newline(struct printbuf *buf)
{ {
unsigned i; unsigned i;
pr_buf(buf, "\n"); pr_char(buf, '\n');
buf->last_newline = buf->pos;
for (i = 0; i < buf->indent; i++) for (i = 0; i < buf->indent; i++)
pr_buf(buf, " "); pr_char(buf, ' ');
buf->last_field = buf->pos;
buf->tabstop = 0;
}
static inline void pr_tab(struct printbuf *buf)
{
BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops));
while (printbuf_remaining(buf) > 1 &&
printbuf_linelen(buf) < buf->tabstops[buf->tabstop])
pr_char(buf, ' ');
buf->last_field = buf->pos;
buf->tabstop++;
}
static inline void pr_tab_rjust(struct printbuf *buf)
{
ssize_t shift = min_t(ssize_t, buf->tabstops[buf->tabstop] -
printbuf_linelen(buf),
printbuf_remaining(buf));
ssize_t move = min_t(ssize_t, buf->pos - buf->last_field,
printbuf_remaining(buf) - shift);
BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops));
if (shift > 0) {
memmove(buf->last_field + shift,
buf->last_field,
move);
memset(buf->last_field, ' ', shift);
buf->pos += shift;
*buf->pos = 0;
}
buf->last_field = buf->pos;
buf->tabstop++;
} }
void bch2_pr_units(struct printbuf *, s64, s64); void bch2_pr_units(struct printbuf *, s64, s64);
#define pr_units(...) bch2_pr_units(__VA_ARGS__) #define pr_units(...) bch2_pr_units(__VA_ARGS__)
static inline void pr_sectors(struct printbuf *out, u64 v)
{
bch2_pr_units(out, v, v << 9);
}
#ifdef __KERNEL__ #ifdef __KERNEL__
static inline void pr_time(struct printbuf *out, u64 time) static inline void pr_time(struct printbuf *out, u64 time)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment