Commit 52410e03 authored by EXT Vivien Delmon's avatar EXT Vivien Delmon

Do not output to file/stream if p_stream is null

* This way the user can chose to output in journald only
parent 7809e113
......@@ -72,7 +72,7 @@ public:
void flush() { m_streamPointer->flush(); }
bool startWrite(MTextStream* p_stream, LogOptions p_options = LogOptions());
bool startWrite(MTextStream* p_stream = nullptr, LogOptions p_options = LogOptions());
/// <summary> Logs a single TSV+JSON line depending on locale and options </summary>
//! \return false if writeStart failed
......
......@@ -24,26 +24,27 @@ Log::~Log()
{
}
bool Log::startWrite(MTextStream* p_stream, LogOptions p_options /*= LogOptions()*/)
bool Log::startWrite(MTextStream* p_stream /*= nullptr*/, LogOptions p_options /*= LogOptions()*/)
{
m_options = p_options;
m_streamPointer = p_stream;
// *m_streamPointer.setLocale(p_locale); // TODO for elapsedSecs only, "C" locale for JSON fields
*m_streamPointer
<< "threadId\t"
<< "elapsedSecs\t"
<< (m_options.testFlag(log::Log::HideDateTime) ? "" : "[dateTime]\t")
<< "severity\t"
<< (m_options.testFlag(log::Log::HideCategory) ? "" : "[category]\t")
<< (m_options.testFlag(log::Log::HideFunction) ? "" : "[function]\t")
<< "eventId\t"
<< "[eventCount]\t"
<< (m_options.testFlag(log::Log::HideFileName) ? "" : "[sourcePath]\t")
<< (m_options.testFlag(log::Log::HideLine ) ? "" : "[sourceLine]\t")
<< "message\t"
<< "args...\t"
<< mEndl;
if (m_streamPointer)
*m_streamPointer
<< "threadId\t"
<< "elapsedSecs\t"
<< (m_options.testFlag(log::Log::HideDateTime) ? "" : "[dateTime]\t")
<< "severity\t"
<< (m_options.testFlag(log::Log::HideCategory) ? "" : "[category]\t")
<< (m_options.testFlag(log::Log::HideFunction) ? "" : "[function]\t")
<< "eventId\t"
<< "[eventCount]\t"
<< (m_options.testFlag(log::Log::HideFileName) ? "" : "[sourcePath]\t")
<< (m_options.testFlag(log::Log::HideLine ) ? "" : "[sourceLine]\t")
<< "message\t"
<< "args...\t"
<< mEndl;
return true;
}
......@@ -62,6 +63,28 @@ int Log::severity(MMsgType p_type)
bool Log::write(const EventData& eventData)
{
// MESSAGE
MString wholeMsg = eventData.format();
for (auto it = eventData.data().cbegin(); it != eventData.data().cend(); ++it)
wholeMsg += '\t' + data::JsonString().write().bind(*it).toString();
wholeMsg.replace(MString("\n"), MString(""));
#ifndef M_OS_WIN
// Test sd_journal_send
sd_journal_send("PRIORITY=%i", severity(eventData.type()),
"DATETIME=%s", mPrintable(writeDateTimeLocal.toString(MDateFormat::ISODate)),
"ELAPSED=%f", (double)writeNSecs/1000000000,
"MESSAGE=%s", mPrintable(wholeMsg),
"CODE_FILE=%s", eventData.file(),
"CODE_LINE=%i", eventData.line(),
"CODE_FUNC=%s", eventData.function(),
"CATEGORY=%s", eventData.category(),
"THREAD_ID=%s", mPrintable(eventData.threadId()),
"ID=%s", mPrintable(eventData.id()),
"EVENT_DATA=%s", mPrintable(eventData.eventData()),
NULL);
#endif
if (!m_streamPointer)
return false;
......@@ -185,30 +208,8 @@ bool Log::write(const EventData& eventData)
}
}
// MESSAGE
MString wholeMsg = eventData.format();
for (auto it = eventData.data().cbegin(); it != eventData.data().cend(); ++it)
wholeMsg += '\t' + data::JsonString().write().bind(*it).toString();
wholeMsg.replace(MString("\n"), MString(""));
*m_streamPointer << wholeMsg << mEndl;
#ifndef M_OS_WIN
// Test sd_journal_send
sd_journal_send("PRIORITY=%i", severity(eventData.type()),
"DATETIME=%s", mPrintable(writeDateTimeLocal.toString(MDateFormat::ISODate)),
"ELAPSED=%f", (double)writeNSecs/1000000000,
"MESSAGE=%s", mPrintable(wholeMsg),
"CODE_FILE=%s", eventData.file(),
"CODE_LINE=%i", eventData.line(),
"CODE_FUNC=%s", eventData.function(),
"CATEGORY=%s", eventData.category(),
"THREAD_ID=%s", mPrintable(eventData.threadId()),
"ID=%s", mPrintable(eventData.id()),
"EVENT_DATA=%s", mPrintable(eventData.eventData()),
NULL);
#endif
return true;
}
......
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