00001 #include "flGlobal.h" 00002 #if FL_DEBUG != 0 00003 #include <pspkernel.h> 00004 #include <pspdebug.h> 00005 #include <pspdisplay.h> 00006 #include <pspgu.h> 00007 #include <pspctrl.h> 00008 #include <string.h> 00009 #include <stdio.h> 00010 00011 #if FL_DEBUG_TIMESTAMP != 0 00012 #include <psprtc.h> 00013 #endif 00014 00015 #if FL_INCLUDE_ALL_C == 0 00016 #include "flDebug.h" 00017 #include "funcLib.h" 00018 #include "flFile.h" 00019 #include "flGraphics.h" 00020 #include "flTexture.h" 00021 #include "flNotify.h" 00022 #endif 00023 00024 bool debugEnabled = true; 00025 00026 void debugInit() { 00027 #if FL_DEBUG_LOGCLEAR != 0 00028 #if FL_FILE != 0 00029 fileDelete(FL_DEBUG_LOGPATH); 00030 #else 00031 sceIoRemove(FL_DEBUG_LOGPATH); 00032 #endif 00033 #endif 00034 } 00035 00036 #if FL_DEBUG_ERROR != 0 00037 #if FL_DEBUG_CALLEDFROM != 0 00038 void debugErrorFrom(const char* inFile, const char* inFunc, int inLine, char* inMessage, ...) { 00039 #else 00040 void debugError(char* inMessage, ...) { 00041 #endif 00042 if(!debugEnabled) 00043 return; 00044 debugEnabled = false; 00045 00046 u32 tempLen = strlen(inMessage); 00047 tempLen += max(DEBUG_ARGEXTEND, (tempLen >> 1)); 00048 char* tempMessage = memQalloc(tempLen); 00049 00050 va_list tempArgList; 00051 va_start(tempArgList, inMessage); 00052 vsprintf(tempMessage, inMessage, tempArgList); 00053 va_end(tempArgList); 00054 00055 #if FL_DEBUG_LOGGING != 0 00056 #if FL_DEBUG_CALLEDFROM != 0 00057 debugLogFrom("ERROR", inFile, inFunc, inLine, tempMessage); 00058 #else 00059 debugLog("ERROR", tempMessage); 00060 #endif 00061 #endif 00062 00063 #if (FL_GRAPHICS != 0) && (FL_TEXTURE != 0) && (FL_DEBUG_ERROR_SCREENSHOT != 0) 00064 debugScreenshot(); 00065 #endif 00066 00067 #if FL_DEBUG_ERROR_SHOW != 0 00068 SceCtrlData inputPad; 00069 int inputLock = 25; 00070 00071 #if FL_GRAPHICS != 0 00072 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00073 #endif 00074 pspDebugScreenClear (); 00075 pspDebugScreenSetTextColor(FL_DEBUG_ERROR_COLOR); 00076 pspDebugScreenPrintf("ERROR!\n\n"); 00077 #if FL_DEBUG_CALLEDFROM != 0 00078 pspDebugScreenPrintf("%s:%s(@%i)> ", inFile, inFunc, inLine); 00079 #endif 00080 pspDebugScreenPrintf(tempMessage); 00081 pspDebugScreenSetTextColor(0xFFFFFFFF); 00082 pspDebugScreenPrintf("\n\nPress [X] to continue or [O] to quit.\n"); 00083 #if FL_GRAPHICS != 0 00084 sceDisplayWaitVblankStart(); 00085 graphicsDispBuffer = graphicsDrawBuffer; 00086 graphicsDrawBuffer = sceGuSwapBuffers(); 00087 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00088 pspDebugScreenSetXY(0, 0); 00089 #endif 00090 00091 while(1) { 00092 if(inputLock > 0) { 00093 inputLock--; 00094 } else { 00095 sceCtrlReadBufferPositive(&inputPad, 1); 00096 if(inputPad.Buttons & PSP_CTRL_CROSS) { 00097 break; 00098 } else if(inputPad.Buttons & PSP_CTRL_CIRCLE) { 00099 flTerminate(); 00100 } 00101 } 00102 } 00103 #else 00104 #if FL_DEBUG_ERROR_NOTIFY 00105 ntfyTrayAdd(tempMessage, FL_DEBUG_ERROR_COLOR); 00106 #endif 00107 #endif 00108 00109 memFree(tempMessage); 00110 00111 debugEnabled = true; 00112 } 00113 #endif 00114 00115 #if FL_DEBUG_WARNING != 0 00116 #if FL_DEBUG_CALLEDFROM != 0 00117 void debugWarningFrom(const char* inFile, const char* inFunc, int inLine, char* inMessage, ...) { 00118 #else 00119 void debugWarning(char* inMessage, ...) { 00120 #endif 00121 if(!debugEnabled) 00122 return; 00123 debugEnabled = false; 00124 00125 u32 tempLen = strlen(inMessage); 00126 tempLen += max(DEBUG_ARGEXTEND, (tempLen >> 1)); 00127 char* tempMessage = memQalloc(tempLen); 00128 00129 va_list tempArgList; 00130 va_start(tempArgList, inMessage); 00131 vsprintf(tempMessage, inMessage, tempArgList); 00132 va_end(tempArgList); 00133 00134 #if FL_DEBUG_LOGGING != 0 00135 #if FL_DEBUG_CALLEDFROM != 0 00136 debugLogFrom("WARNING", inFile, inFunc, inLine, tempMessage); 00137 #else 00138 debugLog("WARNING", tempMessage); 00139 #endif 00140 #endif 00141 00142 #if (GRAPHICS != 0) && (TEXTURE != 0) && (DEBUG_WARNING_SCREENSHOT != 0) 00143 debugScreenshot(); 00144 #endif 00145 00146 #if FL_DEBUG_WARNING_SHOW != 0 00147 SceCtrlData inputPad; 00148 int inputLock = 25; 00149 00150 #if FL_GRAPHICS != 0 00151 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00152 #endif 00153 pspDebugScreenClear (); 00154 pspDebugScreenSetTextColor(FL_DEBUG_WARNING_COLOR); 00155 pspDebugScreenPrintf("WARNING!\n\n"); 00156 #if FL_DEBUG_CALLEDFROM != 0 00157 pspDebugScreenPrintf("%s:%s(@%i)> ", inFile, inFunc, inLine); 00158 #endif 00159 pspDebugScreenPrintf(tempMessage); 00160 pspDebugScreenSetTextColor(0xFFFFFFFF); 00161 pspDebugScreenPrintf("\n\nPress [X] to continue.\n"); 00162 #if FL_GRAPHICS != 0 00163 sceDisplayWaitVblankStart(); 00164 graphicsDispBuffer = graphicsDrawBuffer; 00165 graphicsDrawBuffer = sceGuSwapBuffers(); 00166 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00167 pspDebugScreenSetXY(0, 0); 00168 #endif 00169 00170 while(1) { 00171 if(inputLock > 0) { 00172 inputLock--; 00173 } else { 00174 sceCtrlReadBufferPositive(&inputPad, 1); 00175 if(inputPad.Buttons & PSP_CTRL_CROSS) { 00176 break; 00177 } 00178 } 00179 } 00180 #else 00181 #if FL_DEBUG_WARNING_NOTIFY 00182 ntfyTrayAdd(tempMessage, FL_DEBUG_WARNING_COLOR); 00183 #endif 00184 #endif 00185 00186 memFree(tempMessage); 00187 00188 debugEnabled = true; 00189 } 00190 #endif 00191 00192 #if FL_DEBUG_DEVWARNING != 0 00193 #if FL_DEBUG_CALLEDFROM != 0 00194 void debugDevWarningFrom(const char* inFile, const char* inFunc, int inLine, char* inMessage, ...) { 00195 #else 00196 void debugDevWarning(char* inMessage, ...) { 00197 #endif 00198 if(!debugEnabled) 00199 return; 00200 debugEnabled = false; 00201 00202 u32 tempLen = strlen(inMessage); 00203 tempLen += max(DEBUG_ARGEXTEND, (tempLen >> 1)); 00204 char* tempMessage = memQalloc(tempLen); 00205 00206 va_list tempArgList; 00207 va_start(tempArgList, inMessage); 00208 vsprintf(tempMessage, inMessage, tempArgList); 00209 va_end(tempArgList); 00210 00211 #if FL_DEBUG_LOGGING != 0 00212 #if FL_DEBUG_CALLEDFROM != 0 00213 debugLogFrom("DEV-WARNING", inFile, inFunc, inLine, tempMessage); 00214 #else 00215 debugLog("DEV-WARNING", tempMessage); 00216 #endif 00217 #endif 00218 00219 #if (GRAPHICS != 0) && (TEXTURE != 0) && (DEBUG_DEVWARNING_SCREENSHOT != 0) 00220 debugScreenshot(); 00221 #endif 00222 00223 #if FL_DEBUG_DEVWARNING_SHOW != 0 00224 SceCtrlData inputPad; 00225 int inputLock = 25; 00226 00227 #if FL_GRAPHICS != 0 00228 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00229 #endif 00230 pspDebugScreenClear (); 00231 pspDebugScreenSetTextColor(FL_DEBUG_DEVWARNING_COLOR); 00232 pspDebugScreenPrintf("(i) DEV WARNING.\n\n"); 00233 #if FL_DEBUG_CALLEDFROM != 0 00234 pspDebugScreenPrintf("%s:%s(@%i)> ", inFile, inFunc, inLine); 00235 #endif 00236 pspDebugScreenPrintf(tempMessage); 00237 pspDebugScreenSetTextColor(0xFFFFFFFF); 00238 pspDebugScreenPrintf("\n\nPress [X] to continue.\n"); 00239 #if FL_GRAPHICS != 0 00240 sceDisplayWaitVblankStart(); 00241 graphicsDispBuffer = graphicsDrawBuffer; 00242 graphicsDrawBuffer = sceGuSwapBuffers(); 00243 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00244 pspDebugScreenSetXY(0, 0); 00245 #endif 00246 00247 while(1) { 00248 if(inputLock > 0) { 00249 inputLock--; 00250 } else { 00251 sceCtrlReadBufferPositive(&inputPad, 1); 00252 if(inputPad.Buttons & PSP_CTRL_CROSS) { 00253 break; 00254 } 00255 } 00256 } 00257 #else 00258 #if FL_DEBUG_DEVWARNING_NOTIFY 00259 ntfyTrayAdd(tempMessage, FL_DEBUG_DEVWARNING_COLOR); 00260 #endif 00261 #endif 00262 00263 memFree(tempMessage); 00264 00265 debugEnabled = true; 00266 } 00267 #endif 00268 00269 #if FL_DEBUG_CALLEDFROM != 0 00270 void debugCriticalErrorFrom(const char* inFile, const char* inFunc, int inLine, char* inMessage, ...) { 00271 #else 00272 void debugCriticalError(char* inMessage, ...) { 00273 #endif 00274 if(!debugEnabled) { 00275 flTerminate(); 00276 return; 00277 } 00278 debugEnabled = false; 00279 00280 u32 tempLen = strlen(inMessage); 00281 tempLen += max(DEBUG_ARGEXTEND, (tempLen >> 1)); 00282 char* tempMessage = memQalloc(tempLen); 00283 00284 va_list tempArgList; 00285 va_start(tempArgList, inMessage); 00286 vsprintf(tempMessage, inMessage, tempArgList); 00287 va_end(tempArgList); 00288 00289 SceCtrlData inputPad; 00290 int inputLock = 25; 00291 00292 #if FL_DEBUG_LOGGING != 0 00293 #if FL_DEBUG_CALLEDFROM != 0 00294 debugLogFrom("CRITICAL-ERROR", inFile, inFunc, inLine, tempMessage); 00295 #else 00296 debugLog("CRITICAL-ERROR", tempMessage); 00297 #endif 00298 #endif 00299 00300 #if (FL_GRAPHICS != 0) && (FL_TEXTURE != 0) && (FL_DEBUG_CRITICALERROR_SCREENSHOT != 0) 00301 debugScreenshot(); 00302 #endif 00303 00304 #if FL_DEBUG_CRITICALERROR_SHOW != 0 00305 flTerminate(); 00306 return; 00307 #endif 00308 00309 #if FL_GRAPHICS != 0 00310 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00311 #endif 00312 pspDebugScreenClear (); 00313 pspDebugScreenSetTextColor(FL_DEBUG_CRITICALERROR_COLOR); 00314 pspDebugScreenPrintf("CRITICAL ERROR!\n\n"); 00315 #if FL_DEBUG_CALLEDFROM != 0 00316 pspDebugScreenPrintf("%s:%s(@%i)> ", inFile, inFunc, inLine); 00317 #endif 00318 pspDebugScreenPrintf(tempMessage); 00319 pspDebugScreenSetTextColor(0xFFFFFFFF); 00320 pspDebugScreenPrintf("\n\nPress [X] or [O] to quit.\n"); 00321 #if FL_GRAPHICS != 0 00322 sceDisplayWaitVblankStart(); 00323 graphicsDispBuffer = graphicsDrawBuffer; 00324 graphicsDrawBuffer = sceGuSwapBuffers(); 00325 pspDebugScreenSetOffset((int)graphicsDrawBuffer); 00326 pspDebugScreenSetXY(0, 0); 00327 #endif 00328 #if GRAPHICS_SIMPLE != 0 00329 sceDisplayWaitVblankStart(); 00330 graphicsSwapBuffers(); 00331 #endif 00332 00333 while(1) { 00334 if(inputLock > 0) { 00335 inputLock--; 00336 } else { 00337 sceCtrlReadBufferPositive(&inputPad, 1); 00338 if((inputPad.Buttons & PSP_CTRL_CROSS) || (inputPad.Buttons & PSP_CTRL_CIRCLE)) { 00339 flTerminate(); 00340 } 00341 } 00342 } 00343 } 00344 00345 #if FL_DEBUG_CALLEDFROM != 0 00346 void debugLogFrom(char* inType, const char* inFile, const char* inFunc, int inLine, char* inMessage, ...) { 00347 u32 tempLen = strlen(inMessage); 00348 tempLen += max(DEBUG_ARGEXTEND, (tempLen >> 1)); 00349 char* tempMessage = memQalloc(tempLen); 00350 00351 va_list tempArgList; 00352 va_start(tempArgList, inMessage); 00353 vsprintf(tempMessage, inMessage, tempArgList); 00354 va_end(tempArgList); 00355 00356 #if FL_FILE != 0 00357 File* tempFile = fileOpen(FL_DEBUG_LOGPATH, FILE_MODE_APPEND); 00358 #else 00359 FILE* tempFile = fopen(FL_DEBUG_LOGPATH, "a"); 00360 #endif 00361 if(!tempFile) { 00362 #if FL_DEBUG_ERROR != 0 00363 debugError("Cannot open log file."); 00364 #endif 00365 memFree(tempMessage); 00366 return; 00367 } 00368 char tempString[256]; 00369 #if FL_DEBUG_TIMESTAMP != 0 00370 pspTime tempTime; 00371 sceRtcGetCurrentClockLocalTime(&tempTime); 00372 #if FL_DEBUG_DATEFORMAT_AMERICAN != 0 00373 sprintf(tempString, "[%02i/%02i/%02i - %02i:%02i:%02i] ", tempTime.month, tempTime.day, tempTime.year, tempTime.hour, tempTime.minutes, tempTime.seconds); 00374 #else 00375 sprintf(tempString, "[%02i/%02i/%02i - %02i:%02i:%02i] ", tempTime.day, tempTime.month, tempTime.year, tempTime.hour, tempTime.minutes, tempTime.seconds); 00376 #endif 00377 filePuts(tempString, tempFile); 00378 #endif 00379 00380 filePuts("[", tempFile); 00381 filePuts(inType, tempFile); 00382 filePuts("] ", tempFile); 00383 00384 sprintf(tempString, "%s:%s(@%i)> ", inFile, inFunc, inLine); 00385 filePuts(tempString, tempFile); 00386 filePuts(tempMessage, tempFile); 00387 filePuts("\r\n", tempFile); 00388 fileClose(tempFile); 00389 00390 memFree(tempMessage); 00391 } 00392 #endif 00393 00394 void debugLog(char* inType, char* inMessage, ...) { 00395 u32 tempLen = strlen(inMessage); 00396 tempLen += max(DEBUG_ARGEXTEND, (tempLen >> 1)); 00397 char* tempMessage = memQalloc(tempLen); 00398 00399 va_list tempArgList; 00400 va_start(tempArgList, inMessage); 00401 vsprintf(tempMessage, inMessage, tempArgList); 00402 va_end(tempArgList); 00403 00404 #if FL_FILE != 0 00405 File* tempFile = fileOpen(FL_DEBUG_LOGPATH, FILE_MODE_APPEND); 00406 #else 00407 FILE* tempFile = fopen(FL_DEBUG_LOGPATH, "a"); 00408 #endif 00409 if(!tempFile) { 00410 #if FL_DEBUG_ERROR != 0 00411 debugError("Cannot open log file."); 00412 #endif 00413 memFree(tempMessage); 00414 return; 00415 } 00416 #if FL_DEBUG_TIMESTAMP != 0 00417 char tempString[256]; 00418 pspTime tempTime; 00419 sceRtcGetCurrentClockLocalTime(&tempTime); 00420 #if FL_DEBUG_DATEFORMAT_AMERICAN != 0 00421 sprintf(tempString, "[%02i/%02i/%04i - %02i:%02i:%02i] ", tempTime.month, tempTime.day, tempTime.year, tempTime.hour, tempTime.minutes, tempTime.seconds); 00422 #else 00423 sprintf(tempString, "[%02i/%02i/%04i - %02i:%02i:%02i] ", tempTime.day, tempTime.month, tempTime.year, tempTime.hour, tempTime.minutes, tempTime.seconds); 00424 #endif 00425 filePuts(tempString, tempFile); 00426 #endif 00427 00428 filePuts("[", tempFile); 00429 filePuts(inType, tempFile); 00430 filePuts("] ", tempFile); 00431 00432 filePuts(tempMessage, tempFile); 00433 filePuts("\r\n", tempFile); 00434 fileClose(tempFile); 00435 00436 memFree(tempMessage); 00437 } 00438 00439 #if FL_DEBUG_CALLEDFROM != 0 00440 void debugStatusLogFrom(const char* inFile, const char* inFunc, int inLine, char* inMessage, ...) { 00441 #else 00442 void debugStatusLog(char* inMessage, ...) { 00443 #endif 00444 u32 tempLen = strlen(inMessage); 00445 tempLen += max(DEBUG_ARGEXTEND, (tempLen >> 1)); 00446 char* tempMessage = memQalloc(tempLen); 00447 00448 va_list tempArgList; 00449 va_start(tempArgList, inMessage); 00450 vsprintf(tempMessage, inMessage, tempArgList); 00451 va_end(tempArgList); 00452 00453 #if FL_DEBUG_CALLEDFROM != 0 00454 debugLogFrom("STATUS", inFile, inFunc, inLine, tempMessage); 00455 #else 00456 debugLog("STATUS", tempMessage); 00457 #endif 00458 00459 #if FL_DEBUG_STATUS_NOTIFY 00460 ntfyTrayAdd(tempMessage, FL_DEBUG_STATUS_COLOR); 00461 #endif 00462 00463 memFree(tempMessage); 00464 } 00465 00466 #if (FL_GRAPHICS != 0) && (FL_TEXTURE != 0) 00467 void debugScreenshot() { 00468 Texture* tempScreen = texScreenshot(); 00469 if(!tempScreen) { 00470 #if DEBUG_ERROR != 0 00471 debugError("Cannot retrieve error screenshot."); 00472 #endif 00473 return; 00474 } 00475 char tempPath[128]; 00476 int tempShotNumber = 0; 00477 sprintf(tempPath, FL_DEBUG_SCREENSHOTPATH, tempShotNumber); 00478 #if FL_FILE != 0 00479 File* tempFile = fileOpen(tempPath, FILE_MODE_READ | FILE_MODE_BINARY); 00480 #else 00481 FILE* tempFile = fopen(tempPath, "rb"); 00482 #endif 00483 while(tempFile) { 00484 fileClose(tempFile); 00485 tempShotNumber++; 00486 sprintf(tempPath, FL_DEBUG_SCREENSHOTPATH, tempShotNumber); 00487 #if FL_FILE != 0 00488 tempFile = fileOpen(tempPath, FILE_MODE_READ | FILE_MODE_BINARY); 00489 #else 00490 tempFile = fopen(tempPath, "rb"); 00491 #endif 00492 } 00493 if(!texSave(tempScreen, tempPath)) { 00494 #if DEBUG_ERROR != 0 00495 debugError("Cannot save error screenshot."); 00496 #endif 00497 return; 00498 } 00499 00500 #if FL_DEBUG_STATUS != 0 00501 debugStatusLog("Saved screenshot: %s", tempPath); 00502 #endif 00503 } 00504 #endif 00505 00506 #endif 00507