Skip to content

Commit

Permalink
Generate coverage at multiple log levels (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 authored Jan 22, 2025
1 parent 990e73c commit 9d853b9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ jobs:

- name: Package Nuget
run: |
sudo apt-get update && sudo apt-get install -y libarchive-tools
sudo apt-get update && sudo apt-get install -y libarchive-tools mono-complete
function extract_file {
local -r arch=$1 filepat=$2 dest=$3
local -r file_in_arch=$(bsdtar -tf "$arch" | grep "$filepat" | head -1)
Expand Down
34 changes: 33 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,40 @@ jobs:
run: VERBOSE=1 make -j $(nproc) waf_test waf_validator
working-directory: Debug

- name: Test
- name: "Test (level: trace)"
run: make test
env:
DDWAF_TEST_LOG_LEVEL: trace
working-directory: Debug

- name: "Test (level: debug)"
run: make test
env:
DDWAF_TEST_LOG_LEVEL: debug
working-directory: Debug

- name: "Test (level: error)"
run: make test
env:
DDWAF_TEST_LOG_LEVEL: error
working-directory: Debug

- name: "Test (level: warn)"
run: make test
env:
DDWAF_TEST_LOG_LEVEL: warn
working-directory: Debug

- name: "Test (level: info)"
run: make test
env:
DDWAF_TEST_LOG_LEVEL: info
working-directory: Debug

- name: "Test (level: off)"
run: make test
env:
DDWAF_TEST_LOG_LEVEL: off
working-directory: Debug

- name: Validate
Expand Down
1 change: 1 addition & 0 deletions src/sha256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Copyright 2021 Datadog, Inc.

#include <array>
#include <cstdint>
#include <cstring>
#include <span>
#include <string>
Expand Down
52 changes: 51 additions & 1 deletion tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

#include "common/gtest_utils.hpp"

#include <string_view>

using namespace std::literals;

const char *level_to_str(DDWAF_LOG_LEVEL level)
{
switch (level) {
Expand All @@ -29,15 +33,61 @@ const char *level_to_str(DDWAF_LOG_LEVEL level)
return "off";
}

DDWAF_LOG_LEVEL str_to_level(std::string_view str)
{
if (str == "trace"sv || str == "TRACE"sv) {
return DDWAF_LOG_TRACE;
}

if (str == "debug"sv || str == "DEBUG"sv) {
return DDWAF_LOG_DEBUG;
}

if (str == "error"sv || str == "ERROR"sv) {
return DDWAF_LOG_ERROR;
}

if (str == "warn"sv || str == "WARN"sv) {
return DDWAF_LOG_WARN;
}

if (str == "info"sv || str == "INFO"sv) {
return DDWAF_LOG_INFO;
}

return DDWAF_LOG_OFF;
}

void log_cb(DDWAF_LOG_LEVEL level, const char *function, const char *file, unsigned line,
const char *message, [[maybe_unused]] uint64_t len)
{
ddwaf::fmt::print("[{}][{}:{}:{}]: {}\n", level_to_str(level), file, function, line, message);
}

// NOLINTNEXTLINE(modernize-avoid-c-arrays)
DDWAF_LOG_LEVEL find_log_level(int argc, char *argv[])
{
// NOLINTNEXTLINE(concurrency-mt-unsafe)
auto *env_level = getenv("DDWAF_TEST_LOG_LEVEL");
if (env_level != nullptr) {
return str_to_level(env_level);
}

DDWAF_LOG_LEVEL level = DDWAF_LOG_TRACE;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--log_level" || arg == "--log-level") {
if (i + 1 < argc) {
level = str_to_level(argv[i + 1]);
}
break;
}
}
return level;
}
int main(int argc, char *argv[])
{
ddwaf_set_log_cb(log_cb, DDWAF_LOG_TRACE);
ddwaf_set_log_cb(log_cb, find_log_level(argc, argv));
ddwaf::memory::set_local_memory_resource(std::pmr::new_delete_resource());
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down

0 comments on commit 9d853b9

Please sign in to comment.