| //+------------------------------------------------------------------+
|
| //| ErrManage.mqh |
|
| //| Copyright 2026, Niquel Mendoza. |
|
| //| https://www.mql5.com |
|
| //+------------------------------------------------------------------+
|
| #property copyright "Copyright 2026, Niquel Mendoza."
|
| #property link "https://www.mql5.com"
|
| #property strict
|
|
|
| #ifndef TSNSTD_SRC_BASICS_ERRMANAGER_MQH
|
| #define TSNSTD_SRC_BASICS_ERRMANAGER_MQH
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| namespace TSN
|
| {
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| #ifndef TSNSTD_TRACE_ERR_MAX_STACK
|
| #define TSNSTD_TRACE_ERR_MAX_STACK (16)
|
| #endif // TSNSTD_TRACE_ERR_MAX_STACK
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| #define TSNSTD_ERRTRACE_SEPARATOR "\r\n"
|
|
|
| //---
|
| struct pack(8) TSNErrorFrame
|
| {
|
| ulong info; // Info proporcionada
|
| int mql_err; // Codigo de error MQL5
|
| int code; // Codigo de error
|
| };
|
|
|
| //---
|
| typedef void (*TSNStdFormatTraceF)(const TSNErrorFrame &trace[], int &trace_idx, string &out);
|
|
|
| //---
|
| #define TSNSTD_ERRORTRACE_PUSH_INTERNAL(CODE, INFO, MQLERR) \
|
| s_frames[s_frames_s].mql_err = MQLERR; \
|
| s_frames[s_frames_s].code = CODE; \
|
| s_frames[s_frames_s++].info = INFO;
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| class CErrorTrace
|
| {
|
| public:
|
| CErrorTrace(void) {}
|
| ~CErrorTrace(void) {}
|
|
|
| //---
|
| static TSNErrorFrame s_frames[TSNSTD_TRACE_ERR_MAX_STACK];
|
| static int s_frames_s;
|
| static TSNStdFormatTraceF s_last_formater;
|
|
|
|
|
| //--- Error mas reciente
|
| // Raiz
|
| template <typename TEnum>
|
| static TEnum LastRootErrAs() { return (TEnum)s_frames[0].code; }
|
| // El de la capa mas alta
|
| template <typename TEnum>
|
| static TEnum LastErr() { return (TEnum)s_frames[s_frames_s - 1].code; }
|
| // Total de errores
|
|
|
|
|
| //--- Suma frame
|
| // En las funciones que fallan debido a otras que a su vez originan (estas propagan el error)
|
| template <typename TClass>
|
| static bool FailWrap(const int code, const ulong info, const int mql_err = 0)
|
| {
|
| s_last_formater = TClass::FormatTrace;
|
| TSNSTD_ERRORTRACE_PUSH_INTERNAL(code, info, mql_err)
|
| return false;
|
| }
|
|
|
| // Fallas inciiales
|
| template <typename TClass>
|
| static bool FailInit(const int code, const ulong info, const int mql_err = 0)
|
| {
|
| s_last_formater = TClass::FormatTrace;
|
| s_frames_s = 0; // Iniciamos el frame (con 0)
|
| TSNSTD_ERRORTRACE_PUSH_INTERNAL(code, info, mql_err)
|
| return false;
|
| }
|
|
|
| //--- Helper para formatear desde aqui
|
| static __forceinline void FormatTrace(string& out)
|
| {
|
| out.Reserve((s_frames_s << 7) + 64); // herustica base por cada error estimso 128 caracterres (<< 7 = *128)
|
| out += "Traceback (most recent call last):" + TSNSTD_ERRTRACE_SEPARATOR;
|
| int idx = s_frames_s - 1;
|
| s_last_formater(s_frames, idx, out);
|
| }
|
| };
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| int CErrorTrace::s_frames_s = 0;
|
| TSNErrorFrame CErrorTrace::s_frames[TSNSTD_TRACE_ERR_MAX_STACK];
|
| TSNStdFormatTraceF CErrorTrace::s_last_formater = NULL;
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| //---
|
| #define TSNSTD_ERRMAN_PUSHINI(OUT, TRACE_IDX) OUT+="["+string(TRACE_IDX)+"] "+__FILE__+":"+__FUNCTION__+"\n ";
|
|
|
| //---
|
| #define TSNSTD_ERRMAN_PUSHOUT(OUT, MESSSAGE, TRACE_IDX) \
|
| OUT += MESSSAGE + TSNSTD_ERRTRACE_SEPARATOR; \
|
| TRACE_IDX--;
|
|
|
| //+------------------------------------------------------------------+
|
| }
|
|
|
|
|
| #endif // TSNSTD_SRC_BASICS_ERRMANAGER_MQH
|
| //+------------------------------------------------------------------+
|