Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / bit_str.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2016 SUSE LINUX GmbH
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14 #include "common/bit_str.h"
15 #include "common/Formatter.h"
16 #include "include/assert.h"
17
18 static void _dump_bit_str(
19     uint64_t bits,
20     std::ostream *out,
21     ceph::Formatter *f,
22     std::function<const char*(uint64_t)> func,
23     bool dump_bit_val)
24 {
25   uint64_t b = bits;
26   int cnt = 0;
27   bool outted = false;
28
29   while (b && cnt < 64) {
30     uint64_t r = bits & (1ULL << cnt++);
31     if (r) {
32       if (out) {
33         if (outted)
34           *out << ",";
35         *out << func(r);
36         if (dump_bit_val) {
37           *out << "(" << r << ")";
38         }
39       } else {
40         assert(f != NULL);
41         if (dump_bit_val) {
42           f->dump_stream("bit_flag") << func(r)
43                                      << "(" << r << ")";
44         } else {
45           f->dump_stream("bit_flag") << func(r);
46         }
47       }
48       outted = true;
49     }
50     b >>= 1;
51   }
52   if (!outted && out)
53       *out << "none";
54 }
55
56 void print_bit_str(
57     uint64_t bits,
58     std::ostream &out,
59     std::function<const char*(uint64_t)> func,
60     bool dump_bit_val)
61 {
62   _dump_bit_str(bits, &out, NULL, func, dump_bit_val);
63 }
64
65 void dump_bit_str(
66     uint64_t bits,
67     ceph::Formatter *f,
68     std::function<const char*(uint64_t)> func,
69     bool dump_bit_val)
70 {
71   _dump_bit_str(bits, NULL, f, func, dump_bit_val);
72 }