Skip to content

Commit fcb229a

Browse files
committed
stderr timestamps for easier logging
1 parent 18aa3dc commit fcb229a

File tree

10 files changed

+54
-48
lines changed

10 files changed

+54
-48
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#### Updates
1111

12+
* Common format for NNG errors now starts with a timestamp for easier logging.
1213
* `listen()` and `dial()` now return (invisible) zero rather than NULL upon success to better align with similar functions.
1314
* Allows setting the environment variable 'NANONEXT_ARM' prior to package installation
1415
+ Fixes installation issues on certain ARM architectures

R/aio.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ call_aio <- function(aio, block = TRUE) {
7878
keep.raw <- .subset2(aio, "callparams")[[2L]]
7979
if (keep.raw) aio[["raw"]] <- res
8080
is.integer(res) && {
81-
message(res, " : ", nng_error(res))
81+
message(Sys.time(), " | ", res, " : ", nng_error(res))
8282
return(invisible(aio))
8383
}
8484
on.exit(expr = {
@@ -107,9 +107,8 @@ call_aio <- function(aio, block = TRUE) {
107107
}
108108
aio[["result"]] <- res
109109
rm("aio", envir = aio)
110-
if (res) {
111-
message(res, " : ", nng_error(res))
112-
}
110+
if (res) message(Sys.time(), " | ", res, " : ", nng_error(res))
111+
113112
}
114113

115114
invisible(aio)

R/context.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ context <- function(socket) {
4545

4646
if (is.environment(socket)) socket <- .subset2(socket, "socket")
4747
res <- .Call(rnng_ctx_open, socket)
48-
if (is.integer(res)) message(res, " : ", nng_error(res))
48+
if (is.integer(res)) message(Sys.time(), " | ", res, " : ", nng_error(res))
4949
res
5050

5151
}
@@ -90,7 +90,7 @@ send_ctx <- function(context, data, mode = c("serial", "raw"), timeout, echo = T
9090
raw = if (is.raw(data)) data else writeBin(object = data, con = raw()))
9191
res <- .Call(rnng_ctx_send, context, data, timeout)
9292
is.integer(res) && {
93-
message(res, " : ", nng_error(res))
93+
message(Sys.time(), " | ", res, " : ", nng_error(res))
9494
return(invisible(res))
9595
}
9696
if (missing(echo) || isTRUE(echo)) res else invisible(0L)
@@ -142,7 +142,7 @@ recv_ctx <- function(context,
142142
if (missing(timeout)) timeout <- -2L
143143
res <- .Call(rnng_ctx_recv, context, timeout)
144144
is.integer(res) && {
145-
message(res, " : ", nng_error(res))
145+
message(Sys.time(), " | ", res, " : ", nng_error(res))
146146
return(invisible(res))
147147
}
148148
on.exit(expr = return(res))
@@ -228,7 +228,7 @@ reply <- function(context,
228228
if (missing(timeout)) timeout <- -2L
229229
res <- .Call(rnng_ctx_recv, context, timeout)
230230
is.integer(res) && {
231-
message(res, " : ", nng_error(res))
231+
message(Sys.time(), " | ", res, " : ", nng_error(res))
232232
return(invisible(res))
233233
}
234234
on.exit(expr = send_aio(context, as.raw(0L), mode = send_mode))
@@ -244,7 +244,7 @@ reply <- function(context,
244244
on.exit()
245245
res <- .Call(rnng_ctx_send, context, data, timeout)
246246
is.integer(res) && {
247-
message(res, " : ", nng_error(res))
247+
message(Sys.time(), " | ", res, " : ", nng_error(res))
248248
return(invisible(res))
249249
}
250250
invisible(0L)
@@ -311,12 +311,12 @@ request <- function(context,
311311
raw = if (is.raw(data)) data else writeBin(object = data, con = raw()))
312312
res <- .Call(rnng_send_aio, context, data, -2L)
313313
is.integer(res) && {
314-
message(res, " : ", nng_error(res))
314+
message(Sys.time(), " | ", res, " : ", nng_error(res))
315315
return(invisible(res))
316316
}
317317
res <- .Call(rnng_recv_aio, context, timeout)
318318
is.integer(res) && {
319-
message(res, " : ", nng_error(res))
319+
message(Sys.time(), " | ", res, " : ", nng_error(res))
320320
return(invisible(res))
321321
}
322322
env <- `class<-`(new.env(), "recvAio")

R/listdial.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ dial <- function(socket,
7878
if (missing(autostart) || isTRUE(autostart)) {
7979
res <- .Call(rnng_dial, .subset2(socket, "socket"), url)
8080
if (is.integer(res)) {
81-
message(res, " : ", nng_error(res))
81+
message(Sys.time(), " | ", res, " : ", nng_error(res))
8282
return(res)
8383
}
8484
} else {
8585
res <- .Call(rnng_dialer_create, .subset2(socket, "socket"), url)
8686
if (is.integer(res)) {
87-
message(res, " : ", nng_error(res))
87+
message(Sys.time(), " | ", res, " : ", nng_error(res))
8888
return(res)
8989
}
9090
}
@@ -103,13 +103,13 @@ dial <- function(socket,
103103
if (missing(autostart) || isTRUE(autostart)) {
104104
res <- .Call(rnng_dial, socket, url)
105105
if (is.integer(res)) {
106-
message(res, " : ", nng_error(res))
106+
message(Sys.time(), " | ", res, " : ", nng_error(res))
107107
return(res)
108108
}
109109
} else {
110110
res <- .Call(rnng_dialer_create, socket, url)
111111
if (is.integer(res)) {
112-
message(res, " : ", nng_error(res))
112+
message(Sys.time(), " | ", res, " : ", nng_error(res))
113113
return(res)
114114
}
115115
}
@@ -199,13 +199,13 @@ listen <- function(socket,
199199
if (missing(autostart) || isTRUE(autostart)) {
200200
res <- .Call(rnng_listen, .subset2(socket, "socket"), url)
201201
if (is.integer(res)) {
202-
message(res, " : ", nng_error(res))
202+
message(Sys.time(), " | ", res, " : ", nng_error(res))
203203
return(res)
204204
}
205205
} else {
206206
res <- .Call(rnng_listener_create, .subset2(socket, "socket"), url)
207207
if (is.integer(res)) {
208-
message(res, " : ", nng_error(res))
208+
message(Sys.time(), " | ", res, " : ", nng_error(res))
209209
return(res)
210210
}
211211
}
@@ -224,13 +224,13 @@ listen <- function(socket,
224224
if (missing(autostart) || isTRUE(autostart)) {
225225
res <- .Call(rnng_listen, socket, url)
226226
if (is.integer(res)) {
227-
message(res, " : ", nng_error(res))
227+
message(Sys.time(), " | ", res, " : ", nng_error(res))
228228
return(res)
229229
}
230230
} else {
231231
res <- .Call(rnng_listener_create, socket, url)
232232
if (is.integer(res)) {
233-
message(res, " : ", nng_error(res))
233+
message(Sys.time(), " | ", res, " : ", nng_error(res))
234234
return(res)
235235
}
236236
}

R/methods.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ NULL
2727
start.nanoListener <- function(x, ...) {
2828

2929
xc <- .Call(rnng_listener_start, x)
30-
if (xc) message(xc, " : ", nng_error(xc))
30+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
3131
invisible(xc)
3232

3333
}
@@ -39,7 +39,7 @@ start.nanoListener <- function(x, ...) {
3939
start.nanoDialer <- function(x, async = TRUE, ...) {
4040

4141
xc <- .Call(rnng_dialer_start, x, async)
42-
if (xc) message(xc, " : ", nng_error(xc))
42+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
4343
invisible(xc)
4444

4545
}
@@ -80,7 +80,7 @@ NULL
8080
close.nanoSocket <- function(con, ...) {
8181

8282
xc <- .Call(rnng_close, con)
83-
if (xc) message(xc, " : ", nng_error(xc))
83+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
8484
invisible(xc)
8585

8686
}
@@ -92,7 +92,7 @@ close.nanoSocket <- function(con, ...) {
9292
close.nanoContext <- function(con, ...) {
9393

9494
xc <- .Call(rnng_ctx_close, con)
95-
if (xc) message(xc, " : ", nng_error(xc))
95+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
9696
invisible(xc)
9797

9898
}
@@ -104,7 +104,7 @@ close.nanoContext <- function(con, ...) {
104104
close.nanoDialer <- function(con, ...) {
105105

106106
xc <- .Call(rnng_dialer_close, con)
107-
if (xc) message(xc, " : ", nng_error(xc))
107+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
108108
invisible(xc)
109109

110110
}
@@ -116,7 +116,7 @@ close.nanoDialer <- function(con, ...) {
116116
close.nanoListener <- function(con, ...) {
117117

118118
xc <- .Call(rnng_listener_close, con)
119-
if (xc) message(xc, " : ", nng_error(xc))
119+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
120120
invisible(xc)
121121

122122
}

R/opts.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ setopt.nanoSocket <- function(object,
5353
string = .Call(rnng_socket_set_string, object, opt, value),
5454
uint64 = .Call(rnng_socket_set_uint64, object, opt, value))
5555

56-
if (xc) message(xc, " : ", nng_error(xc))
56+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
5757
invisible(xc)
5858

5959
}
@@ -83,7 +83,7 @@ setopt.nanoDialer <- function(object,
8383
string = .Call(rnng_dialer_set_string, object, opt, value),
8484
uint64 = .Call(rnng_dialer_set_uint64, object, opt, value))
8585

86-
if (xc) message(xc, " : ", nng_error(xc))
86+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
8787
invisible(xc)
8888

8989
}
@@ -113,7 +113,7 @@ setopt.nanoListener <- function(object,
113113
string = .Call(rnng_listener_set_string, object, opt, value),
114114
uint64 = .Call(rnng_listener_set_uint64, object, opt, value))
115115

116-
if (xc) message(xc, " : ", nng_error(xc))
116+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
117117
invisible(xc)
118118

119119
}
@@ -144,7 +144,7 @@ setopt.nanoContext <- function(object,
144144
string = .Call(rnng_ctx_set_string, object, opt, value),
145145
uint64 = .Call(rnng_ctx_set_uint64, object, opt, value))
146146

147-
if (xc) message(xc, " : ", nng_error(xc))
147+
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
148148
invisible(xc)
149149

150150
}

R/sendrecv.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ send <- function(socket,
4242
raw = if (is.raw(data)) data else writeBin(object = data, con = raw()))
4343
res <- .Call(rnng_send, socket, data, block)
4444
is.integer(res) && {
45-
message(res, " : ", nng_error(res))
45+
message(Sys.time(), " | ", res, " : ", nng_error(res))
4646
return(invisible(res))
4747
}
4848
if (missing(echo) || isTRUE(echo)) res else invisible(0L)
@@ -91,7 +91,7 @@ send_aio <- function(socket, data, mode = c("serial", "raw"), timeout) {
9191
raw = if (is.raw(data)) data else writeBin(object = data, con = raw()))
9292
res <- .Call(rnng_send_aio, socket, data, timeout)
9393
is.integer(res) && {
94-
message(res, " : ", nng_error(res))
94+
message(Sys.time(), " | ", res, " : ", nng_error(res))
9595
return(invisible(res))
9696
}
9797
env <- `class<-`(new.env(), "sendAio")
@@ -153,7 +153,7 @@ recv <- function(socket,
153153
mode <- match.arg(mode)
154154
res <- .Call(rnng_recv, socket, block)
155155
is.integer(res) && {
156-
message(res, " : ", nng_error(res))
156+
message(Sys.time(), " | ", res, " : ", nng_error(res))
157157
return(invisible(res))
158158
}
159159
on.exit(expr = return(res))
@@ -220,7 +220,7 @@ recv_aio <- function(socket,
220220
if (missing(timeout)) timeout <- -2L
221221
res <- .Call(rnng_recv_aio, socket, timeout)
222222
is.integer(res) && {
223-
message(res, " : ", nng_error(res))
223+
message(Sys.time(), " | ", res, " : ", nng_error(res))
224224
return(invisible(res))
225225
}
226226
env <- `class<-`(new.env(), "recvAio")

R/socket.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ socket <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
6464

6565
protocol <- match.arg(protocol)
6666
res <- .Call(rnng_protocol_open, protocol)
67-
if (is.integer(res)) message(res, " : ", nng_error(res))
67+
if (is.integer(res)) message(Sys.time(), " | ", res, " : ", nng_error(res))
6868
if (!missing(dial)) {
6969
dial(res, url = dial, autostart = autostart)
7070
}
@@ -114,8 +114,11 @@ socket <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
114114
subscribe <- function(socket, topic = NULL) {
115115

116116
xc <- .Call(rnng_socket_set_string, socket, "sub:subscribe" , topic)
117-
if (xc) message(xc, " : ", nng_error(xc)) else message("subscribed topic: ",
118-
if (is.null(topic)) "ALL" else topic)
117+
if (xc) {
118+
message(Sys.time(), " | ", xc, " : ", nng_error(xc))
119+
} else {
120+
cat("subscribed topic: ", if (is.null(topic)) "ALL" else topic)
121+
}
119122
invisible(xc)
120123

121124
}
@@ -163,8 +166,11 @@ subscribe <- function(socket, topic = NULL) {
163166
unsubscribe <- function(socket, topic = NULL) {
164167

165168
xc <- .Call(rnng_socket_set_string, socket, "sub:unsubscribe" , topic)
166-
if (xc) message(xc, " : ", nng_error(xc)) else message("unsubscribed topic: ",
167-
if (is.null(topic)) "ALL" else topic)
169+
if (xc) {
170+
message(Sys.time(), " | ", xc, " : ", nng_error(xc))
171+
} else {
172+
cat("unsubscribed topic: ", if (is.null(topic)) "ALL" else topic)
173+
}
168174
invisible(xc)
169175

170176
}

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ ncurl <- function(http, ...) {
133133
res <- .Call(rnng_ncurl, http, args)
134134
missing(res) && return(invisible())
135135
if (is.integer(res)) {
136-
message(res, " : ", nng_error(res))
136+
message(Sys.time(), " | ", res, " : ", nng_error(res))
137137
return(invisible(res))
138138
} else if (is.character(res)) {
139139
continue <- if (interactive()) readline(paste0("Follow redirect to <", res, ">? [Y/n] ")) else "n"

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ aio
333333
#> < recvAio >
334334
#> - $data for message data
335335
str(aio$data)
336-
#> num [1:100000000] -0.123 -0.79 -0.694 -0.84 -1.527 ...
336+
#> num [1:100000000] 0.298 0.163 0.278 1.051 0.654 ...
337337
```
338338

339339
In this example the calculation is returned, but other operations may
@@ -358,27 +358,27 @@ pub <- socket("pub", listen = "inproc://nanobroadcast")
358358
sub <- socket("sub", dial = "inproc://nanobroadcast")
359359

360360
sub |> subscribe(topic = "examples")
361-
#> subscribed topic: examples
361+
#> subscribed topic: examples
362362
pub |> send(c("examples", "this is an example"), mode = "raw", echo = FALSE)
363363
sub |> recv(mode = "character", keep.raw = FALSE)
364364
#> [1] "examples" "this is an example"
365365

366366
pub |> send(c("other", "this other topic will not be received"), mode = "raw", echo = FALSE)
367367
sub |> recv(mode = "character", keep.raw = FALSE)
368-
#> 8 : Try again
368+
#> 2022-02-27 23:51:22 | 8 : Try again
369369

370370
# specify NULL to subscribe to ALL topics
371371
sub |> subscribe(topic = NULL)
372-
#> subscribed topic: ALL
372+
#> subscribed topic: ALL
373373
pub |> send(c("newTopic", "this is a new topic"), mode = "raw", echo = FALSE)
374374
sub |> recv("character", keep.raw = FALSE)
375375
#> [1] "newTopic" "this is a new topic"
376376

377377
sub |> unsubscribe(topic = NULL)
378-
#> unsubscribed topic: ALL
378+
#> unsubscribed topic: ALL
379379
pub |> send(c("newTopic", "this topic will now not be received"), mode = "raw", echo = FALSE)
380380
sub |> recv("character", keep.raw = FALSE)
381-
#> 8 : Try again
381+
#> 2022-02-27 23:51:22 | 8 : Try again
382382

383383
# however the topics explicitly subscribed to are still received
384384
pub |> send(c("examples", "this example will still be received"), mode = "raw", echo = FALSE)
@@ -402,11 +402,11 @@ ncurl("http://httpbin.org/headers")
402402
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
403403
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
404404
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
405-
#> [76] 2d 36 32 31 34 61 61 62 34 2d 37 66 66 39 38 64 34 62 30 36 62 62 39 38 38
406-
#> [101] 65 31 36 30 33 36 66 39 66 22 0a 20 20 7d 0a 7d 0a
405+
#> [76] 2d 36 32 31 63 30 65 37 61 2d 35 36 36 30 35 32 63 63 33 66 61 65 64 30 63
406+
#> [101] 39 35 31 38 61 62 30 34 30 22 0a 20 20 7d 0a 7d 0a
407407
#>
408408
#> $data
409-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-6214aab4-7ff98d4b06bb988e16036f9f\"\n }\n}\n"
409+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-621c0e7a-566052cc3faed0c9518ab040\"\n }\n}\n"
410410
```
411411

412412
For advanced use, supports additional HTTP methods such as POST or PUT.

0 commit comments

Comments
 (0)