Skip to content

Commit

Permalink
picoev: support -d picoev_verbose_errors, do not use any naked `epr…
Browse files Browse the repository at this point in the history
…intln` by default, since that is a low level module
  • Loading branch information
spytheman committed Oct 29, 2024
1 parent 45cbf4f commit 3939737
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 6 additions & 0 deletions vlib/picoev/errors.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module picoev

@[if picoev_verbose_errors ?]
fn elog(msg string) {
eprintln(msg)
}
20 changes: 10 additions & 10 deletions vlib/picoev/picoev.v
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn (mut pv Picoev) add(fd int, events int, timeout int, callback voidptr) in

if pv.update_events(fd, events | picoev_add) != 0 {
if pv.delete(fd) != 0 {
eprintln('Error during del')
elog('Error during del')
}

return -1
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn (mut pv Picoev) delete(fd int) int {
}

if pv.update_events(fd, picoev_del) != 0 {
eprintln('Error during update_events. event: `picoev.picoev_del`')
elog('Error during update_events. event: `picoev.picoev_del`')
return -1
}

Expand All @@ -158,7 +158,7 @@ fn (mut pv Picoev) loop_once(max_wait_in_sec int) int {
pv.loop.now = get_time()

if pv.poll_once(max_wait_in_sec) != 0 {
eprintln('Error during poll_once')
elog('Error during poll_once')
return -1
}

Expand Down Expand Up @@ -216,7 +216,7 @@ fn accept_callback(listen_fd int, events int, cb_arg voidptr) {
return
}

eprintln('Error during accept')
elog('Error during accept')
return
}

Expand All @@ -231,7 +231,7 @@ fn accept_callback(listen_fd int, events int, cb_arg voidptr) {
}

setup_sock(accepted_fd) or {
eprintln('setup_sock failed, fd: ${accepted_fd}, listen_fd: ${listen_fd}, err: ${err.code()}')
elog('setup_sock failed, fd: ${accepted_fd}, listen_fd: ${listen_fd}, err: ${err.code()}')
pv.error_callback(pv.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{},
err)
close_socket(accepted_fd) // Close fd on failure
Expand All @@ -244,7 +244,7 @@ fn accept_callback(listen_fd int, events int, cb_arg voidptr) {
@[inline]
pub fn (mut pv Picoev) close_conn(fd int) {
if pv.delete(fd) != 0 {
eprintln('Error during del')
elog('Error during del')
}
close_socket(fd)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ fn raw_callback(fd int, events int, context voidptr) {
return
}

eprintln('Error during req_read')
elog('Error during req_read')

// fatal error
pv.close_conn(fd)
Expand Down Expand Up @@ -344,14 +344,14 @@ fn raw_callback(fd int, events int, context voidptr) {
}

fn default_error_callback(data voidptr, req picohttpparser.Request, mut res picohttpparser.Response, error IError) {
eprintln('picoev: ${error}')
elog('picoev: ${error}')
res.end()
}

// new creates a `Picoev` struct and initializes the main loop
pub fn new(config Config) !&Picoev {
listening_socket_fd := listen(config) or {
eprintln('Error during listen: ${err}')
elog('Error during listen: ${err}')
return err
}

Expand Down Expand Up @@ -384,7 +384,7 @@ pub fn new(config Config) !&Picoev {
}

if pv.loop == unsafe { nil } {
eprintln('Failed to create loop')
elog('Failed to create loop')
close_socket(listening_socket_fd)
return unsafe { nil }
}
Expand Down

0 comments on commit 3939737

Please sign in to comment.