2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 line
2.0 KiB

  1. #include "IZ_intercept.h"
  2. typedef enum {
  3. IZ_LOG_INTERCEPT_EXTRACT_STATE_INITIAL,
  4. IZ_LOG_INTERCEPT_EXTRACT_STATE_OPEN_BRACKET,
  5. IZ_LOG_INTERCEPT_EXTRACT_STATE_CLOSE_BRACKET,
  6. IZ_LOG_INTERCEPT_EXTRACT_STATE_SPACE,
  7. IZ_LOG_INTERCEPT_EXTRACT_STATE_MESSAGE,
  8. } IZ_LogInterceptExtractState;
  9. void IZ_LogInterceptExtractWSMessageToFormatted(const char* raw_line, char* formatted_line) {
  10. IZ_LogInterceptExtractState state = IZ_LOG_INTERCEPT_EXTRACT_STATE_INITIAL;
  11. unsigned int offset = 0;
  12. IZ_memset(formatted_line, 0, 1024);
  13. for (unsigned int i = 0; i < 1024; i += 1) {
  14. if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_INITIAL && raw_line[i] == '[') {
  15. state = IZ_LOG_INTERCEPT_EXTRACT_STATE_OPEN_BRACKET;
  16. continue;
  17. }
  18. if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_OPEN_BRACKET && raw_line[i] == ']') {
  19. state = IZ_LOG_INTERCEPT_EXTRACT_STATE_CLOSE_BRACKET;
  20. continue;
  21. }
  22. if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_CLOSE_BRACKET && raw_line[i] == ' ') {
  23. offset = i + 1;
  24. state = IZ_LOG_INTERCEPT_EXTRACT_STATE_SPACE;
  25. continue;
  26. }
  27. if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_SPACE) {
  28. if (isspace(raw_line[i])) {
  29. continue;
  30. }
  31. state = IZ_LOG_INTERCEPT_EXTRACT_STATE_MESSAGE;
  32. }
  33. if (raw_line[i] == '\r' || raw_line[i] == '\n' || raw_line[i] == '\0') {
  34. formatted_line[i - offset] = '\0';
  35. break;
  36. }
  37. if (iscntrl(raw_line[i])) {
  38. continue;
  39. }
  40. formatted_line[i - offset] = raw_line[i];
  41. }
  42. }
  43. void IZ_LogInterceptHandleFromWS(i32 level, const char* line) {
  44. static char buffer[1024];
  45. IZ_LogInterceptExtractWSMessageToFormatted(line, buffer);
  46. switch (level) {
  47. // TODO level is a bit field...check if each level is OR'd.
  48. case LLL_ERR:
  49. IZ_LogError("net/ws", "%s", buffer);
  50. return;
  51. case LLL_WARN:
  52. IZ_LogWarn(true, "net/ws", "%s", buffer);
  53. return;
  54. case LLL_NOTICE:
  55. IZ_LogWarn(false, "net/ws", "%s", buffer);
  56. return;
  57. case LLL_USER:
  58. default:
  59. break;
  60. }
  61. IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "net/ws", "%s", buffer);
  62. }
  63. void IZ_LogInterceptWSMessages(i32 level) {
  64. lws_set_log_level(level, IZ_LogInterceptHandleFromWS);
  65. }