11#ifdef GSI_COMMON_DEBUG
14#include "../../common/nitro/screen.h"
16#define vprintf VPrintf
22static struct GSIDebugInstance gGSIDebugInstance;
25char* gGSIDebugCatStrings[GSIDebugCat_Count] =
27 " APP",
" GP ",
"PEER",
" QR2",
" SB",
" V2",
" AD",
" NN",
"HTTP",
"CDKY",
" CMN"
29char* gGSIDebugTypeStrings[GSIDebugType_Count] =
31 " NET",
"FILE",
" MEM",
"STAT",
"MISC"
33char* gGSIDebugLevelStrings[GSIDebugLevel_Count] =
35 "*ERR",
"****",
"----",
" ",
" ",
" ",
" ->"
37char* gGSIDebugLevelDescriptionStrings[8] =
39 "None",
"<None+1>",
"<Normal>",
"<Debug>",
"<Verbose>",
"<Verbose+1>",
"<Verbose+2>",
"<Hardcore>"
45static gsi_u32 gsiDebugLog2(gsi_u32 theInt)
59static void gsiDebugCallback(GSIDebugCategory category, GSIDebugType type,
60 GSIDebugLevel level,
const char * format, va_list params)
64 vprintf(format, params);
68 vprintf(format, params);
72 vprintf(format, params);
75 static char string[256];
76 vsprintf(
string, format, params);
77 OutputDebugStringA(
string);
79 #elif defined(_LINUX) || defined(_MACOSX)
82 vprintf(format, params);
84 VPrintf(format, params);
85 #elif defined(_REVOLUTION)
86 static char string[256];
87 vsprintf(
string, format, params);
91 static char string[256];
92 va_start(argptr, format);
93 vsprintf(
string, format, argptr);
95 gsDebugTTyPrint(
string);
110void gsDebugVaList(GSIDebugCategory theCat, GSIDebugType theType,
111 GSIDebugLevel theLevel,
const char* theTokenStr,
112 va_list theParamList)
115 GSIDebugLevel aCurLevel;
118 assert(theCat <= GSIDebugCat_Count);
119 assert(theType <= GSIDebugType_Count);
120 assert(theLevel <= (1<<GSIDebugLevel_Count));
124 if (gGSIDebugInstance.mInitialized == 0)
130 gGSIDebugInstance.mInitialized = 1;
131 gsiInitializeCriticalSection(&gGSIDebugInstance.mDebugCrit);
134 gsiEnterCriticalSection(&gGSIDebugInstance.mDebugCrit);
137 aCurLevel = gGSIDebugInstance.mGSIDebugLevel[theCat][theType];
138 if (aCurLevel & theLevel)
142 if (gGSIDebugInstance.mGSIDebugFile)
144 fprintf(gGSIDebugInstance.mGSIDebugFile,
"[%s][%s][%s] ",
145 gGSIDebugCatStrings[theCat],
146 gGSIDebugTypeStrings[theType],
147 gGSIDebugLevelStrings[gsiDebugLog2(theLevel)]);
150 vfprintf(gGSIDebugInstance.mGSIDebugFile, theTokenStr,
155 if (gGSIDebugInstance.mDebugCallback != NULL)
157 (*gGSIDebugInstance.mDebugCallback)(theCat, theType, theLevel,
158 theTokenStr, theParamList);
162 gsiDebugCallback(theCat, theType, theLevel,
163 theTokenStr, theParamList);
167 gsiLeaveCriticalSection(&gGSIDebugInstance.mDebugCrit);
174void gsDebugFormat(GSIDebugCategory theCat, GSIDebugType theType,
175 GSIDebugLevel theLevel,
const char* theTokenStr,
178 va_list aParameterList;
181 assert(theCat <= GSIDebugCat_Count);
182 assert(theType <= GSIDebugType_Count);
183 assert(theLevel <= (1<<GSIDebugLevel_Count));
187 va_start(aParameterList, theTokenStr);
190 gsDebugVaList(theCat, theType, theLevel, theTokenStr, aParameterList);
198static void HexEncode16(
const char* theInStream,
char* theOutStream,
199 unsigned int theInLen)
201 const int aRowWidth = 64;
202 const char aReplaceChar =
'.';
203 const int aTextOffSet = 41;
204 char* aTextOutStream = (theOutStream+aTextOffSet);
205 const unsigned int aWriteBit = theInLen & 1;
207 assert(theInLen <= 16);
210 memset(theOutStream,
' ', aRowWidth);
216 unsigned char aChar = (
unsigned char)(*theInStream++);
219 sprintf(theOutStream,
"%02X", aChar);
223 *(aTextOutStream++) = (
char)aChar;
225 *(aTextOutStream++) = aReplaceChar;
231 if ((theInLen & 1) == aWriteBit)
232 *theOutStream++ =
' ';
239 *(aTextOutStream) =
'\0';
246void gsDebugBinary(GSIDebugCategory theCat, GSIDebugType theType,
247 GSIDebugLevel theLevel,
const char* theBuffer, gsi_i32 theLength)
249 int aBytesLeft = theLength;
250 const char* aReadPos = theBuffer;
254 while(aBytesLeft > 0)
256 gsi_i32 aBytesToRead = min(aBytesLeft, 16);
258 HexEncode16(aReadPos, aHexStr, (
unsigned int)aBytesToRead);
259 gsDebugFormat(theCat, theType, theLevel,
" %s\r\n", aHexStr);
261 aReadPos += aBytesToRead;
262 aBytesLeft -= aBytesToRead;
269void gsSetDebugLevel(GSIDebugCategory theCat, GSIDebugType theType,
270 GSIDebugLevel theLevel)
273 assert(theCat <= GSIDebugCat_Count);
274 assert(theType <= GSIDebugType_Count);
277 if (theCat == GSIDebugCat_Count)
280 for (; i<GSIDebugCat_Count; i++)
281 gsSetDebugLevel((GSIDebugCategory)i, theType, theLevel);
283 gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc,
285 "Debug level set to %s for all categories (SDKs)\r\n",
286 gGSIDebugLevelDescriptionStrings[gsiDebugLog2(theLevel)]);
292 if (theType == GSIDebugType_Count)
295 for (; i<GSIDebugType_Count; i++)
296 gsSetDebugLevel(theCat, (GSIDebugType)i, theLevel);
298 gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc,
300 "Debug level set to %s for all types\r\n",
301 gGSIDebugLevelDescriptionStrings[gsiDebugLog2(theLevel)]);
306 if (gGSIDebugInstance.mGSIDebugLevel[theCat][theType] != theLevel)
309 gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc,
310 GSIDebugLevel_Comment,
311 "Changing debug level: [%s][%s][%02X]\r\n",
312 gGSIDebugCatStrings[theCat],
313 gGSIDebugTypeStrings[theType],
315 gGSIDebugInstance.mGSIDebugLevel[theCat][theType] = theLevel;
325void gsSetDebugFile(FILE* theFile)
327 if (theFile != gGSIDebugInstance.mGSIDebugFile)
330 if (gGSIDebugInstance.mGSIDebugFile != NULL)
332 gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc,
333 GSIDebugLevel_Comment,
"Debug disabled in this file\r\n");
338 gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc,
339 GSIDebugLevel_Comment,
"Debug enabled in this file\r\n");
342 gGSIDebugInstance.mGSIDebugFile = theFile;
350FILE* gsOpenDebugFile(
const char* theFileName)
356 assert(theFileName != NULL);
359 aFile = fopen(theFileName,
"w+");
361 gsSetDebugFile(aFile);
370FILE* gsGetDebugFile()
372 return gGSIDebugInstance.mGSIDebugFile;
380void gsSetDebugCallback(GSIDebugCallback theCallback)
382 gGSIDebugInstance.mDebugCallback = theCallback;