Skip to content

Commit 95ef0b7

Browse files
committed
tidy up logging
1 parent 44b12cc commit 95ef0b7

File tree

9 files changed

+84
-52
lines changed

9 files changed

+84
-52
lines changed

R/context.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ context <- function(socket, quietly = TRUE) {
5151
if (is.integer(res)) {
5252
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
5353
} else if (!missing(quietly) && !isTRUE(quietly)) {
54-
cat(format.POSIXct(Sys.time()), "[ context open ] id:",
55-
attr(res, "id"), "| socket:", attr(res, "socket"))
54+
cat(format.POSIXct(Sys.time()), "[ ctxt open ] id:",
55+
attr(res, "id"), "| sock:", attr(res, "socket"), "\n")
5656
}
5757
res
5858

R/listdial.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ dial <- function(socket,
8484
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
8585
return(invisible(res))
8686
}
87-
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ dialer start ]", url)
87+
if (!missing(quietly) && !isTRUE(quietly)) {
88+
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
89+
attr(res, "socket"), "| url:", url, "\n")
90+
}
8891
} else {
8992
res <- .Call(rnng_dialer_create, .subset2(socket, "socket"), url)
9093
if (is.integer(res)) {
@@ -110,7 +113,10 @@ dial <- function(socket,
110113
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
111114
return(invisible(res))
112115
}
113-
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ dialer start ]", url)
116+
if (!missing(quietly) && !isTRUE(quietly)) {
117+
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
118+
attr(res, "socket"), "| url:", url, "\n")
119+
}
114120
} else {
115121
res <- .Call(rnng_dialer_create, socket, url)
116122
if (is.integer(res)) {
@@ -208,7 +214,10 @@ listen <- function(socket,
208214
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
209215
return(invisible(res))
210216
}
211-
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ listener start ]", url)
217+
if (!missing(quietly) && !isTRUE(quietly)) {
218+
cat(format.POSIXct(Sys.time()), "[ list start ] sock:",
219+
attr(res, "socket"), "| url:", url, "\n")
220+
}
212221
} else {
213222
res <- .Call(rnng_listener_create, .subset2(socket, "socket"), url)
214223
if (is.integer(res)) {
@@ -234,7 +243,10 @@ listen <- function(socket,
234243
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
235244
return(invisible(res))
236245
}
237-
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ listener start ]", url)
246+
if (!missing(quietly) && !isTRUE(quietly)) {
247+
cat(format.POSIXct(Sys.time()), "[ list start ] sock:",
248+
attr(res, "socket"), "| url:", url, "\n")
249+
}
238250
} else {
239251
res <- .Call(rnng_listener_create, socket, url)
240252
if (is.integer(res)) {

R/methods.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ start.nanoListener <- function(x, quietly = TRUE, ...) {
3131
if (xc) {
3232
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
3333
} else if (!missing(quietly) && !isTRUE(quietly)) {
34-
cat(format.POSIXct(Sys.time()), "[ listener start ]", attr(x, "url"))
34+
cat(format.POSIXct(Sys.time()), "[ list start ] sock:",
35+
attr(x, "socket"), "| url:", attr(x, "url"), "\n")
3536
}
3637
invisible(xc)
3738

@@ -47,7 +48,8 @@ start.nanoDialer <- function(x, async = TRUE, quietly = TRUE, ...) {
4748
if (xc) {
4849
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
4950
} else if (!missing(quietly) && !isTRUE(quietly)) {
50-
cat(format.POSIXct(Sys.time()), "[ dialer start ]", attr(x, "url"))
51+
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
52+
attr(x, "socket"), "| url:", attr(x, "url"), "\n")
5153
}
5254
invisible(xc)
5355

@@ -95,8 +97,8 @@ close.nanoSocket <- function(con, quietly = TRUE, ...) {
9597
if (xc) {
9698
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
9799
} else if (!missing(quietly) && !isTRUE(quietly)) {
98-
cat(format.POSIXct(Sys.time()), "[ socket close ] id:",
99-
attr(con, "id"), "| protocol:", attr(con, "protocol"))
100+
cat(format.POSIXct(Sys.time()), "[ sock close ] id:",
101+
attr(con, "id"), "| protocol:", attr(con, "protocol"), "\n")
100102
}
101103
invisible(xc)
102104

@@ -112,8 +114,8 @@ close.nanoContext <- function(con, quietly = TRUE, ...) {
112114
if (xc) {
113115
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
114116
} else if (!missing(quietly) && !isTRUE(quietly)) {
115-
cat(format.POSIXct(Sys.time()), "[ context close ] id:",
116-
attr(con, "id"), "| socket:", attr(con, "socket"))
117+
cat(format.POSIXct(Sys.time()), "[ ctxt close ] id:",
118+
attr(con, "id"), "| sock:", attr(con, "socket"), "\n")
117119
}
118120
invisible(xc)
119121

@@ -129,7 +131,8 @@ close.nanoDialer <- function(con, quietly = TRUE, ...) {
129131
if (xc) {
130132
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
131133
} else if (!missing(quietly) && !isTRUE(quietly)) {
132-
cat(format.POSIXct(Sys.time()), "[ dialer close ]", attr(con, "url"))
134+
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
135+
attr(con, "socket"), "| url:", attr(con, "url"), "\n")
133136
}
134137

135138
invisible(xc)
@@ -146,7 +149,8 @@ close.nanoListener <- function(con, quietly = TRUE, ...) {
146149
if (xc) {
147150
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
148151
} else if (!missing(quietly) && !isTRUE(quietly)) {
149-
cat(format.POSIXct(Sys.time()), "[ listener close ]", attr(con, "url"))
152+
cat(format.POSIXct(Sys.time()), "[ list close ] sock:",
153+
attr(con, "socket"), "| url:", attr(con, "url"), "\n")
150154
}
151155

152156
invisible(xc)

R/nano.R

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ nano <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
9191
nano[["dial"]] <- function(url = "inproc://nanonext",
9292
autostart = TRUE,
9393
quietly = TRUE) dial(nano,
94-
url = url,
95-
autostart = autostart,
96-
quietly = quietly)
94+
url = url,
95+
autostart = autostart,
96+
quietly = quietly)
9797
nano[["listen"]] <- function(url = "inproc://nanonext",
9898
autostart = TRUE,
9999
quietly = TRUE) listen(nano,
100-
url = url,
101-
autostart = autostart,
102-
quietly = quietly)
100+
url = url,
101+
autostart = autostart,
102+
quietly = quietly)
103103
nano[["recv"]] <- function(mode = c("serial", "character", "complex", "double",
104104
"integer", "logical", "numeric", "raw"),
105105
block = FALSE,
@@ -137,10 +137,20 @@ nano <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
137137
value = value)
138138

139139
if (protocol == "sub") {
140-
nano[["subscribe"]] <- function(topic = NULL) subscribe(socket,
141-
topic = topic)
142-
nano[["unsubscribe"]] <- function(topic = NULL) unsubscribe(socket,
143-
topic = topic)
140+
nano[["subscribe"]] <- function(topic = NULL,
141+
quietly = TRUE) subscribe(socket,
142+
topic = topic,
143+
quietly = quietly)
144+
nano[["unsubscribe"]] <- function(topic = NULL,
145+
quietly = TRUE) unsubscribe(socket,
146+
topic = topic,
147+
quietly = quietly)
148+
}
149+
if (protocol == "surveyor") {
150+
nano[["survey_time"]] <- function(time,
151+
quietly = TRUE) survey_time(socket,
152+
time = time,
153+
quietly = quietly)
144154
}
145155

146156
nano

R/socket.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ socket <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
7171
if (is.integer(res)) {
7272
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
7373
} else if (!missing(quietly) && !isTRUE(quietly)) {
74-
cat(format.POSIXct(Sys.time()), "[ socket open ] id:",
75-
attr(res, "id"), "| protocol:", attr(res, "protocol"))
74+
cat(format.POSIXct(Sys.time()), "[ sock open ] id:",
75+
attr(res, "id"), "| protocol:", attr(res, "protocol"), "\n")
7676
}
7777
if (!missing(dial)) {
78-
dial(res, url = dial, autostart = autostart)
78+
dial(res, url = dial, autostart = autostart, quietly = quietly)
7979
}
8080
if (!missing(listen)) {
81-
listen(res, url = listen, autostart = autostart)
81+
listen(res, url = listen, autostart = autostart, quietly = quietly)
8282
}
8383
res
8484

@@ -128,7 +128,8 @@ subscribe <- function(socket, topic = NULL, quietly = TRUE) {
128128
if (xc) {
129129
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
130130
} else if (!missing(quietly) && !isTRUE(quietly)) {
131-
cat("subscribed topic:", if (is.null(topic)) "ALL" else topic)
131+
cat(format.POSIXct(Sys.time()), "[ subscribe ] sock:", attr(socket, "id"),
132+
"| topic:", if (is.null(topic)) "ALL" else topic, "\n")
132133
}
133134
invisible(xc)
134135

@@ -182,7 +183,8 @@ unsubscribe <- function(socket, topic = NULL, quietly = TRUE) {
182183
if (xc) {
183184
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
184185
} else if (!missing(quietly) && !isTRUE(quietly)) {
185-
cat("unsubscribed topic:", if (is.null(topic)) "ALL" else topic)
186+
cat(format.POSIXct(Sys.time()), "[ unsubscribe ] sock:", attr(socket, "id"),
187+
"| topic:", if (is.null(topic)) "ALL" else topic, "\n")
186188
}
187189
invisible(xc)
188190

@@ -196,6 +198,8 @@ unsubscribe <- function(socket, topic = NULL, quietly = TRUE) {
196198
#'
197199
#' @param socket a Socket or Context using the surveyor protocol.
198200
#' @param time the survey timeout in ms.
201+
#' @param quietly [default TRUE] if FALSE, confirmation of the setting is printed
202+
#' to the console (stdout) if successful, useful for logging purposes.
199203
#'
200204
#' @return Zero (invisibly) on success.
201205
#'
@@ -233,9 +237,13 @@ unsubscribe <- function(socket, topic = NULL, quietly = TRUE) {
233237
#'
234238
#' @export
235239
#'
236-
survey_time <- function(socket, time) {
240+
survey_time <- function(socket, time, quietly = TRUE) {
237241

238-
setopt(socket, type = "ms", opt = "surveyor:survey-time", value = time)
242+
res <- setopt(socket, type = "ms", opt = "surveyor:survey-time", value = time)
243+
if (!res && !missing(quietly) && !isTRUE(quietly)) {
244+
cat(format.POSIXct(Sys.time()), "[ survey ] sock:", attr(socket, "id"),
245+
"| set time:", time, "\n")
246+
}
239247

240248
}
241249

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ is_nul_byte <- function(x) {
7474
#'
7575
.mirai_scm <- function() {
7676

77-
identical(parent.env(parent.env(parent.frame())), getNamespace("mirai")) || return()
77+
identical(parent.env(parent.env(parent.frame())), getNamespace("mirai")) || return(invisible())
7878
.Call(rnng_scm)
7979

8080
}

README.Rmd

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ For a 'sendAio' object, calling the result causes it to be stored in the AIO as
189189
```{r async2}
190190
191191
res <- send_aio(s1, data.frame(a = 1, b = 2))
192-
res
193192
call_aio(res)
194193
res
195194
res$result
@@ -201,7 +200,6 @@ For a 'recvAio' object, calling the message causes it to be stored in the AIO as
201200
```{r async3}
202201
203202
msg <- recv_aio(s2)
204-
msg
205203
call_aio(msg)
206204
msg
207205
msg$data
@@ -294,11 +292,11 @@ pub |> send(c("other", "this other topic will not be received"), mode = "raw", e
294292
sub |> recv(mode = "character", keep.raw = FALSE)
295293
296294
# specify NULL to subscribe to ALL topics
297-
sub |> subscribe(topic = NULL)
295+
sub |> subscribe(topic = NULL, quietly = FALSE)
298296
pub |> send(c("newTopic", "this is a new topic"), mode = "raw", echo = FALSE)
299297
sub |> recv("character", keep.raw = FALSE)
300298
301-
sub |> unsubscribe(topic = NULL)
299+
sub |> unsubscribe(topic = NULL, quietly = FALSE)
302300
pub |> send(c("newTopic", "this topic will now not be received"), mode = "raw", echo = FALSE)
303301
sub |> recv("character", keep.raw = FALSE)
304302

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ AIO as `$result`. An exit code of 0 denotes a successful send.
237237

238238
``` r
239239
res <- send_aio(s1, data.frame(a = 1, b = 2))
240-
res
241-
#> < sendAio >
242-
#> ~ use call_aio() to retrieve result
243240
call_aio(res)
244241
res
245242
#> < sendAio >
@@ -253,9 +250,6 @@ the AIO as `$raw` (if kept) and `$data`.
253250

254251
``` r
255252
msg <- recv_aio(s2)
256-
msg
257-
#> < recvAio >
258-
#> ~ use call_aio() to retrieve message
259253
call_aio(msg)
260254
msg
261255
#> < recvAio >
@@ -331,9 +325,10 @@ call_aio(aio)
331325

332326
aio
333327
#> < recvAio >
328+
#> - $raw for raw message
334329
#> - $data for message data
335330
str(aio$data)
336-
#> num [1:100000000] 0.885 -2.081 0.385 -1.136 -0.962 ...
331+
#> num [1:100000000] -0.9199 1.2476 -0.3885 -1.3692 -0.0557 ...
337332
```
338333

339334
In this example the calculation is returned, but other operations may
@@ -364,18 +359,20 @@ sub |> recv(mode = "character", keep.raw = FALSE)
364359

365360
pub |> send(c("other", "this other topic will not be received"), mode = "raw", echo = FALSE)
366361
sub |> recv(mode = "character", keep.raw = FALSE)
367-
#> 2022-02-28 13:28:29 [ 8 ] Try again
362+
#> 2022-03-02 15:43:48 [ 8 ] Try again
368363

369364
# specify NULL to subscribe to ALL topics
370-
sub |> subscribe(topic = NULL)
365+
sub |> subscribe(topic = NULL, quietly = FALSE)
366+
#> 2022-03-02 15:43:48 [ subscribe ] sock: 10 | topic: ALL
371367
pub |> send(c("newTopic", "this is a new topic"), mode = "raw", echo = FALSE)
372368
sub |> recv("character", keep.raw = FALSE)
373369
#> [1] "newTopic" "this is a new topic"
374370

375-
sub |> unsubscribe(topic = NULL)
371+
sub |> unsubscribe(topic = NULL, quietly = FALSE)
372+
#> 2022-03-02 15:43:48 [ unsubscribe ] sock: 10 | topic: ALL
376373
pub |> send(c("newTopic", "this topic will now not be received"), mode = "raw", echo = FALSE)
377374
sub |> recv("character", keep.raw = FALSE)
378-
#> 2022-02-28 13:28:29 [ 8 ] Try again
375+
#> 2022-03-02 15:43:48 [ 8 ] Try again
379376

380377
# however the topics explicitly subscribed to are still received
381378
pub |> send(c("examples", "this example will still be received"), mode = "raw", echo = FALSE)
@@ -399,11 +396,11 @@ ncurl("http://httpbin.org/headers")
399396
#> [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
400397
#> [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
401398
#> [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
402-
#> [76] 2d 36 32 31 63 63 64 66 64 2d 35 31 66 65 37 31 64 63 33 33 65 62 38 38 63
403-
#> [101] 31 37 39 33 64 62 66 64 66 22 0a 20 20 7d 0a 7d 0a
399+
#> [76] 2d 36 32 31 66 39 30 62 34 2d 32 33 32 38 30 62 34 66 32 61 34 62 31 62 65
400+
#> [101] 32 32 30 32 63 39 33 35 35 22 0a 20 20 7d 0a 7d 0a
404401
#>
405402
#> $data
406-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-621ccdfd-51fe71dc33eb88c1793dbfdf\"\n }\n}\n"
403+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-621f90b4-23280b4f2a4b1be2202c9355\"\n }\n}\n"
407404
```
408405

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

man/survey_time.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)