~codership/galera/3.x

« back to all changes in this revision

Viewing changes to galerautils/src/gu_config.cpp

  • Committer: Alexey Yurchenko
  • Date: 2014-02-06 19:27:50 UTC
  • mfrom: (153.2.15 2.x)
  • Revision ID: alexey.yurchenko@codership.com-20140206192750-n9to0wtjevl69gok
Tags: release_25.3.3(percona)
References lp:1260193 - all supported parameters are now registered before parsing the initial options string, so unrecognized options can be detected.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (C) 2010-2012 Codership Oy <info@codership.com>
 
1
// Copyright (C) 2010-2014 Codership Oy <info@codership.com>
2
2
 
3
3
/**
4
4
 * @file
5
5
 * Configuration management implementation
6
6
 *
7
 
 * $Id: gu_config.cpp 3190 2013-08-07 21:49:17Z alex $
 
7
 * $Id: gu_config.cpp 3455 2014-02-01 18:11:09Z alex $
8
8
 */
9
9
 
10
10
#include "gu_config.h"
18
18
const char gu::Config::ESCAPE        = '\\'; // escape symbol
19
19
 
20
20
void
21
 
gu::Config::parse (param_map_t& pmap, const std::string& params)
 
21
gu::Config::parse (
 
22
    std::vector<std::pair<std::string, std::string> >& params_vector,
 
23
    const std::string& param_list)
22
24
{
23
 
    if (0 == params[0]) return;
24
 
 
25
 
    std::vector<std::string> pv = gu::tokenize (params, PARAM_SEP, ESCAPE);
 
25
    assert(params_vector.empty()); // we probably want a clean list
 
26
 
 
27
    if (param_list.empty()) return;
 
28
 
 
29
    std::vector<std::string> pv = gu::tokenize (param_list, PARAM_SEP, ESCAPE);
26
30
 
27
31
    for (size_t i = 0; i < pv.size(); ++i)
28
32
    {
53
57
            gu::trim(kvv[1]);
54
58
            std::string& value = kvv[1];
55
59
 
56
 
            param_map_t::iterator pi = pmap.find(key);
57
 
 
58
 
            if (pi != pmap.end())
59
 
            {
60
 
                log_warn << "Element " << pv[i] << " overwrites previous value"
61
 
                         << " '" << pi->second << "' of '" << key << "' by '"
62
 
                         << value << "'";
63
 
            }
64
 
 
65
 
            log_debug << "Found param '" << key << "' = '" << value << "'";
66
 
            pmap[key] = value;
 
60
            params_vector.push_back(std::make_pair(key, value));
67
61
        }
68
62
        else if (kvv.size() > 1)
69
63
        {
73
67
    }
74
68
}
75
69
 
 
70
void
 
71
gu::Config::parse (const std::string& param_list)
 
72
{
 
73
    if (param_list.empty()) return;
 
74
 
 
75
    std::vector<std::pair<std::string, std::string> > pv;
 
76
 
 
77
    parse (pv, param_list);
 
78
 
 
79
    bool not_found(false);
 
80
 
 
81
    for (size_t i = 0; i < pv.size(); ++i)
 
82
    {
 
83
        const std::string& key  (pv[i].first);
 
84
        const std::string& value(pv[i].second);
 
85
 
 
86
        try
 
87
        {
 
88
            set(key, value);
 
89
        }
 
90
        catch (NotFound& e)
 
91
        {
 
92
            log_error << "Unrecognized parameter '" << key << '\'';
 
93
            /* Throw later so that all invalid parameters get logged.*/
 
94
            not_found = true;
 
95
        }
 
96
 
 
97
        log_debug << "Set parameter '" << key << "' = '" << value << "'";
 
98
    }
 
99
 
 
100
    if (not_found) throw gu::NotFound();
 
101
}
 
102
 
76
103
gu::Config::Config() : params_() {}
77
104
 
78
 
gu::Config::Config (const std::string& params) : params_()
79
 
{
80
 
    Config::parse (params_, params);
81
 
}
82
 
 
83
105
void
84
106
gu::Config::set_longlong (const std::string& key, long long val)
85
107
{
93
115
            val >>= 40;
94
116
            num_mod = "T";
95
117
        }
96
 
        if (!(val & ((1 << 30) - 1)))
 
118
        else if (!(val & ((1 << 30) - 1)))
97
119
        {
98
120
            val >>= 30;
99
121
            num_mod = "G";
154
176
                              << " too large for requested type (int).";
155
177
}
156
178
 
 
179
void
 
180
gu::Config::print (std::ostream& os, bool const notset) const
 
181
{
 
182
    struct _print_param
 
183
    {
 
184
        void operator() (std::ostream& os, bool const notset,
 
185
                         param_map_t::const_iterator& pi)
 
186
        {
 
187
            const Parameter& p(pi->second);
 
188
 
 
189
            if (p.is_set() || notset)
 
190
            {
 
191
                os << pi->first << " = " << p.value() << "; ";
 
192
            }
 
193
        }
 
194
    }
 
195
    print_param;
 
196
 
 
197
    for (param_map_t::const_iterator pi(params_.begin());
 
198
         pi != params_.end(); ++pi)
 
199
    {
 
200
        print_param(os, notset, pi);
 
201
    }
 
202
}
 
203
 
157
204
std::ostream& gu::operator<<(std::ostream& ost, const gu::Config& c)
158
205
{
159
 
    const gu::Config::param_map_t& pmap = c.params();
160
 
    gu::Config::param_map_t::const_iterator pi = pmap.begin();
161
 
 
162
 
    if (pi != pmap.end())
163
 
    {
164
 
        ost << pi->first << " = " << pi->second;
165
 
        ++pi;
166
 
        for (; pi != pmap.end(); ++pi)
167
 
        {
168
 
//        ost << "'" << pi->first << "' = '" << pi->second << "'\n";
169
 
            ost << "; " << pi->first << " = " << pi->second;
170
 
        }
171
 
    }
172
 
 
 
206
    c.print(ost);
173
207
    return ost;
174
208
}
175
209
 
176
210
gu_config_t*
177
 
gu_config_create (const char* params)
 
211
gu_config_create ()
178
212
{
179
213
    try
180
214
    {
181
 
        const std::string& ps(params ? params : "");
182
 
        return (reinterpret_cast<gu_config_t*>(new gu::Config(ps)));
 
215
        return (reinterpret_cast<gu_config_t*>(new gu::Config()));
183
216
    }
184
217
    catch (gu::Exception& e)
185
218
    {
203
236
    }
204
237
}
205
238
 
206
 
static long
 
239
static int
207
240
config_check_set_args (gu_config_t* cnf, const char* key, const char* func)
208
241
{
209
242
    if (cnf && key && key[0] != '\0') return 0;
217
250
    return -EINVAL;
218
251
}
219
252
 
220
 
static long
 
253
static int
221
254
config_check_get_args (gu_config_t* cnf, const char* key, const void* val_ptr,
222
255
                       const char* func)
223
256
{
243
276
    return (conf->has (key));
244
277
}
245
278
 
246
 
long
 
279
bool
 
280
gu_config_is_set (gu_config_t* cnf, const char* key)
 
281
{
 
282
    if (config_check_set_args (cnf, key, __FUNCTION__)) return false;
 
283
 
 
284
    gu::Config* conf = reinterpret_cast<gu::Config*>(cnf);
 
285
 
 
286
    return (conf->is_set (key));
 
287
}
 
288
 
 
289
void
 
290
gu_config_add (gu_config_t* cnf, const char* key, const char* const val)
 
291
{
 
292
    if (config_check_set_args (cnf, key, __FUNCTION__)) gu_throw_error(EINVAL);
 
293
 
 
294
    gu::Config* conf = reinterpret_cast<gu::Config*>(cnf);
 
295
 
 
296
    if (val != NULL)
 
297
        conf->add (key, val);
 
298
    else
 
299
        conf->add (key);
 
300
}
 
301
 
 
302
int
247
303
gu_config_get_string (gu_config_t* cnf, const char* key, const char** val)
248
304
{
249
305
    if (config_check_get_args (cnf, key, val, __FUNCTION__)) return -EINVAL;
261
317
    }
262
318
}
263
319
 
264
 
long
 
320
int
265
321
gu_config_get_int64  (gu_config_t* cnf, const char* key, int64_t* val)
266
322
{
267
323
    if (config_check_get_args (cnf, key, val, __FUNCTION__)) return -EINVAL;
277
333
    {
278
334
        return 1;
279
335
    }
 
336
    catch (gu::NotSet&)
 
337
    {
 
338
        return 1;
 
339
    }
280
340
    catch (gu::Exception& e)
281
341
    {
282
342
        log_error << "Failed to parse parameter '" << key << "': " << e.what();
284
344
    }
285
345
}
286
346
 
287
 
long
 
347
int
288
348
gu_config_get_double (gu_config_t* cnf, const char* key, double* val)
289
349
{
290
350
    if (config_check_get_args (cnf, key, val, __FUNCTION__)) return -EINVAL;
300
360
    {
301
361
        return 1;
302
362
    }
 
363
    catch (gu::NotSet&)
 
364
    {
 
365
        return 1;
 
366
    }
303
367
    catch (gu::Exception& e)
304
368
    {
305
369
        log_error << "Failed to parse parameter '" << key << "': " << e.what();
307
371
    }
308
372
}
309
373
 
310
 
long
 
374
int
311
375
gu_config_get_ptr    (gu_config_t* cnf, const char* key, void** val)
312
376
{
313
377
    if (config_check_get_args (cnf, key, val, __FUNCTION__)) return -EINVAL;
323
387
    {
324
388
        return 1;
325
389
    }
 
390
    catch (gu::NotSet&)
 
391
    {
 
392
        return 1;
 
393
    }
326
394
    catch (gu::Exception& e)
327
395
    {
328
396
        log_error << "Failed to parse parameter '" << key << "': " << e.what();
330
398
    }
331
399
}
332
400
 
333
 
long
 
401
int
334
402
gu_config_get_bool   (gu_config_t* cnf, const char* key, bool* val)
335
403
{
336
404
    if (config_check_get_args (cnf, key, val, __FUNCTION__)) return -EINVAL;
346
414
    {
347
415
        return 1;
348
416
    }
 
417
    catch (gu::NotSet&)
 
418
    {
 
419
        return 1;
 
420
    }
349
421
    catch (gu::Exception& e)
350
422
    {
351
423
        log_error << "Failed to parse parameter '" << key << "': " << e.what();