Crow  0.0.4
crow.hpp
1 /*
2  _____ _____ _____ _ _ _
3 | | __ | | | | | Crow - a Sentry client for C++
4 | --| -| | | | | | version 0.0.4
5 |_____|__|__|_____|_____| https://github.com/nlohmann/crow
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2018 Niels Lohmann <http://nlohmann.me>.
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29 
30 #ifndef NLOHMANN_CROW_HPP
31 #define NLOHMANN_CROW_HPP
32 
33 #include <future>
34 #include <mutex>
35 #include <thirdparty/json/json.hpp>
36 
37 using json = nlohmann::json;
38 
40 namespace nlohmann
41 {
42 class crow;
43 
47 class crow
48 {
49  public:
73  explicit crow(const std::string& dsn,
74  const json& context = nullptr,
75  bool install_handlers = true);
76 
83  void install_handler();
84 
94  crow(const crow& other);
95 
103  ~crow();
104 
121  void capture_message(const std::string& message,
122  const json& attributes = nullptr,
123  bool asynchronous = true);
124 
137  void capture_exception(const std::exception& exception,
138  const json& context = nullptr,
139  bool asynchronous = true,
140  bool handled = true);
141 
150  void add_breadcrumb(const std::string& message,
151  const json& attributes = nullptr);
152 
160  std::string get_last_event_id() const;
161 
178  const json& get_context() const;
179 
187  void add_user_context(const json& data);
188 
196  void add_tags_context(const json& data);
197 
205  void add_request_context(const json& data);
206 
214  void add_extra_context(const json& data);
215 
226  void merge_context(const json& context);
227 
235  void clear_context();
236 
241  private:
248  std::string post(json payload) const;
249 
250  void enqueue_post(bool asynchronous);
251 
257  static void new_termination_handler();
258 
259  private:
261  const bool m_enabled = true;
263  std::string m_public_key;
265  std::string m_secret_key;
267  std::string m_store_url;
269  json m_payload = {};
271  mutable std::future<std::string> m_pending_future;
273  std::terminate_handler existing_termination_handler = nullptr;
275  std::mutex m_payload_mutex;
277  mutable std::mutex m_pending_future_mutex;
279  static crow* m_client_that_installed_termination_handler;
280 };
281 
282 }
283 
284 #endif
void add_user_context(const json &data)
add elements to the "user" context for future events
Definition: crow.cpp:254
void add_request_context(const json &data)
add elements to the "request" context for future events
Definition: crow.cpp:266
const json & get_context() const
return current context
Definition: crow.cpp:249
void merge_context(const json &context)
add context information to payload for future events
Definition: crow.cpp:278
void install_handler()
install termination handler to handle uncaught exceptions
Definition: crow.cpp:88
crow(const std::string &dsn, const json &context=nullptr, bool install_handlers=true)
create a client
Definition: crow.cpp:51
void add_extra_context(const json &data)
add elements to the "extra" context for future events
Definition: crow.cpp:272
void capture_exception(const std::exception &exception, const json &context=nullptr, bool asynchronous=true, bool handled=true)
capture an exception
Definition: crow.cpp:158
std::string get_last_event_id() const
return the id of the last reported event
Definition: crow.cpp:229
namespace for Niels Lohmann
Definition: crow.hpp:40
void clear_context()
reset context for future events
Definition: crow.cpp:297
~crow()
destructor
Definition: crow.cpp:109
void add_breadcrumb(const std::string &message, const json &attributes=nullptr)
add a breadcrumb to the current context
Definition: crow.cpp:181
a C++ client for Sentry
Definition: crow.hpp:47
void capture_message(const std::string &message, const json &attributes=nullptr, bool asynchronous=true)
capture a message
Definition: crow.cpp:118
void add_tags_context(const json &data)
add elements to the "tags" context for future events
Definition: crow.cpp:260