diff --git a/autotest/gcore/misc.py b/autotest/gcore/misc.py index 18d1f4c806c0..62f8b29a5663 100755 --- a/autotest/gcore/misc.py +++ b/autotest/gcore/misc.py @@ -13,6 +13,7 @@ # SPDX-License-Identifier: MIT ############################################################################### +import datetime import os import shutil @@ -721,6 +722,70 @@ def test_misc_13(): assert out_ds is None +############################################################################### +# Test parsing of CPL_DEBUG and CPL_TIMESTAMP + + +@pytest.fixture +def debug_output(): + + messages = [] + + def handle(ecls, ecode, emsg): + messages.append(emsg) + + def log_message(category, message): + messages.clear() + gdal.Debug(category, message) + return messages[0] if messages else None + + log_message.handle = handle + + with gdaltest.error_handler(handle): + yield log_message + + +@pytest.mark.parametrize( + "booleans", + [("YES", "NO"), ("TRUE", "FALSE"), ("ON", "OFF"), ("1", "0")], + ids="_".join, +) +def test_misc_cpl_debug(debug_output, booleans): + + on, off = booleans + + assert debug_output("GDAL", "msg") is None + + with gdal.config_option("CPL_DEBUG", off): + assert debug_output("GDAL", "msg") is None + + with gdal.config_option("CPL_DEBUG", on): + assert debug_output("GDAL", "message") == "GDAL: message" + + with gdal.config_option("CPL_TIMESTAMP", off): + assert debug_output("GDAL", "message") == "GDAL: message" + + with gdal.config_option("CPL_TIMESTAMP", on): + output = debug_output("GDAL", "message") + assert str(datetime.datetime.now().year) in output + assert output.endswith("GDAL: message") + + +def test_misc_cpl_debug_filtering(debug_output): + + with gdal.config_option("CPL_DEBUG", "GDAL"): + assert debug_output("GDAL", "msg") == "GDAL: msg" + assert debug_output("GDAL_WARP", "msg") is None + assert debug_output("", "msg") == ": msg" + + with gdal.config_option("CPL_DEBUG", "GDAL_WARP_TRANSLATE_ETC"): + assert debug_output("GDAL", "msg") == "GDAL: msg" + assert debug_output("TRANSLATE", "msg") == "TRANSLATE: msg" + + with gdal.config_option("CPL_DEBUG", ""): + assert debug_output("GDAL", "msg") == "GDAL: msg" + + ############################################################################### # Test ConfigureLogging() diff --git a/port/cpl_error.cpp b/port/cpl_error.cpp index f751837377b0..b25c6511d4aa 100644 --- a/port/cpl_error.cpp +++ b/port/cpl_error.cpp @@ -590,11 +590,18 @@ static void CPLvDebug(const char *pszCategory, /* -------------------------------------------------------------------- */ /* Does this message pass our current criteria? */ /* -------------------------------------------------------------------- */ - if (pszDebug == nullptr) + if (pszDebug == nullptr || EQUAL(pszDebug, "NO") || + EQUAL(pszDebug, "OFF") || EQUAL(pszDebug, "FALSE") || + EQUAL(pszDebug, "0")) + { return; + } - if (!EQUAL(pszDebug, "ON") && !EQUAL(pszDebug, "")) + if (!EQUAL(pszDebug, "ON") && !EQUAL(pszDebug, "YES") && + !EQUAL(pszDebug, "TRUE") && !EQUAL(pszDebug, "1") && + !EQUAL(pszDebug, "")) { + // check if value of CPL_DEBUG contains the category const size_t nLen = strlen(pszCategory); size_t i = 0; @@ -623,7 +630,7 @@ static void CPLvDebug(const char *pszCategory, pszMessage[0] = '\0'; #ifdef TIMESTAMP_DEBUG - if (CPLGetConfigOption("CPL_TIMESTAMP", nullptr) != nullptr) + if (CPLTestBool(CPLGetConfigOption("CPL_TIMESTAMP", "NO"))) { static struct CPLTimeVal tvStart; static const auto unused = CPLGettimeofday(&tvStart, nullptr);