Skip to content

Commit e5d59c0

Browse files
authored
CXX-3320 migrate instance and logger to mongocxx::v1 (#1469)
1 parent 28c703e commit e5d59c0

File tree

25 files changed

+1423
-302
lines changed

25 files changed

+1423
-302
lines changed

.evergreen/scripts/test.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,23 @@ else
289289
command -V valgrind
290290
valgrind --version
291291
run_test() {
292+
valgrind_args=(
293+
"--leak-check=full"
294+
"--track-origins=yes"
295+
"--num-callers=50"
296+
"--error-exitcode=1"
297+
"--error-limit=no"
298+
"--read-var-info=yes"
299+
"--suppressions=../etc/memcheck.suppressions"
300+
)
301+
302+
# Avoid noisy diagnostics caused by deliberate subprocess termination.
303+
if [[ "${1:?}" =~ test_instance ]]; then
304+
valgrind_args+=("--trace-children=no")
305+
fi
306+
292307
echo "Running ${1:?}..."
293-
valgrind --leak-check=full --track-origins=yes --num-callers=50 --error-exitcode=1 --error-limit=no --read-var-info=yes --suppressions=../etc/memcheck.suppressions "${1:?}" "${test_args[@]:?}" || return
308+
valgrind "${1:?}" "${test_args[@]:?}" || return
294309
echo "Running ${1:?}... done."
295310
}
296311
fi
@@ -304,7 +319,6 @@ else
304319
run_test ./src/mongocxx/test/test_command_monitoring_specs
305320
run_test ./src/mongocxx/test/test_instance
306321
run_test ./src/mongocxx/test/test_transactions_specs
307-
run_test ./src/mongocxx/test/test_logging
308322
run_test ./src/mongocxx/test/test_retryable_reads_specs
309323
run_test ./src/mongocxx/test/test_read_write_concern_specs
310324
run_test ./src/mongocxx/test/test_unified_format_specs

src/bsoncxx/lib/bsoncxx/v1/exception.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//
1818

1919
#include <string>
20+
#include <system_error>
2021

2122
#include <bsoncxx/private/immortal.hh>
2223
#include <bsoncxx/private/type_traits.hh>

src/mongocxx/include/mongocxx/v1/exception.hpp

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
#include <mongocxx/v1/detail/prelude.hpp>
2222

23+
#include <bsoncxx/v1/detail/macros.hpp>
24+
25+
#include <mongocxx/v1/config/export.hpp>
26+
27+
#include <system_error>
28+
#include <type_traits>
29+
2330
namespace mongocxx {
2431
namespace v1 {
2532

@@ -28,14 +35,60 @@ namespace v1 {
2835
///
2936
/// @attention This feature is experimental! It is not ready for use!
3037
///
31-
enum class source_errc {};
38+
enum class source_errc {
39+
zero, ///< Zero.
40+
mongocxx, ///< From the mongocxx library.
41+
mongoc, ///< From the mongoc library.
42+
mongocrypt, ///< From the mongocrypt library.
43+
server, ///< From the MongoDB server.
44+
};
45+
46+
///
47+
/// The error category for @ref mongocxx::v1::source_errc.
48+
///
49+
/// @attention This feature is experimental! It is not ready for use!
50+
///
51+
MONGOCXX_ABI_EXPORT_CDECL(std::error_category const&) source_error_category();
52+
53+
///
54+
/// Support implicit conversion to `std::error_condition`.
55+
///
56+
/// @attention This feature is experimental! It is not ready for use!
57+
///
58+
inline std::error_condition make_error_condition(source_errc code) {
59+
return {static_cast<int>(code), v1::source_error_category()};
60+
}
3261

3362
///
3463
/// Enumeration identifying the type (cause) of a @ref mongocxx::v1 error.
3564
///
3665
/// @attention This feature is experimental! It is not ready for use!
3766
///
38-
enum class type_errc {};
67+
enum class type_errc {
68+
zero, ///< Zero.
69+
invalid_argument, ///< An invalid argument passed to the throwing function.
70+
runtime_error, ///< An erroneous condition was detected at runtime.
71+
};
72+
73+
///
74+
/// The error category for @ref mongocxx::v1::type_errc.
75+
///
76+
/// @attention This feature is experimental! It is not ready for use!
77+
///
78+
MONGOCXX_ABI_EXPORT_CDECL(std::error_category const&) type_error_category();
79+
80+
///
81+
/// Support implicit conversion to `std::error_condition`.
82+
///
83+
/// @attention This feature is experimental! It is not ready for use!
84+
///
85+
inline std::error_condition make_error_condition(type_errc code) {
86+
return {static_cast<int>(code), v1::type_error_category()};
87+
}
88+
89+
BSONCXX_PRIVATE_WARNINGS_PUSH();
90+
BSONCXX_PRIVATE_WARNINGS_DISABLE(MSVC(4251));
91+
BSONCXX_PRIVATE_WARNINGS_DISABLE(MSVC(4275));
3992

4093
///
4194
/// Base class for all exceptions thrown by @ref mongocxx::v1.
@@ -45,11 +98,29 @@ enum class type_errc {};
4598
///
4699
/// @attention This feature is experimental! It is not ready for use!
47100
///
48-
class exception {};
101+
class exception : public std::system_error {
102+
public:
103+
~exception() override;
104+
105+
exception(exception&&) noexcept = default;
106+
exception& operator=(exception&&) noexcept = default;
107+
exception(exception const&) = default;
108+
exception& operator=(exception const&) = default;
109+
110+
using std::system_error::system_error;
111+
};
112+
113+
BSONCXX_PRIVATE_WARNINGS_POP();
49114

50115
} // namespace v1
51116
} // namespace mongocxx
52117

118+
template <>
119+
struct std::is_error_condition_enum<mongocxx::v1::source_errc> : true_type {};
120+
121+
template <>
122+
struct std::is_error_condition_enum<mongocxx::v1::type_errc> : true_type {};
123+
53124
#include <mongocxx/v1/detail/postlude.hpp>
54125

55126
///

src/mongocxx/include/mongocxx/v1/instance.hpp

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020

2121
#include <mongocxx/v1/detail/prelude.hpp>
2222

23+
#include <mongocxx/v1/logger-fwd.hpp>
24+
25+
#include <mongocxx/v1/config/export.hpp>
26+
27+
#include <memory>
28+
#include <system_error>
29+
#include <type_traits>
30+
2331
namespace mongocxx {
2432
namespace v1 {
2533

@@ -50,15 +58,123 @@ namespace v1 {
5058
/// @par Special exemptions
5159
/// Only the following API are permitted to be used outside the lifetime of an instance object:
5260
/// - @ref mongocxx::v1::logger
61+
/// - @ref mongocxx::v1::default_logger
5362
///
5463
/// @see
5564
/// - [Initialization and Cleanup (mongoc)](https://mongoc.org/libmongoc/current/init-cleanup.html)
5665
///
57-
class instance {};
66+
class instance {
67+
private:
68+
class impl;
69+
std::unique_ptr<impl> _impl;
70+
71+
public:
72+
///
73+
/// Cleanup the mongocxx (and mongoc) library.
74+
///
75+
/// Calls [`mongoc_cleanup()`](https://mongoc.org/libmongoc/current/mongoc_cleanup.html).
76+
///
77+
MONGOCXX_ABI_EXPORT_CDECL() ~instance();
78+
79+
///
80+
/// This class is not moveable.
81+
///
82+
instance(instance&&) = delete;
83+
84+
///
85+
/// This class is not moveable.
86+
///
87+
instance& operator=(instance&&) = delete;
88+
89+
///
90+
/// This class is not copyable.
91+
///
92+
instance(instance const&) = delete;
93+
94+
///
95+
/// This class is not copyable.
96+
///
97+
instance& operator=(instance const&) = delete;
98+
99+
///
100+
/// Initialize the mongoc library with unstructured log messages disabled.
101+
///
102+
/// Calls [`mongoc_init()`](https://mongoc.org/libmongoc/current/mongoc_init.html) after disabling unstructured
103+
/// log messages by calling `mongoc_log_set_handler(nullptr, nullptr)`.
104+
///
105+
/// @important To use mongoc's default log message handler, construct this object with
106+
/// @ref instance(v1::default_logger tag) instead.
107+
///
108+
/// @throws mongocxx::v1::exception with @ref mongocxx::v1::instance::errc::multiple_instances if an `instance`
109+
/// object has already been created.
110+
///
111+
/// @see
112+
/// - [Custom Log Handlers (mongoc)](https://mongoc.org/libmongoc/current/unstructured_log.html#custom-log-handlers)
113+
///
114+
MONGOCXX_ABI_EXPORT_CDECL() instance();
115+
116+
///
117+
/// Initialize the mongoc library with the custom unstructured log message handler.
118+
///
119+
/// Calls [`mongoc_init`](https://mongoc.org/libmongoc/current/mongoc_init.html) after registering the custom
120+
/// unstructured log handler by calling `mongoc_log_set_handler()`.
121+
///
122+
/// @param handler Disable unstructured logging when null.
123+
///
124+
/// @throws mongocxx::v1::exception with @ref mongocxx::v1::instance::errc::multiple_instances if an `instance`
125+
/// object has already been created.
126+
///
127+
/// @see
128+
/// - [Custom Log Handlers (mongoc)](https://mongoc.org/libmongoc/current/unstructured_log.html#custom-log-handlers)
129+
///
130+
explicit MONGOCXX_ABI_EXPORT_CDECL() instance(std::unique_ptr<v1::logger> handler);
131+
132+
///
133+
/// Initialize the mongoc library with its default unstructured log handler.
134+
///
135+
/// Calls [`mongoc_init`](https://mongoc.org/libmongoc/current/mongoc_init.html) without registering any custom
136+
/// unstructured log handler.
137+
///
138+
/// @param tag Unused: only for overload resolution.
139+
///
140+
/// @throws mongocxx::v1::exception with @ref mongocxx::v1::instance::errc::multiple_instances if an `instance`
141+
/// object has already been created.
142+
///
143+
explicit MONGOCXX_ABI_EXPORT_CDECL() instance(v1::default_logger tag);
144+
145+
///
146+
/// Errors codes which may be returned by @ref mongocxx::v1::instance.
147+
///
148+
/// @attention This feature is experimental! It is not ready for use!
149+
///
150+
enum class errc {
151+
zero, ///< Zero.
152+
multiple_instances, ///< Cannot construct multiple instance objects in a given process.
153+
};
154+
155+
///
156+
/// The error category for @ref mongocxx::v1::instance::errc.
157+
///
158+
/// @attention This feature is experimental! It is not ready for use!
159+
///
160+
static MONGOCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
161+
162+
///
163+
/// Support implicit conversion to `std::error_code`.
164+
///
165+
/// @attention This feature is experimental! It is not ready for use!
166+
///
167+
friend std::error_code make_error_code(errc v) {
168+
return {static_cast<int>(v), error_category()};
169+
}
170+
};
58171

59172
} // namespace v1
60173
} // namespace mongocxx
61174

175+
template <>
176+
struct std::is_error_code_enum<mongocxx::v1::instance::errc> : true_type {};
177+
62178
#include <mongocxx/v1/detail/postlude.hpp>
63179

64180
///

src/mongocxx/include/mongocxx/v1/logger-fwd.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616

1717
#include <mongocxx/v1/detail/prelude.hpp>
1818

19+
//
20+
21+
#include <mongocxx/v1/config/export.hpp>
22+
1923
namespace mongocxx {
2024
namespace v1 {
2125

2226
enum class log_level;
2327

24-
class logger;
28+
class MONGOCXX_ABI_EXPORT logger;
29+
2530
class default_logger;
2631

2732
} // namespace v1

0 commit comments

Comments
 (0)