@@ -24,69 +24,93 @@ ddb_can_print(d_iov_t *iov)
2424 uint32_t len = iov -> iov_len ;
2525 int i ;
2626
27- for (i = 0 ; i < len ; i ++ ) {
27+ for (i = 0 ; i < len ; i ++ ) {
2828 if (str [i ] == '\0' )
2929 return true;
30- if (!isprint (str [i ]) && str [i ] != '\n' && str [i ] != '\r' )
30+
31+ if (!isprint (str [i ]))
3132 return false;
3233 }
34+
3335 return true;
3436}
3537
3638/*
37- * Converts contents of an iov to something that is more printable.
39+ * Converts contents of an @ iov to something that is more printable.
3840 *
39- * Returns number of characters that would have been written if buf_len was long
40- * enough, not including null terminator
41+ * Returns number of characters that would have been written if @buf is large enough,
42+ * not including the null terminator.
4143 */
42- int
43- ddb_iov_to_printable_buf (d_iov_t * iov , char buf [], uint32_t buf_len )
44+ uint32_t
45+ ddb_iov_to_printable_buf (d_iov_t * iov , char buf [], uint32_t buf_len , const char * prefix )
4446{
47+ char tmp [32 ];
48+ uint32_t new_len ;
49+ uint32_t result = 0 ;
50+ int i ;
51+
4552 if (iov -> iov_len == 0 || iov -> iov_buf == NULL )
4653 return 0 ;
4754
4855 if (ddb_can_print (iov ))
4956 return snprintf (buf , buf_len , "%.*s" , (int )iov -> iov_len , (char * )iov -> iov_buf );
5057
51- switch (iov -> iov_len ) {
52- case sizeof (uint8_t ):
53- return snprintf (buf , buf_len , "uint8:0x%x" , ((uint8_t * )iov -> iov_buf )[0 ]);
54- case sizeof (uint16_t ):
55- return snprintf (buf , buf_len , "uint16:0x%04hx" , ((uint16_t * )iov -> iov_buf )[0 ]);
56- case sizeof (uint32_t ):
57- return snprintf (buf , buf_len , "uint32:0x%x" , ((uint32_t * )iov -> iov_buf )[0 ]);
58- case sizeof (uint64_t ):
59- return snprintf (buf , buf_len , "uint64:0x%lx" , ((uint64_t * )iov -> iov_buf )[0 ]);
60- default :
61- {
62- char tmp_buf [32 ];
63- uint32_t new_len ;
64- uint32_t result = 0 ;
65- int i ;
66-
67- result += snprintf (buf , buf_len , "bin(%lu):0x" , iov -> iov_len );
68-
69- for (i = 0 ; i < iov -> iov_len ; i ++ ) {
70- new_len = snprintf (tmp_buf , ARRAY_SIZE (tmp_buf ), "%02x" ,
71- ((uint8_t * )iov -> iov_buf )[i ]);
72- if (new_len + result > buf_len ) {
73- /* Buffer not big enough */
74- result += new_len ;
75- } else {
76- result += sprintf (buf + result , "%s" , tmp_buf );
77- }
78- }
58+ if (prefix != NULL )
59+ result = snprintf (buf , buf_len , "%s" , prefix );
7960
80- if (result > buf_len ) {
81- buf [buf_len - 1 ] = '\0' ;
82- buf [buf_len - 2 ] = '.' ;
83- buf [buf_len - 3 ] = '.' ;
84- buf [buf_len - 4 ] = '.' ;
61+ for (i = 0 ; i < iov -> iov_len ; i ++ ) {
62+ new_len = snprintf (tmp , ARRAY_SIZE (tmp ), "%02x" , ((uint8_t * )iov -> iov_buf )[i ]);
63+ if (new_len + result > buf_len )
64+ result += new_len ; /* Buffer is not big enough. */
65+ else
66+ result += sprintf (buf + result , "%s" , tmp );
67+ }
8568
86- }
87- return result ;
69+ if (result > buf_len ) {
70+ buf [buf_len - 1 ] = '\0' ;
71+ for (i = 2 ; buf_len >= i && i <= 4 ; i ++ )
72+ buf [buf_len - i ] = '.' ;
8873 }
74+
75+ return result ;
76+ }
77+
78+ /*
79+ * Converts contents of an @key to something that is more printable.
80+ *
81+ * Returns number of characters that would have been written if @buf is long enough,
82+ * not including the null terminator.
83+ */
84+ uint32_t
85+ ddb_key_to_printable_buf (daos_key_t * key , enum daos_otype_t otype , char buf [], uint32_t buf_len )
86+ {
87+ char tmp [32 ];
88+
89+ if (key -> iov_len == 0 || key -> iov_buf == NULL )
90+ return 0 ;
91+
92+ if (ddb_key_is_lexical (otype ))
93+ return snprintf (buf , buf_len , "%.*s" , (int )key -> iov_len , (char * )key -> iov_buf );
94+
95+ if (ddb_key_is_int (otype )) {
96+ switch (key -> iov_len ) {
97+ case sizeof (uint8_t ):
98+ return snprintf (buf , buf_len , "uint8:0x%x" , ((uint8_t * )key -> iov_buf )[0 ]);
99+ case sizeof (uint16_t ):
100+ return snprintf (buf , buf_len , "uint16:0x%04hx" ,
101+ ((uint16_t * )key -> iov_buf )[0 ]);
102+ case sizeof (uint32_t ):
103+ return snprintf (buf , buf_len , "uint32:0x%x" , ((uint32_t * )key -> iov_buf )[0 ]);
104+ case sizeof (uint64_t ):
105+ return snprintf (buf , buf_len , "uint64:0x%lx" ,
106+ ((uint64_t * )key -> iov_buf )[0 ]);
107+ /* Fall through. */
108+ }
89109 }
110+
111+ snprintf (tmp , ARRAY_SIZE (tmp ), "bin(%lu):0x" , key -> iov_len );
112+
113+ return ddb_iov_to_printable_buf (key , buf , buf_len , tmp );
90114}
91115
92116void
@@ -115,22 +139,22 @@ ddb_print_key(struct ddb_ctx *ctx, struct ddb_key *key, uint32_t indent)
115139
116140 memset (buf , 0 , buf_len );
117141
118- ddb_iov_to_printable_buf (& key -> ddbk_key , buf , buf_len );
142+ ddb_key_to_printable_buf (& key -> ddbk_key , key -> ddbk_otype , buf , buf_len );
119143
120144 print_indent (ctx , indent );
121- if (ddb_can_print (& key -> ddbk_key )) {
122- ddb_printf (ctx , DF_IDX " '%s' (%lu)%s\n" ,
123- DP_IDX (key -> ddbk_idx ),
124- buf ,
125- key -> ddbk_key .iov_len ,
126- key -> ddbk_child_type == VOS_ITER_SINGLE ? " (SV)" :
127- key -> ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)" : "" );
128- return ;
129- }
130145
131- ddb_printf (ctx , DF_IDX " {%s}%s\n" , DP_IDX (key -> ddbk_idx ), buf ,
132- key -> ddbk_child_type == VOS_ITER_SINGLE ? " (SV)" :
133- key -> ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)" : "" );
146+ if (ddb_key_is_lexical (key -> ddbk_otype ) ||
147+ (!ddb_key_is_int (key -> ddbk_otype ) && ddb_can_print (& key -> ddbk_key )))
148+ ddb_printf (ctx , DF_IDX " '%s' (%lu)%s\n" , DP_IDX (key -> ddbk_idx ), buf ,
149+ key -> ddbk_key .iov_len ,
150+ key -> ddbk_child_type == VOS_ITER_SINGLE ? " (SV)"
151+ : key -> ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)"
152+ : "" );
153+ else
154+ ddb_printf (ctx , DF_IDX " {%s}%s\n" , DP_IDX (key -> ddbk_idx ), buf ,
155+ key -> ddbk_child_type == VOS_ITER_SINGLE ? " (SV)"
156+ : key -> ddbk_child_type == VOS_ITER_RECX ? " (ARRAY)"
157+ : "" );
134158}
135159
136160void
0 commit comments