Skip to content

Commit 8cfe269

Browse files
committed
Make vim and gdb support optional and use sstream
instead of fstream in test fixture.
1 parent 6289943 commit 8cfe269

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
external_libs/ext-basics/.clang-format

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ option(EXTLOG_EXAMPLES "build examples" OFF)
1111
option(EXTLOG_WARNINGS "enable warnings" ON)
1212
option(EXTLOG_CHECKED "user assert" ON)
1313
option(EXTLOG_TESTS "build tests" OFF)
14+
option(EXTLOG_ENABLE_VIM_GDB "support vim / gdb" ON)
1415

1516
## general setup and includes
1617
set(CMAKE_CXX_STANDARD 17)
@@ -56,7 +57,7 @@ set(ext_common_compile_definitions
5657
$<IF:$<BOOL:${ext_is_big_endian}>,EXT_BIG_ENDIAN,EXT_LITTLE_ENDIAN>
5758
$<$<BOOL:${EXT_CXX_COMPILER_IS_GCC}>:EXT_GCC>
5859
$<$<BOOL:${EXT_CXX_COMPILER_IS_CLANG}>:EXT_CLANG>
59-
$<$<BOOL:${ext_l1_cache_line_size}>:EXT_KNOWN_L1_CACHE_LINE_SIZE=${ext_l1_cache_line_size}>
60+
$<$<BOOL:${EXTLOG_ENABLE_VIM_GDB}>:EXT_LOGGING_ENABLE_VIM_GDB>
6061
)
6162

6263

@@ -68,8 +69,8 @@ target_include_directories(ext-logging PUBLIC
6869
target_compile_features(ext-logging PUBLIC cxx_std_17)
6970
target_compile_options(ext-logging PRIVATE ${ext_stone-warnings})
7071
target_compile_definitions(ext-logging PUBLIC ${ext_common_compile_definitions})
71-
target_compile_definitions(ext-logging PUBLIC EXT_SHARED_LIB)
7272
target_compile_definitions(ext-logging PRIVATE EXT_IN_LIB=1)
73+
7374
target_link_libraries(ext-logging PUBLIC
7475
ext::basics
7576
Threads::Threads

include/ext/logging/definitions.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ EXT_EXPORT_VC extern bool append_newline;
7171
EXT_EXPORT_VC extern bool threads;
7272
EXT_EXPORT_VC extern bool filename;
7373
EXT_EXPORT_VC extern bool function;
74+
#ifdef EXT_LOGGING_ENABLE_VIM_GDB
7475
EXT_EXPORT_VC extern bool vim;
7576
EXT_EXPORT_VC extern bool gdb;
77+
#endif
7678
EXT_EXPORT_VC extern std::ostream* stream;
7779
} // namespace configuration
7880

src/logging.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ bool configuration::prefix_newline{false};
2929
bool configuration::append_newline{true};
3030
bool configuration::filename{true};
3131
bool configuration::function{true};
32+
#ifdef EXT_LOGGING_ENABLE_VIM_GDB
3233
bool configuration::vim{false};
3334
bool configuration::gdb{false};
35+
#endif
3436
std::ostream* configuration::stream = &std::cout;
3537

3638
EXT_INIT_PRIORITY_VC_LOW
@@ -45,16 +47,19 @@ _detail::logger::logger(
4547
if (configuration::prefix_newline) {
4648
_ss << "\n";
4749
}
50+
51+
#ifdef EXT_LOGGING_ENABLE_VIM_GDB
4852
// # vim <filename> +<lineno>
4953
if (configuration::vim) {
5054
_ss << "# vim " << file_name << " +" << line_no << "\n";
5155
}
5256

5357
if (configuration::gdb) {
54-
#ifndef _WIN32
58+
#ifndef _WIN32
5559
_ss << "# break " << ext::util::filename(file_name) << ":" << line_no << "\n";
56-
#endif
60+
#endif
5761
}
62+
#endif
5863

5964
if (true) {
6065
_ss << "[" << id << "] ";
@@ -69,15 +74,13 @@ _detail::logger::logger(
6974

7075
// log filename
7176
if (configuration::filename) {
72-
if (!configuration::gdb) {
7377
_ss << " "
7478
#ifndef _WIN32
7579
<< ext::util::filename(file_name)
7680
#else
7781
<< file_name
7882
#endif
7983
<< ":" << line_no;
80-
}
8184
}
8285

8386
// log function name
@@ -104,6 +107,7 @@ void _detail::logger::write() {
104107
_detail::logger::~logger() {
105108
try {
106109
write();
107-
} catch (...) {}
110+
} catch (...) {
111+
}
108112
}
109113
}} // namespace ext::logging

tests/logging.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright - 2016-2020 - Jan Christoph Uhde <[email protected]>
22
// Please see LICENSE.md for license or visit https://github.com/extcpp/basics
33
#include <cstring>
4-
#include <fstream>
54
#include <filesystem>
5+
#include <sstream>
66

77
#include <gtest/gtest.h>
88

@@ -17,23 +17,20 @@
1717
using namespace std::literals;
1818

1919
struct LoggingTest : public ::testing::Test {
20-
LoggingTest() : _log{"test.log", std::ios::trunc} {
20+
LoggingTest() : _log{} {
2121
using namespace ext::logging;
2222
configuration::stream = &_log;
23+
#ifdef EXT_LOGGING_ENABLE_VIM_GDB
2324
configuration::gdb = false;
2425
configuration::vim = false;
26+
#endif
2527
configuration::prefix_newline = false;
2628
configuration::append_newline = true;
2729
set_level_all(level::EXT_LOGGING_DEFAULT_LEVEL);
2830
};
2931

30-
3132
void compare(std::string const& expected = ""){
32-
_log.close();
33-
std::ifstream file{"test.log"};
34-
std::istreambuf_iterator<char> eos;
35-
std::string logged{std::istreambuf_iterator<char>(file), eos};
36-
ASSERT_EQ(expected, logged);
33+
ASSERT_EQ(expected, _log.str());
3734
}
3835

3936
std::string path() {
@@ -48,24 +45,28 @@ struct LoggingTest : public ::testing::Test {
4845
return std::to_string(_line);
4946
}
5047

51-
std::ofstream _log;
48+
std::stringstream _log;
5249
std::size_t _line;
5350

5451
};
5552
using LoggingDeathTest = LoggingTest;
5653

5754
TEST_F(LoggingTest, logging_no_crash_gdb_vim) {
5855
using namespace ext::logging;
56+
#ifdef EXT_LOGGING_ENABLE_VIM_GDB
5957
configuration::gdb = true;
6058
configuration::vim = true;
59+
#endif
6160

6261
_line = __LINE__ + 1;
6362
ASSERT_NO_THROW(EXT_LOG("babe") << "cafe?");
6463

6564
compare(
65+
#ifdef EXT_LOGGING_ENABLE_VIM_GDB
6666
"# vim "s + path() + " +" + line() +"\n"
6767
"# break logging.cpp:" + line() + "\n"
68-
"[babe] warning in TestBody(): 'cafe?'\n"
68+
#endif
69+
"[babe] warning logging.cpp:"s + line() + " in TestBody(): 'cafe?'\n"
6970
);
7071
}
7172

0 commit comments

Comments
 (0)