Skip to content

Commit

Permalink
a switch to disable logging in optimized builds #1103
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrooommmaaa authored and Daniel Wyatt committed May 30, 2020
1 parent eeaad5e commit 6e74b75
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,34 @@ rnp_clear_debug()
debugc = 0;
}

/* -1 -- not initialized
0 -- logging is off
1 -- logging is on
*/
int8_t _rnp_log_switch =
#ifdef NDEBUG
-1 // lazy-initialize later
#else
1 // always on in debug build
#endif
;

void
set_rnp_log_switch(int8_t value)
{
_rnp_log_switch = value;
}

bool
rnp_log_switch()
{
if (_rnp_log_switch < 0) {
const char *var = getenv(RNP_LOG_CONSOLE);
_rnp_log_switch = (var && strcmp(var, "0")) ? 1 : 0;
}
return !!_rnp_log_switch;
}

/* portable replacement for strcasecmp(3) */
int
rnp_strcasecmp(const char *s1, const char *s2)
Expand Down
9 changes: 9 additions & 0 deletions src/lib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@
#include <limits.h>

#define RNP_MSG(msg) (void) fprintf(stdout, msg);

bool rnp_log_switch();
void set_rnp_log_switch(int8_t);

#define RNP_LOG_FD(fd, ...) \
do { \
if (!rnp_log_switch()) \
break; \
(void) fprintf((fd), "[%s() %s:%d] ", __func__, __FILE__, __LINE__); \
(void) fprintf((fd), __VA_ARGS__); \
(void) fprintf((fd), "\n"); \
Expand Down Expand Up @@ -133,6 +139,9 @@ bool rnp_set_debug(const char *);
bool rnp_get_debug(const char *);
void rnp_clear_debug();

/* environment variable name */
static const char RNP_LOG_CONSOLE[] = "RNP_LOG_CONSOLE";

/* Portable way to convert bits to bytes */

#define BITS_TO_BYTES(b) (((b) + (CHAR_BIT - 1)) / CHAR_BIT)
Expand Down
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ add_executable(rnp_tests
large-mpi.cpp
load-g10.cpp
load-pgp.cpp
log-switch.cpp
partial-length.cpp
rnp_tests.cpp
s2k-iterations.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/tests/cli_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ def setup(loglvl):
RNPK = os.getenv('RNP_TESTS_RNPKEYS_PATH') or 'rnpkeys'
os.mkdir(RNPDIR, 0o700)

os.environ["RNP_LOG_CONSOLE"] = "1"

GPGDIR = path.join(WORKDIR, '.gpg')
GPGHOME = path_for_gpg(GPGDIR) if is_windows() else GPGDIR
GPG = os.getenv('RNP_TESTS_GPG_PATH') or find_utility('gpg')
Expand Down
91 changes: 91 additions & 0 deletions src/tests/log-switch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2018-2019 [Ribose Inc](https://www.ribose.com).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "rnp_tests.h"
#include "support.h"

static const char LOGTEST_FILENAME[] = "logtest.log";

TEST_F(rnp_tests, test_log_switch)
{
FILE *stream = fopen(LOGTEST_FILENAME, "w");
assert_non_null(stream);
bool saved_rnp_log_switch = rnp_log_switch();

// reset _rnp_log_switch manually
set_rnp_log_switch(0);
RNP_LOG_FD(stream, "x");
fflush(stream);
assert_int_equal(0, ftell(stream)); // nothing was written

// enable _rnp_log_switch manually
set_rnp_log_switch(1);
RNP_LOG_FD(stream, "x");
fflush(stream);
assert_int_not_equal(0, ftell(stream)); // something was written

fclose(stream);
assert_int_equal(0, unlink(LOGTEST_FILENAME));

stream = fopen(LOGTEST_FILENAME, "w");
assert_non_null(stream);

const char *saved_env = getenv(RNP_LOG_CONSOLE);

// let _rnp_log_switch initialize to 0 from unset environment variable
assert_int_equal(0, unsetenv(RNP_LOG_CONSOLE));
set_rnp_log_switch(-1);
RNP_LOG_FD(stream, "x");
fflush(stream);
assert_int_equal(0, ftell(stream)); // nothing was written

// let _rnp_log_switch initialize to 0 from environment variable "0"
setenv(RNP_LOG_CONSOLE, "0", 1);
set_rnp_log_switch(-1);
RNP_LOG_FD(stream, "x");
fflush(stream);
assert_int_equal(0, ftell(stream)); // nothing was written

// let _rnp_log_switch initialize to 1 from environment variable "1"
setenv(RNP_LOG_CONSOLE, "1", 1);
set_rnp_log_switch(-1);
RNP_LOG_FD(stream, "x");
fflush(stream);
assert_int_not_equal(0, ftell(stream)); // something was written

// restore environment variable
if (saved_env) {
assert_int_equal(0, setenv(RNP_LOG_CONSOLE, saved_env, 1));
} else {
unsetenv(RNP_LOG_CONSOLE);
}

// restore _rnp_log_switch
set_rnp_log_switch(saved_rnp_log_switch ? 1 : 0);

fclose(stream);
assert_int_equal(0, unlink(LOGTEST_FILENAME));
}
2 changes: 2 additions & 0 deletions src/tests/rnp_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ void test_kbx_nsigs(void **state);

void test_issue_1115(void **state);

void test_log_switch(void **state);

#define assert_true(a) EXPECT_TRUE((a))
#define assert_false(a) EXPECT_FALSE((a))
#define assert_string_equal(a, b) EXPECT_STREQ((a), (b))
Expand Down

0 comments on commit 6e74b75

Please sign in to comment.