3 #if defined(ECERE_BOOTSTRAP)
12 #if defined(__WIN32__)
14 #define WIN32_LEAN_AND_MEAN
16 #define GetFreeSpace _GetFreeSpace
20 #elif defined(__unix__) || defined(__APPLE__)
33 // IMPLEMENTED IN _System.c
34 bool System_MoveFile(char * source, char * dest);
35 bool System_RenameFile(char * oldName, char * newName);
36 bool System_DeleteFile(char * fileName);
37 bool System_MakeDir(char * path);
38 bool System_RemoveDir(char * path);
39 char * System_GetWorkingDir(char * buf, int size);
40 bool System_ChangeWorkingDir(char * buf);
41 char * System_GetEnvironment(char * envName, char * envValue, int max);
42 void System_SetEnvironment(char * envName, char * envValue);
43 void System_UnsetEnvironment(char * envName);
44 bool System_Execute(char * env, char * command, va_list args);
45 bool System_ShellOpen(char * fileName, va_list args);
46 void System_GetFreeSpace(char * path, FileSize * size);
54 #if !defined(ECERE_BOOTSTRAP)
65 import "GuiApplication"
68 public enum LoggingMode { noLogging, stdOut, stdErr, debug, logFile, msgBox, buffer };
70 // GENERAL ERROR CODES
71 public enum ErrorLevel
79 public define AllErrors = ErrorLevel::Minor;
81 public class ErrorCode
83 ErrorLevel level:2:12;
87 public enum SysErrorCode : ErrorCode
89 allocationFailed = ErrorCode { Fatal, 1 },
90 nameInexistant = ErrorCode { Fatal, 2 },
91 nameExists = ErrorCode { Fatal, 3 },
92 missingLibrary = ErrorCode { Fatal, 4 },
93 fileNotFound = ErrorCode { Minor, 5 },
94 writeFailed = ErrorCode { Major, 6 }
97 static define DEFAULT_BUFFER_SIZE = 100 * MAX_F_STRING;
99 static char * errorMessages[] =
102 "Memory allocation failed",
103 "Inexistant string identifier specified",
104 "Identic string identifier already exists",
105 "Shared library loading failed",
107 "Couldn't write to file"
110 // --- File, directory & environment manipulation ---
113 public bool MoveFile(char * source, char * dest)
115 return System_MoveFile(source, dest);
118 public bool RenameFile(char * oldName, char * newName)
120 return System_RenameFile(oldName, newName);
125 public bool DeleteFile(char * fileName)
127 return System_DeleteFile(fileName);
130 public bool MakeDir(char * path)
132 return System_MakeDir(path);
135 public bool RemoveDir(char * path)
137 return System_RemoveDir(path);
140 public char * GetWorkingDir(char * buf, int size)
142 return System_GetWorkingDir(buf, size);
145 public bool ChangeWorkingDir(char * buf)
147 return System_ChangeWorkingDir(buf);
150 public char * GetEnvironment(char * envName, char * envValue, int max)
152 return System_GetEnvironment(envName, envValue, max);
155 public void SetEnvironment(char * envName, char * envValue)
157 System_SetEnvironment(envName, envValue);
160 public void UnsetEnvironment(char * envName)
162 System_UnsetEnvironment(envName);
165 public bool Execute(char * command, ...)
169 va_start(args, command);
170 result = System_Execute(null, command, args);
175 public bool ExecuteEnv(char * env, char * command, ...)
179 va_start(args, command);
180 result = System_Execute(env, command, args);
185 public bool ShellOpen(char * fileName, ...)
189 result = System_ShellOpen(fileName, args);
194 public void GetFreeSpace(char * path, FileSize * size)
196 System_GetFreeSpace(path, size);
199 // --- Uncagotegorized Functions ---
200 public void Logf(char * format, ...)
203 char string[MAX_F_STRING];
204 va_start(args, format);
205 vsprintf(string,format,args);
210 public void Log(char * text)
212 switch(globalSystem.errorLoggingMode)
215 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
217 uint16 * _wtext = UTF8toUTF16(text, null);
218 OutputDebugString(_wtext);
234 if((f = FileOpen(globalSystem.logFile, append)))
243 strcat(globalSystem.errorBuffer, text);
248 public void DumpErrors(bool display)
250 if(globalSystem.errorBuffer && globalSystem.errorBuffer[0])
254 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
255 if(globalSystem.errorLoggingMode == buffer)
256 printf(globalSystem.errorBuffer);
260 sprintf(title, "%s - Error Log", guiApp.appName);
261 MessageBox(HWND_DESKTOP, globalSystem.errorBuffer, title, MB_OK|MB_ICONWARNING);
264 printf(globalSystem.errorBuffer);
267 globalSystem.errorBuffer[0] = '\0';
271 public void LogErrorCode(ErrorCode errorCode, char * details)
273 if(errorCode.level <= globalSystem.errorLevel)
276 Logf("System Error [%d]: %s (%s).\n",
278 errorMessages[errorCode.code],
281 Logf("System Error [%d]: %s.\n",
283 errorMessages[errorCode.code]);
285 globalSystem.lastErrorCode = errorCode;
288 public uint GetLastErrorCode()
290 return globalSystem.lastErrorCode;
293 public void ResetError()
295 globalSystem.lastErrorCode = 0;
298 public void SetErrorLevel(ErrorLevel level)
300 globalSystem.errorLevel = level;
303 public void SetLoggingMode(LoggingMode mode, void * where)
305 globalSystem.errorLoggingMode = mode;
310 strcpy(globalSystem.logFile, where);
311 file = FileOpen(globalSystem.logFile, write);
314 else if(mode == buffer || mode == msgBox)
316 if(!globalSystem.errorBuffer)
318 globalSystem.errorBufferSize = DEFAULT_BUFFER_SIZE;
319 globalSystem.errorBuffer = new char[DEFAULT_BUFFER_SIZE];
321 globalSystem.errorBuffer[0] = 0;
323 else if(mode == debug)
325 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
326 uint16 * _wappName = UTF8toUTF16(guiApp.appName, null);
327 OutputDebugString(L"\n");
328 OutputDebugString(_wappName);
329 OutputDebugString(L" - Logging Errors...\n");
336 if(globalSystem.errorBuffer)
338 delete globalSystem.errorBuffer;
339 globalSystem.errorBufferSize = 0;
344 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
345 static DWORD REAL_ExceptionHandler(EXCEPTION_POINTERS *exception)
347 EXCEPTION_RECORD * record = exception->ExceptionRecord;
348 char exceptionString[1024] = "", title[1024];
350 switch(record->ExceptionCode)
352 case EXCEPTION_ACCESS_VIOLATION:
353 if(record->ExceptionInformation[0])
354 sprintf(exceptionString, "Access Violation Writing to 0x%08X", record->ExceptionInformation[1]);
356 sprintf(exceptionString, "Access Violation Reading from 0x%08X", record->ExceptionInformation[1]);
358 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
359 sprintf(exceptionString, "Array Bounds Exceeded");
361 case EXCEPTION_BREAKPOINT:
362 sprintf(exceptionString, "Breakpoint Encountered");
364 case EXCEPTION_DATATYPE_MISALIGNMENT:
365 sprintf(exceptionString, "Data Type Misalignment");
367 case EXCEPTION_FLT_DENORMAL_OPERAND:
368 sprintf(exceptionString, "Floating-Point Denormal Operand");
370 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
371 sprintf(exceptionString, "Floating-Point Divide by Zero");
373 case EXCEPTION_FLT_INEXACT_RESULT:
374 sprintf(exceptionString, "Floating-Point Inexact Result");
376 case EXCEPTION_FLT_INVALID_OPERATION:
377 sprintf(exceptionString, "Floating-Point Invalid Operation");
379 case EXCEPTION_FLT_OVERFLOW:
380 sprintf(exceptionString, "Floating-Point Overflow");
382 case EXCEPTION_FLT_STACK_CHECK:
383 sprintf(exceptionString, "Floating-Point Stack Check");
385 case EXCEPTION_FLT_UNDERFLOW:
386 sprintf(exceptionString, "Floating-Point Underflow");
388 case EXCEPTION_ILLEGAL_INSTRUCTION:
389 sprintf(exceptionString, "Illegal Instruction");
391 case EXCEPTION_IN_PAGE_ERROR:
392 sprintf(exceptionString, "In Page Error");
394 case EXCEPTION_INT_DIVIDE_BY_ZERO:
395 sprintf(exceptionString, "Integer Divide by Zero");
397 case EXCEPTION_INT_OVERFLOW:
398 sprintf(exceptionString, "Integer Overflow");
400 case EXCEPTION_INVALID_DISPOSITION:
401 sprintf(exceptionString, "Invalid Disposition");
403 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
404 sprintf(exceptionString, "Non Continuable Exception");
406 case EXCEPTION_PRIV_INSTRUCTION:
407 sprintf(exceptionString, "Unallowed Instruction");
409 case EXCEPTION_SINGLE_STEP:
410 sprintf(exceptionString, "Single Step Exception");
412 case EXCEPTION_STACK_OVERFLOW:
413 return EXCEPTION_CONTINUE_SEARCH;
415 sprintf(exceptionString, "Stack Overflow");
420 sprintf(title, "%s - Fatal Error", guiApp.appName);
422 if(globalSystem.errorBuffer && globalSystem.errorBuffer[0])
424 strcat(exceptionString, "\n\nWould you like to view the error log?");
425 if(MessageBox(HWND_DESKTOP, exceptionString, title, MB_YESNO|MB_ICONERROR) == IDYES)
429 MessageBox(HWND_DESKTOP, exceptionString, title, MB_OK|MB_ICONERROR);
431 return EXCEPTION_EXECUTE_HANDLER;
435 private struct System
437 LoggingMode errorLoggingMode;
440 char logFile[MAX_LOCATION];
441 ErrorCode lastErrorCode;
442 ErrorLevel errorLevel;
444 Semaphore eventSemaphore;
446 //FileSystem fileSystems;
449 OldList fileMonitors;
450 Mutex fileMonitorMutex;
451 Thread fileMonitorThread;
452 bool systemTerminate;