3 #if defined(ECERE_BOOTSTRAP)
13 #if defined(__WIN32__)
15 #define WIN32_LEAN_AND_MEAN
17 #define GetFreeSpace _GetFreeSpace
21 #elif defined(__unix__) || defined(__APPLE__)
36 // IMPLEMENTED IN _System.c
37 bool System_MoveFile(char * source, char * dest);
38 bool System_RenameFile(char * oldName, char * newName);
39 bool System_DeleteFile(char * fileName);
40 bool System_MakeDir(char * path);
41 bool System_RemoveDir(char * path);
42 char * System_GetWorkingDir(char * buf, int size);
43 bool System_ChangeWorkingDir(char * buf);
44 char * System_GetEnvironment(char * envName, char * envValue, int max);
45 void System_SetEnvironment(char * envName, char * envValue);
46 void System_UnsetEnvironment(char * envName);
47 bool System_Execute(char * env, char * command, va_list args);
48 bool System_ShellOpen(char * fileName, va_list args);
49 void System_GetFreeSpace(char * path, FileSize64 * size);
59 #if !defined(ECERE_BOOTSTRAP)
70 import "GuiApplication"
73 public enum LoggingMode { noLogging, stdOut, stdErr, debug, logFile, msgBox, buffer };
75 // GENERAL ERROR CODES
76 public enum ErrorLevel
84 public define AllErrors = ErrorLevel::Minor;
86 public class ErrorCode
88 ErrorLevel level:2:12;
92 public enum SysErrorCode : ErrorCode
94 allocationFailed = ErrorCode { Fatal, 1 },
95 nameInexistant = ErrorCode { Fatal, 2 },
96 nameExists = ErrorCode { Fatal, 3 },
97 missingLibrary = ErrorCode { Fatal, 4 },
98 fileNotFound = ErrorCode { Minor, 5 },
99 writeFailed = ErrorCode { Major, 6 }
102 static define DEFAULT_BUFFER_SIZE = 100 * MAX_F_STRING;
104 static Array<String> errorMessages
107 $"Memory allocation failed",
108 $"Inexistant string identifier specified",
109 $"Identic string identifier already exists",
110 $"Shared library loading failed",
112 $"Couldn't write to file"
115 // --- File, directory & environment manipulation ---
118 public bool MoveFile(char * source, char * dest)
120 return System_MoveFile(source, dest);
123 public bool RenameFile(char * oldName, char * newName)
125 return System_RenameFile(oldName, newName);
130 public bool DeleteFile(char * fileName)
132 return System_DeleteFile(fileName);
135 public bool MakeDir(char * path)
137 return System_MakeDir(path);
140 public bool RemoveDir(char * path)
142 return System_RemoveDir(path);
145 public char * GetWorkingDir(char * buf, int size)
147 return System_GetWorkingDir(buf, size);
150 public bool ChangeWorkingDir(char * buf)
152 return System_ChangeWorkingDir(buf);
155 public char * GetEnvironment(char * envName, char * envValue, int max)
157 return System_GetEnvironment(envName, envValue, max);
160 public void SetEnvironment(char * envName, char * envValue)
162 System_SetEnvironment(envName, envValue);
165 public void UnsetEnvironment(char * envName)
167 System_UnsetEnvironment(envName);
170 public bool Execute(char * command, ...)
174 va_start(args, command);
175 result = System_Execute(null, command, args);
180 public bool ExecuteEnv(char * env, char * command, ...)
184 va_start(args, command);
185 result = System_Execute(env, command, args);
190 public bool ShellOpen(char * fileName, ...)
194 result = System_ShellOpen(fileName, args);
199 public void GetFreeSpace(char * path, FileSize64 * size)
201 System_GetFreeSpace(path, size);
204 // --- Uncagotegorized Functions ---
205 public void Logf(char * format, ...)
208 char string[MAX_F_STRING];
209 va_start(args, format);
210 vsnprintf(string, sizeof(string), format, args);
211 string[sizeof(string)-1] = 0;
216 public void Log(char * text)
218 switch(globalSystem.errorLoggingMode)
221 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
223 uint16 * _wtext = UTF8toUTF16(text, null);
224 OutputDebugString(_wtext);
240 if((f = FileOpen(globalSystem.logFile, append)))
249 strcat(globalSystem.errorBuffer, text);
254 public void DumpErrors(bool display)
256 if(globalSystem.errorBuffer && globalSystem.errorBuffer[0])
260 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
261 if(globalSystem.errorLoggingMode == buffer)
262 printf(globalSystem.errorBuffer);
266 sprintf(title, "%s - Error Log", guiApp.appName);
267 MessageBox(HWND_DESKTOP, globalSystem.errorBuffer, title, MB_OK|MB_ICONWARNING);
270 printf(globalSystem.errorBuffer);
273 globalSystem.errorBuffer[0] = '\0';
277 public void LogErrorCode(ErrorCode errorCode, char * details)
279 if(errorCode.level <= globalSystem.errorLevel)
282 Logf("System Error [%d]: %s (%s).\n",
284 errorMessages[errorCode.code],
287 Logf("System Error [%d]: %s.\n",
289 errorMessages[errorCode.code]);
291 globalSystem.lastErrorCode = errorCode;
294 public uint GetLastErrorCode()
296 return globalSystem.lastErrorCode;
299 public void ResetError()
301 globalSystem.lastErrorCode = 0;
304 public void SetErrorLevel(ErrorLevel level)
306 globalSystem.errorLevel = level;
309 public void SetLoggingMode(LoggingMode mode, void * where)
311 globalSystem.errorLoggingMode = mode;
316 strcpy(globalSystem.logFile, where);
317 file = FileOpen(globalSystem.logFile, write);
320 else if(mode == buffer || mode == msgBox)
322 if(!globalSystem.errorBuffer)
324 globalSystem.errorBufferSize = DEFAULT_BUFFER_SIZE;
325 globalSystem.errorBuffer = new char[DEFAULT_BUFFER_SIZE];
327 globalSystem.errorBuffer[0] = 0;
329 else if(mode == debug)
331 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
332 uint16 * _wappName = UTF8toUTF16(guiApp.appName, null);
333 OutputDebugString(L"\n");
334 OutputDebugString(_wappName);
335 OutputDebugString(L" - Logging Errors...\n");
342 if(globalSystem.errorBuffer)
344 delete globalSystem.errorBuffer;
345 globalSystem.errorBufferSize = 0;
350 static define errorLogMsg = $"\n\nWould you like to view the error log?";
352 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
353 static DWORD REAL_ExceptionHandler(EXCEPTION_POINTERS *exception)
355 EXCEPTION_RECORD * record = exception->ExceptionRecord;
356 char exceptionString[1024] = "", title[1024];
358 switch(record->ExceptionCode)
360 case EXCEPTION_ACCESS_VIOLATION:
361 if(record->ExceptionInformation[0])
362 sprintf(exceptionString, "Access Violation Writing to 0x%08X", record->ExceptionInformation[1]);
364 sprintf(exceptionString, "Access Violation Reading from 0x%08X", record->ExceptionInformation[1]);
366 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
367 sprintf(exceptionString, "Array Bounds Exceeded");
369 case EXCEPTION_BREAKPOINT:
370 sprintf(exceptionString, "Breakpoint Encountered");
372 case EXCEPTION_DATATYPE_MISALIGNMENT:
373 sprintf(exceptionString, "Data Type Misalignment");
375 case EXCEPTION_FLT_DENORMAL_OPERAND:
376 sprintf(exceptionString, "Floating-Point Denormal Operand");
378 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
379 sprintf(exceptionString, "Floating-Point Divide by Zero");
381 case EXCEPTION_FLT_INEXACT_RESULT:
382 sprintf(exceptionString, "Floating-Point Inexact Result");
384 case EXCEPTION_FLT_INVALID_OPERATION:
385 sprintf(exceptionString, "Floating-Point Invalid Operation");
387 case EXCEPTION_FLT_OVERFLOW:
388 sprintf(exceptionString, "Floating-Point Overflow");
390 case EXCEPTION_FLT_STACK_CHECK:
391 sprintf(exceptionString, "Floating-Point Stack Check");
393 case EXCEPTION_FLT_UNDERFLOW:
394 sprintf(exceptionString, "Floating-Point Underflow");
396 case EXCEPTION_ILLEGAL_INSTRUCTION:
397 sprintf(exceptionString, "Illegal Instruction");
399 case EXCEPTION_IN_PAGE_ERROR:
400 sprintf(exceptionString, "In Page Error");
402 case EXCEPTION_INT_DIVIDE_BY_ZERO:
403 sprintf(exceptionString, "Integer Divide by Zero");
405 case EXCEPTION_INT_OVERFLOW:
406 sprintf(exceptionString, "Integer Overflow");
408 case EXCEPTION_INVALID_DISPOSITION:
409 sprintf(exceptionString, "Invalid Disposition");
411 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
412 sprintf(exceptionString, "Non Continuable Exception");
414 case EXCEPTION_PRIV_INSTRUCTION:
415 sprintf(exceptionString, "Unallowed Instruction");
417 case EXCEPTION_SINGLE_STEP:
418 sprintf(exceptionString, "Single Step Exception");
420 case EXCEPTION_STACK_OVERFLOW:
421 return EXCEPTION_CONTINUE_SEARCH;
423 sprintf(exceptionString, "Stack Overflow");
428 sprintf(title, "%s - Fatal Error", guiApp.appName);
430 if(globalSystem.errorBuffer && globalSystem.errorBuffer[0])
432 strcat(exceptionString, errorLogMsg);
433 if(MessageBox(HWND_DESKTOP, exceptionString, title, MB_YESNO|MB_ICONERROR) == IDYES)
437 MessageBox(HWND_DESKTOP, exceptionString, title, MB_OK|MB_ICONERROR);
439 return EXCEPTION_EXECUTE_HANDLER;
443 private struct System
445 LoggingMode errorLoggingMode;
448 char logFile[MAX_LOCATION];
449 ErrorCode lastErrorCode;
450 ErrorLevel errorLevel;
452 Semaphore eventSemaphore;
454 //FileSystem fileSystems;
457 OldList fileMonitors;
458 Mutex fileMonitorMutex;
459 Thread fileMonitorThread;
460 bool systemTerminate;