Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nadrino committed Apr 25, 2021
1 parent bf82601 commit 7211774
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions include/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ namespace {
// Keep in mind that every parameter you set will be applied only in the context of the source file you're in
// It is an inherent feature as a **header-only** library
static void setMaxLogLevel(int maxLogLevel_);
static void setMaxLogLevel(LogLevel maxLogLevel_);
static void setMaxLogLevel(const LogLevel &maxLogLevel_);
static void setEnableColors(bool enableColors_);
static void setPropagateColorsOnUserHeader(bool propagateColorsOnUserHeader_);
static void setPrefixLevel(PrefixLevel prefixLevel_);
static void setPrefixLevel(const PrefixLevel &prefixLevel_);
static void setUserHeaderStr(const std::string &userHeaderStr_);
static void setPrefixFormat(const std::string &prefixFormat_);

Expand All @@ -77,13 +77,13 @@ namespace {

// Macro-Related Methods
// Those intended to be called using the above preprocessor macros
Logger(LogLevel logLevel_, char const * fileName_, int lineNumber_);
Logger(const LogLevel &logLevel_, char const * fileName_, const int &lineNumber_);
virtual ~Logger();

protected:

static void buildCurrentPrefix();
static void getFormattedUserHeaderStr(std::string &formattedUserHeaderBuffer_);
static void formatUserHeaderStr(std::string &strBuffer_);

static void hookStreamBuffer();
static std::string getLogLevelColorStr(const LogLevel &selectedLogLevel_);
Expand Down
25 changes: 11 additions & 14 deletions include/implementation/Logger.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace {
void Logger::setMaxLogLevel(int maxLogLevel_) {
Logger::setMaxLogLevel(static_cast<Logger::LogLevel>(maxLogLevel_));
}
void Logger::setMaxLogLevel(LogLevel maxLogLevel_) {
void Logger::setMaxLogLevel(const LogLevel &maxLogLevel_) {
_maxLogLevel_ = maxLogLevel_;
}
void Logger::setEnableColors(bool enableColors_) {
Expand All @@ -37,7 +37,7 @@ namespace {
void Logger::setPropagateColorsOnUserHeader(bool propagateColorsOnUserHeader_) {
_propagateColorsOnUserHeader_ = propagateColorsOnUserHeader_;
}
void Logger::setPrefixLevel(PrefixLevel prefixLevel_) {
void Logger::setPrefixLevel(const PrefixLevel &prefixLevel_) {
_prefixLevel_ = prefixLevel_;
}
void Logger::setUserHeaderStr(const std::string &userHeaderStr_) {
Expand Down Expand Up @@ -70,7 +70,7 @@ namespace {
}

// C-tor D-tor
Logger::Logger(LogLevel logLevel_, char const *fileName_, int lineNumber_) {
Logger::Logger(const LogLevel &logLevel_, char const *fileName_, const int &lineNumber_) {

hookStreamBuffer(); // hook the stream buffer to an object we can handle
if (logLevel_ != _currentLogLevel_) _isNewLine_ = true; // force reprinting the prefix if the verbosity has changed
Expand All @@ -92,14 +92,11 @@ namespace {

if (_currentLogLevel_ > _maxLogLevel_) return;

{ // guard
// std::lock_guard<std::mutex> guard(_loggerMutex_);
printFormat(fmt_str, std::forward<TT>(args)...);
if (not _disablePrintfLineJump_ and fmt_str[strlen(fmt_str) - 1] != '\n') {
Logger::printFormat(fmt_str, std::forward<TT>(args)...);
if (not _disablePrintfLineJump_ and fmt_str[strlen(fmt_str) - 1] != '\n') {
_outputStream_ << std::endl;
_isNewLine_ = true;
}
} // guard

}
template<typename T> Logger &Logger::operator<<(const T &data) {
Expand Down Expand Up @@ -138,7 +135,7 @@ namespace {
// Nothing else -> NONE level
if( Logger::_prefixLevel_ == Logger::PrefixLevel::NONE ){
if( not _userHeaderStr_.empty() ){
Logger::getFormattedUserHeaderStr(_currentPrefix_);
Logger::formatUserHeaderStr(_currentPrefix_);
_currentPrefix_ += " "; // extra space
}
return;
Expand Down Expand Up @@ -197,7 +194,7 @@ namespace {
// "{USER_HEADER}" -> User prefix can have doubled spaces and spaces on the left
if( not _userHeaderStr_.empty() ){
strBuffer = "";
Logger::getFormattedUserHeaderStr(strBuffer);
Logger::formatUserHeaderStr(strBuffer);
LoggerUtils::replaceSubstringInsideInputString(_currentPrefix_, "{USER_HEADER}", strBuffer);
}

Expand All @@ -218,11 +215,11 @@ namespace {
_currentPrefix_ += ": ";
}
}
void Logger::getFormattedUserHeaderStr(std::string &formattedUserHeaderBuffer_) {
void Logger::formatUserHeaderStr(std::string &strBuffer_) {
if( not _userHeaderStr_.empty() ){
if(_enableColors_ and _propagateColorsOnUserHeader_) formattedUserHeaderBuffer_ += getLogLevelColorStr(_currentLogLevel_);
formattedUserHeaderBuffer_ += _userHeaderStr_;
if(_enableColors_ and _propagateColorsOnUserHeader_) formattedUserHeaderBuffer_ += "\033[0m";
if(_enableColors_ and _propagateColorsOnUserHeader_) strBuffer_ += getLogLevelColorStr(_currentLogLevel_);
strBuffer_ += _userHeaderStr_;
if(_enableColors_ and _propagateColorsOnUserHeader_) strBuffer_ += "\033[0m";
}
}
std::string Logger::getLogLevelColorStr(const LogLevel &selectedLogLevel_) {
Expand Down
21 changes: 12 additions & 9 deletions include/implementation/LoggerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ namespace LoggerUtils{
};

//! String Utils
inline bool doesStringContainsSubstring(std::string string_, std::string substring_, bool ignoreCase_ = false);
inline std::string padString(const std::string& inputStr_, unsigned int padSize_, const char& padChar = ' ');
inline bool doesStringContainsSubstring(const std::string &string_, const std::string &substring_, bool ignoreCase_ = false);
inline std::string padString(const std::string& inputStr_, const unsigned int &padSize_, const char& padChar = ' ');
inline std::string toLowerCase(const std::string &inputStr_);
inline std::string stripStringUnicode(const std::string &inputStr_);
inline std::string repeatString(const std::string &inputStr_, int amount_);
inline void replaceSubstringInsideInputString(std::string &input_str_, const std::string &substr_to_look_for_, const std::string &substr_to_replace_);
inline std::vector<std::string> splitString(const std::string& input_string_, const std::string& delimiter_);
inline std::string formatString( const std::string& strToFormat_ ); // 0 args overrider
template<typename ... Args> inline std::string formatString( const std::string& strToFormat_, const Args& ... args );

inline void removeRepeatedCharInsideInputStr(std::string &inputStr_, const std::string &doubledChar_);
inline void replaceSubstringInsideInputString(std::string &input_str_, const std::string &substr_to_look_for_, const std::string &substr_to_replace_);

// Hardware Utils
inline int getTerminalWidth();
Expand All @@ -91,16 +91,19 @@ namespace LoggerUtils{
namespace LoggerUtils{

// String Utils
inline bool doesStringContainsSubstring(std::string string_, std::string substring_, bool ignoreCase_){
inline bool doesStringContainsSubstring(const std::string &string_, const std::string &substring_, bool ignoreCase_){
if(substring_.size() > string_.size()) return false;
if(ignoreCase_){
string_ = toLowerCase(string_);
substring_ = toLowerCase(substring_);
if(toLowerCase(string_).find(toLowerCase(substring_)) != std::string::npos){
return true;
}
}
else if(string_.find(substring_) != std::string::npos){
return true;
}
if(string_.find(substring_) != std::string::npos) return true;
else return false;
return false;
}
inline std::string padString(const std::string& inputStr_, unsigned int padSize_, const char& padChar){
inline std::string padString(const std::string& inputStr_, const unsigned int &padSize_, const char& padChar){
std::string outputString;
int padDelta = int(inputStr_.size()) - int(padSize_);
while( padDelta < 0 ){
Expand Down

0 comments on commit 7211774

Please sign in to comment.