Skip to content

Commit e5e13de

Browse files
authored
Merge pull request #47 from kimkulling/bugfix/sonarcube_findings
Refactor Logger methods for clarity and const correctness
2 parents 00ef0db + 0358aef commit e5e13de

File tree

2 files changed

+47
-49
lines changed

2 files changed

+47
-49
lines changed

code/Common/Logger.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,41 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3030
#include <string>
3131

3232
namespace cppcore {
33-
34-
static void appendDomain(const String &domain, String &logMsg) {
35-
if (!domain.isEmpty()) {
36-
logMsg += '(';
37-
logMsg += domain;
38-
logMsg += ')';
39-
}
40-
}
41-
42-
static ::std::string stripFilename(const ::std::string &filename) {
43-
if (filename.empty()) {
44-
return filename;
33+
namespace {
34+
void appendDomain(const String &domain, String &logMsg) {
35+
if (!domain.isEmpty()) {
36+
logMsg += '(';
37+
logMsg += domain;
38+
logMsg += ')';
39+
}
4540
}
46-
47-
::std::string::size_type pos = filename.find_last_of("/");
48-
if (pos == ::std::string::npos) {
49-
return filename;
41+
42+
::std::string stripFilename(const ::std::string &filename) {
43+
if (filename.empty()) {
44+
return filename;
45+
}
46+
47+
::std::string::size_type pos = filename.find_last_of("/");
48+
if (pos == ::std::string::npos) {
49+
return filename;
50+
}
51+
const ::std::string strippedName = filename.substr(pos + 1, filename.size() - pos - 1);
52+
53+
return strippedName;
5054
}
51-
const ::std::string strippedName = filename.substr(pos + 1, filename.size() - pos - 1);
52-
53-
return strippedName;
54-
}
55-
56-
static void addTraceInfo(const String &file, int line, String &msg) {
57-
if (Logger::getInstance()->getVerboseMode() == Logger::VerboseMode::Trace) {
58-
msg += String(" (", 2);
59-
std::string stripped = stripFilename(file.c_str());
60-
msg += String(stripped.c_str(), stripped.size());
61-
msg += String(", ", 2);
62-
std::stringstream ss;
63-
ss << line;
64-
std::string lineno = ss.str();
65-
msg += String(lineno.c_str(), lineno.size());
66-
msg += ')';
55+
56+
void addTraceInfo(const String &file, int line, String &msg) {
57+
if (Logger::getInstance()->getVerboseMode() == Logger::VerboseMode::Trace) {
58+
msg += String(" (", 2);
59+
std::string stripped = stripFilename(file.c_str());
60+
msg += String(stripped.c_str(), stripped.size());
61+
msg += String(", ", 2);
62+
std::stringstream ss;
63+
ss << line;
64+
std::string lineno = ss.str();
65+
msg += String(lineno.c_str(), lineno.size());
66+
msg += ')';
67+
}
6768
}
6869
}
6970

@@ -82,7 +83,7 @@ bool AbstractLogStream::isActive() const {
8283
Logger *Logger::sLogger = nullptr;
8384

8485
Logger *Logger::create() {
85-
if (nullptr == sLogger) {
86+
if (sLogger == nullptr) {
8687
sLogger = new Logger;
8788
}
8889

@@ -98,7 +99,7 @@ void Logger::set(Logger *logger) {
9899
}
99100

100101
Logger *Logger::getInstance() {
101-
if (nullptr == sLogger) {
102+
if (sLogger == nullptr) {
102103
static_cast<void>(create());
103104
}
104105

@@ -189,8 +190,7 @@ void Logger::print(const String &msg, PrintMode mode) {
189190
}
190191
}
191192

192-
if (msg.size() > 8) {
193-
if (msg[6] == '=' && msg[7] == '>') {
193+
if (msg.size() > 8 && msg[6] == '=' && msg[7] == '>') {
194194
mIntention += 2;
195195
}
196196
}
@@ -231,7 +231,7 @@ void Logger::registerLogStream(AbstractLogStream *pLogStream) {
231231
mLogStreams.add(pLogStream);
232232
}
233233

234-
void Logger::unregisterLogStream(AbstractLogStream *logStream) {
234+
void Logger::unregisterLogStream(const AbstractLogStream *logStream) {
235235
if (nullptr == logStream) {
236236
return;
237237
}
@@ -254,8 +254,8 @@ Logger::~Logger() {
254254
}
255255
}
256256

257-
String Logger::getDateTime() {
258-
static const uint32_t Space = 2;
257+
String Logger::getDateTime() const {
258+
static constexpr uint32_t Space = 2;
259259
DateTime currentDateTime;
260260
std::stringstream stream;
261261
stream.fill('0');

include/cppcore/Common/Logger.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,11 @@ class DLL_CPPCORE_EXPORT AbstractLogStream {
7272

7373
//-------------------------------------------------------------------------------------------------
7474
///
75-
/// @brief This class implements a simple logger.
76-
///
75+
/// @brief This class implements a simple logger.
7776
/// The logger is implemented as a singleton. You can attach several log streams to it, which can
78-
/// be used to log the messages to several output channels like a window or a log file or something
79-
/// else.
80-
/// The granularity of the logged messages can be controlled by the severity of the logger. The
81-
/// supported modes are normal ( no debug and info messages ), verbose ( all messages will be
77+
/// be used to log the messages to several output channels like a window or a log file or something
78+
/// else. The granularity of the logged messages can be controlled by the severity of the logger.
79+
/// The supported modes are normal ( no debug and info messages ), verbose ( all messages will be
8280
/// logged ) and debug ( the debug messages will be logged as well, be careful with this option ).
8381
//-------------------------------------------------------------------------------------------------
8482
class DLL_CPPCORE_EXPORT Logger final {
@@ -87,8 +85,8 @@ class DLL_CPPCORE_EXPORT Logger final {
8785
enum class VerboseMode {
8886
Invalid = -1, ///< Invalid marker
8987
Normal, ///< Only warnings and errors will be logged.
90-
Verbose, ///< Normal messages will be logged as well.
91-
Debug, ///< All debug messages will be logged as well.
88+
Verbose, ///< Normal messages will be logged as well.
89+
Debug, ///< All debug messages will be logged as well.
9290
Trace, ///< Will enable the tracing.
9391
Count ///< Number of enums
9492
};
@@ -166,12 +164,12 @@ class DLL_CPPCORE_EXPORT Logger final {
166164

167165
/// @brief Unregisters a registered log stream.
168166
/// @param[in] pLogStream A pointer showing to the log stream.
169-
void unregisterLogStream(AbstractLogStream *pLogStream);
167+
void unregisterLogStream(const AbstractLogStream *pLogStream);
170168

171169
private:
172170
Logger();
173171
~Logger();
174-
String getDateTime();
172+
String getDateTime() const;
175173

176174
private:
177175
// @brief The Standard log stream.

0 commit comments

Comments
 (0)