Skip to content

Commit 87d0342

Browse files
committed
add quietly, stdout informational logging
1 parent fcb229a commit 87d0342

File tree

20 files changed

+191
-94
lines changed

20 files changed

+191
-94
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+
* 'quietly' argument added to functions that create or destroy objects. Set to FALSE to enable printing of informational messages for logging purposes.
1213
* Common format for NNG errors now starts with a timestamp for easier logging.
1314
* `listen()` and `dial()` now return (invisible) zero rather than NULL upon success to better align with similar functions.
1415
* Allows setting the environment variable 'NANONEXT_ARM' prior to package installation

R/aio.R

Lines changed: 1 addition & 1 deletion
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(Sys.time(), " | ", res, " : ", nng_error(res))
81+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
8282
return(invisible(aio))
8383
}
8484
on.exit(expr = {

R/context.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#' and listeners, while still benefiting from separate state tracking.
88
#'
99
#' @param socket a Socket or nano object.
10+
#' @param quietly [default TRUE] if FALSE, confirmation that the context has been
11+
#' successfully opened is printed to the console (stdout), useful for logging
12+
#' purposes.
1013
#'
1114
#' @return A new Context (object of class 'nanoContext' and 'nano').
1215
#'
@@ -41,11 +44,16 @@
4144
#'
4245
#' @export
4346
#'
44-
context <- function(socket) {
47+
context <- function(socket, quietly = TRUE) {
4548

4649
if (is.environment(socket)) socket <- .subset2(socket, "socket")
4750
res <- .Call(rnng_ctx_open, socket)
48-
if (is.integer(res)) message(Sys.time(), " | ", res, " : ", nng_error(res))
51+
if (is.integer(res)) {
52+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
53+
} else if (!missing(quietly) && !isTRUE(quietly)) {
54+
cat(format.POSIXct(Sys.time()), "[ context open ] id:",
55+
attr(res, "id"), "| socket:", attr(res, "socket"))
56+
}
4957
res
5058

5159
}
@@ -90,7 +98,7 @@ send_ctx <- function(context, data, mode = c("serial", "raw"), timeout, echo = T
9098
raw = if (is.raw(data)) data else writeBin(object = data, con = raw()))
9199
res <- .Call(rnng_ctx_send, context, data, timeout)
92100
is.integer(res) && {
93-
message(Sys.time(), " | ", res, " : ", nng_error(res))
101+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
94102
return(invisible(res))
95103
}
96104
if (missing(echo) || isTRUE(echo)) res else invisible(0L)
@@ -142,7 +150,7 @@ recv_ctx <- function(context,
142150
if (missing(timeout)) timeout <- -2L
143151
res <- .Call(rnng_ctx_recv, context, timeout)
144152
is.integer(res) && {
145-
message(Sys.time(), " | ", res, " : ", nng_error(res))
153+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
146154
return(invisible(res))
147155
}
148156
on.exit(expr = return(res))
@@ -228,7 +236,7 @@ reply <- function(context,
228236
if (missing(timeout)) timeout <- -2L
229237
res <- .Call(rnng_ctx_recv, context, timeout)
230238
is.integer(res) && {
231-
message(Sys.time(), " | ", res, " : ", nng_error(res))
239+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
232240
return(invisible(res))
233241
}
234242
on.exit(expr = send_aio(context, as.raw(0L), mode = send_mode))
@@ -244,7 +252,7 @@ reply <- function(context,
244252
on.exit()
245253
res <- .Call(rnng_ctx_send, context, data, timeout)
246254
is.integer(res) && {
247-
message(Sys.time(), " | ", res, " : ", nng_error(res))
255+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
248256
return(invisible(res))
249257
}
250258
invisible(0L)
@@ -311,12 +319,12 @@ request <- function(context,
311319
raw = if (is.raw(data)) data else writeBin(object = data, con = raw()))
312320
res <- .Call(rnng_send_aio, context, data, -2L)
313321
is.integer(res) && {
314-
message(Sys.time(), " | ", res, " : ", nng_error(res))
322+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
315323
return(invisible(res))
316324
}
317325
res <- .Call(rnng_recv_aio, context, timeout)
318326
is.integer(res) && {
319-
message(Sys.time(), " | ", res, " : ", nng_error(res))
327+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
320328
return(invisible(res))
321329
}
322330
env <- `class<-`(new.env(), "recvAio")

R/listdial.R

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#' @param autostart [default TRUE] whether to start the dialer. Set to FALSE if
1212
#' you wish to set configuration options on the dialer as it is not
1313
#' generally possible to change these once started.
14+
#' @param quietly [default TRUE] if FALSE, confirmation of a successful start is
15+
#' printed to the console (stdout), useful for logging purposes.
1416
#'
1517
#' @return Zero (invisibly) on success. A new Dialer (object of class 'nanoDialer'
1618
#' and 'nano') is created and bound to the Socket.
@@ -70,21 +72,23 @@
7072
#'
7173
dial <- function(socket,
7274
url = "inproc://nanonext",
73-
autostart = TRUE) {
75+
autostart = TRUE,
76+
quietly = TRUE) {
7477

7578
is.character(url) || stop("'url' should be a character string")
7679
if (is.environment(socket)) {
7780

7881
if (missing(autostart) || isTRUE(autostart)) {
7982
res <- .Call(rnng_dial, .subset2(socket, "socket"), url)
8083
if (is.integer(res)) {
81-
message(Sys.time(), " | ", res, " : ", nng_error(res))
84+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
8285
return(res)
8386
}
87+
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ dialer start ]", url)
8488
} else {
8589
res <- .Call(rnng_dialer_create, .subset2(socket, "socket"), url)
8690
if (is.integer(res)) {
87-
message(Sys.time(), " | ", res, " : ", nng_error(res))
91+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
8892
return(res)
8993
}
9094
}
@@ -103,13 +107,14 @@ dial <- function(socket,
103107
if (missing(autostart) || isTRUE(autostart)) {
104108
res <- .Call(rnng_dial, socket, url)
105109
if (is.integer(res)) {
106-
message(Sys.time(), " | ", res, " : ", nng_error(res))
110+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
107111
return(res)
108112
}
113+
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ dialer start ]", url)
109114
} else {
110115
res <- .Call(rnng_dialer_create, socket, url)
111116
if (is.integer(res)) {
112-
message(Sys.time(), " | ", res, " : ", nng_error(res))
117+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
113118
return(res)
114119
}
115120
}
@@ -124,7 +129,7 @@ dial <- function(socket,
124129
#'
125130
#' Creates a new Listener and binds it to a Socket.
126131
#'
127-
#' @param socket a Socket or nano object.
132+
#' @inheritParams dial
128133
#' @param url [default 'inproc://nanonext'] a URL to dial or listen at, specifying
129134
#' the transport and address as a character string e.g. 'inproc://anyvalue'
130135
#' or 'tcp://127.0.0.1:5555' (see \link{transports}).
@@ -191,21 +196,23 @@ dial <- function(socket,
191196
#'
192197
listen <- function(socket,
193198
url = "inproc://nanonext",
194-
autostart = TRUE) {
199+
autostart = TRUE,
200+
quietly = TRUE) {
195201

196202
is.character(url) || stop("'url' should be a character string")
197203
if (is.environment(socket)) {
198204

199205
if (missing(autostart) || isTRUE(autostart)) {
200206
res <- .Call(rnng_listen, .subset2(socket, "socket"), url)
201207
if (is.integer(res)) {
202-
message(Sys.time(), " | ", res, " : ", nng_error(res))
208+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
203209
return(res)
204210
}
211+
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ listener start ]", url)
205212
} else {
206213
res <- .Call(rnng_listener_create, .subset2(socket, "socket"), url)
207214
if (is.integer(res)) {
208-
message(Sys.time(), " | ", res, " : ", nng_error(res))
215+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
209216
return(res)
210217
}
211218
}
@@ -224,13 +231,14 @@ listen <- function(socket,
224231
if (missing(autostart) || isTRUE(autostart)) {
225232
res <- .Call(rnng_listen, socket, url)
226233
if (is.integer(res)) {
227-
message(Sys.time(), " | ", res, " : ", nng_error(res))
234+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
228235
return(res)
229236
}
237+
if (!missing(quietly) && !isTRUE(quietly)) cat(format.POSIXct(Sys.time()), "[ listener start ]", url)
230238
} else {
231239
res <- .Call(rnng_listener_create, socket, url)
232240
if (is.integer(res)) {
233-
message(Sys.time(), " | ", res, " : ", nng_error(res))
241+
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
234242
return(res)
235243
}
236244
}

R/methods.R

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#' failures somewhat more difficult. If FALSE, failure, such as if the
1212
#' connection is refused, will be returned immediately, and no further
1313
#' action will be taken.
14+
#' @inheritParams nano
1415
#' @param ... not used.
1516
#'
1617
#' @return Zero (invisibly) on success.
@@ -24,10 +25,14 @@ NULL
2425
#' @method start nanoListener
2526
#' @export
2627
#'
27-
start.nanoListener <- function(x, ...) {
28+
start.nanoListener <- function(x, quietly = TRUE, ...) {
2829

2930
xc <- .Call(rnng_listener_start, x)
30-
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
31+
if (xc) {
32+
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
33+
} else if (!missing(quietly) && !isTRUE(quietly)) {
34+
cat(format.POSIXct(Sys.time()), "[ listener start ]", attr(x, "url"))
35+
}
3136
invisible(xc)
3237

3338
}
@@ -36,10 +41,14 @@ start.nanoListener <- function(x, ...) {
3641
#' @method start nanoDialer
3742
#' @export
3843
#'
39-
start.nanoDialer <- function(x, async = TRUE, ...) {
44+
start.nanoDialer <- function(x, async = TRUE, quietly = TRUE, ...) {
4045

4146
xc <- .Call(rnng_dialer_start, x, async)
42-
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
47+
if (xc) {
48+
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
49+
} else if (!missing(quietly) && !isTRUE(quietly)) {
50+
cat(format.POSIXct(Sys.time()), "[ dialer start ]", attr(x, "url"))
51+
}
4352
invisible(xc)
4453

4554
}
@@ -49,6 +58,9 @@ start.nanoDialer <- function(x, async = TRUE, ...) {
4958
#' Close Connection on a Socket, Context, Dialer or Listener.
5059
#'
5160
#' @param con a Socket, Context, Dialer or Listener.
61+
#' @param quietly [default TRUE] if FALSE, confirmation that the object has been
62+
#' successfully closed is printed to the console (stdout), useful for logging
63+
#' purposes.
5264
#' @param ... not used.
5365
#'
5466
#' @return Zero (invisibly) on success.
@@ -77,10 +89,15 @@ NULL
7789
#' @method close nanoSocket
7890
#' @export
7991
#'
80-
close.nanoSocket <- function(con, ...) {
92+
close.nanoSocket <- function(con, quietly = TRUE, ...) {
8193

8294
xc <- .Call(rnng_close, con)
83-
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
95+
if (xc) {
96+
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
97+
} else if (!missing(quietly) && !isTRUE(quietly)) {
98+
cat(format.POSIXct(Sys.time()), "[ socket close ] id:",
99+
attr(con, "id"), "| protocol:", attr(con, "protocol"))
100+
}
84101
invisible(xc)
85102

86103
}
@@ -89,10 +106,15 @@ close.nanoSocket <- function(con, ...) {
89106
#' @method close nanoContext
90107
#' @export
91108
#'
92-
close.nanoContext <- function(con, ...) {
109+
close.nanoContext <- function(con, quietly = TRUE, ...) {
93110

94111
xc <- .Call(rnng_ctx_close, con)
95-
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
112+
if (xc) {
113+
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
114+
} else if (!missing(quietly) && !isTRUE(quietly)) {
115+
cat(format.POSIXct(Sys.time()), "[ context close ] id:",
116+
attr(con, "id"), "| socket:", attr(con, "socket"))
117+
}
96118
invisible(xc)
97119

98120
}
@@ -101,10 +123,15 @@ close.nanoContext <- function(con, ...) {
101123
#' @method close nanoDialer
102124
#' @export
103125
#'
104-
close.nanoDialer <- function(con, ...) {
126+
close.nanoDialer <- function(con, quietly = TRUE, ...) {
105127

106128
xc <- .Call(rnng_dialer_close, con)
107-
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
129+
if (xc) {
130+
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
131+
} else if (!missing(quietly) && !isTRUE(quietly)) {
132+
cat(format.POSIXct(Sys.time()), "[ dialer close ]", attr(con, "url"))
133+
}
134+
108135
invisible(xc)
109136

110137
}
@@ -113,10 +140,15 @@ close.nanoDialer <- function(con, ...) {
113140
#' @method close nanoListener
114141
#' @export
115142
#'
116-
close.nanoListener <- function(con, ...) {
143+
close.nanoListener <- function(con, quietly = TRUE, ...) {
117144

118145
xc <- .Call(rnng_listener_close, con)
119-
if (xc) message(Sys.time(), " | ", xc, " : ", nng_error(xc))
146+
if (xc) {
147+
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
148+
} else if (!missing(quietly) && !isTRUE(quietly)) {
149+
cat(format.POSIXct(Sys.time()), "[ listener close ]", attr(con, "url"))
150+
}
151+
120152
invisible(xc)
121153

122154
}

0 commit comments

Comments
 (0)