Skip to content

Commit 1659d50

Browse files
committed
implement global package-wide logging levels
1 parent 95ef0b7 commit 1659d50

22 files changed

+146
-154
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export(dial)
2929
export(is_nul_byte)
3030
export(is_resolved)
3131
export(listen)
32+
export(logging)
3233
export(nano)
3334
export(ncurl)
3435
export(nng_error)

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* `is_resolved()` added as an auxiliary function to query whether an Aio has resolved in a non-blocking fashion.
77
* `is_nul_byte()` added as a helper function for request/reply setups.
88
* `survey_time()` added as a convenience function for surveyor/respondent patterns.
9+
* `logging()` function to specify a global package logging level - currently supports 'error' and 'info'.
910
* `ncurl()` adds a '...' argument. Support for HTTP methods other than GET.
1011

1112
#### Updates
1213

13-
* Argument 'quietly' added to functions that create or destroy objects. Set to FALSE to enable printing of informational messages for logging purposes.
14-
* Common format for NNG errors now starts with a timestamp for easier logging.
14+
* Common format for NNG errors and informational events now starts with a timestamp for easier logging.
1515
* `listen()` and `dial()` now return (invisible) zero rather than NULL upon success to better align with similar functions.
1616
* Allows setting the environment variable 'NANONEXT_ARM' prior to package installation
1717
+ Fixes installation issues on certain ARM architectures

R/context.R

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
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.
1310
#'
1411
#' @return A new Context (object of class 'nanoContext' and 'nano').
1512
#'
@@ -44,15 +41,12 @@
4441
#'
4542
#' @export
4643
#'
47-
context <- function(socket, quietly = TRUE) {
44+
context <- function(socket) {
4845

4946
if (is.environment(socket)) socket <- .subset2(socket, "socket")
5047
res <- .Call(rnng_ctx_open, socket)
5148
if (is.integer(res)) {
5249
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
53-
} else if (!missing(quietly) && !isTRUE(quietly)) {
54-
cat(format.POSIXct(Sys.time()), "[ ctxt open ] id:",
55-
attr(res, "id"), "| sock:", attr(res, "socket"), "\n")
5650
}
5751
res
5852

R/listdial.R

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
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.
1614
#'
1715
#' @return Zero (invisibly) on success. A new Dialer (object of class 'nanoDialer'
1816
#' and 'nano') is created and bound to the Socket.
@@ -72,8 +70,7 @@
7270
#'
7371
dial <- function(socket,
7472
url = "inproc://nanonext",
75-
autostart = TRUE,
76-
quietly = TRUE) {
73+
autostart = TRUE) {
7774

7875
is.character(url) || stop("'url' should be a character string")
7976
if (is.environment(socket)) {
@@ -84,7 +81,7 @@ dial <- function(socket,
8481
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
8582
return(invisible(res))
8683
}
87-
if (!missing(quietly) && !isTRUE(quietly)) {
84+
if (logging()) {
8885
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
8986
attr(res, "socket"), "| url:", url, "\n")
9087
}
@@ -113,7 +110,7 @@ dial <- function(socket,
113110
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
114111
return(invisible(res))
115112
}
116-
if (!missing(quietly) && !isTRUE(quietly)) {
113+
if (logging()) {
117114
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
118115
attr(res, "socket"), "| url:", url, "\n")
119116
}
@@ -135,7 +132,7 @@ dial <- function(socket,
135132
#'
136133
#' Creates a new Listener and binds it to a Socket.
137134
#'
138-
#' @inheritParams dial
135+
#' @param socket a Socket or nano object.
139136
#' @param url [default 'inproc://nanonext'] a URL to dial or listen at, specifying
140137
#' the transport and address as a character string e.g. 'inproc://anyvalue'
141138
#' or 'tcp://127.0.0.1:5555' (see \link{transports}).
@@ -202,8 +199,7 @@ dial <- function(socket,
202199
#'
203200
listen <- function(socket,
204201
url = "inproc://nanonext",
205-
autostart = TRUE,
206-
quietly = TRUE) {
202+
autostart = TRUE) {
207203

208204
is.character(url) || stop("'url' should be a character string")
209205
if (is.environment(socket)) {
@@ -214,7 +210,7 @@ listen <- function(socket,
214210
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
215211
return(invisible(res))
216212
}
217-
if (!missing(quietly) && !isTRUE(quietly)) {
213+
if (logging()) {
218214
cat(format.POSIXct(Sys.time()), "[ list start ] sock:",
219215
attr(res, "socket"), "| url:", url, "\n")
220216
}
@@ -243,7 +239,7 @@ listen <- function(socket,
243239
message(Sys.time(), " [ ", res, " ] ", nng_error(res))
244240
return(invisible(res))
245241
}
246-
if (!missing(quietly) && !isTRUE(quietly)) {
242+
if (logging()) {
247243
cat(format.POSIXct(Sys.time()), "[ list start ] sock:",
248244
attr(res, "socket"), "| url:", url, "\n")
249245
}

R/methods.R

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
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
1514
#' @param ... not used.
1615
#'
1716
#' @return Zero (invisibly) on success.
@@ -25,12 +24,12 @@ NULL
2524
#' @method start nanoListener
2625
#' @export
2726
#'
28-
start.nanoListener <- function(x, quietly = TRUE, ...) {
27+
start.nanoListener <- function(x, ...) {
2928

3029
xc <- .Call(rnng_listener_start, x)
3130
if (xc) {
3231
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
33-
} else if (!missing(quietly) && !isTRUE(quietly)) {
32+
} else if (logging()) {
3433
cat(format.POSIXct(Sys.time()), "[ list start ] sock:",
3534
attr(x, "socket"), "| url:", attr(x, "url"), "\n")
3635
}
@@ -42,12 +41,12 @@ start.nanoListener <- function(x, quietly = TRUE, ...) {
4241
#' @method start nanoDialer
4342
#' @export
4443
#'
45-
start.nanoDialer <- function(x, async = TRUE, quietly = TRUE, ...) {
44+
start.nanoDialer <- function(x, async = TRUE, ...) {
4645

4746
xc <- .Call(rnng_dialer_start, x, async)
4847
if (xc) {
4948
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
50-
} else if (!missing(quietly) && !isTRUE(quietly)) {
49+
} else if (logging()) {
5150
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
5251
attr(x, "socket"), "| url:", attr(x, "url"), "\n")
5352
}
@@ -60,9 +59,6 @@ start.nanoDialer <- function(x, async = TRUE, quietly = TRUE, ...) {
6059
#' Close Connection on a Socket, Context, Dialer or Listener.
6160
#'
6261
#' @param con a Socket, Context, Dialer or Listener.
63-
#' @param quietly [default TRUE] if FALSE, confirmation that the object has been
64-
#' successfully closed is printed to the console (stdout), useful for logging
65-
#' purposes.
6662
#' @param ... not used.
6763
#'
6864
#' @return Zero (invisibly) on success.
@@ -91,12 +87,12 @@ NULL
9187
#' @method close nanoSocket
9288
#' @export
9389
#'
94-
close.nanoSocket <- function(con, quietly = TRUE, ...) {
90+
close.nanoSocket <- function(con, ...) {
9591

9692
xc <- .Call(rnng_close, con)
9793
if (xc) {
9894
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
99-
} else if (!missing(quietly) && !isTRUE(quietly)) {
95+
} else if (logging()) {
10096
cat(format.POSIXct(Sys.time()), "[ sock close ] id:",
10197
attr(con, "id"), "| protocol:", attr(con, "protocol"), "\n")
10298
}
@@ -108,12 +104,12 @@ close.nanoSocket <- function(con, quietly = TRUE, ...) {
108104
#' @method close nanoContext
109105
#' @export
110106
#'
111-
close.nanoContext <- function(con, quietly = TRUE, ...) {
107+
close.nanoContext <- function(con, ...) {
112108

113109
xc <- .Call(rnng_ctx_close, con)
114110
if (xc) {
115111
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
116-
} else if (!missing(quietly) && !isTRUE(quietly)) {
112+
} else if (logging()) {
117113
cat(format.POSIXct(Sys.time()), "[ ctxt close ] id:",
118114
attr(con, "id"), "| sock:", attr(con, "socket"), "\n")
119115
}
@@ -125,12 +121,12 @@ close.nanoContext <- function(con, quietly = TRUE, ...) {
125121
#' @method close nanoDialer
126122
#' @export
127123
#'
128-
close.nanoDialer <- function(con, quietly = TRUE, ...) {
124+
close.nanoDialer <- function(con, ...) {
129125

130126
xc <- .Call(rnng_dialer_close, con)
131127
if (xc) {
132128
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
133-
} else if (!missing(quietly) && !isTRUE(quietly)) {
129+
} else if (logging()) {
134130
cat(format.POSIXct(Sys.time()), "[ dial start ] sock:",
135131
attr(con, "socket"), "| url:", attr(con, "url"), "\n")
136132
}
@@ -143,12 +139,12 @@ close.nanoDialer <- function(con, quietly = TRUE, ...) {
143139
#' @method close nanoListener
144140
#' @export
145141
#'
146-
close.nanoListener <- function(con, quietly = TRUE, ...) {
142+
close.nanoListener <- function(con, ...) {
147143

148144
xc <- .Call(rnng_listener_close, con)
149145
if (xc) {
150146
message(Sys.time(), " [ ", xc, " ] ", nng_error(xc))
151-
} else if (!missing(quietly) && !isTRUE(quietly)) {
147+
} else if (logging()) {
152148
cat(format.POSIXct(Sys.time()), "[ list close ] sock:",
153149
attr(con, "socket"), "| url:", attr(con, "url"), "\n")
154150
}

R/nano.R

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#' Dialer/Listener.
77
#'
88
#' @inheritParams socket
9-
#' @param quietly [default TRUE] if FALSE, confirmation of successful socket
10-
#' creation and dialer or listener start is printed to the console (stdout),
11-
#' useful for logging purposes.
129
#'
1310
#' @return An nano object of class 'nanoObject'.
1411
#'
@@ -58,48 +55,43 @@ nano <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
5855
"pub", "sub", "surveyor", "respondent"),
5956
dial = NULL,
6057
listen = NULL,
61-
autostart = TRUE,
62-
quietly = TRUE) {
58+
autostart = TRUE) {
6359

6460
protocol <- match.arg(protocol)
6561

6662
nano <- `class<-`(new.env(), "nanoObject")
67-
socket <- socket(protocol, quietly = quietly)
63+
socket <- socket(protocol)
6864
makeActiveBinding("socket", function(x) socket, nano)
6965

7066
if (!missing(dial)) {
71-
dial(nano, url = dial, autostart = autostart, quietly = quietly)
67+
dial(nano, url = dial, autostart = autostart)
7268
if(!isTRUE(autostart)) {
73-
nano[["dialer_start"]] <- function(async = TRUE, quietly = TRUE) {
69+
nano[["dialer_start"]] <- function(async = TRUE) {
7470
rm("dialer_start", envir = nano)
75-
start(.subset2(nano, "dialer")[[1L]], async = async, quietly = quietly)
71+
start(.subset2(nano, "dialer")[[1L]], async = async)
7672
}
7773
}
7874
}
7975

8076
if (!missing(listen)) {
81-
listen(nano, url = listen, autostart = autostart, quietly = quietly)
77+
listen(nano, url = listen, autostart = autostart)
8278
if(!isTRUE(autostart)) {
83-
nano[["listener_start"]] <- function(quietly = TRUE) {
79+
nano[["listener_start"]] <- function() {
8480
rm("listener_start", envir = nano)
85-
start(.subset2(nano, "listener")[[1L]], quietly = quietly)
81+
start(.subset2(nano, "listener")[[1L]])
8682
}
8783
}
8884
}
8985

90-
nano[["close"]] <- function(quietly = TRUE) close(socket, quietly = quietly)
86+
nano[["close"]] <- function() close(socket)
9187
nano[["dial"]] <- function(url = "inproc://nanonext",
92-
autostart = TRUE,
93-
quietly = TRUE) dial(nano,
94-
url = url,
95-
autostart = autostart,
96-
quietly = quietly)
88+
autostart = TRUE) dial(nano,
89+
url = url,
90+
autostart = autostart)
9791
nano[["listen"]] <- function(url = "inproc://nanonext",
98-
autostart = TRUE,
99-
quietly = TRUE) listen(nano,
100-
url = url,
101-
autostart = autostart,
102-
quietly = quietly)
92+
autostart = TRUE) listen(nano,
93+
url = url,
94+
autostart = autostart)
10395
nano[["recv"]] <- function(mode = c("serial", "character", "complex", "double",
10496
"integer", "logical", "numeric", "raw"),
10597
block = FALSE,
@@ -137,20 +129,13 @@ nano <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
137129
value = value)
138130

139131
if (protocol == "sub") {
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)
132+
nano[["subscribe"]] <- function(topic = NULL) subscribe(socket,
133+
topic = topic)
134+
nano[["unsubscribe"]] <- function(topic = NULL) unsubscribe(socket,
135+
topic = topic)
148136
}
149137
if (protocol == "surveyor") {
150-
nano[["survey_time"]] <- function(time,
151-
quietly = TRUE) survey_time(socket,
152-
time = time,
153-
quietly = quietly)
138+
nano[["survey_time"]] <- function(time) survey_time(socket, time = time)
154139
}
155140

156141
nano

R/nanonext-package.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,9 @@
122122
#'
123123
NULL
124124

125+
.onLoad <- function(libname, pkgname) {
126+
logging <- logging()
127+
logging <<- logging
128+
invisible()
129+
}
130+

0 commit comments

Comments
 (0)