Skip to content

Commit c23a8ea

Browse files
committed
use custom logger
1 parent 0f8bf94 commit c23a8ea

File tree

8 files changed

+51
-18
lines changed

8 files changed

+51
-18
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "lib/logger"]
2+
path = lib/logger
3+
url = [email protected]:coding-cpp/logger.git

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ add_executable(json
1919
./src/parse.cpp
2020
./src/type.cpp
2121

22+
# Logger
23+
./lib/logger/src/config.cpp
24+
./lib/logger/src/log.cpp
25+
2226
./example/main.cpp
2327
)
2428

2529
target_include_directories(json
2630
PUBLIC
2731
$<INSTALL_INTERFACE:include>
2832
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
33+
$<INSTALL_INTERFACE:lib/logger/include>
34+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/logger/include>
2935
)

example/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int main(int argc, char **argv) {
2323

2424
parser jsonParser;
2525
object obj = jsonParser.loads(person.dumps(2));
26-
std::cout << obj.dumps(4) << std::endl;
26+
logger::success(obj.dumps(2));
2727

2828
return EXIT_SUCCESS;
2929
}

include/json/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <variant>
88
#include <vector>
99

10+
#include <logger/log.h>
11+
1012
#include <json/type.h>
1113

1214
namespace json {

include/json/parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <cctype>
44
#include <sstream>
55

6+
#include <logger/log.h>
7+
68
#include <json/object.h>
79

810
namespace json {

lib/logger

Submodule logger added at 0b9c104

src/object.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ json::object::object(all_values data) {
2121
else if (data.index() == 6)
2222
*this = std::get<std::map<std::string, object>>(data);
2323
else
24-
throw std::runtime_error("Invalid data type passed!");
24+
logger::error("Invalid data type passed",
25+
"json::object::object(all_values data)");
2526
return;
2627
}
2728
json::object::~object() { return; }
@@ -91,18 +92,23 @@ json::object &json::object::operator[](const size_t index) {
9192
this->assertIsArray();
9293

9394
if (index >= this->_array.size()) {
94-
throw std::out_of_range("Index out of range");
95+
logger::error("Index out of range",
96+
"json::object &json::object::operator[](const size_t index)");
9597
}
9698

9799
return this->_array[index];
98100
}
99101
const json::object &json::object::operator[](const size_t index) const {
100102
if (!this->_type.get() == type::array) {
101-
throw std::runtime_error("This object is not an array");
103+
logger::error("This object is not an array",
104+
"const json::object &json::object::operator[](const size_t "
105+
"index) const");
102106
}
103107

104108
if (index >= this->_array.size()) {
105-
throw std::out_of_range("Index out of range");
109+
logger::error("Index out of range",
110+
"const json::object &json::object::operator[](const size_t "
111+
"index) const");
106112
}
107113

108114
return this->_array.at(index);
@@ -126,15 +132,19 @@ void json::object::push_back(object data) {
126132
json::object &json::object::operator[](const std::string &key) {
127133

128134
if (!this->_type.get() == type::map) {
129-
throw std::runtime_error("This object is not an object");
135+
logger::error(
136+
"This object is not an object",
137+
"json::object &json::object::operator[](const std::string &key)");
130138
}
131139

132140
this->_type.set(type::map);
133141
return this->_map[key];
134142
}
135143
const json::object &json::object::operator[](const std::string &key) const {
136144
if (this->_type.get() != type::map) {
137-
throw std::runtime_error("This object is not an object");
145+
logger::error("This object is not an object",
146+
"const json::object &json::object::operator[](const "
147+
"std::string &key) const");
138148
}
139149

140150
return this->_map.at(key);
@@ -155,7 +165,8 @@ size_t json::object::size() {
155165
} else if (this->_type.get() == type::map) {
156166
return this->_map.size();
157167
} else {
158-
throw std::runtime_error("Cannot get size of non-[array, map] objects");
168+
logger::error("Cannot get size of non-[array, map] objects",
169+
"size_t json::object::size()");
159170
}
160171
return -1;
161172
}
@@ -165,7 +176,8 @@ void json::object::clear() {
165176
} else if (this->_type.get() == type::map) {
166177
this->_map.clear();
167178
} else {
168-
throw std::runtime_error("Cannot clear non-[array, map] objects");
179+
logger::error("Cannot clear non-[array, map] objects",
180+
"void json::object::clear()");
169181
}
170182
return;
171183
}
@@ -182,7 +194,8 @@ void json::object::reset() {
182194
void json::object::dump(std::string path, size_t indent) {
183195
std::ofstream jsonFile(path);
184196
if (!jsonFile.is_open()) {
185-
std::runtime_error("Unable to open " + path);
197+
logger::error("Unable to open " + path,
198+
"void json::object::dump(std::string path, size_t indent)");
186199
}
187200

188201
std::string stringified = this->dumps(indent);
@@ -280,7 +293,8 @@ void json::object::setArrayIfUndefined() {
280293
void json::object::assertIsArray() {
281294
this->setArrayIfUndefined();
282295
if (this->_type.get() != type::array) {
283-
throw std::runtime_error("This object is not an array");
296+
logger::error("This object is not an array",
297+
"void json::object::assertIsArray()");
284298
}
285299
}
286300

@@ -299,7 +313,8 @@ void json::object::setMapIfUndefined() {
299313
void json::object::assertIsMap() {
300314
this->setMapIfUndefined();
301315
if (!this->_type.get() == type::map) {
302-
throw std::runtime_error("This object is not an object");
316+
logger::error("This object is not an object",
317+
"void json::object::assertIsMap()");
303318
}
304319
}
305320

src/parse.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ json::parser::~parser() { return; }
99
json::object json::parser::load(std::string path) {
1010
std::ifstream file(path);
1111
if (!file.is_open())
12-
throw std::runtime_error("File not found");
12+
logger::error("File not found " + path,
13+
"json::object json::parser::load(std::string path)");
1314

1415
std::stringstream buffer;
1516
buffer << file.rdbuf();
@@ -42,7 +43,8 @@ json::object json::parser::parseValue() {
4243
else if (firstChar == '{')
4344
return this->parseMap();
4445

45-
throw std::runtime_error("Invalid JSON (first character)");
46+
logger::error("Invalid JSON", "json::object json::parser::parseValue()");
47+
return object();
4648
}
4749

4850
json::object json::parser::parseBooleanNullOrUndefined() {
@@ -64,7 +66,9 @@ json::object json::parser::parseBooleanNullOrUndefined() {
6466
else if (keyword == "undefined")
6567
return object();
6668

67-
throw std::runtime_error("Invalid JSON (bool, null, undefined)");
69+
logger::error("Invalid JSON",
70+
"json::object json::parser::parseBooleanNullOrUndefined()");
71+
return object();
6872
}
6973
json::object json::parser::parseNumber() {
7074
std::string number = "";
@@ -152,12 +156,12 @@ json::object json::parser::parseMap() {
152156
}
153157

154158
if (currChar != '"')
155-
throw std::runtime_error("Invalid JSON (map key)");
159+
logger::error("Invalid JSON", "json::object json::parser::parseMap()");
156160

157161
this->index++;
158162
currChar = this->getCurrChar();
159163
if (currChar == '"')
160-
throw std::runtime_error("Invalid JSON (map key)");
164+
logger::error("Invalid JSON", "json::object json::parser::parseMap()");
161165

162166
key = "";
163167
while (true) {
@@ -176,7 +180,7 @@ json::object json::parser::parseMap() {
176180
this->skipWhitespace();
177181
currChar = this->getCurrChar();
178182
if (currChar != ':')
179-
throw std::runtime_error("Invalid JSON (map-key value)");
183+
logger::error("Invalid JSON", "json::object json::parser::parseMap()");
180184

181185
this->index++;
182186
result[key] = this->parseValue();

0 commit comments

Comments
 (0)