Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

105 Zeilen
4.3 KiB

  1. #pragma once
  2. #include "stdio.h"
  3. #include "math.h"
  4. #include "stdarg.h"
  5. #include "stdlib.h"
  6. #include "types.hpp"
  7. #include "print.hpp"
  8. struct Error {
  9. String type;
  10. String message;
  11. };
  12. Error* error = nullptr;
  13. auto delete_error() -> void {
  14. free(error);
  15. error = nullptr;
  16. }
  17. auto create_error(const char* c_func_name, const char* c_file_name,
  18. u32 c_file_line, String type, const char* format, ...) -> void {
  19. error = (Error*) malloc(sizeof(Error));
  20. va_list args;
  21. va_start(args, format);
  22. error->message.length = print_va_args_to_string(&(error->message.data), format, &args);
  23. va_end(args);
  24. error->type = type;
  25. print("\n%{color<}%{->Str} error:%{>color} %{->Str}\n",
  26. console_red, &(error->type), &(error->message));
  27. print("in");
  28. s32 spacing = 30-((s32)strlen(c_file_name) + (s32)log10(c_file_line));
  29. if (spacing < 1) spacing = 1;
  30. for (s32 i = 0; i < spacing; ++i)
  31. print(" ");
  32. print("%s (%u) ", c_file_name, c_file_line);
  33. print("-> %s\n", c_func_name);
  34. }
  35. #define __create_error(keyword, ...) \
  36. create_error( \
  37. __FUNCTION__, __FILE__, __LINE__, \
  38. make_heap_string(keyword), \
  39. __VA_ARGS__)
  40. #define create_assertion_error(...) \
  41. __create_error("assert", __VA_ARGS__)
  42. #define create_generic_error(...) \
  43. __create_error("generic", __VA_ARGS__)
  44. #define assert(condition, format, ...) \
  45. do { \
  46. if (!(condition)) { \
  47. char* msg; \
  48. print_to_string(&msg, format, __VA_ARGS__); \
  49. create_assertion_error("Assertion-error: %s\n" \
  50. " condition: %s\n" \
  51. " in: %s:%d", \
  52. msg, #condition, \
  53. __FILE__, __LINE__ ); \
  54. } \
  55. } while(0)
  56. #define log_location() \
  57. do { \
  58. printf("in"); \
  59. s32 spacing = 30-((s32)strlen(__FILE__) + (s32)log10(__LINE__)); \
  60. if (spacing < 1) spacing = 1; \
  61. for (s32 i = 0; i < spacing;++i) \
  62. printf(" "); \
  63. printf("%s (%d) ", __FILE__, __LINE__); \
  64. printf("-> %s\n", __FUNCTION__); \
  65. } while(0)
  66. #define if_error_log_location_and_return(val) \
  67. do { \
  68. if (error) { \
  69. log_location(); \
  70. return val; \
  71. } \
  72. } while(0)
  73. #define try_or_else_return(val) \
  74. if (1) \
  75. goto label(body,__LINE__); \
  76. else \
  77. while (1) \
  78. if (1) { \
  79. if (error) { \
  80. log_location(); \
  81. return val; \
  82. } \
  83. break; \
  84. } \
  85. else label(body,__LINE__):
  86. #define check_error_struct try_or_else_return({})
  87. #define check_error_void try_or_else_return(;)
  88. #define check_error try_or_else_return(0)