Skip to content

Commit 8ee428b

Browse files
committed
Updated example with setLogger
1 parent 81d6e66 commit 8ee428b

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ C++ YAML parser/reader and writer of *.yaml/*.yml files with keeping user format
88

99
include files:
1010

11-
- src.wsjcpp/wsjcpp_core/wsjcpp_core.h
12-
- src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp
1311
- src/wsjcpp_yaml.cpp
1412
- src/wsjcpp_yaml.h
1513

@@ -28,20 +26,33 @@ In you main file configure logger:
2826
#include <string.h>
2927
#include <iostream>
3028
#include "wsjcpp_yaml.h"
31-
#include "wsjcpp_core.h"
3229

30+
class MyLogger : public IWsjcppYamlLog {
31+
public:
32+
// IWsjcppYamlLog
33+
virtual void err(const std::string &TAG, const std::string &sMessage) override {
34+
std::cerr << TAG << " [error] : " << sMessage << std::endl;
35+
};
36+
virtual void throw_err(const std::string &TAG, const std::string &sMessage) override {
37+
std::cerr << TAG << " [critical_error] : " << sMessage << std::endl;
38+
throw std::runtime_error(TAG + " [critical_error] : " + sMessage);
39+
};
40+
virtual void warn(const std::string &TAG, const std::string &sMessage) override {
41+
std::cerr << TAG << " [warn] : " << sMessage << std::endl;
42+
};
43+
virtual void info(const std::string &TAG, const std::string &sMessage) override {
44+
std::cout << TAG << " [info] : " << sMessage << std::endl;
45+
};
46+
};
3347

3448
int main(int argc, char* argv[]) {
3549
std::string TAG = "MAIN";
3650
std::string appLogPath = ".logs";
37-
WsjcppLog::setLogDirectory(appLogPath);
38-
if (!WsjcppCore::dirExists(appLogPath)) {
39-
WsjcppCore::makeDir(appLogPath);
40-
}
41-
WsjcppLog::info(TAG, "Hello!");
51+
MyLogger *pLogger = new MyLogger();
4252

4353
// now you can use WsjcppYaml
4454
WsjcppYaml yaml;
55+
yaml.setLogger(pLogger);
4556
if (yaml.loadFromString(
4657
"# yaml content\n"
4758
"yaml1: nice format\n"
@@ -57,7 +68,7 @@ int main(int argc, char* argv[]) {
5768
" p2: v4 \n"
5869
"param2: 111\n"
5970
)) {
60-
WsjcppLog::throw_err(TAG, "Error parsing");
71+
yaml.throw_err(TAG, "Error parsing");
6172
return -1;
6273
}
6374

src/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
#include "wsjcpp_yaml.h"
55
// #include "wsjcpp_core.h"
66

7+
class MyLogger : public IWsjcppYamlLog {
8+
public:
9+
// IWsjcppYamlLog
10+
virtual void err(const std::string &TAG, const std::string &sMessage) override {
11+
std::cerr << TAG << " [error] : " << sMessage << std::endl;
12+
};
13+
virtual void throw_err(const std::string &TAG, const std::string &sMessage) override {
14+
std::cerr << TAG << " [critical_error] : " << sMessage << std::endl;
15+
throw std::runtime_error(TAG + " [critical_error] : " + sMessage);
16+
};
17+
virtual void warn(const std::string &TAG, const std::string &sMessage) override {
18+
std::cerr << TAG << " [warn] : " << sMessage << std::endl;
19+
};
20+
virtual void info(const std::string &TAG, const std::string &sMessage) override {
21+
std::cout << TAG << " [info] : " << sMessage << std::endl;
22+
};
23+
};
24+
725
int main(int argc, char* argv[]) {
826
std::string TAG = "MAIN";
927
// WsjcppCore::initRandom();
@@ -14,7 +32,10 @@ int main(int argc, char* argv[]) {
1432
// if (!WsjcppCore::dirExists(appLogPath)) {
1533
// WsjcppCore::makeDir(appLogPath);
1634
// }
35+
MyLogger *pLogger = new MyLogger();
36+
1737
WsjcppYaml yaml;
38+
yaml.setLogger(pLogger);
1839
std::string sError;
1940
std::string sFilePath = "./unit-tests.wsjcpp/data-tests/read-file/example-voiting-app/docker-compose.yml";
2041
std::cout << "Reading file " << sFilePath << "..." << std::endl;

src/wsjcpp_yaml.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,21 +1264,19 @@ WsjcppYaml::WsjcppYaml() {
12641264
m_pLog = nullptr;
12651265
}
12661266

1267-
// ---------------------------------------------------------------------
1268-
12691267
WsjcppYaml::~WsjcppYaml() {
12701268
delete m_pRoot;
12711269
}
12721270

1273-
// ---------------------------------------------------------------------
1271+
void WsjcppYaml::setLogger(IWsjcppYamlLog *pLog) {
1272+
m_pLog = pLog;
1273+
}
12741274

12751275
void WsjcppYaml::clear() {
12761276
delete m_pRoot;
12771277
m_pRoot = nullptr;
12781278
}
12791279

1280-
// ---------------------------------------------------------------------
1281-
12821280
bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError) {
12831281
std::string sTextContent;
12841282
if (!WsjcppYaml::readTextFile(sFileName, sTextContent, sError)) {
@@ -1290,6 +1288,7 @@ bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError)
12901288
// ---------------------------------------------------------------------
12911289

12921290
bool WsjcppYaml::saveToFile(const std::string &sFileName, std::string &sError) {
1291+
this->info(TAG, "Saving to " + sFileName);
12931292
std::string sBuffer = m_pRoot->toString() + "\n"; // last empty line must be always
12941293
if (!WsjcppYaml::writeFile(sFileName, sBuffer)) {
12951294
sError = "Could not save to file";

src/wsjcpp_yaml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ class WsjcppYaml : public IWsjcppYamlLog {
270270
WsjcppYaml();
271271
~WsjcppYaml();
272272
void clear();
273+
void setLogger(IWsjcppYamlLog *pLog);
273274
bool loadFromFile(const std::string &sFileName, std::string &sError);
274275
bool saveToFile(const std::string &sFileName, std::string &sError);
275276
bool loadFromString(const std::string &sBufferName, const std::string &sBuffer, std::string &sError);

0 commit comments

Comments
 (0)