console.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // console.c
  3. // gravity
  4. //
  5. // Created by Marco Bambini on 23/02/15.
  6. // Copyright (c) 2015 CreoLabs. All rights reserved.
  7. //
  8. #include "console.h"
  9. // MUST BE CALLED FROM main file
  10. const char *current_filepath (const char *base, const char *target_file) {
  11. // if it is an absolute path then return the path itself
  12. if (target_file[0] == '/') return target_file;
  13. static char buffer[4096];
  14. static size_t skip = strlen("GravityObjC/ObjC/main.c");
  15. // __FILE__ macro contains full path to main.c file
  16. // for example: /Users/marco/SQLabs/Butterfly/gravity/main/main.c
  17. snprintf(buffer, strlen(base) - skip, "%s", base);
  18. strcat(buffer, "/shared/");
  19. strcat(buffer, target_file);
  20. return buffer;
  21. }
  22. void report_log (const char *message, void *xdata) {
  23. #pragma unused(xdata)
  24. printf("LOG: %s\n", message);
  25. }
  26. void report_error (gravity_vm *vm, error_type_t error_type, const char *message, error_desc_t error_desc, void *xdata) {
  27. #pragma unused(vm, xdata)
  28. const char *type = "N/A";
  29. switch (error_type) {
  30. case GRAVITY_ERROR_NONE: type = "NONE"; break;
  31. case GRAVITY_ERROR_SYNTAX: type = "SYNTAX"; break;
  32. case GRAVITY_ERROR_SEMANTIC: type = "SEMANTIC"; break;
  33. case GRAVITY_ERROR_RUNTIME: type = "RUNTIME"; break;
  34. case GRAVITY_WARNING: type = "WARNING"; break;
  35. case GRAVITY_ERROR_IO: type = "I/O"; break;
  36. }
  37. if (error_type == GRAVITY_ERROR_RUNTIME) printf("RUNTIME ERROR: ");
  38. else printf("%s ERROR on %d (%d,%d): ", type, error_desc.fileid, error_desc.lineno, error_desc.colno);
  39. printf("%s\n", message);
  40. }