Commit 7beb29a3 authored by Martin Marinov's avatar Martin Marinov Committed by GitHub Enterprise

REFORM-838 Add and generalize Journal code refactored from ReForm (#2)

* Add the Journal sources
* Remove ReForm specific code in Journal and allow various configuration options
* Fix a macro that was incorrect if JOURNAL_ON was undefined
* Journal can work either with Boost or the std::experimental filesystem implementations
* OStringStream using std::ostringstream now and sets the locale to "C"
* Debug::FIle now uses OStringStream
* Add IOutputStream:Base::print(float)
* Fix initialization bugs
* Allow comment streaming, allow existing journal entries to be output as comments, improve code consistency
* Document the Journal functions
* Add Journal::output_path()
parent 99f338eb
......@@ -32,10 +32,11 @@ endfunction(base_add_subdir)
base_add_subdir(Code)
base_add_subdir(Config)
base_add_subdir(Debug)
base_add_subdir(Utils)
base_add_subdir(Journal)
base_add_subdir(Progress)
base_add_subdir(Security)
base_add_subdir(Test)
base_add_subdir(Utils)
target_include_directories(${output_lib} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..")
......
......@@ -5,6 +5,7 @@
#include "DebCallStack.hh"
#include "Base/Utils/Environment.hh"
#include "Base/Utils/OStringStream.hh"
#include <string>
#include <fstream>
......@@ -42,8 +43,8 @@ void File::line_break(const bool _cnsl) { print('\n', _cnsl); }
void File::print(const std::string& _s)
{
for (size_t i = 0, n = _s.size(); i < n; ++i)
print(_s[i]);
for (const auto c : _s)
print(c);
}
void File::print(const char* const _s)
......@@ -57,28 +58,30 @@ void File::print(const char* const _s)
void File::print(const size_t _i)
{
char buffer[128];
#if defined(_MSC_VER) && _MSC_VER < 1900 // MSVC versions older than VC2015
sprintf_s(buffer, sizeof(buffer), "%Iu", _i);
#else // MSVC 2015 and everything else
sprintf_s(buffer, sizeof(buffer), "%zu", _i);
#endif
print(buffer);
Base::OStringStream strm;
strm.print(_i);
print(strm.str);
}
void File::print(const int _i)
{
char buffer[64];
sprintf_s(buffer, sizeof(buffer), "%i", _i);
print(buffer);
Base::OStringStream strm;
strm.print(_i);
print(strm.str);
}
void File::print(float _f)
{
Base::OStringStream strm;
strm.print(_f);
print(strm.str);
}
void File::print(double _d)
{
char buffer[64];
sprintf_s(buffer, sizeof(buffer), double_format, _d);
print(buffer);
Base::OStringStream strm;
strm.print(_d);
print(strm.str);
}
void File::print(const Base::Command& _co)
......
......@@ -36,6 +36,7 @@ public:
void print(const std::string& _s);
void print(const size_t _i);
void print(const int _i);
void print(float _f);
void print(double _d);
void print(const Base::Command& _co);
......
......@@ -62,12 +62,13 @@ class BASEDLLEXPORT Stream : public Base::IOutputStream
public:
Stream(File& _file) : file_(_file) {}
Base::IOutputStream& print(const int);
Base::IOutputStream& print(const size_t);
Base::IOutputStream& print(const double);
Base::IOutputStream& print(const char* const);
Base::IOutputStream& print(const char);
Base::IOutputStream& print(const Base::Command&);
Base::IOutputStream& print(const int) override;
Base::IOutputStream& print(const size_t) override;
Base::IOutputStream& print(const float) override;
Base::IOutputStream& print(const double) override;
Base::IOutputStream& print(const char* const) override;
Base::IOutputStream& print(const char) override;
Base::IOutputStream& print(const Base::Command&) override;
private:
File& file_;
......
......@@ -74,6 +74,12 @@ Base::IOutputStream& Stream::print(const size_t _i)
return *this;
};
Base::IOutputStream& Stream::print(const float _f)
{
file_.print(_f);
return *this;
};
Base::IOutputStream& Stream::print(const double _d)
{
file_.print(_d);
......
set(my_headers
${CMAKE_CURRENT_SOURCE_DIR}/JournalStream.hh
PARENT_SCOPE
)
set(my_sources
${CMAKE_CURRENT_SOURCE_DIR}/JournalStream.cc
PARENT_SCOPE
)
This diff is collapsed.
This diff is collapsed.
......@@ -48,6 +48,7 @@ public:
virtual IOutputStream& print(const char) = 0;
virtual IOutputStream& print(const int) = 0;
virtual IOutputStream& print(const size_t) = 0;
virtual IOutputStream& print(const float) = 0;
virtual IOutputStream& print(const double) = 0;
virtual IOutputStream& print(const char* const) = 0;
virtual IOutputStream& print(const Command&) = 0;
......@@ -160,17 +161,43 @@ public:
OutputStreamAdaptT(const ArgT& _arg) : strm_(_arg) {}
#endif
virtual IOutputStream& print(const char _c) { strm_ << _c; return *this; }
virtual IOutputStream& print(const int _i) { strm_ << _i; return *this; }
virtual IOutputStream& print(const size_t _i) { strm_ << _i; return *this; }
virtual IOutputStream& print(const double _d) { strm_ << _d; return *this; }
virtual IOutputStream& print(const char* const _str)
{
strm_ << _str;
return *this;
IOutputStream& print(const char _c) override
{
strm_ << _c;
return *this;
}
virtual IOutputStream& print(const Command& _cmd)
IOutputStream& print(const int _i) override
{
strm_ << _i;
return *this;
}
IOutputStream& print(const size_t _i) override
{
strm_ << _i;
return *this;
}
IOutputStream& print(const float _f) override
{
strm_ << _f;
return *this;
}
IOutputStream& print(const double _d) override
{
strm_ << _d;
return *this;
}
IOutputStream& print(const char* const _str) override
{
strm_ << _str;
return *this;
}
IOutputStream& print(const Command& _cmd) override
{
if ((_cmd.cmd & Command::END_LF) == (int)Command::END_LF)
strm_ << '\n'; // end line on both END_LF and END_ERR
......@@ -182,6 +209,7 @@ public:
protected:
StreamT strm_;
private:
OutputStreamAdaptT(const OutputStreamAdaptT& _oth);
OutputStreamAdaptT& operator=(const OutputStreamAdaptT&);
......@@ -237,35 +265,38 @@ inline FormatT<> format(const char* const _frmt, const T& _vrbl)
return FormatT<>(_frmt, _vrbl);
}
namespace FormatHex {
template <int byte_nmbr> struct CastT;
template <> struct CastT<4> { typedef uint32_t Type; };
template <> struct CastT<8> { typedef uint64_t Type; };
const char* const PREFIX_VOID = "";
const char* const PREFIX_0x = "0x";
const char* const PREFIX_0X = "0X";
}//FormatHex
//! Format a 32bit uint variable for streaming in hex (e.g. for hash)
template <typename T> //!< 32 or 64 bit type
inline FormatT<> format_hex(const T _vrbl)
//! Format an unsigned int variable for streaming in hex (e.g. for hash)
template <typename T> //!< 32 or 64 bit type
inline FormatT<> format_hex(
const T _vrbl, const char* const _prfx = FormatHex::PREFIX_VOID)
{
return format_hex(typename FormatHex::CastT<sizeof(T)>::Type(_vrbl));
return format_hex(typename FormatHex::CastT<sizeof(T)>::Type(_vrbl), _prfx);
}
//! Format a 32-bit unsigned int variable for streaming in hex (e.g. for hash)
template <>
inline FormatT<> format_hex(const uint32_t _vrbl)
inline FormatT<> format_hex(const uint32_t _vrbl, const char* const _prfx)
{
return FormatT<>("%" PRIx32, _vrbl);
return FormatT<>("%s%" PRIx32, _prfx, _vrbl);
}
//! Format a 64bit size_t variable for streaming in hex (e.g. for hash)
//! Format a 64-bit unsigned int variable for streaming in hex (e.g. for hash)
template <>
inline FormatT<> format_hex(const uint64_t _vrbl)
inline FormatT<> format_hex(const uint64_t _vrbl, const char* const _prfx)
{
return FormatT<>("%" PRIx64, _vrbl);
return FormatT<>("%s%" PRIx64, _prfx, _vrbl);
}
}//namespace Base
#endif//BASE_IOUTPUTSTREAM_HH_INCLUDE
......@@ -4,17 +4,28 @@
#include "OStringStream.hh"
#include <stdarg.h>
#include <sstream>
#include <iomanip>
#ifndef _MSC_VER
#include <cstdio>
#include <cstdarg>
#include <sstream>
#endif
namespace Base {
#ifndef DBL_DECIMAL_DIG
#define DBL_DECIMAL_DIG 17
#endif // DBL_DECIMAL_DIG
#ifndef FLT_DECIMAL_DIG
#define FLT_DECIMAL_DIG 9
#endif // FLT_DECIMAL_DIG
namespace Base
{
namespace
{
namespace {
// NOTE: This should be portable, but have not tested
template <size_t _bffr_size>
int sprintf_s(char (&_bffr)[_bffr_size], const char* _frmt, ...)
{
......@@ -29,7 +40,7 @@ int sprintf_s(char (&_bffr)[_bffr_size], const char* _frmt, ...)
return res;
}
}//namespace
} // namespace
int print(char* _bffr, const size_t _bffr_size, const char* _frmt, ...)
{
......@@ -44,6 +55,15 @@ int print(char* _bffr, const size_t _bffr_size, const char* _frmt, ...)
return res;
}
struct CLocaleOStringStream : public std::ostringstream
{
CLocaleOStringStream() { this->imbue(std::locale("C")); }
void set_precision_double() { *this << std::setprecision(DBL_DECIMAL_DIG); }
void set_precision_float() { *this << std::setprecision(FLT_DECIMAL_DIG); }
};
IOutputStream& OStringStream::print(const char _c)
{
str.append(1, _c);
......@@ -52,21 +72,36 @@ IOutputStream& OStringStream::print(const char _c)
IOutputStream& OStringStream::print(const int _i)
{
str.append(std::to_string(_i));
CLocaleOStringStream strm;
strm << _i;
str.append(strm.str());
return *this;
}
IOutputStream& OStringStream::print(const size_t _i)
{
str.append(std::to_string(_i));
CLocaleOStringStream strm;
strm << _i;
str.append(strm.str());
return *this;
}
IOutputStream& OStringStream::print(const float _f)
{
CLocaleOStringStream strm;
strm.set_precision_float();
strm << _f;
str.append(strm.str());
return *this;
}
IOutputStream& OStringStream::print(const double _d)
{
char buf[128];
sprintf_s(buf, "%.17g", _d);
return OStringStream::print(buf);
CLocaleOStringStream strm;
strm.set_precision_double();
strm << _d;
str.append(strm.str());
return *this;
}
IOutputStream& OStringStream::print(const char* const _str)
......@@ -77,21 +112,17 @@ IOutputStream& OStringStream::print(const char* const _str)
IOutputStream& OStringStream::print(const Command& _cmd)
{
// NOTE: Does nothing, strings should support
if ((_cmd.cmd & Command::END_LF) == (int)Command::END_LF)
return OStringStream::print('\n');
return OStringStream::print('\n');
return *this;
}
//template <const char _frmt, const int _bffr_size = 128>
//IOutputStream& format(IOutputStream& _os, const double _d)
// template <const char _frmt, const int _bffr_size = 128>
// IOutputStream& format(IOutputStream& _os, const double _d)
//{
// char bffr[_bffr_size];
// sprintf_s(bffr, _frmt, _d);
// return std::string(bffr);
//}
}//namespace Base
} // namespace Base
......@@ -10,12 +10,15 @@ namespace Base {
class BASEDLLEXPORT OStringStream : public IOutputStream
{
public:
virtual IOutputStream& print(const char);
virtual IOutputStream& print(const int);
virtual IOutputStream& print(const size_t);
virtual IOutputStream& print(const double);
virtual IOutputStream& print(const char* const);
virtual IOutputStream& print(const Command&);
IOutputStream& print(const char) override;
IOutputStream& print(const int) override;
IOutputStream& print(const size_t) override;
IOutputStream& print(const float) override;
IOutputStream& print(const double) override;
IOutputStream& print(const char* const) override;
IOutputStream& print(const Command&) override;
void clear() { str.clear(); }
std::string str;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment