File size: 5,109 Bytes
24b81cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
class DebugPrint
{
static private const int MSG_LOG = 0;
static private const int MSG_WARNING = 1;
static private const int MSG_ERROR = 2;
static private const int MSG_COUNT = 3;
static private string s_MsgPrefix[MSG_COUNT];
static private string s_MsgStackMarkStart;
static private string s_MsgStackMarkEnd;
static private bool s_MsgStackMarked;
static private bool s_TraceAllLogs;
static void OnInit()
{
s_MsgPrefix[MSG_LOG] = "Log";
s_MsgPrefix[MSG_WARNING] = "Warning";
s_MsgPrefix[MSG_ERROR] = "Error";
s_MsgStackMarkStart = "-- Stack trace --";
s_MsgStackMarked = false;
s_MsgStackMarkEnd = "-----------------";
s_TraceAllLogs = false;
}
/**
\brief Prints debug message with normal prio
\param msg \p string Debug message for print
\return \p void None
@code
DebugPrint.Log("Hello World");
>> [Log]: Hello World;
@endcode
*/
static void Log(string msg)
{
LogMessage(msg, MSG_LOG, s_TraceAllLogs);
}
/**
\brief Prints debug message as normal message and prints stack trace of calls
\param msg \p string Debug message for print
\return \p void None
@code
DebugPrint.LogAndTrace("Hello World, this is normal log");
>> [Log]: Hello World, this is normal log
>> -- Stack trace --
>> OnKeyPress() Scripts/mission/missionGameplay.c : 230
>> OnKeyPress() Scripts/DayZGame.c : 346
>> -----------------
@endcode
*/
static void LogAndTrace(string msg)
{
LogMessage(msg, MSG_LOG, true);
}
/**
\brief Prints debug message as warning message
\param msg \p string Debug message for warning print
\return \p void None
@code
DebugPrint.LogWarning("Hello World, this is warning log");
>> [Warning]: Hello World, this is warning log
@endcode
*/
static void LogWarning(string msg)
{
LogMessage(msg, MSG_WARNING, s_TraceAllLogs);
}
/**
\brief Prints debug message as warning message and prints stack trace of calls
\param msg \p string Debug message for warning print
\return \p void None
@code
DebugPrint.LogWarningAndTrace("Hello World, this is warning log");
>> [Warning]: Hello World, this is warning log
>> -- Stack trace --
>> OnKeyPress() Scripts/mission/missionGameplay.c : 230
>> OnKeyPress() Scripts/DayZGame.c : 346
>> -----------------
@endcode
*/
static void LogWarningAndTrace(string msg)
{
LogMessage(msg, MSG_WARNING, true);
}
/**
\brief Prints debug message as error message
\param msg \p string Debug message for error print
\return \p void None
@code
DebugPrint.LogError("Hello World, this is error log");
>> [Error]: Hello World, this is error log
@endcode
*/
static void LogError(string msg)
{
LogMessage(msg, MSG_ERROR, s_TraceAllLogs);
}
/**
\brief Prints debug message as error message and prints stack trace of calls
\param msg \p string Debug message for error print
\return \p void None
@code
DebugPrint.LogErrorAndTrace("Hello World, this is error log");
>> [Error]: Hello World, this is error log
>> -- Stack trace --
>> OnKeyPress() Scripts/mission/missionGameplay.c : 230
>> OnKeyPress() Scripts/DayZGame.c : 346
>> -----------------
@endcode
*/
static void LogErrorAndTrace(string msg)
{
LogMessage(msg, MSG_ERROR, true);
}
/**
\brief Function adjust received message for debug console (Do not use)
\param msg \p string Message for adjust
\return \p string Adjusted Message
@code
string msg = DebugPrint.AdjustDebugLog("s = 'Hello World, this is error log'");
DebugPrint.Log(msg);
>> Hello World, this is error log
@endcode
*/
static string AdjustDebugLog(string msg)
{
if ( IsStackTrace(msg) )
{
return TrimStackTrace(msg);
}
if ( IsDebugLog(msg) )
{
return TrimDebugLog(msg);
}
return msg;
}
static void EnableTracingLogs(bool enable)
{
s_TraceAllLogs = enable;
}
static private bool IsDebugLog(string msg)
{
for ( int i = 0; i < MSG_COUNT; ++i )
{
if ( msg.IndexOf(s_MsgPrefix[i]) != -1 )
{
return true;
}
}
return false;
}
static private string TrimDebugLog(string msg)
{
int msg_lenght = msg.Length();
int log_start = msg.IndexOf("'") + 1;
if ( log_start == -1 )
{
return msg;
}
int log_lenght = msg_lenght - log_start - 2;
return msg.Substring(log_start, log_lenght);
}
static private bool IsStackTrace(string msg)
{
if ( s_MsgStackMarked && msg.IndexOf(s_MsgStackMarkEnd) != -1 )
{
s_MsgStackMarked = false;
return false;
}
if ( s_MsgStackMarked )
{
return true;
}
if ( msg.IndexOf(s_MsgStackMarkStart) != -1 )
{
s_MsgStackMarked = true;
return true;
}
return false;
}
static private string TrimStackTrace(string msg)
{
if ( msg.IndexOf("DebugPrint.c") != -1 )
{
return string.Empty;
}
return msg;
}
static private void LogMessage(string msg, int msg_type, bool trace=false)
{
string mesg = "["+s_MsgPrefix[msg_type]+"]: "+msg;
Print(mesg);
if ( trace )
{
DumpStack();
}
}
};
|