1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2017 Couchbase, Inc.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#include "logger.h"
19
20namespace cb {
21namespace logger {
22
23Config::Config(const cJSON& json) {
24    auto* root = const_cast<cJSON*>(&json);
25    auto* obj = cJSON_GetObjectItem(root, "filename");
26    if (obj != nullptr) {
27        if (obj->type != cJSON_String) {
28            throw std::invalid_argument(
29                    R"(cb::logger::Config: "filename" must be a string)");
30        }
31        filename.assign(obj->valuestring);
32    }
33
34    obj = cJSON_GetObjectItem(root, "buffersize");
35    if (obj != nullptr) {
36        if (obj->type != cJSON_Number) {
37            throw std::invalid_argument(
38                    R"(cb::logger::Config: "buffersize" must be an unsigned int)");
39        }
40        buffersize = static_cast<unsigned int>(obj->valueint);
41    }
42
43    obj = cJSON_GetObjectItem(root, "cyclesize");
44    if (obj != nullptr) {
45        if (obj->type != cJSON_Number) {
46            throw std::invalid_argument(
47                    R"(cb::logger::Config: "cyclesize" must be an unsigned int)");
48        }
49        cyclesize = static_cast<unsigned int>(obj->valueint);
50    }
51
52    obj = cJSON_GetObjectItem(root, "unit_test");
53    unit_test = false;
54    if (obj != nullptr) {
55        if (obj->type == cJSON_True) {
56            unit_test = true;
57        } else if (obj->type != cJSON_False) {
58            throw std::invalid_argument(
59                    R"(Config: "unit_test" must be a bool)");
60        }
61    }
62
63    obj = cJSON_GetObjectItem(root, "console");
64    if (obj != nullptr) {
65        if (obj->type == cJSON_False) {
66            console = false;
67        } else if (obj->type != cJSON_True) {
68            throw std::invalid_argument(
69                    R"(Config: "console" must be a bool)");
70        }
71    }
72}
73
74bool Config::operator==(const Config& other) const {
75    return (this->filename == other.filename) &&
76           (this->buffersize == other.buffersize) &&
77           (this->cyclesize == other.cyclesize) &&
78           (this->unit_test == other.unit_test) &&
79           (this->console == other.console);
80}
81
82bool Config::operator!=(const Config& other) const {
83    return !(*this == other);
84}
85
86} // namespace logger
87} // namespace cb
88