Skip to content

Commit

Permalink
Merge pull request #11220 from dbaston/cpl-debug-yes
Browse files Browse the repository at this point in the history
CPLDebug: Accept values of YES,TRUE,1
  • Loading branch information
rouault authored Nov 7, 2024
2 parents 22cf156 + b47a2f2 commit 0defeab
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
65 changes: 65 additions & 0 deletions autotest/gcore/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# SPDX-License-Identifier: MIT
###############################################################################

import datetime
import os
import shutil

Expand Down Expand Up @@ -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()

Expand Down
13 changes: 10 additions & 3 deletions port/cpl_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0defeab

Please sign in to comment.