Commit 433c0a1d authored by Martin Marinov's avatar Martin Marinov

Add Journal::environment_base_path() using refactored code from compose_base_path.

Ensure Journal stream is always flushed when `\n` is printed
parent d67726dd
......@@ -16,12 +16,9 @@ namespace COMPONENT
namespace Journal
{
// Also define this as the component name
// Define this as the component name string
#define COMPONENT_NAME_STRING
// Also define this as the component name in UPPER case
#define COMPONENT_NAME_STRING_UPPER
#include <Base/Journal/JournalConfig.inc.cc>
} // namespace Journal
......@@ -45,10 +42,6 @@ namespace Journal
#error Please define the component name string when including this
#endif//COMPONENT_NAME_STRING
#ifndef COMPONENT_NAME_STRING_UPPER
#error Please define the component name string in upper case when including this
#endif//COMPONENT_NAME_STRING_UPPER
std::string& base_path_string()
{// make sure this constructor is called when the base_path_str is accessed
static std::string base_path_str = ".";
......@@ -71,10 +64,10 @@ struct Setup
{
Setup()
{
const char* const vrbl_name = COMPONENT_NAME_STRING_UPPER"_JOURNAL_BASE";
if (System::Environment::variable(vrbl_name, base_path_string()))
{
set_on(true); // ... turn on Journal by default
if (::Journal::environment_base_path(
COMPONENT_NAME_STRING, base_path_string()))
{ // ... turn on Journal by default
set_on(true);
}
}
};
......@@ -86,7 +79,7 @@ void set_on(const bool _on)
if (on() == _on)
return;
::Journal::set_on(_on, "ReForm", base_path());
::Journal::set_on(_on, COMPONENT_NAME_STRING, base_path());
if (!on())
return;
......
......@@ -40,34 +40,38 @@ template <class T> String to_string(const T& _v)
return os.str;
}
static String compose_base_path(const char* const _cmpn_name)
bool environment_base_path(const char* const _cmpn_name, String& _base_path)
{
String envr_vrbl(_cmpn_name);
const auto to_upper = [](const char _c)
{
return static_cast<char>(::toupper(_c));
const auto to_upper = [](const char _c)
{
return static_cast<char>(::toupper(_c));
};
std::transform(
envr_vrbl.begin(), envr_vrbl.end(), envr_vrbl.begin(), to_upper);
envr_vrbl += "_JOURNAL_BASE";
std::string base_fldr;
if (System::Environment::variable(envr_vrbl.c_str(), base_fldr))
return base_fldr; // environment variable defined, we are done
return System::Environment::variable(envr_vrbl.c_str(), _base_path);
}
static String compose_base_path(const char* const _cmpn_name)
{
String base_path;
if (environment_base_path(_cmpn_name, base_path))
return base_path; // environment variable defined, we are done
#ifdef _MSC_VER
// Get the Windows Local App Data folder
if (!System::Environment::variable("LOCALAPPDATA", base_fldr)) // undefined?
return base_fldr; // exit here
base_fldr += "\\";
if (!System::Environment::variable("LOCALAPPDATA", base_path)) // undefined?
return base_path; // exit here
base_path += "\\";
#else // Unix-like systems
if (!System::Environment::variable("HOME", base_fldr)) // undefined?
return base_fldr; // exit here
base_fldr += "/.";
if (!System::Environment::variable("HOME", base_path)) // undefined?
return base_path; // exit here
base_path += "/.";
#endif // _MSC_VER
base_fldr += _cmpn_name;
return base_fldr;
base_path += _cmpn_name;
return base_path;
}
const auto ENDL = Base::ENDL;
......@@ -199,6 +203,8 @@ public:
IOutputStream& print(const char _c) override
{
if (_c == '\n')
return print(ENDL);
strm_ << _c;
return os();
}
......@@ -229,7 +235,8 @@ public:
IOutputStream& print(const char* const _str) override
{
strm_ << _str;
for (const auto* c = _str; *c != '\0'; ++c)
print(*c);
return os();
}
......
......@@ -241,6 +241,12 @@ typedef std::unique_ptr<Stream> StreamPtr;
// the global stream pointer is exposed for efficiency only, do *not* use it
extern StreamPtr strm_ptr;
/*!
Obtain the value of the environment variable toupper(cmpn_name)_JOURNAL_BASE
if it is defined or otherwise get false and leave base_path unchanged.
*/
bool environment_base_path(const char* const _cmpn_name, String& _base_path);
/*!
Turn on/off the journal, set the base path optionally. If no base path is
provided, the base path will be composed from the component name as follows:
......
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