Skip to content

Commit 832fb61

Browse files
authored
Gtest IT: log also to stderr, not only to file (#461)
## Motivation [A recent CI run](https://github.com/scylladb/cpp-rs-driver/actions/runs/26300848077/job/77426773254?pr=456) experienced a panic inside the Rust Driver. I couldn't debug it, because no logs were printed. ## What's done ### C++ Gtest Logger output #### Destination Before, the logs were only output to a file. Now, they are additionally printed to stderr. #### Format 1. Timestamp display was improved, made in line with Rust tracing subscriber's one. 2. Log level is now coloured (IFF stderr points to a terminal). It uses the same palette as, again, Rust tracing subscriber. ### CI 1. `RUST_LOG=trace` is now passed, so the logs are visible in the CI run console. 2. `RUST_BACKTRACE=full` is now passed, which will aid in debugging.
2 parents 588a850 + 7ffd093 commit 832fb61

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

.github/workflows/build-lint-and-test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ on:
88

99
env:
1010
CARGO_TERM_COLOR: always
11+
RUST_BACKTRACE: full
12+
RUST_LOG: trace
1113
# Should include `INTEGRATION_TEST_BIN` from the `Makefile`
1214
# TODO: Remove `build/libscylla-cpp-driver.*` after https://github.com/scylladb/cpp-rs-driver/issues/164 is fixed.
1315
INTEGRATION_TEST_BIN: |

MAINTENANCE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ other information / procedures useful for maintainers. It is heavily inspired by
55

66
## Documentation
77

8-
TODO (no docs written yet)
8+
Comprehensive Sphinx-compatible documentation lives at /docs.
99
Note: Due to API compatibility, cpp-driver docs apply to cpp-rs-driver in general. Though, some discrepancies should be well-documented in cpp-rs-driver (some of them are already mentioned in `cassandra.h`).
1010

1111
## Version bump commit

tests/src/integration/logger.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ using namespace test::driver;
3232
#ifdef _WIN32
3333
#define FILE_MODE 0
3434
#define LOCALTIME(tm, time) localtime_s(tm, time)
35+
#define GMTIME(tm, time) gmtime_s(tm, time)
36+
#include <io.h>
37+
#define IS_STDERR_TTY() _isatty(_fileno(stderr))
3538
#else
3639
#define FILE_MODE S_IRWXU | S_IRWXG | S_IROTH
3740
#define LOCALTIME(tm, time) localtime_r(time, tm)
41+
#define GMTIME(tm, time) gmtime_r(time, tm)
42+
#include <unistd.h>
43+
#define IS_STDERR_TTY() isatty(STDERR_FILENO)
3844
#endif
3945

4046
Logger::Logger()
@@ -125,7 +131,34 @@ void Logger::log(const CassLogMessage* log, void* data) {
125131
// Create the formatted log message and output to the file
126132
std::string severity = cass_log_level_string(log->severity);
127133
logger->output_ << date.str() << " " << time.str() << " " << severity << ": " << message << " ("
128-
<< log->file << ":" << log->line << ") " << std::endl;
134+
<< log->file << ":" << log->line << ") " << std::endl;
135+
136+
// Also log to stderr so that CI output captures driver logs in real-time.
137+
static bool use_color = IS_STDERR_TTY();
138+
const char* color = "";
139+
const char* reset = "";
140+
const char* dim = "";
141+
if (use_color) {
142+
reset = "\033[0m";
143+
dim = "\033[2m";
144+
switch (log->severity) {
145+
case CASS_LOG_TRACE: color = "\033[35m"; break;
146+
case CASS_LOG_DEBUG: color = "\033[34m"; break;
147+
case CASS_LOG_INFO: color = "\033[32m"; break;
148+
case CASS_LOG_WARN: color = "\033[33m"; break;
149+
default: color = "\033[31m"; break;
150+
}
151+
}
152+
cass_uint64_t const epoch_secs = log->time_ms / 1000;
153+
unsigned short const ms = log->time_ms % 1000;
154+
time_t const raw = static_cast<time_t>(epoch_secs);
155+
struct tm utc;
156+
GMTIME(&utc, &raw);
157+
char ts[24];
158+
strftime(ts, sizeof(ts), "%Y-%m-%dT%H:%M:%S", &utc);
159+
std::cerr << ts << "." << std::setfill('0') << std::setw(3) << ms << "Z" << " [" << color << severity << reset << "] "
160+
<< dim << "(" << log->file << ":" << log->line << ")" << reset
161+
<< " " << message << std::endl;
129162

130163
// Determine if the log message matches any of the criteria
131164
for (std::vector<std::string>::const_iterator iterator = logger->search_criteria_.begin();

0 commit comments

Comments
 (0)