3 #if defined(ECERE_BOOTSTRAP)
13 #if defined(__WIN32__)
15 #define WIN32_LEAN_AND_MEAN
17 #define GetFreeSpace _GetFreeSpace
18 #define String String_
23 #elif defined(__unix__) || defined(__APPLE__)
38 FILE *eC_stdout(void);
39 FILE *eC_stderr(void);
41 // IMPLEMENTED IN _System.c
42 bool System_MoveFile(char * source, char * dest);
43 bool System_RenameFile(char * oldName, char * newName);
44 bool System_DeleteFile(char * fileName);
45 bool System_MakeDir(char * path);
46 bool System_RemoveDir(char * path);
47 char * System_GetWorkingDir(char * buf, int size);
48 bool System_ChangeWorkingDir(char * buf);
49 char * System_GetEnvironment(char * envName, char * envValue, int max);
50 void System_SetEnvironment(char * envName, char * envValue);
51 void System_UnsetEnvironment(char * envName);
52 bool System_Execute(char * env, char * command, va_list args);
53 bool System_ShellOpen(char * fileName, va_list args);
54 void System_GetFreeSpace(char * path, FileSize64 * size);
64 #if !defined(ECERE_BOOTSTRAP)
75 import "GuiApplication"
78 public enum LoggingMode { noLogging, stdOut, stdErr, debug, logFile, msgBox, buffer };
80 // GENERAL ERROR CODES
81 public enum ErrorLevel
89 public define AllErrors = ErrorLevel::Minor;
91 public class ErrorCode
93 ErrorLevel level:2:12;
97 public enum SysErrorCode : ErrorCode
99 allocationFailed = ErrorCode { Fatal, 1 },
100 nameInexistant = ErrorCode { Fatal, 2 },
101 nameExists = ErrorCode { Fatal, 3 },
102 missingLibrary = ErrorCode { Fatal, 4 },
103 fileNotFound = ErrorCode { Minor, 5 },
104 writeFailed = ErrorCode { Major, 6 }
107 static define DEFAULT_BUFFER_SIZE = 100 * MAX_F_STRING;
109 static Array<String> errorMessages
112 $"Memory allocation failed",
113 $"Inexistant string identifier specified",
114 $"Identic string identifier already exists",
115 $"Shared library loading failed",
117 $"Couldn't write to file"
120 // --- File, directory & environment manipulation ---
123 public bool MoveFile(char * source, char * dest)
125 return System_MoveFile(source, dest);
128 public bool RenameFile(char * oldName, char * newName)
130 return System_RenameFile(oldName, newName);
135 public bool DeleteFile(char * fileName)
137 return System_DeleteFile(fileName);
140 public bool MakeDir(char * path)
142 return System_MakeDir(path);
145 public bool RemoveDir(char * path)
147 return System_RemoveDir(path);
150 public char * GetWorkingDir(char * buf, int size)
152 return System_GetWorkingDir(buf, size);
155 public bool ChangeWorkingDir(char * buf)
157 return System_ChangeWorkingDir(buf);
160 public char * GetEnvironment(char * envName, char * envValue, int max)
162 return System_GetEnvironment(envName, envValue, max);
165 public void SetEnvironment(char * envName, char * envValue)
167 System_SetEnvironment(envName, envValue);
170 public void UnsetEnvironment(char * envName)
172 System_UnsetEnvironment(envName);
175 public bool Execute(char * command, ...)
179 va_start(args, command);
180 result = System_Execute(null, command, args);
185 public bool ExecuteEnv(char * env, char * command, ...)
189 va_start(args, command);
190 result = System_Execute(env, command, args);
195 public bool ShellOpen(char * fileName, ...)
199 result = System_ShellOpen(fileName, args);
204 public void GetFreeSpace(char * path, FileSize64 * size)
206 System_GetFreeSpace(path, size);
209 // --- Uncagotegorized Functions ---
210 public void Logf(char * format, ...)
213 char string[MAX_F_STRING];
214 va_start(args, format);
215 vsnprintf(string, sizeof(string), format, args);
216 string[sizeof(string)-1] = 0;
221 public void Log(char * text)
223 switch(globalSystem.errorLoggingMode)
226 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
228 uint16 * _wtext = UTF8toUTF16(text, null);
229 OutputDebugString(_wtext);
235 fputs(text, eC_stdout());
239 fputs(text, eC_stderr());
245 if((f = FileOpen(globalSystem.logFile, append)))
254 strcat(globalSystem.errorBuffer, text);
259 public void DumpErrors(bool display)
261 if(globalSystem.errorBuffer && globalSystem.errorBuffer[0])
265 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
266 if(globalSystem.errorLoggingMode == buffer)
267 printf(globalSystem.errorBuffer);
271 sprintf(title, "%s - Error Log", guiApp.appName);
272 MessageBox(HWND_DESKTOP, globalSystem.errorBuffer, title, MB_OK|MB_ICONWARNING);
275 printf(globalSystem.errorBuffer);
278 globalSystem.errorBuffer[0] = '\0';
282 public void LogErrorCode(ErrorCode errorCode, char * details)
284 if(errorCode.level <= globalSystem.errorLevel)
287 Logf("System Error [%d]: %s (%s).\n",
289 errorMessages[errorCode.code],
292 Logf("System Error [%d]: %s.\n",
294 errorMessages[errorCode.code]);
296 globalSystem.lastErrorCode = errorCode;
299 public uint GetLastErrorCode()
301 return globalSystem.lastErrorCode;
304 public void ResetError()
306 globalSystem.lastErrorCode = 0;
309 public void SetErrorLevel(ErrorLevel level)
311 globalSystem.errorLevel = level;
314 public void SetLoggingMode(LoggingMode mode, void * where)
316 globalSystem.errorLoggingMode = mode;
321 strcpy(globalSystem.logFile, where);
322 file = FileOpen(globalSystem.logFile, write);
325 else if(mode == buffer || mode == msgBox)
327 if(!globalSystem.errorBuffer)
329 globalSystem.errorBufferSize = DEFAULT_BUFFER_SIZE;
330 globalSystem.errorBuffer = new char[DEFAULT_BUFFER_SIZE];
332 globalSystem.errorBuffer[0] = 0;
334 else if(mode == debug)
336 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
337 uint16 * _wappName = UTF8toUTF16(guiApp.appName, null);
338 OutputDebugString(L"\n");
339 OutputDebugString(_wappName);
340 OutputDebugString(L" - Logging Errors...\n");
347 if(globalSystem.errorBuffer)
349 delete globalSystem.errorBuffer;
350 globalSystem.errorBufferSize = 0;
355 static define errorLogMsg = $"\n\nWould you like to view the error log?";
357 #if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
358 static DWORD REAL_ExceptionHandler(EXCEPTION_POINTERS *exception)
360 EXCEPTION_RECORD * record = exception->ExceptionRecord;
361 char exceptionString[1024] = "", title[1024];
363 switch(record->ExceptionCode)
365 case EXCEPTION_ACCESS_VIOLATION:
366 if(record->ExceptionInformation[0])
367 sprintf(exceptionString, "Access Violation Writing to 0x%08X", record->ExceptionInformation[1]);
369 sprintf(exceptionString, "Access Violation Reading from 0x%08X", record->ExceptionInformation[1]);
371 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
372 sprintf(exceptionString, "Array Bounds Exceeded");
374 case EXCEPTION_BREAKPOINT:
375 sprintf(exceptionString, "Breakpoint Encountered");
377 case EXCEPTION_DATATYPE_MISALIGNMENT:
378 sprintf(exceptionString, "Data Type Misalignment");
380 case EXCEPTION_FLT_DENORMAL_OPERAND:
381 sprintf(exceptionString, "Floating-Point Denormal Operand");
383 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
384 sprintf(exceptionString, "Floating-Point Divide by Zero");
386 case EXCEPTION_FLT_INEXACT_RESULT:
387 sprintf(exceptionString, "Floating-Point Inexact Result");
389 case EXCEPTION_FLT_INVALID_OPERATION:
390 sprintf(exceptionString, "Floating-Point Invalid Operation");
392 case EXCEPTION_FLT_OVERFLOW:
393 sprintf(exceptionString, "Floating-Point Overflow");
395 case EXCEPTION_FLT_STACK_CHECK:
396 sprintf(exceptionString, "Floating-Point Stack Check");
398 case EXCEPTION_FLT_UNDERFLOW:
399 sprintf(exceptionString, "Floating-Point Underflow");
401 case EXCEPTION_ILLEGAL_INSTRUCTION:
402 sprintf(exceptionString, "Illegal Instruction");
404 case EXCEPTION_IN_PAGE_ERROR:
405 sprintf(exceptionString, "In Page Error");
407 case EXCEPTION_INT_DIVIDE_BY_ZERO:
408 sprintf(exceptionString, "Integer Divide by Zero");
410 case EXCEPTION_INT_OVERFLOW:
411 sprintf(exceptionString, "Integer Overflow");
413 case EXCEPTION_INVALID_DISPOSITION:
414 sprintf(exceptionString, "Invalid Disposition");
416 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
417 sprintf(exceptionString, "Non Continuable Exception");
419 case EXCEPTION_PRIV_INSTRUCTION:
420 sprintf(exceptionString, "Unallowed Instruction");
422 case EXCEPTION_SINGLE_STEP:
423 sprintf(exceptionString, "Single Step Exception");
425 case EXCEPTION_STACK_OVERFLOW:
426 return EXCEPTION_CONTINUE_SEARCH;
428 sprintf(exceptionString, "Stack Overflow");
433 sprintf(title, "%s - Fatal Error", guiApp.appName);
435 if(globalSystem.errorBuffer && globalSystem.errorBuffer[0])
437 strcat(exceptionString, errorLogMsg);
438 if(MessageBox(HWND_DESKTOP, exceptionString, title, MB_YESNO|MB_ICONERROR) == IDYES)
442 MessageBox(HWND_DESKTOP, exceptionString, title, MB_OK|MB_ICONERROR);
444 return EXCEPTION_EXECUTE_HANDLER;
448 private struct System
450 LoggingMode errorLoggingMode;
453 char logFile[MAX_LOCATION];
454 ErrorCode lastErrorCode;
455 ErrorLevel errorLevel;
457 Semaphore eventSemaphore;
459 //FileSystem fileSystems;
462 OldList fileMonitors;
463 Mutex fileMonitorMutex;
464 Thread fileMonitorThread;
465 bool systemTerminate;