Skip to content

Commit e6748b8

Browse files
add small changes
1 parent 7177ea8 commit e6748b8

4 files changed

Lines changed: 42 additions & 20 deletions

File tree

google/cloud/odbc/bq_driver/internal/odbc_sql_tables.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ StatusRecordOr<ResultSet> GetResultSetForTables(
444444

445445
// 3. Execute tasks using the generic utility
446446
std::shared_ptr<TraceOptions> trace_option = TraceOptions::GetTraceOption();
447-
int max_threads = (trace_option != nullptr) ? trace_option->max_threads : 0;
447+
int max_threads = trace_option->max_threads;
448448
auto parallel_results_or = ExecuteParallelTasks<TaskInput, TaskResult>(
449449
max_threads, tasks, parallel_func);
450450

google/cloud/odbc/bq_driver/internal/trace_utils.cc

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,14 @@ FileLogSink::~FileLogSink() {
6969
fp_ = nullptr;
7070
}
7171
}
72-
// Required for custom log formatting and writing to the driver's default log
73-
// file
72+
// Required for writing to the driver's default log file
7473
void FileLogSink::Send(absl::LogEntry const& entry) {
7574
std::lock_guard<std::mutex> lock(log_mutex_);
7675
// Logging disabled or never initialized
7776
if (!fp_ || !opts_) return;
7877

7978
auto& opts = *opts_;
80-
81-
static absl::TimeZone const kTimeZone = absl::LocalTimeZone();
82-
std::string time_str =
83-
absl::FormatTime("%Y-%m-%d %H:%M:%S", entry.timestamp(), kTimeZone);
84-
85-
char const* log_tag = absl::LogSeverityName(entry.log_severity());
86-
87-
absl::string_view full_path = entry.source_filename();
88-
size_t pos = full_path.rfind('/');
89-
if (pos == absl::string_view::npos) pos = full_path.rfind('\\');
90-
91-
absl::string_view file_name =
92-
pos == absl::string_view::npos ? full_path : full_path.substr(pos + 1);
93-
94-
std::string formatted_msg =
95-
absl::StrFormat("[%s] [%s] [%s:%d] %s\n", log_tag, time_str, file_name,
96-
entry.source_line(), entry.text_message());
79+
auto formatted_msg = GetFormattedMsg(entry);
9780

9881
std::size_t new_log_size = formatted_msg.size();
9982
std::uintmax_t max_file_size_bytes =
@@ -131,6 +114,27 @@ absl::LogSeverity GetAbslSeverity(LogLevel level) {
131114
}
132115
}
133116

117+
// Formats log msg in format: [LOG_LEVEL] [TIME] [FILE:LINE] MSG
118+
std::string GetFormattedMsg(absl::LogEntry const& entry) {
119+
static absl::TimeZone const kTimeZone = absl::LocalTimeZone();
120+
std::string time_str =
121+
absl::FormatTime("%Y-%m-%d %H:%M:%S", entry.timestamp(), kTimeZone);
122+
123+
char const* log_tag = absl::LogSeverityName(entry.log_severity());
124+
absl::string_view full_path = entry.source_filename();
125+
126+
size_t pos = full_path.rfind('/');
127+
if (pos == absl::string_view::npos) pos = full_path.rfind('\\');
128+
129+
absl::string_view file_name =
130+
pos == absl::string_view::npos ? full_path : full_path.substr(pos + 1);
131+
132+
std::string msg =
133+
absl::StrFormat("[%s] [%s] [%s:%d] %s\n", log_tag, time_str, file_name,
134+
entry.source_line(), entry.text_message());
135+
return msg;
136+
}
137+
134138
void ClearOldLogFiles(std::string const& base_dir, int next_index,
135139
int max_file_count) {
136140
if (max_file_count < 1) return;

google/cloud/odbc/bq_driver/internal/trace_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ void UpdateTraceOption(std::optional<int> log_level,
165165
std::optional<int> log_file_count);
166166

167167
std::string GetLogFileWithIndex(std::string const& log_path);
168+
std::string GetFormattedMsg(absl::LogEntry const& entry);
169+
168170
////////////////////////////////////////////////////////////////////
169171
// Additional Helper methods for validating and formatting strings
170172
// based on parameter types.

google/cloud/odbc/bq_driver/internal/trace_utils_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ TEST(GetLogFileWithIndex, CustomLogPath) {
8181
EXPECT_EQ(actual, expected);
8282
}
8383

84+
TEST(GetFormattedMsg, FormatMsg_success) {
85+
absl::Time ts = absl::FromUnixSeconds(1700000000);
86+
absl::LogEntry entry(absl::LogSeverity::kInfo, // severity
87+
"C:\\project\\test_file.cc", // source file
88+
42, // line
89+
ts, // timestamp
90+
"Test message" // message
91+
);
92+
93+
std::string result = GetFormattedMsg(entry);
94+
95+
EXPECT_TRUE(absl::StrContains(result, "[INFO]"));
96+
EXPECT_TRUE(absl::StrContains(result, "test_file.cc:42"));
97+
EXPECT_TRUE(absl::StrContains(result, "Test message"));
98+
}
99+
84100
TEST(ClearOldLogFiles, WhenMaxFileCountIsOne) {
85101
std::string dir = std::filesystem::temp_directory_path().string();
86102
std::string file = dir + "/Tracetestingcountone_0.log";

0 commit comments

Comments
 (0)