summaryrefslogtreecommitdiff
path: root/src/utils/log.c
blob: e1822c0bded48e7758f23a46127cc2fa3aebb93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "log.h"

#include "utils/file.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void _printLogBack(const char *format, const char *file, int line, ...) {
  va_list args;
  va_start(args, end);
  char *errorStr;
  vasprintf(&errorStr, format, args);

  printf("\e[0;31mError: %s at compiler %s:%d\e[0m\n", errorStr, file, line);
  free(errorStr);
}

void _printErrorBack(const char *format, const char *file, int line,
                     char *begin, char *end, ...) {
  va_list args;
  va_start(args, end);
  char *errorStr;
  vasprintf(&errorStr, format, args);

  size_t file_index = SIZE_MAX;
  for (size_t i = 0; i < fileCodes_length; ++i) {
    char *fileCode_begin = fileCodes[i];
    char *fileCode_end = fileCodes[i] + strlen(fileCodes[i]);
    if (begin >= fileCode_begin && end <= fileCode_end) {
      file_index = i;
      break;
    }
  }

  const char firstColor[] = "\e[0;36m";
  const char secondColor[] = "\e[0;31m";
  fprintf(stderr, "%sError: %s at compiler %s:%d\e[0m\n", firstColor, errorStr,
          file, line);
  free(errorStr);

  if (file_index == SIZE_MAX) {
    return;
  }

  size_t file_line = 1;
  char *file_line_begin = fileCodes[file_index];
  char *file_line_end = fileCodes[file_index];
  for (char *iter = fileCodes[file_index]; *iter != '\0'; ++iter) {
    if (*iter == '\n') {
      if (iter <= begin) {
        file_line_begin = iter + 1;
      } else if (iter >= end) {
        file_line_end = iter;
      }
      if (iter <= end) {
        ++file_line;
      }
    }
  }

  fprintf(stderr, "\e%sAt %s:%ld\n", firstColor, fileCodes_names[file_index],
          file_line);

  for (char *iter = file_line_begin; iter < file_line_end; ++iter) {
    if (iter == begin) {
      fprintf(stderr, "%s", secondColor);
    } else if (iter == end) {
      fprintf(stderr, "%s", firstColor);
    }
    fprintf(stderr, "%c", *iter);
  }
  fprintf(stderr, "\e[0m");
}