123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpTest.h"
- #include "modVer.h"
- #include "SendLogDefine.h"
- #define SYSVAL_CHANGED_ENTITY_NAME "SampleEntity"
- #define SYSVAL_CHANGED_KEY_NAME "SampleState"
- typedef ULONG LogTypeSeverity[Log_Debug + 1][Severity_High + 1];
- class CLogSubscribeEntity : public CEntityBase
- , public ITimerListener
- , public ISysVarListener
- , public IBroadcastListener
- , public ILogListener
- , public ITerminalStateChangedListener
- {
- public:
- CLogSubscribeEntity():m_logCalCount(NULL), m_bSysValUpdated(false)
- , m_bTerminalStateChangedCheck(false)
- {
- m_logCalCount = new LogTypeSeverity[0x1000];
- if (m_logCalCount) {
- memset(m_logCalCount, 0, sizeof(LogTypeSeverity) * (0x1000));
- }
- }
- virtual ~CLogSubscribeEntity() {
- if (m_logCalCount)
- delete[] m_logCalCount;
- }
- virtual const char *GetEntityName() const { return "TestSubscribe"; }
- const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
- /*for {ITimerListener} implement*/
- void OnTimeout(DWORD dwTimerID)
- {
- bool isCorrected = true;
- LOG_TRACE("timeout, timer id: %d", dwTimerID);
- if (m_logCalCount) {
- //LogAssert: [Log_Debug][Severity_Middle]
- //LogTrace: [Log_Debug][Severity_None]
- //LogEvent: [Log_Event][]
- //LogWarn: [Log_Warning][]
- //LogError: [Log_Error][]
- for (SeverityLevelEnum severity = Severity_None; severity <= Severity_High && isCorrected; severity = (SeverityLevelEnum)(severity + 1)) {
- for (LogTypeEnum logType = Log_Ignore; logType <= Log_Debug && isCorrected; logType = (LogTypeEnum)(logType + 1)) {
- if (logType == Log_Ignore) { /*SendLog api*/
- if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 1) { isCorrected = false; break; }
- }else if (logType == Log_Debug) {
- if (severity == Severity_None) { /*LOG_TRACE api & SendLog api*/
- if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 2) { isCorrected = false; break; }
- }
- else {/*SendLog api*/
- if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 1) { isCorrected = false; break; }
- }
- } else { /*LogXXX & SendLog*/
- if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 2) { isCorrected = false; break; }
- }
- }
- }
- }
- if (!isCorrected) {
- if (m_logCalCount) {
- for (SeverityLevelEnum severity = Severity_None; severity <= Severity_High; severity = (SeverityLevelEnum)(severity + 1)) {
- for (LogTypeEnum logType = Log_Ignore; logType <= Log_Debug; logType = (LogTypeEnum)(logType + 1)) {
- Dbg("logType:%d, severity: %d: times: %u", logType, severity, m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity]);
- }
- }
- }
- THROW_FATAL("The received log entries is not correct!");
- GetFunction()->PostQuit();
- }
- if (!m_bSysValUpdated) {
- THROW_ERROR("Test SysValue Updated Failed!, entity: %s, key: %s", SYSVAL_CHANGED_ENTITY_NAME, SYSVAL_CHANGED_KEY_NAME);
- }
- if (!m_bTerminalStateChangedCheck) {
- THROW_ERROR("Test terminal state changed Failed!");
- }
- GetFunction()->KillTimer(1);
- }
- /*for {ISysVarListener} implement */
- virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- LOG_TRACE(">>>OnSysVarEvent: entity:%s key:%s value changed from %s to %s", pszEntityName, pszKey, pszOldValue, pszValue);
- if (CSimpleStringA(pszEntityName).Compare(SYSVAL_CHANGED_ENTITY_NAME) == 0) {
- if (CSimpleStringA(pszKey).Compare(SYSVAL_CHANGED_KEY_NAME) == 0) {
- m_bSysValUpdated = true;
- }
- }
- }
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
- {
- LOG_FUNCTION();
- pTransactionContext->SendAnswer(__onTest());
- }
- virtual void OnStarted()
- {
- LOG_FUNCTION();
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- LOG_FUNCTION();
- Dbg("to unsubscribe all entity log.");
- auto rc = GetFunction()->UnsubscribeLog(m_logSubUUID);
- if (IS_FAILURED(rc)) {
- Dbg("unsubscribe all entity log failed: %s", SpStrError(rc));
- }
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- void OnBroadcastEvent(CUUID SubID,
- const char* pszEntityName,
- DWORD dwMessageId,
- DWORD dwMessageSignature,
- CAutoBuffer Buffer)
- {
- Dbg("OnBroadcastEvent: %s, %s, 0x%X, 0x%X, buffer size: %u", (LPCTSTR)SubID.ToString(), pszEntityName,
- dwMessageId, dwMessageSignature, Buffer.GetCount());
- }
- void OnLog(const CAutoArray<CUUID>& SubIDs
- , const CUUID nLogID
- , const LogTypeEnum eLogType
- , const SeverityLevelEnum eLevel
- , const DWORD dwSysError, const DWORD dwUserCode
- , const DWORD dwEntityInstanceID, const WORD wEntityDevelID
- , const CAutoArray<DWORD>& Param
- , const char* pszEntityName, const char* pszModuleName, const char* pszMessage)
- {
- if (m_logCalCount) {
- m_logCalCount[wEntityDevelID][eLogType][eLevel]++;
- }
- bool bDisplay = true;
- do
- {
- if (wEntityDevelID == SAMPLE_ENTITY_DEV_ID) {
-
- if (CSimpleStringA(SENDLOG_String_Context).Compare(pszMessage)) {
- THROW_ERROR("Log from testLogSender is not correct!");
- break;
- }
- if (Param.GetCount() > 0) {
- if (Param.GetCount() != 3) {
- THROW_ERROR("Log from testLogSender param count is not correct: %d", Param.GetCount());
- break;
- }
- if (Param[0] != eLevel) {
- THROW_ERROR("Log from testLogSender param[0] is not correct: %d", Param[0]);
- break;
- }
- if (Param[1] != eLogType) {
- THROW_ERROR("Log from testLogSender param[1] is not correct: %d", Param[1]);
- break;
- }
- if (Param[2] != dwUserCode) {
- THROW_ERROR("Log from testLogSender param[2] is not correct: %d", Param[2]);
- break;
- }
- }
- }
- } while (false);
- if (bDisplay) {
- Dbg("OnLog(%s, %d, %d, 0x%X, 0x%X, 0x%X, 0x%04X, %s, %s, %s",
- (LPCTSTR)nLogID.ToString(), eLogType, eLevel, dwSysError, dwUserCode,
- dwEntityInstanceID, wEntityDevelID, pszEntityName, pszModuleName, pszMessage);
- }
- }
- void OnStateChanged(FrameworkStateEnum oldState, FrameworkStateEnum curState, const char* triggerEntity)
- {
- Dbg("OnStateChanged: from %s to %s trigged by %s",
- SpStrFrameworkState(oldState), SpStrFrameworkState(curState), triggerEntity);
- m_bTerminalStateChangedCheck = true;
- }
- private:
- CUUID m_logSubUUID;
- LogTypeSeverity* m_logCalCount;
- BOOL m_bSysValUpdated;
- BOOL m_bTerminalStateChangedCheck;
- ErrorCodeEnum __onTest()
- {
- //Sleep(30000);
- Dbg("to subscribe all entity log.");
- IFFAILRET(GetFunction()->SubscribeLog(m_logSubUUID, this, Log_Ignore, Severity_None, Error_IgnoreAll, -2, NULL, false));
-
- REQUIRE(Error_Param == GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, NULL));
- REQUIRE(Error_Param == GetFunction()->RegistSysVarEvent(NULL, this));
- REQUIRE(Error_NotExist == GetFunction()->RegistSysVarEvent("NotExistSysKey", this));
- IFFAILRET(GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, this));
- REQUIRE(Error_Duplication == GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, this));
- IFFAILRET(GetFunction()->UnregistSysVarEvent(SYSVAL_CHANGED_KEY_NAME));
- REQUIRE(Error_NotInit == GetFunction()->UnregistSysVarEvent(SYSVAL_CHANGED_KEY_NAME));
- REQUIRE(Error_NotExist == GetFunction()->UnregistSysVarEvent("NotExistSysKey"));
- //subscribe
- Dbg("to listeren specified sys key: %s", SYSVAL_CHANGED_KEY_NAME);
- IFFAILRET(GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, this));
- Dbg("to listeren terminal state");
- IFFAILRET(GetFunction()->RegistTerminalStateChangeEvent(this));
- Dbg("to unregist listeren terminal state");
- IFFAILRET(GetFunction()->UnregistTerminalStateChangeEvent());
- Dbg("regist terminal state again.");
- IFFAILRET(GetFunction()->RegistTerminalStateChangeEvent(this));
- #if 1
- //after TestLogSender started, it schedules a timer fired 10s and send some log. This entity would catch it
- //so here set a timer to confirm its validity, the interval must great than 10s.
- GetFunction()->SetTimer(1, this, SENDLOG_LOG_SEND_INTERVAL + 2000);
- #endif
- return Error_Succeed;
- }
- };
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CLogSubscribeEntity)
- SP_END_ENTITY_MAP()
|