Browse Source

Z991239-385 #comment 异常类的暂时添加

gifur 5 years ago
parent
commit
17c46010a3
4 changed files with 135 additions and 12 deletions
  1. 67 0
      Common/SpCatch.h
  2. 44 0
      spbase/SpCatch.cpp
  3. 11 6
      spbase/SpEntity.cpp
  4. 13 6
      spbase/SpFSM.cpp

+ 67 - 0
Common/SpCatch.h

@@ -0,0 +1,67 @@
+#ifndef _RVC_CATCH_H__
+#define _RVC_CATCH_H__
+
+#pragma once
+
+#include <exception>
+#include <cstring>
+#include <iostream>
+
+/** we should integrate it with SpBase.h at the future*/
+
+#define SPCATCH_INTERNAL_LINEINFO ::SP::Catch::SourceLineInfo( __FILE__, static_cast<std::size_t>( __LINE__ ) )
+
+#define SPCATCH_NOT_IMPLEMENTED throw ::SP::Catch::NotImplementedException( SPCATCH_INTERNAL_LINEINFO )
+
+namespace SP {
+
+	namespace Catch {
+
+		struct SourceLineInfo {
+
+			SourceLineInfo();
+			SourceLineInfo(char const* file, std::size_t line);
+			
+			SourceLineInfo(SourceLineInfo const& rhs) = default;
+			SourceLineInfo(SourceLineInfo&& rhs) = default;
+			SourceLineInfo& operator = (SourceLineInfo const& rhs) = default;
+			SourceLineInfo& operator =(SourceLineInfo && rhs) = default;
+
+			bool operator == (SourceLineInfo const& rhs) const;
+			bool operator < (SourceLineInfo const& rhs) const;
+
+			char const* file;
+			std::size_t line;
+
+		};
+
+		std::ostream& operator << (std::ostream& os, SourceLineInfo const& info) {
+#ifndef __GNUG__
+			os << info.file << '(' << info.line << ')';
+#else
+			os << info.file << ':' << info.line;
+#endif
+			return os;
+		}
+
+
+		class NotImplementedException : public std::exception
+		{
+		public:
+			NotImplementedException(SourceLineInfo const& lineInfo);
+
+			virtual ~NotImplementedException() noexcept {}
+
+			virtual const char* what() const noexcept;
+
+		private:
+			std::string m_what;
+			SourceLineInfo m_lineInfo;
+		};
+
+	}
+
+
+}
+
+#endif /** _RVC_CATCH_H__*/

+ 44 - 0
spbase/SpCatch.cpp

@@ -0,0 +1,44 @@
+#include "SpCatch.h"
+#include <sstream>
+
+namespace SP {
+
+	namespace Catch {
+
+		SourceLineInfo::SourceLineInfo()
+			:file(""), line(0)
+		{
+		}
+
+		SourceLineInfo::SourceLineInfo(char const* file, std::size_t line)
+			:file(file),line(line)
+		{
+
+		}
+
+		bool SourceLineInfo::operator<(SourceLineInfo const& rhs) const
+		{
+			return line < rhs.line || (line == rhs.line && (std::strcmp(file, rhs.file) < 0));
+		}
+
+		bool SourceLineInfo::operator==(SourceLineInfo const& rhs) const
+		{
+			return line == rhs.line && (file == rhs.file || std::strcmp(file, rhs.file) == 0);
+		}
+
+		NotImplementedException::NotImplementedException(SourceLineInfo const& lineInfo)
+			: m_lineInfo(lineInfo)
+		{
+			std::ostringstream oss;
+			oss << lineInfo << ": function ";
+			oss << "not implemented";
+			m_what = oss.str();
+		}
+
+		const char* NotImplementedException::what() const noexcept
+		{
+			return m_what.c_str();
+		}
+
+	}
+}

+ 11 - 6
spbase/SpEntity.cpp

@@ -143,9 +143,10 @@ static void on_log(void *inst,
 				arr, ent->cfg->name, ent->mod->cfg->name, msg);
 		}
 	}
-	catch (const std::exception&)
+	catch (const std::exception& ex)
 	{
-
+		Dbg("Exception occurs: %s at <%s>[%d]{%s}", ex.what(),  __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
 	}
 	
 }
@@ -164,7 +165,10 @@ static void task_callback(threadpool_t *threadpool, void *arg, param_size_t para
 		pTask->Process();
 		pTask->DecRef();
 	}
-	catch (...) {
+	catch (...) 
+	{
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
 	}
 #else
 	GetSpModule()->SetThreadEntity(pEntity);
@@ -348,6 +352,7 @@ void SpEntity::on_entity_prestart(int trigger_entity_id, int argc, char *argv[])
 	pTransactionContext.Attach(new SpMUITransactionContext(this, SpMUITransactionContext::OP_START));
 	for (int i = 0; i < argc; ++i)
 		Args[i] = argv[i];
+	sp_dbg_debug("module version: %s", m_pEntityBase->GetEntityVersion());
 	m_pEntityBase->OnPreStart(Args, pTransactionContext);
 }
 
@@ -395,10 +400,8 @@ void SpEntity::on_entity_redirect_subscribe(sp_uid_t *uid, int from_entity_id, c
 
 void SpEntity::__on_entity_prestart(sp_mod_entity_stub_t *, int trigger_entity_id, int argc, char *argv[], void *user_data)
 {
-	sp_dbg_debug("==> %s", __FUNCTION__);
 	SpEntity *pThis = static_cast<SpEntity*>(user_data);
 	pThis->on_entity_prestart(trigger_entity_id, argc, argv);
-	sp_dbg_debug("<== %s", __FUNCTION__);
 }
 
 void SpEntity::__on_entity_stop(sp_mod_entity_stub_t *, int trigger_entity_id, int cause_code, void *user_data)
@@ -460,7 +463,9 @@ int SpEntity::on_accept(int epid, int svc_id, int conn_id, iobuffer_t **conn_pkt
 	try {
 		m_redirect_entity_cache = NULL; // user can set entity_cache value in OnNewSession by invoking RedirectSession method
 		pServerSessionBase = m_pEntityBase->OnNewSession(remote_ent->cfg->name, pszParam);
-	} catch (...) {
+	} catch (...) 
+	{
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
 		Error = Error_Exception;
 	}
 	FREE(pszParam);

+ 13 - 6
spbase/SpFSM.cpp

@@ -34,7 +34,8 @@ static void __fsm_cb(threadpool_t *threadpool, void *arg, param_size_t param1, p
 	}
 	catch (...)
 	{
-		
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
 	}
 	
 }
@@ -61,7 +62,8 @@ static void __tmr_cb(sp_tmr_t *timer, int err, void *user_data)
 	}
 	catch (...)
 	{
-
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
 	}
 	
 }
@@ -135,7 +137,8 @@ void FSMBase::PostEventLIFO(FSMEvent *e)
 	}
 	catch (...)
 	{
-
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
 	}
 
 	
@@ -151,7 +154,8 @@ void FSMBase::PostEventFIFO(FSMEvent *e)
 	}
 	catch (...)
 	{
-
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
 	}
 
 	
@@ -170,7 +174,8 @@ void FSMBase::Trans( int next )
 	}
 	catch (...)
 	{
-
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
 	}
 }
 
@@ -307,7 +312,9 @@ ErrorCodeEnum FSMBase::ScheduleTimer(int iTimerId, unsigned int timeout)
 	}
 	catch (...)
 	{
-		return Error_Assert;
+		Dbg("Exception occurs at <%s>[%d]{%s}", __FUNCTION__, __LINE__, _GetFileName(__FILE__));
+		//std::terminate();
+		return Error_Exception;
 	}
 	
 }