Browse Source

add Log::put_multiline() methods

master
Tomasz Sowa 6 months ago
parent
commit
5d2788d0d8
  1. 48
      src/log/log.cpp
  2. 67
      src/log/log.h

48
src/log/log.cpp

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018-2021, Tomasz Sowa
* Copyright (c) 2018-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -684,5 +684,51 @@ void Log::save_log()
}
Log & Log::put_multiline(const char * prefix, const char * msg)
{
put_multiline_generic(prefix, msg);
return *this;
}
Log & Log::put_multiline(const wchar_t * prefix, const wchar_t * msg)
{
put_multiline_generic(prefix, msg);
return *this;
}
Log & Log::put_multiline(const char * prefix, const std::string & msg)
{
put_multiline_generic(prefix, msg.c_str());
return *this;
}
Log & Log::put_multiline(const wchar_t * prefix, const std::wstring & msg)
{
put_multiline_generic(prefix, msg.c_str());
return *this;
}
Log & Log::put_multiline(const std::string & prefix, const std::string & msg)
{
put_multiline_generic(prefix.c_str(), msg.c_str());
return *this;
}
Log & Log::put_multiline(const std::wstring & prefix, const std::wstring & msg)
{
put_multiline_generic(prefix.c_str(), msg.c_str());
return *this;
}
} // namespace

67
src/log/log.h

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018-2021, Tomasz Sowa
* Copyright (c) 2018-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -156,6 +156,17 @@ public:
virtual Log & put_binary_blob(const char * blob, size_t blob_len);
virtual Log & put_binary_blob(const std::string & blob);
/*
* put multiline message
* first and last new characters are trimmed
* at the beginning of each line a prefix is inserted
*/
virtual Log & put_multiline(const char * prefix, const char * msg);
virtual Log & put_multiline(const wchar_t * prefix, const wchar_t * msg);
virtual Log & put_multiline(const char * prefix, const std::string & msg);
virtual Log & put_multiline(const wchar_t * prefix, const std::wstring & msg);
virtual Log & put_multiline(const std::string & prefix, const std::string & msg);
virtual Log & put_multiline(const std::wstring & prefix, const std::wstring & msg);
protected:
@ -184,6 +195,10 @@ protected:
virtual void save_log();
virtual void save_log_and_clear();
template<typename CharType>
void put_multiline_generic(const CharType * prefix, const CharType * msg);
};
@ -221,11 +236,61 @@ Log & Log::log_string_generic(const StringType & value, size_t max_size)
template<typename CharType>
void Log::put_multiline_generic(const CharType * prefix, const CharType * msg)
{
bool put_prefix = true;
bool was_new_line = false;
bool was_something_printed = false;
while( *msg )
{
if( static_cast<CharType>(*msg) == static_cast<CharType>('\n') )
{
was_new_line = true;
put_prefix = true;
}
else
{
if( was_new_line )
{
if( was_something_printed )
{
operator<<(logend);
}
was_new_line = false;
}
if( put_prefix )
{
operator<<(prefix);
put_prefix = false;
}
operator<<(*msg);
was_something_printed = true;
}
msg += 1;
}
if( was_something_printed )
{
operator<<(logend);
}
}
} // namespace
#endif

Loading…
Cancel
Save