Skip to content

Commit

Permalink
HPCC-31774 Record log message source
Browse files Browse the repository at this point in the history
Signed-off-by: Shamser Ahmed <[email protected]>
  • Loading branch information
shamser committed May 15, 2024
1 parent 72cb3d3 commit 37dbc20
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
40 changes: 26 additions & 14 deletions common/sysinfologger/sysinfologger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#define ATTR_CLASS "@class"
#define ATTR_CODE "@code"
#define ATTR_HIDDEN "@hidden"
#define ATTR_SOURCE "@source"


static unsigned readDigits(char const * & p, unsigned numDigits)
Expand Down Expand Up @@ -86,6 +87,10 @@ class CSysInfoLoggerMsg : implements ISysInfoLoggerMsg
{
return msgPtree->getPropInt(ATTR_CODE);
}
const char * querySource() const override
{
return msgPtree->queryProp(ATTR_SOURCE);
}
const char * queryMsg() const override
{
return msgPtree->queryProp(".");
Expand All @@ -94,20 +99,21 @@ class CSysInfoLoggerMsg : implements ISysInfoLoggerMsg
{
return msgPtree->getPropBool(ATTR_HIDDEN);
}
static IPropertyTree * createMsgPTree(const LogMsgCategory & cat, LogMsgCode code, const char * msg, unsigned __int64 ts, bool hidden)
static IPropertyTree * createMsgPTree(const LogMsgCategory & cat, LogMsgCode code, const char * source, const char * msg, unsigned __int64 ts, bool hidden)
{
Owned<IPropertyTree> msgPtree = createPTree(MSG_NODE);
msgPtree->setPropBool(ATTR_HIDDEN, false);
msgPtree->setPropInt64(ATTR_TIMESTAMP, ts);
msgPtree->setPropInt(ATTR_CODE, code);
msgPtree->setProp(ATTR_AUDIENCE, LogMsgAudienceToFixString(cat.queryAudience()));
msgPtree->setProp(ATTR_CLASS, LogMsgClassToFixString(cat.queryClass()));
msgPtree->setProp(ATTR_SOURCE, source);
msgPtree->setProp(".", msg);
return msgPtree.getClear();
}
static StringBuffer &buildMsgMatchXpath(const LogMsgAudience aud, const LogMsgClass logClass, LogMsgCode code, unsigned __int64 ts, StringBuffer & xpath)
static StringBuffer &buildMsgMatchXpath(const LogMsgAudience aud, const LogMsgClass logClass, LogMsgCode code, const char * source, unsigned __int64 ts, StringBuffer & xpath)
{
return xpath.appendf(MSG_NODE "[" ATTR_TIMESTAMP "='%" I64F "u'][" ATTR_CODE "='%d'][" ATTR_AUDIENCE "='%s'][" ATTR_CLASS "='%s']", ts, (int) code, LogMsgAudienceToFixString(aud), LogMsgClassToFixString(logClass) );
return xpath.appendf(MSG_NODE"[" ATTR_TIMESTAMP "='%" I64F "u'][" ATTR_SOURCE "='%s'][" ATTR_CODE "='%d'][" ATTR_AUDIENCE "='%s'][" ATTR_CLASS "='%s']", ts, source, (int) code, LogMsgAudienceToFixString(aud), LogMsgClassToFixString(logClass) );
}
};

Expand Down Expand Up @@ -197,7 +203,7 @@ ISysInfoLoggerMsgIterator * createSysInfoLoggerMsgIterator(bool visibleOnly, boo
return new CSysInfoLoggerMsgIterator(hiddenOnly, visibleOnly, year, month, day);
}

void logSysInfoError(const LogMsgCategory & cat, LogMsgCode code, const char * msg, unsigned __int64 ts)
void logSysInfoError(const LogMsgCategory & cat, LogMsgCode code, const char *source, const char * msg, unsigned __int64 ts)
{
if (ts==0)
ts = getTimeStampNowValue();
Expand All @@ -211,15 +217,15 @@ void logSysInfoError(const LogMsgCategory & cat, LogMsgCode code, const char * m
if (!root->hasProp("@version"))
root->addProp("@version", SYS_INFO_VERSION);

root->addPropTree(MSG_NODE, CSysInfoLoggerMsg::createMsgPTree(cat, code, msg, ts, false));
root->addPropTree(MSG_NODE, CSysInfoLoggerMsg::createMsgPTree(cat, code, source, msg, ts, false));
conn->close();
};

bool hideLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, unsigned __int64 ts)
bool hideLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, const char *source, unsigned __int64 ts)
{
StringBuffer xpath;
getRootPath(ts, xpath).append("/");
CSysInfoLoggerMsg::buildMsgMatchXpath(cat.queryAudience(), cat.queryClass(), code, ts, xpath);
CSysInfoLoggerMsg::buildMsgMatchXpath(cat.queryAudience(), cat.queryClass(), code, source, ts, xpath);
xpath.append("[1]"); // Match first message only

Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_READ | RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
Expand All @@ -229,7 +235,7 @@ bool hideLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, unsigned __int64 t
return true;
}

bool deleteLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, unsigned __int64 ts)
bool deleteLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, const char *source, unsigned __int64 ts)
{
StringBuffer xpath;
getRootPath(ts, xpath);
Expand All @@ -239,7 +245,7 @@ bool deleteLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, unsigned __int64
throw makeStringExceptionV(-1, "logSysInfoError: unable to create connection to '%s'", xpath.str());

IPropertyTree * root = conn->queryRoot();
CSysInfoLoggerMsg::buildMsgMatchXpath(cat.queryAudience(), cat.queryClass(), code, ts, xpath.clear());
CSysInfoLoggerMsg::buildMsgMatchXpath(cat.queryAudience(), cat.queryClass(), code, source, ts, xpath.clear());
xpath.append("[1]"); // Match first message only
IPropertyTree * msgPtree = root->queryPropTree(xpath.str());
if (msgPtree && root)
Expand Down Expand Up @@ -324,6 +330,8 @@ unsigned deleteOlderThanLogSysInfoMsg(bool visibleOnly, bool hiddenOnly, unsigne
#ifdef _USE_CPPUNIT
#include "unittests.hpp"

#define SOURCE_CPPUNIT "cppunit"

std::atomic_bool initialized {false};
CriticalSection crit;

Expand Down Expand Up @@ -454,13 +462,17 @@ class CSysInfoLoggerTester : public CppUnit::TestFixture
{
const ISysInfoLoggerMsg & sysInfoMsg = iter->query();

//WrittenLogMessage wm{sysInfoMsg.queryTimeStamp(), 0};
if (strcmp(sysInfoMsg.querySource(), SOURCE_CPPUNIT)!=0)
continue; // not a message written by this unittest so ignore

// Lookup messages in writtenMessages using timestamp
unsigned __int64 msgTs = sysInfoMsg.queryTimeStamp();
auto matched = std::find_if(writtenMessages.begin(), writtenMessages.end(), [msgTs] (const auto & wm){ return (wm.ts == msgTs); });
if (matched==writtenMessages.end())
continue; // not a message written by this unittest so ignore
matchedMessages.insert(matched->testCaseIndex);
throw makeStringExceptionV(-1, "Message read doesn't match a message written by unittest (ts=%" I64F "u)", msgTs);

// Make sure written messages matches message read back
matchedMessages.insert(matched->testCaseIndex);
TestCase & testCase = testCases[matched->testCaseIndex];
ASSERT(testCase.hidden==sysInfoMsg.queryIsHidden());
ASSERT(testCase.code==sysInfoMsg.queryLogMsgCode());
Expand Down Expand Up @@ -514,10 +526,10 @@ class CSysInfoLoggerTester : public CppUnit::TestFixture
dateTime.setString(testCase.dateTimeStamp);

unsigned __int64 ts = dateTime.getTimeStamp();
logSysInfoError(testCase.cat, testCase.code, testCase.msg, ts);
logSysInfoError(testCase.cat, testCase.code, SOURCE_CPPUNIT, testCase.msg, ts);
writtenMessages.push_back({ts, testCaseIndex++});
if (testCase.hidden)
ASSERT(hideLogSysInfoMsg(testCase.cat, testCase.code, ts)==true);
ASSERT(hideLogSysInfoMsg(testCase.cat, testCase.code, SOURCE_CPPUNIT, ts)==true);
}
catch (IException *e)
{
Expand Down
7 changes: 4 additions & 3 deletions common/sysinfologger/sysinfologger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface ISysInfoLoggerMsg
virtual LogMsgAudience queryAudience() const = 0;
virtual LogMsgClass queryClass() const = 0;
virtual LogMsgCode queryLogMsgCode() const = 0;
virtual const char * querySource() const = 0;
virtual const char * queryMsg() const = 0;
virtual bool queryIsHidden() const = 0;
};
Expand All @@ -43,9 +44,9 @@ interface ISysInfoLoggerMsgIterator : implements IScmIterator
};

SYSINFO_API ISysInfoLoggerMsgIterator * createSysInfoLoggerMsgIterator(bool visibleOnly=true, bool hiddenOnly=false, unsigned year=0, unsigned month=0, unsigned day=0);
SYSINFO_API void logSysInfoError(const LogMsgCategory & cat, LogMsgCode code, const char * msg, unsigned __int64 ts=0);
SYSINFO_API bool hideLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, unsigned __int64 ts);
SYSINFO_API bool deleteLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, unsigned __int64 ts);
SYSINFO_API void logSysInfoError(const LogMsgCategory & cat, LogMsgCode code, const char *source, const char * msg, unsigned __int64 ts);
SYSINFO_API bool hideLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, const char *source, unsigned __int64 ts);
SYSINFO_API bool deleteLogSysInfoMsg(LogMsgCategory & cat, LogMsgCode code, const char *source, unsigned __int64 ts);
SYSINFO_API unsigned deleteOlderThanLogSysInfoMsg(bool visibleOnly=true, bool hiddenOnly=false, unsigned year=0, unsigned month=0, unsigned day=0);

#endif

0 comments on commit 37dbc20

Please sign in to comment.