Browse Source

Merge branch 'api2021'

master
Tomasz Sowa 3 months ago
parent
commit
6c41e0a803
  1. 57
      src/Makefile.dep
  2. 3
      src/convert/convert.h
  3. 225
      src/convert/double.cpp
  4. 80
      src/convert/double.h
  5. 6
      src/convert/patternreplacer.h
  6. 68
      src/convert/text.cpp
  7. 20
      src/convert/text.h
  8. 106
      src/convert/text_private.h
  9. 61
      src/date/date.h
  10. 377
      src/log/log.cpp
  11. 162
      src/log/log.h
  12. 2
      src/mainoptions/mainoptionsparser.cpp
  13. 706
      src/space/space.cpp
  14. 315
      src/space/space.h
  15. 8
      src/space/spaceparser.cpp
  16. 149
      src/textstream/stream.h
  17. 345
      src/textstream/textstream.h
  18. 41
      src/utf8/utf8.cpp
  19. 20
      src/utf8/utf8.h
  20. 28
      src/utf8/utf8_private.h
  21. 128
      src/utf8/utf8_templates.h
  22. 25
      tests/Makefile.dep

57
src/Makefile.dep

@ -3,41 +3,42 @@
./convert/inttostr.o: ./convert/inttostr.h
./convert/misc.o: ./convert/misc.h ./convert/text.h
./convert/text.o: ./convert/text.h ./convert/text_private.h
./convert/double.o: ./convert/double.h textstream/textstream.h
./convert/double.o: textstream/stream.h space/space.h textstream/types.h
./convert/double.o: convert/inttostr.h utf8/utf8.h textstream/stream.h
./convert/double.o: utf8/utf8_templates.h utf8/utf8_private.h date/date.h
./convert/double.o: membuffer/membuffer.h textstream/types.h
./date/date.o: ./date/date.h convert/inttostr.h
./log/filelog.o: ./log/filelog.h textstream/textstream.h space/space.h
./log/filelog.o: textstream/types.h convert/inttostr.h date/date.h
./log/filelog.o: membuffer/membuffer.h textstream/types.h utf8/utf8.h
./log/filelog.o: utf8/utf8_templates.h utf8/utf8_private.h
./log/log.o: ./log/log.h textstream/textstream.h space/space.h
./log/log.o: textstream/types.h convert/inttostr.h date/date.h
./log/log.o: membuffer/membuffer.h textstream/types.h ./log/filelog.h
./log/log.o: utf8/utf8.h utf8/utf8_templates.h utf8/utf8_private.h
./log/filelog.o: ./log/filelog.h textstream/textstream.h textstream/stream.h
./log/filelog.o: space/space.h textstream/types.h convert/inttostr.h
./log/filelog.o: utf8/utf8.h textstream/stream.h utf8/utf8_templates.h
./log/filelog.o: utf8/utf8_private.h date/date.h membuffer/membuffer.h
./log/filelog.o: textstream/types.h
./log/log.o: ./log/log.h textstream/textstream.h textstream/stream.h
./log/log.o: space/space.h textstream/types.h convert/inttostr.h utf8/utf8.h
./log/log.o: textstream/stream.h utf8/utf8_templates.h utf8/utf8_private.h
./log/log.o: date/date.h membuffer/membuffer.h textstream/types.h
./log/log.o: ./log/filelog.h
./space/space.o: ./space/space.h textstream/types.h convert/inttostr.h
./space/space.o: utf8/utf8.h textstream/textstream.h space/space.h
./space/space.o: date/date.h membuffer/membuffer.h textstream/types.h
./space/space.o: utf8/utf8_templates.h utf8/utf8_private.h convert/convert.h
./space/space.o: ./convert/inttostr.h convert/patternreplacer.h
./space/space.o: convert/strtoint.h ./convert/text.h ./convert/misc.h
./space/space.o: utf8/utf8.h textstream/stream.h utf8/utf8_templates.h
./space/space.o: utf8/utf8_private.h convert/convert.h ./convert/inttostr.h
./space/space.o: convert/patternreplacer.h textstream/textstream.h
./space/space.o: textstream/stream.h space/space.h date/date.h
./space/space.o: membuffer/membuffer.h textstream/types.h convert/strtoint.h
./space/space.o: ./convert/text.h ./convert/misc.h ./convert/double.h
./space/spaceparser.o: ./space/spaceparser.h ./space/space.h
./space/spaceparser.o: textstream/types.h convert/inttostr.h utf8/utf8.h
./space/spaceparser.o: textstream/textstream.h space/space.h date/date.h
./space/spaceparser.o: membuffer/membuffer.h textstream/types.h
./space/spaceparser.o: utf8/utf8_templates.h utf8/utf8_private.h
./space/spaceparser.o: convert/strtoint.h ./convert/text.h ./convert/misc.h
./utf8/utf8.o: ./utf8/utf8.h textstream/textstream.h space/space.h
./utf8/utf8.o: textstream/types.h convert/inttostr.h date/date.h
./utf8/utf8.o: membuffer/membuffer.h textstream/types.h utf8/utf8_templates.h
./space/spaceparser.o: textstream/stream.h utf8/utf8_templates.h
./space/spaceparser.o: utf8/utf8_private.h convert/strtoint.h
./space/spaceparser.o: ./convert/text.h ./convert/misc.h
./utf8/utf8.o: ./utf8/utf8.h textstream/stream.h utf8/utf8_templates.h
./utf8/utf8.o: utf8/utf8_private.h
./utf8/utf8_private.o: utf8/utf8_private.h textstream/textstream.h
./utf8/utf8_private.o: space/space.h textstream/types.h convert/inttostr.h
./utf8/utf8_private.o: date/date.h membuffer/membuffer.h textstream/types.h
./utf8/utf8_private.o: utf8/utf8_private.h
./csv/csvparser.o: ./csv/csvparser.h space/space.h textstream/types.h
./csv/csvparser.o: convert/inttostr.h utf8/utf8.h textstream/textstream.h
./csv/csvparser.o: date/date.h membuffer/membuffer.h textstream/types.h
./csv/csvparser.o: convert/inttostr.h utf8/utf8.h textstream/stream.h
./csv/csvparser.o: utf8/utf8_templates.h utf8/utf8_private.h
./mainoptions/mainoptionsparser.o: ./mainoptions/mainoptionsparser.h
./mainoptions/mainoptionsparser.o: space/space.h textstream/types.h
./mainoptions/mainoptionsparser.o: convert/inttostr.h utf8/utf8.h
./mainoptions/mainoptionsparser.o: textstream/textstream.h date/date.h
./mainoptions/mainoptionsparser.o: membuffer/membuffer.h textstream/types.h
./mainoptions/mainoptionsparser.o: utf8/utf8_templates.h utf8/utf8_private.h
./mainoptions/mainoptionsparser.o: textstream/stream.h utf8/utf8_templates.h
./mainoptions/mainoptionsparser.o: utf8/utf8_private.h

3
src/convert/convert.h

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2012-2018, Tomasz Sowa
* Copyright (c) 2012-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -43,5 +43,6 @@
#include "patternreplacer.h"
#include "strtoint.h"
#include "text.h"
#include "double.h"
#endif

225
src/convert/double.cpp

@ -0,0 +1,225 @@
/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name Tomasz Sowa nor the names of contributors to this
* project may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstdlib>
#include <cwchar>
#include "double.h"
#include "textstream/textstream.h"
namespace pt
{
float to_float(const char * str, const char ** after)
{
char * after_local;
float res = std::strtof(str, &after_local);
if( after )
{
*after = after_local;
}
return res;
}
float to_float(const wchar_t * str, const wchar_t ** after)
{
wchar_t * after_local;
float res = std::wcstof(str, &after_local);
if( after )
{
*after = after_local;
}
return res;
}
double to_double(const char * str, const char ** after)
{
char * after_local;
double res = std::strtod(str, &after_local);
if( after )
{
*after = after_local;
}
return res;
}
double to_double(const wchar_t * str, const wchar_t ** after)
{
wchar_t * after_local;
double res = std::wcstod(str, &after_local);
if( after )
{
*after = after_local;
}
return res;
}
long double to_long_double(const char * str, const char ** after)
{
char * after_local;
long double res = std::strtold(str, &after_local);
if( after )
{
*after = after_local;
}
return res;
}
long double to_long_double(const wchar_t * str, const wchar_t ** after)
{
wchar_t * after_local;
long double res = std::wcstold(str, &after_local);
if( after )
{
*after = after_local;
}
return res;
}
float to_float(const std::string & str, const char ** after)
{
return to_float(str.c_str(), after);
}
float to_float(const std::wstring & str, const wchar_t ** after)
{
return to_float(str.c_str(), after);
}
double to_double(const std::string & str, const char ** after)
{
return to_double(str.c_str(), after);
}
double to_double(const std::wstring & str, const wchar_t ** after)
{
return to_double(str.c_str(), after);
}
long double to_long_double(const std::string & str, const char ** after)
{
return to_long_double(str.c_str(), after);
}
long double to_long_double(const std::wstring & str, const wchar_t ** after)
{
return to_long_double(str.c_str(), after);
}
std::string to_str(float val)
{
TextStream str;
str << val;
return str.to_str();;
}
std::wstring to_wstr(float val)
{
TextStream str;
str << val;
return str.to_wstr();;
}
std::string to_str(double val)
{
TextStream str;
str << val;
return str.to_str();;
}
std::wstring to_wstr(double val)
{
TextStream str;
str << val;
return str.to_wstr();;
}
std::string to_str(long double val)
{
TextStream str;
str << val;
return str.to_str();;
}
std::wstring to_wstr(long double val)
{
TextStream str;
str << val;
return str.to_wstr();;
}
}

80
src/convert/double.h

@ -0,0 +1,80 @@
/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name Tomasz Sowa nor the names of contributors to this
* project may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_picotools_membuffer_convert_double
#define headerfile_picotools_membuffer_convert_double
#include <string>
namespace pt
{
float to_float(const char * str, const char ** after = nullptr);
float to_float(const wchar_t * str, const wchar_t ** after = nullptr);
double to_double(const char * str, const char ** after = nullptr);
double to_double(const wchar_t * str, const wchar_t ** after = nullptr);
long double to_long_double(const char * str, const char ** after = nullptr);
long double to_long_double(const wchar_t * str, const wchar_t ** after = nullptr);
float to_float(const std::string & str, const char ** after = nullptr);
float to_float(const std::wstring & str, const wchar_t ** after = nullptr);
double to_double(const std::string & str, const char ** after = nullptr);
double to_double(const std::wstring & str, const wchar_t ** after = nullptr);
long double to_long_double(const std::string & str, const char ** after = nullptr);
long double to_long_double(const std::wstring & str, const wchar_t ** after = nullptr);
std::string to_str(float val);
std::wstring to_wstr(float val);
std::string to_str(double val);
std::wstring to_wstr(double val);
std::string to_str(long double val);
std::wstring to_wstr(long double val);
}
#endif

6
src/convert/patternreplacer.h

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* Copyright (c) 2018-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -86,7 +86,7 @@ private:
params.clear();
AddParams(types...);
ReplacePattern(pattern);
buffer.to_string(output_string);
buffer.to_str(output_string);
params.clear();
buffer.clear();
@ -104,7 +104,7 @@ private:
{
buffer.clear();
buffer << type;
buffer.to_string(temp_str);
buffer.to_str(temp_str);
params.push_back(temp_str);
temp_str.clear();

68
src/convert/text.cpp

@ -463,8 +463,76 @@ bool is_substr_nc(const std::wstring & short_str, const std::wstring & long_str)
}
void trim_first_white(std::string & str, bool check_additional_chars, bool treat_new_line_as_white)
{
pt_private::trim_first_white_generic(str, check_additional_chars, treat_new_line_as_white);
}
void trim_first_white(std::wstring & str, bool check_additional_chars, bool treat_new_line_as_white)
{
pt_private::trim_first_white_generic(str, check_additional_chars, treat_new_line_as_white);
}
void trim_last_white(std::string & str, bool check_additional_chars, bool treat_new_line_as_white)
{
pt_private::trim_last_white_generic(str, check_additional_chars, treat_new_line_as_white);
}
void trim_last_white(std::wstring & str, bool check_additional_chars, bool treat_new_line_as_white)
{
pt_private::trim_last_white_generic(str, check_additional_chars, treat_new_line_as_white);
}
void trim_white(std::string & str, bool check_additional_chars, bool treat_new_line_as_white)
{
pt_private::trim_white_generic(str, check_additional_chars, treat_new_line_as_white);
}
void trim_white(std::wstring & str, bool check_additional_chars, bool treat_new_line_as_white)
{
pt_private::trim_white_generic(str, check_additional_chars, treat_new_line_as_white);
}
void trim_first(std::string & str, wchar_t c)
{
pt_private::trim_first_generic(str, c);
}
void trim_first(std::wstring & str, wchar_t c)
{
pt_private::trim_first_generic(str, c);
}
void trim_last(std::string & str, wchar_t c)
{
pt_private::trim_last_generic(str, c);
}
void trim_last(std::wstring & str, wchar_t c)
{
pt_private::trim_last_generic(str, c);
}
void trim(std::string & str, wchar_t c)
{
pt_private::trim_generic(str, c);
}
void trim(std::wstring & str, wchar_t c)
{
pt_private::trim_generic(str, c);
}

20
src/convert/text.h

@ -143,6 +143,26 @@ bool is_substr_nc(const std::string & short_str, const std::string & long_str);
bool is_substr_nc(const std::wstring & short_str, const std::wstring & long_str);
void trim_first_white(std::string & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_first_white(std::wstring & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_last_white(std::string & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_last_white(std::wstring & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_white(std::string & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_white(std::wstring & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_first(std::string & str, wchar_t c);
void trim_first(std::wstring & str, wchar_t c);
void trim_last(std::string & str, wchar_t c);
void trim_last(std::wstring & str, wchar_t c);
void trim(std::string & str, wchar_t c);
void trim(std::wstring & str, wchar_t c);
}

106
src/convert/text_private.h

@ -299,6 +299,112 @@ bool is_substr_nc_generic(const StringType1 * short_str, const StringType2 * lon
template<typename StringType>
void trim_first_white_generic(StringType & s, bool check_additional_chars, bool treat_new_line_as_white)
{
size_t i;
if( s.empty() )
return;
// looking for white characters at the beginning
for(i=0 ; i<s.size() && is_white(s[i], check_additional_chars, treat_new_line_as_white) ; ++i);
// deleting white characters at the beginning
if( i != 0 )
s.erase(0, i);
}
template<typename StringType>
void trim_last_white_generic(StringType & s, bool check_additional_chars, bool treat_new_line_as_white)
{
size_t i;
if( s.empty() )
return;
// looking for white characters at the end
for(i=s.size()-1 ; i>0 && is_white(s[i], check_additional_chars, treat_new_line_as_white) ; --i);
if( i==0 && is_white(s[i], check_additional_chars, treat_new_line_as_white) )
{
// the whole string has white characters
s.clear();
return;
}
// deleting white characters at the end
if( i != s.size() - 1 )
s.erase(i+1, StringType::npos);
}
template<typename StringType>
void trim_white_generic(StringType & s, bool check_additional_chars, bool treat_new_line_as_white)
{
if( s.empty() )
return;
trim_last_white_generic(s, check_additional_chars, treat_new_line_as_white);
trim_first_white_generic(s, check_additional_chars, treat_new_line_as_white);
}
template<typename StringType>
void trim_first_generic(StringType & s, wchar_t c)
{
size_t i;
if( s.empty() )
return;
// looking for the 'c' characters at the beginning
for(i=0 ; i<s.size() && s[i]==c ; ++i);
// deleting the 'c' characters at the beginning
if( i != 0 )
s.erase(0, i);
}
template<typename StringType>
void trim_last_generic(StringType & s, wchar_t c)
{
size_t i;
if( s.empty() )
return;
// looking for the 'c' characters at the end
for(i=s.size()-1 ; i>0 && s[i]==c ; --i);
if( i==0 && s[i]==c )
{
// the whole string has the 'c' characters
s.clear();
return;
}
// deleting 'c' characters at the end
if( i != s.size() - 1 )
s.erase(i+1, StringType::npos);
}
template<typename StringType>
void trim_generic(StringType & s, wchar_t c)
{
if( s.empty() )
return;
trim_last_generic(s, c);
trim_first_generic(s, c);
}
} // namespace pt_private

61
src/date/date.h

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2012-2018, Tomasz Sowa
* Copyright (c) 2012-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -251,12 +251,21 @@ public:
int WeekDay() const;
/*
this method outputs to the given stream the number of a month in Roman numerals
e.g: if month is equal to 3 then III will be put to 'out'
month should be in the range [1,12]
*/
template<class Stream>
static void SerializeMonthAsRoman(Stream & out, int month);
/*
this method outputs to the given stream: YYYY-MM-DD, eg. 1990-02-12
ISO 8601 format
*/
template<class Stream>
void SerializeYearMonthDay(Stream & out) const;
void SerializeYearMonthDay(Stream & out, bool roman_month = false) const;
/*
@ -271,7 +280,7 @@ public:
this method outputs to the given stream: MM-DD, eg. 02-12 (02 month, 12 day)
*/
template<class Stream>
void SerializeMonthDay(Stream & out) const;
void SerializeMonthDay(Stream & out, bool roman_month = false) const;
/*
@ -286,7 +295,7 @@ public:
ISO 8601 format
*/
template<class Stream>
void Serialize(Stream & out) const;
void Serialize(Stream & out, bool roman_month = false, bool with_seconds = true) const;
/*
@ -547,13 +556,32 @@ void Date::SerializeInt(Stream & out, int val) const
}
template<class Stream>
void Date::SerializeMonthAsRoman(Stream & out, int month)
{
const char * month_roman[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII" };
if( month >= 1 && month <= 12 )
{
const char * month_str = month_roman[month - 1];
for( ; *month_str ; ++month_str)
out << *month_str;
}
}
template<class Stream>
void Date::SerializeYearMonthDay(Stream & out) const
void Date::SerializeYearMonthDay(Stream & out, bool roman_month) const
{
SerializeInt(out, year, 4);
out << '-';
SerializeInt(out, month);
if( roman_month )
SerializeMonthAsRoman(out, month);
else
SerializeInt(out, month);
out << '-';
SerializeInt(out, day);
}
@ -571,9 +599,13 @@ void Date::SerializeHourMinSec(Stream & out) const
template<class Stream>
void Date::SerializeMonthDay(Stream & out) const
void Date::SerializeMonthDay(Stream & out, bool roman_month) const
{
SerializeInt(out, month);
if( roman_month )
SerializeMonthAsRoman(out, month);
else
SerializeInt(out, month);
out << '-';
SerializeInt(out, day);
}
@ -589,17 +621,22 @@ void Date::SerializeHourMin(Stream & out) const
template<class Stream>
void Date::Serialize(Stream & out) const
void Date::Serialize(Stream & out, bool roman_month, bool with_seconds) const
{
SerializeYearMonthDay(out);
SerializeYearMonthDay(out, roman_month);
out << ' ';
SerializeHourMinSec(out);
if( with_seconds )
SerializeHourMinSec(out);
else
SerializeHourMin(out);
}
template<class Stream>
void Date::SerializeISO(Stream & out) const
{
SerializeYearMonthDay(out);
SerializeYearMonthDay(out, false);
out << 'T';
SerializeHourMinSec(out);
out << 'Z';

377
src/log/log.cpp

@ -41,7 +41,7 @@
#include "date/date.h"
#include "utf8/utf8.h"
#ifdef PT_HAS_MORM
#ifdef PT_HAS_MORM_LIBRARY
#include "morm.h"
#endif
@ -54,113 +54,156 @@ Log::Log()
{
buffer = nullptr;
file_log = nullptr;
current_level = 4;
max_buffer_length = 2 * 1024 * 1024; // 2MB
max_buffer_length = 2 * 1024 * 1024;
}
Log::~Log()
{
// IMPROVE ME
// I am not sure if this is a correct behaviour
// the log buffer and file logger may not exist now
// (life time of objects)
// may would be better to have a flag 'clear_at_the_end'
// and if true then call this method?
}
save_log_and_clear();
bool Log::can_put_log()
{
return (buffer && file_log && current_level <= file_log->get_log_level());
}
void Log::SetLogBuffer(WTextStream * buffer)
bool Log::is_char_stream() const
{
this->buffer = buffer;
return false;
}
WTextStream * Log::GetLogBuffer()
bool Log::is_wchar_stream() const
{
return buffer;
return true;
}
void Log::SetMaxBufferLength(size_t max_buffer_length)
void Log::clear()
{
this->max_buffer_length = max_buffer_length;
if( buffer )
{
buffer->clear();
}
}
size_t Log::GetMaxBufferLength()
bool Log::empty() const
{
return max_buffer_length;
if( buffer )
{
return buffer->empty();
}
return true;
}
void Log::SetFileLog(FileLog * file_log)
size_t Log::size() const
{
this->file_log = file_log;
if( buffer )
{
buffer->size();
}
return 0;
}
FileLog * Log::GetFileLog()
void Log::reserve(size_t len)
{
return file_log;
if( buffer )
{
buffer->reserve(len);
}
}
Log & Log::IntMinWidth(size_t min_width)
size_t Log::capacity() const
{
if( buffer )
{
buffer->int_min_width(min_width);
buffer->capacity();
}
return *this;
return 0;
}
Log & Log::operator<<(const void * s)
void Log::to_str(std::string & str, bool clear_string) const
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( buffer )
{
(*buffer) << s;
buffer->to_str(str, clear_string);
}
}
return *this;
void Log::to_str(std::wstring & str, bool clear_string) const
{
if( buffer )
{
buffer->to_str(str, clear_string);
}
}
Log & Log::operator<<(const char * s)
std::string Log::to_str() const
{
if( buffer && file_log && s && current_level <= file_log->get_log_level() )
if( buffer )
{
utf8_to_wide(s, *buffer, false);
return buffer->to_str();
}
return *this;
return std::string();
}
std::wstring Log::to_wstr() const
{
if( buffer )
{
return buffer->to_wstr();
}
Log & Log::operator<<(const std::string & s)
return std::wstring();
}
char Log::get_char(size_t index) const
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( buffer )
{
utf8_to_wide(s, *buffer, false);
return buffer->get_char(index);
}
return *this;
return 0;
}
wchar_t Log::get_wchar(size_t index) const
{
if( buffer )
{
return buffer->get_wchar(index);
}
return 0;
}
Log & Log::operator<<(const std::string * s)
Log & Log::operator<<(const char * s)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
utf8_to_wide(*s, *buffer, false);
(*buffer) << s;
}
return *this;
@ -168,12 +211,20 @@ Log & Log::operator<<(const std::string * s)
Log & Log::operator<<(const std::string & s)
{
if( can_put_log() )
{
(*buffer) << s;
}
return *this;
}
Log & Log::operator<<(const wchar_t * s)
{
if( buffer && file_log && s && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
}
@ -182,10 +233,9 @@ Log & Log::operator<<(const wchar_t * s)
}
Log & Log::operator<<(const std::wstring & s)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
}
@ -194,49 +244,67 @@ Log & Log::operator<<(const std::wstring & s)
}
Log & Log::operator<<(const std::wstring * s)
Log & Log::operator<<(char val)
{
if( buffer && file_log && s && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << *s;
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(unsigned char val)
{
if( can_put_log() )
{
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(int s)
Log & Log::operator<<(wchar_t val)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
{
(*buffer) << s;
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(bool val)
{
if( can_put_log() )
{
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(long s)
Log & Log::operator<<(short val)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
(*buffer) << val;
}
return *this;
return *this;
}
Log & Log::operator<<(long long s)
Log & Log::operator<<(int val)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
(*buffer) << val;
}
return *this;
@ -244,29 +312,85 @@ Log & Log::operator<<(long long s)
Log & Log::operator<<(char s)
Log & Log::operator<<(long val)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(wchar_t s)
Log & Log::operator<<(long long val)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(size_t s)
Log & Log::operator<<(unsigned short val)
{
if( can_put_log() )
{
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(unsigned int val)
{
if( can_put_log() )
{
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(unsigned long val)
{
if( can_put_log() )
{
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(unsigned long long val)
{
if( can_put_log() )
{
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(float val)
{
if( can_put_log() )
{
(*buffer) << val;
}
return *this;
}
Log & Log::operator<<(double s)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
{
@ -277,10 +401,20 @@ Log & Log::operator<<(size_t s)
}
Log & Log::operator<<(long double val)
{
if( can_put_log() )
{
(*buffer) << val;
}
Log & Log::operator<<(double s)
return *this;
}
Log & Log::operator<<(const void * s)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
}
@ -289,10 +423,20 @@ Log & Log::operator<<(double s)
}
Log & Log::operator<<(const Stream & stream)
{
if( can_put_log() )
{
(*buffer) << stream;
}
return *this;
}
Log & Log::operator<<(const Space & s)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << s;
}
@ -301,10 +445,9 @@ Log & Log::operator<<(const Space & s)
}
Log & Log::operator<<(const Date & date)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << date;
}
@ -313,7 +456,7 @@ Log & Log::operator<<(const Date & date)
}
#ifdef PT_HAS_MORM
#ifdef PT_HAS_MORM_LIBRARY
Log & Log::operator<<(morm::Model & model)
{
operator<<(model.to_string());
@ -322,13 +465,35 @@ Log & Log::operator<<(morm::Model & model)
#endif
Log & Log::write(const char * buf, size_t len)
{
if( can_put_log() )
{
buffer->write(buf, len);
}
return *this;
}
Log & Log::write(const wchar_t * buf, size_t len)
{
if( can_put_log() )
{
buffer->write(buf, len);
}
return *this;
}
Log & Log::operator<<(Manipulators m)
{
switch(m)
{
case lend:
case logend:
if( buffer && file_log && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
(*buffer) << '\n';
@ -337,27 +502,22 @@ Log & Log::operator<<(Manipulators m)
}
break;
case lsave:
case logsave:
save_log_and_clear();
break;
case l1:
case log1:
current_level = 1;
break;
case l2:
case log2:
current_level = 2;
break;
case l3:
case log3:
current_level = 3;
break;
case l4:
case log4:
current_level = 4;
break;
@ -370,18 +530,63 @@ Log & Log::operator<<(Manipulators m)
}
Log & Log::LogString(const std::string & value, size_t max_size)
void Log::set_log_buffer(WTextStream * buffer)
{
return log_string_generic(value, max_size);
this->buffer = buffer;
}
WTextStream * Log::get_log_buffer()
{
return buffer;
}
void Log::set_max_buffer_length(size_t max_buffer_length)
{
this->max_buffer_length = max_buffer_length;
}
size_t Log::get_max_buffer_length()
{
return max_buffer_length;
}
void Log::set_file_log(FileLog * file_log)
{
this->file_log = file_log;
}
FileLog * Log::get_file_log()
{
return file_log;
}
Log & Log::int_min_width(size_t min_width)
{
if( buffer )
{
buffer->int_min_width(min_width);
}
return *this;
}
Log & Log::LogString(const std::wstring & value, size_t max_size)
Log & Log::put_string(const std::string & value, size_t max_size)
{
return log_string_generic(value, max_size);
}
Log & Log::put_string(const std::wstring & value, size_t max_size)
{
return log_string_generic(value, max_size);
}
char Log::get_hex_digit(unsigned char c)
@ -393,7 +598,6 @@ return c - 10 + 'A';
}
void Log::to_hex(char * buf, unsigned char c)
{
buf[0] = get_hex_digit(c >> 4);
@ -402,12 +606,12 @@ void Log::to_hex(char * buf, unsigned char c)
}
Log & Log::LogBinary(const char * blob, size_t blob_len)
Log & Log::put_binary_blob(const char * blob, size_t blob_len)
{
size_t i=0;
char buf[3];
if( buffer && file_log && blob && current_level <= file_log->get_log_level() )
if( can_put_log() )
{
while( i < blob_len )
{
@ -454,13 +658,12 @@ char buf[3];
}
Log & Log::LogBinary(const std::string & blob)
Log & Log::put_binary_blob(const std::string & blob)
{
return LogBinary(blob.c_str(), blob.size());
return put_binary_blob(blob.c_str(), blob.size());
}
void Log::save_log_and_clear()
{
save_log();
@ -483,7 +686,3 @@ void Log::save_log()
} // namespace

162
src/log/log.h

@ -45,22 +45,16 @@
namespace morm
{
class Model;
}
namespace pt
{
class Log
class Log : public Stream
{
public:
/*
log1 - the first level
log2
@ -76,81 +70,91 @@ public:
log3,
log4,
l1,
l2,
l3,
l4,
logend,
lend,
logsave,
lsave,
};
Log();
virtual ~Log();
virtual void SetLogBuffer(WTextStream * buffer);
virtual WTextStream * GetLogBuffer();
void SetFileLog(FileLog * file_log);
FileLog * GetFileLog();
void SetMaxBufferLength(size_t max_buffer_length);
size_t GetMaxBufferLength();
virtual Log & IntMinWidth(size_t min_width);
virtual Log & operator<<(const void * s);
virtual Log & operator<<(const char * s);
virtual Log & operator<<(const std::string * s);
virtual Log & operator<<(const std::string & s);
virtual Log & operator<<(const wchar_t * s);
virtual Log & operator<<(const std::wstring * s);
virtual Log & operator<<(const std::wstring & s);
virtual Log & operator<<(char s);
virtual Log & operator<<(wchar_t s);
virtual Log & operator<<(int s);
virtual Log & operator<<(long s);
virtual Log & operator<<(long long s);
// add unsigned long, unsigned int
virtual Log & operator<<(size_t s);
//virtual Log & operator<<(float s); // added
virtual Log & operator<<(double s);
virtual Log & operator<<(const Space & space);
virtual Log & operator<<(const Date & date);
#ifdef PT_HAS_MORM
~Log();
bool is_char_stream() const;
bool is_wchar_stream() const;
void clear();
bool empty() const;
size_t size() const;
void reserve(size_t len);
size_t capacity() const;
void to_str(std::string & str, bool clear_string = true) const;