My Project
Loading...
Searching...
No Matches
gwlog.c File Reference
#include "gwion_util.h"
#include "gwion_ast.h"
#include "parser.h"

Go to the source code of this file.

Functions

void gwion_parser_set_default_pos (const pos_t pos)
 
ANN void pos_ini (pos_t *pos)
 
static void _gwlog_error (const char *main, const char *explain, const char *filename, const loc_t loc, const uint error_code, const enum libprettyerr_errtype errtype)
 
static ANN void _gwlog_warning (const char *main, const char *filename, const loc_t loc)
 
static ANN void _gwlog_related (const char *main, const char *filename, const loc_t loc)
 
static ANN void _gwlog_hint (const char *main, const char *filename NUSED, const loc_t loc NUSED)
 
ANN void gwlog_set_func (gwlog_error_function_t error, gwlog_warning_function_t warning, gwlog_warning_function_t related, gwlog_warning_function_t hint)
 
static ANN char * get_src (const char *filename, const loc_t loc)
 
static const char * get_filename (const char *filename)
 
static void nosrc (const perr_printer_t *printer, const perr_t *err, const char *main, const char *explain)
 
void gwlog_error (const char *main, const char *explain, const char *filename, const loc_t loc, const uint error_code)
 
static ANN void _gwlog_secondary (const char *main, const char *filename, const enum libprettyerr_errtype type, const loc_t loc)
 
ANN void gwlog_warning (const char *main, const char *filename, const loc_t loc)
 
ANN void gwlog_related (const char *main, const char *filename, const loc_t loc)
 
ANN void gwlog_hint (const char *main, const char *filename, const loc_t loc)
 

Variables

static pos_t default_pos = { .line = 1, .column = 1 }
 
static gwlog_error_function_t _error = _gwlog_error
 
static gwlog_warning_function_t _warning = _gwlog_warning
 
static gwlog_warning_function_t _related = _gwlog_related
 
static gwlog_warning_function_t _hint = _gwlog_hint
 

Function Documentation

◆ _gwlog_error()

static void _gwlog_error ( const char * main,
const char * explain,
const char * filename,
const loc_t loc,
const uint error_code,
const enum libprettyerr_errtype errtype )
static

Definition at line 85 of file gwlog.c.

87 {
88#ifdef __FUZZING__
89 return;
90#endif
91 char *line = get_src(filename, loc);
92
93 perr_printer_t printer;
94 perr_printer_init(&printer, stderr, line,
95 true, // use utf8,
96 perr_runner_basic_style);
97
98 printer.rounded = true;
99
100 const size_t sz = line ? (loc.last.line == loc.first.line
101 ? (size_t)(loc.last.column - loc.first.column)
102 : strlen(line) - loc.first.column) : 0;
103 const perr_t err = PERR_Error(
104 errtype, PERR_Str(loc.first.line, line),
105 PERR_Pos(loc.first.column - 1, sz), main,
106 explain, error_code, get_filename(filename));
107
108 if (line) {
109 perr_print_error(&printer, &err);
110 xfree(line);
111 } else
112 nosrc(&printer, &err, main, explain);
113}
static ANN char * get_src(const char *filename, const loc_t loc)
Definition gwlog.c:40
static const char * get_filename(const char *filename)
Definition gwlog.c:55
static void nosrc(const perr_printer_t *printer, const perr_t *err, const char *main, const char *explain)
Definition gwlog.c:68
int main(int argc, char **argv)
Definition main.c:4
struct pos_t last
Definition gwlog.h:17
struct pos_t first
Definition gwlog.h:16
short unsigned int column
Definition gwlog.h:9
short unsigned int line
Definition gwlog.h:8

◆ _gwlog_hint()

static ANN void _gwlog_hint ( const char * main,
const char *filename NUSED,
const loc_t loc NUSED )
static

Definition at line 20 of file gwlog.c.

21 {
22 gw_err("{-}hint:{0} %s\n", main);
23}

◆ _gwlog_related()

ANN void _gwlog_related ( const char * main,
const char * filename,
const loc_t loc )
static

Definition at line 151 of file gwlog.c.

152 {
153 _gwlog_secondary(main, filename, PERR_INFO, loc);
154}
static ANN void _gwlog_secondary(const char *main, const char *filename, const enum libprettyerr_errtype type, const loc_t loc)
Definition gwlog.c:120

◆ _gwlog_secondary()

static ANN void _gwlog_secondary ( const char * main,
const char * filename,
const enum libprettyerr_errtype type,
const loc_t loc )
static

Definition at line 120 of file gwlog.c.

122 {
123 perr_printer_t printer;
124 char * line = get_src(filename, loc);
125
126 perr_printer_init(&printer, stderr, line,
127 true, // use utf8,
128 perr_runner_secondary_style);
129 printer.rounded = true;
130
131 const perr_t err = PERR_Secondary(
132 type, PERR_Str(loc.first.line, line),
133 PERR_Pos(loc.first.column - 1, loc.last.column - loc.first.column), main,
134 get_filename(filename));
135 if (line) {
136 perr_print_error(&printer, &err);
137 xfree(line);
138 } else
139 nosrc(&printer, &err, main, NULL);
140}
return NULL
Definition macro.c:41

◆ _gwlog_warning()

ANN void _gwlog_warning ( const char * main,
const char * filename,
const loc_t loc )
static

Definition at line 142 of file gwlog.c.

143 {
144 _gwlog_secondary(main, filename, PERR_WARNING, loc);
145}

◆ get_filename()

static const char * get_filename ( const char * filename)
inlinestatic

Definition at line 55 of file gwlog.c.

55 {
56#ifndef BUILD_ON_WINDOWS
57 const char *pwd = getenv("PWD");
58 if(!pwd) return filename;
59#else
60 TCHAR pwd[MAX_PATH];
61 GetCurrentDirectory(MAX_PATH, pwd);
62#endif
63 size_t sz = strlen(pwd);
64 return !strncmp(pwd, filename, sz - 1) ? filename + sz + 1 : filename;
65}

◆ get_src()

static ANN char * get_src ( const char * filename,
const loc_t loc )
static

Definition at line 40 of file gwlog.c.

40 {
41 char * line = NULL;
42 size_t len = 0;
43 uint i = 0;
44 FILE * f = fopen(filename, "r");
45 if (!f) return NULL;
46 fseek(f, 0, SEEK_SET);
47 ssize_t ret;
48 while ((ret = getline(&line, &len, f)) != -1 && ++i < loc.first.line);
49 fclose(f);
50 if (ret != -1) return line;
51 if(line) xfree(line);
52 return NULL;
53}

◆ gwion_parser_set_default_pos()

void gwion_parser_set_default_pos ( const pos_t pos)

Definition at line 7 of file gwlog.c.

7 {
8 default_pos = pos;
9}
static pos_t default_pos
Definition gwlog.c:5

◆ gwlog_error()

void gwlog_error ( const char * main,
const char * explain,
const char * filename,
const loc_t loc,
const uint error_code )

Definition at line 115 of file gwlog.c.

116 {
117 _error(main, explain, filename, loc, error_code, PERR_ERROR);
118}
static gwlog_error_function_t _error
Definition gwlog.c:25

◆ gwlog_hint()

ANN void gwlog_hint ( const char * main,
const char * filename,
const loc_t loc )

Definition at line 160 of file gwlog.c.

161 {
162 _hint(main, filename, loc);
163}
static gwlog_warning_function_t _hint
Definition gwlog.c:28

◆ gwlog_related()

ANN void gwlog_related ( const char * main,
const char * filename,
const loc_t loc )

Definition at line 155 of file gwlog.c.

156 {
157 _related(main, filename, loc);
158}
static gwlog_warning_function_t _related
Definition gwlog.c:27

◆ gwlog_set_func()

ANN void gwlog_set_func ( gwlog_error_function_t error,
gwlog_warning_function_t warning,
gwlog_warning_function_t related,
gwlog_warning_function_t hint )

Definition at line 30 of file gwlog.c.

33 {
34 _error = error;
35 _warning = warning;
36 _related = related;
37 _hint = hint;
38}
static gwlog_warning_function_t _warning
Definition gwlog.c:26

◆ gwlog_warning()

ANN void gwlog_warning ( const char * main,
const char * filename,
const loc_t loc )

Definition at line 146 of file gwlog.c.

147 {
148 _warning(main, filename, loc);
149}

◆ nosrc()

static void nosrc ( const perr_printer_t * printer,
const perr_t * err,
const char * main,
const char * explain )
static

Definition at line 68 of file gwlog.c.

69 {
70 size_t len;
71 char base[16];
72 char color[16];
73 strcpy(base, "+R");
74 const int status = tcol_color_parse(color, 16, base, 2, &len);
75 if (status != TermColorErrorNone)
76 color[0] = 0;
77 else
78 color[len] = 0;
79 gw_err("[internal]\n");
80 perr_print_line_number(printer, err, color);
81 gw_err("%s\n", main);
82 if (explain) gw_err("%s\n", explain);
83}

◆ pos_ini()

ANN void pos_ini ( pos_t * pos)

Definition at line 11 of file gwlog.c.

11{ *pos = default_pos; }

Variable Documentation

◆ _error

Definition at line 25 of file gwlog.c.

◆ _hint

Definition at line 28 of file gwlog.c.

◆ _related

Definition at line 27 of file gwlog.c.

◆ _warning

Definition at line 26 of file gwlog.c.

◆ default_pos

pos_t default_pos = { .line = 1, .column = 1 }
static

Definition at line 5 of file gwlog.c.

5{ .line = 1, .column = 1 };