Skip to content

Commit 8909702

Browse files
architapruthiArchita P
authored andcommitted
Update LogService class in CppMicroServices (CppMicroServices#1009)
* Update LogService class --------- Co-authored-by: Archita P <architap@mathworks.com>
1 parent 1315d98 commit 8909702

29 files changed

Lines changed: 1497 additions & 119 deletions

compendium/ConfigurationAdmin/src/CMLogger.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,27 @@ namespace cppmicroservices
128128
currLogger->Log(sr, level, message, ex);
129129
}
130130
}
131+
132+
std::shared_ptr<cppmicroservices::logservice::Logger>
133+
CMLogger::getLogger(const std::string& name) const
134+
{
135+
auto currLogger = std::atomic_load(&logService);
136+
if (currLogger)
137+
{
138+
return currLogger->getLogger(name);
139+
}
140+
return nullptr;
141+
}
142+
143+
std::shared_ptr<cppmicroservices::logservice::Logger>
144+
CMLogger::getLogger(const cppmicroservices::Bundle& bundle, std::string const& name) const
145+
{
146+
auto currLogger = std::atomic_load(&logService);
147+
if (currLogger)
148+
{
149+
return currLogger->getLogger(bundle, name);
150+
}
151+
return nullptr;
152+
}
131153
} // namespace cmimpl
132154
} // namespace cppmicroservices

compendium/ConfigurationAdmin/src/CMLogger.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ namespace cppmicroservices
6161
logservice::SeverityLevel level,
6262
std::string const& message,
6363
const std::exception_ptr ex) override;
64+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger> getLogger(const std::string& name) const override;
65+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger> getLogger(const cppmicroservices::Bundle& bundle, std::string const& name) const override;
6466

6567
// methods from the cppmicroservices::ServiceTrackerCustomizer interface
6668
std::shared_ptr<TrackedParamType> AddingService(

compendium/ConfigurationAdmin/test/Mocks.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ namespace cppmicroservices
5858
cppmicroservices::logservice::SeverityLevel,
5959
std::string const&,
6060
const std::exception_ptr));
61+
MOCK_CONST_METHOD1(getLogger,
62+
std::shared_ptr<cppmicroservices::logservice::Logger>(const std::string&));
63+
MOCK_CONST_METHOD2(getLogger,
64+
std::shared_ptr<cppmicroservices::logservice::Logger>(const cppmicroservices::Bundle&, const std::string&));
6165
};
6266

6367
/**
@@ -86,6 +90,16 @@ namespace cppmicroservices
8690
const std::exception_ptr) override
8791
{
8892
}
93+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger>
94+
getLogger(const std::string&) const override
95+
{
96+
return nullptr;
97+
}
98+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger>
99+
getLogger(const cppmicroservices::Bundle&, const std::string&) const override
100+
{
101+
return nullptr;
102+
}
89103
};
90104

91105
/**

compendium/ConfigurationAdmin/test/TestCMLogger.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "../src/CMLogger.hpp"
3434
#include "Mocks.hpp"
35+
#include <gmock/gmock.h>
3536

3637
using cppmicroservices::logservice::LogService;
3738
using cppmicroservices::logservice::SeverityLevel;
@@ -103,6 +104,9 @@ namespace cppmicroservices
103104
EXPECT_CALL(*(mockLogger.get()), Log(testing::_, SeverityLevel::LOG_WARNING, testing::_)).Times(1);
104105
EXPECT_CALL(*(mockLogger.get()), Log(testing::_, SeverityLevel::LOG_ERROR, testing::_, testing::_))
105106
.Times(1);
107+
EXPECT_CALL(*(mockLogger.get()), getLogger("test")).Times(1);
108+
const cppmicroservices::Bundle mockBundle;
109+
EXPECT_CALL(*(mockLogger.get()), getLogger(mockBundle, "test")).Times(1);
106110
// exercise methods on instance of CMLogger
107111
CMLogger logger(bundleContext);
108112
logger.Log(SeverityLevel::LOG_DEBUG, "some sample debug message");
@@ -115,6 +119,8 @@ namespace cppmicroservices
115119
SeverityLevel::LOG_ERROR,
116120
"some sample error message with service reference",
117121
std::make_exception_ptr(std::runtime_error("error occured")));
122+
auto resultLogger = logger.getLogger("test");
123+
auto resultBundleLogger = logger.getLogger(mockBundle, "test");
118124
reg.Unregister();
119125
});
120126
}

compendium/DeclarativeServices/src/SCRLogger.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,28 @@ namespace cppmicroservices
126126
currLogger->Log(sr, level, message, ex);
127127
}
128128
}
129+
130+
std::shared_ptr<cppmicroservices::logservice::Logger>
131+
SCRLogger::getLogger(const std::string& name) const
132+
{
133+
auto currLogger = std::atomic_load(&logService);
134+
if (currLogger)
135+
{
136+
return currLogger->getLogger(name);
137+
}
138+
return nullptr;
139+
}
140+
141+
std::shared_ptr<cppmicroservices::logservice::Logger>
142+
SCRLogger::getLogger(const cppmicroservices::Bundle& bundle, const std::string& name) const
143+
{
144+
auto currLogger = std::atomic_load(&logService);
145+
if (currLogger)
146+
{
147+
return currLogger->getLogger(bundle, name);
148+
}
149+
return nullptr;
150+
}
151+
129152
} // namespace scrimpl
130153
} // namespace cppmicroservices

compendium/DeclarativeServices/src/SCRLogger.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ namespace cppmicroservices
5959
logservice::SeverityLevel level,
6060
std::string const& message,
6161
const std::exception_ptr ex) override;
62+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger> getLogger(const std::string& name) const override;
63+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger> getLogger(const cppmicroservices::Bundle& bundle, const std::string& name) const override;
6264

6365
// methods from the cppmicroservices::ServiceTrackerCustomizer interface
6466
std::shared_ptr<TrackedParamType> AddingService(

compendium/DeclarativeServices/test/gtest/Mocks.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ namespace cppmicroservices
143143
cppmicroservices::logservice::SeverityLevel,
144144
std::string const&,
145145
const std::exception_ptr));
146+
MOCK_CONST_METHOD1(getLogger,
147+
std::shared_ptr<cppmicroservices::logservice::Logger>(const std::string&));
148+
MOCK_CONST_METHOD2(getLogger,
149+
std::shared_ptr<cppmicroservices::logservice::Logger>(const cppmicroservices::Bundle&, const std::string&));
146150
};
147151

148152
#ifdef _MSC_VER
@@ -175,6 +179,16 @@ namespace cppmicroservices
175179
const std::exception_ptr) override
176180
{
177181
}
182+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger>
183+
getLogger(const std::string&) const override
184+
{
185+
return nullptr;
186+
}
187+
[[nodiscard]] std::shared_ptr<cppmicroservices::logservice::Logger>
188+
getLogger(const cppmicroservices::Bundle&, const std::string&) const override
189+
{
190+
return nullptr;
191+
}
178192
};
179193

180194
class MockComponentManager : public ComponentManager

compendium/DeclarativeServices/test/gtest/SCRLoggerTest.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace cppmicroservices
8585
logger.Log(dummyRef,
8686
SeverityLevel::LOG_DEBUG,
8787
"sample log message",
88-
std::make_exception_ptr(std::runtime_error("error occured")));
88+
std::make_exception_ptr(std::runtime_error("error occured")));
8989
});
9090
}
9191

@@ -102,6 +102,10 @@ namespace cppmicroservices
102102
EXPECT_CALL(*(mockLogger.get()), Log(testing::_, SeverityLevel::LOG_WARNING, testing::_)).Times(1);
103103
EXPECT_CALL(*(mockLogger.get()), Log(testing::_, SeverityLevel::LOG_ERROR, testing::_, testing::_))
104104
.Times(1);
105+
EXPECT_CALL(*(mockLogger.get()), getLogger("test")).Times(1);
106+
const cppmicroservices::Bundle mockBundle;
107+
EXPECT_CALL(*(mockLogger.get()), getLogger(mockBundle, "test")).Times(1);
108+
105109
// exercise methods on instance of SCRLogger
106110
cppmicroservices::scrimpl::SCRLogger logger(bundleContext);
107111
logger.Log(SeverityLevel::LOG_DEBUG, "some sample debug message");
@@ -114,6 +118,8 @@ namespace cppmicroservices
114118
SeverityLevel::LOG_ERROR,
115119
"some sample error message with service reference",
116120
std::make_exception_ptr(std::runtime_error("error occured")));
121+
auto resultLogger = logger.getLogger("test");
122+
auto resultBundleLogger = logger.getLogger(mockBundle, "test");
117123
});
118124
}
119125

compendium/LogService/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
set(_public_headers
22
include/cppmicroservices/logservice/LogService.hpp
3+
include/cppmicroservices/logservice/LoggerFactory.hpp
4+
include/cppmicroservices/logservice/Logger.hpp
35
)
46

57
set(_version "1.0.0")

compendium/LogService/include/cppmicroservices/logservice/LogService.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define CPPMICROSERVICES_LOG_SERVICE_H__
2424

2525
#include "cppmicroservices/ServiceReferenceBase.h"
26+
#include "cppmicroservices/logservice/LoggerFactory.hpp"
2627

2728
#include <cstdint>
2829
#include <exception>
@@ -69,7 +70,7 @@ namespace cppmicroservices
6970
*
7071
* @remarks This class is thread safe.
7172
*/
72-
class LogService
73+
class LogService : public LoggerFactory
7374
{
7475
public:
7576
virtual ~LogService() = default;
@@ -115,6 +116,7 @@ namespace cppmicroservices
115116
std::string const& message,
116117
const std::exception_ptr ex)
117118
= 0;
119+
118120
};
119121

120122
} // namespace logservice

0 commit comments

Comments
 (0)