Skip to content

Commit

Permalink
ref(backend): Cleanup backend implementations (Mudlet#78)
Browse files Browse the repository at this point in the history
Cleans up the Backends implementation ahead of the refactor for Breakpad.
  • Loading branch information
jan-auer authored Oct 29, 2019
1 parent d3c8fef commit 4b1882e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static bool sdk_disabled() {

static void flush_scope() {
if (!sdk_disabled() && g_options->backend) {
g_options->backend->flush_scope_state(g_scope);
g_options->backend->flush_scope(g_scope);
}
}

Expand Down
24 changes: 0 additions & 24 deletions src/backends/base_backend.cpp

This file was deleted.

18 changes: 12 additions & 6 deletions src/backends/base_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ namespace sentry {
namespace backends {
class Backend {
public:
Backend();
virtual ~Backend();
virtual void start();
virtual void shutdown();
virtual void flush_scope_state(const sentry::Scope &scope);
virtual void add_breadcrumb(sentry::Value breadcrumb);
Backend() {
}
virtual ~Backend() {
}
virtual void start() {
}
virtual void shutdown() {
}
virtual void flush_scope(const sentry::Scope &scope) {
}
virtual void add_breadcrumb(sentry::Value breadcrumb) {
}

private:
Backend(const Backend &) = delete;
Expand Down
17 changes: 2 additions & 15 deletions src/backends/breakpad_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@
using namespace sentry;
using namespace backends;

class backends::BreakpadBackendImpl {
public:
BreakpadBackendImpl();
};

BreakpadBackendImpl::BreakpadBackendImpl() {
}

BreakpadBackend::BreakpadBackend()
: m_impl(new backends::BreakpadBackendImpl()) {
}

BreakpadBackend::~BreakpadBackend() {
delete m_impl;
BreakpadBackend::BreakpadBackend() {
}

void BreakpadBackend::start() {
}

void BreakpadBackend::flush_scope_state(const sentry::Scope &scope) {
void BreakpadBackend::flush_scope(const sentry::Scope &scope) {
}

void BreakpadBackend::add_breadcrumb(sentry::Value breadcrumb) {
Expand Down
7 changes: 2 additions & 5 deletions src/backends/breakpad_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@
namespace sentry {
namespace backends {

class BreakpadBackendImpl;

class BreakpadBackend : public Backend {
public:
BreakpadBackend();
~BreakpadBackend();

void start();
void flush_scope_state(const sentry::Scope &scope);
void flush_scope(const sentry::Scope &scope);
void add_breadcrumb(sentry::Value breadcrumb);

private:
BreakpadBackendImpl *m_impl;
};
} // namespace backends
} // namespace sentry
Expand Down
68 changes: 22 additions & 46 deletions src/backends/crashpad_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdio.h>
#include <atomic>
#include <map>
#include <mutex>
#include <string>
#include <vector>

Expand All @@ -12,38 +11,16 @@
#include "client/settings.h"

#include "../attachment.hpp"
#include "../internal.hpp"
#include "../options.hpp"
#include "../path.hpp"
#include "../value.hpp"

#include "crashpad_backend.hpp"

using namespace sentry;
using namespace backends;

class backends::CrashpadBackendImpl {
public:
CrashpadBackendImpl();

Path event_filename;
Path breadcrumb_filename;
std::mutex breadcrumb_lock;
int breadcrumb_fileid;
int breadcrumbs_in_segment;
};

CrashpadBackendImpl::CrashpadBackendImpl() {
breadcrumb_fileid = 0;
breadcrumbs_in_segment = 0;
}

CrashpadBackend::CrashpadBackend()
: m_impl(new backends::CrashpadBackendImpl()) {
}

CrashpadBackend::~CrashpadBackend() {
delete m_impl;
: breadcrumb_fileid(0), breadcrumbs_in_segment(0) {
}

void CrashpadBackend::start() {
Expand All @@ -64,19 +41,18 @@ void CrashpadBackend::start() {
base::FilePath(attachment.path().as_osstr()));
}

m_impl->event_filename = current_run_folder.join(SENTRY_EVENT_FILE_NAME);
event_filename = current_run_folder.join(SENTRY_EVENT_FILE);
file_attachments.emplace(
SENTRY_EVENT_FILE_ATTACHMENT_NAME,
base::FilePath(
current_run_folder.join(SENTRY_EVENT_FILE_NAME).as_osstr()));
SENTRY_EVENT_FILE,
base::FilePath(current_run_folder.join(SENTRY_EVENT_FILE).as_osstr()));
file_attachments.emplace(
SENTRY_BREADCRUMB1_FILE_ATTACHMENT_NAME,
SENTRY_BREADCRUMBS1_FILE,
base::FilePath(
current_run_folder.join(SENTRY_BREADCRUMB1_FILE).as_osstr()));
current_run_folder.join(SENTRY_BREADCRUMBS1_FILE).as_osstr()));
file_attachments.emplace(
SENTRY_BREADCRUMB2_FILE_ATTACHMENT_NAME,
SENTRY_BREADCRUMBS2_FILE,
base::FilePath(
current_run_folder.join(SENTRY_BREADCRUMB2_FILE).as_osstr()));
current_run_folder.join(SENTRY_BREADCRUMBS2_FILE).as_osstr()));

std::vector<std::string> arguments;
arguments.push_back("--no-rate-limit");
Expand Down Expand Up @@ -109,10 +85,10 @@ void CrashpadBackend::start() {
}
}

void CrashpadBackend::flush_scope_state(const sentry::Scope &scope) {
void CrashpadBackend::flush_scope(const sentry::Scope &scope) {
mpack_writer_t writer;
mpack_writer_init_stdfile(&writer, m_impl->event_filename.open("wb"), true);
Value event = Value::new_event();
mpack_writer_init_stdfile(&writer, event_filename.open("wb"), true);
Value event = Value::new_object();
scope.apply_to_event(event, SENTRY_SCOPE_NONE);
event.to_msgpack(&writer);
mpack_error_t err = mpack_writer_destroy(&writer);
Expand All @@ -123,27 +99,27 @@ void CrashpadBackend::flush_scope_state(const sentry::Scope &scope) {
}

void CrashpadBackend::add_breadcrumb(sentry::Value breadcrumb) {
std::lock_guard<std::mutex> _blck(m_impl->breadcrumb_lock);
std::lock_guard<std::mutex> _blck(breadcrumb_lock);
const sentry_options_t *opts = sentry_get_options();

if (m_impl->breadcrumbs_in_segment == 0 ||
m_impl->breadcrumbs_in_segment == SENTRY_BREADCRUMBS_MAX) {
m_impl->breadcrumb_fileid = m_impl->breadcrumb_fileid == 0 ? 1 : 0;
m_impl->breadcrumbs_in_segment = 0;
m_impl->breadcrumb_filename =
if (breadcrumbs_in_segment == 0 ||
breadcrumbs_in_segment == SENTRY_BREADCRUMBS_MAX) {
breadcrumb_fileid = breadcrumb_fileid == 0 ? 1 : 0;
breadcrumbs_in_segment = 0;
breadcrumb_filename =
opts->runs_folder.join(opts->run_id.c_str())
.join(m_impl->breadcrumb_fileid == 0 ? SENTRY_BREADCRUMB1_FILE
: SENTRY_BREADCRUMB2_FILE);
.join(breadcrumb_fileid == 0 ? SENTRY_BREADCRUMBS1_FILE
: SENTRY_BREADCRUMBS2_FILE);
}

std::string mpack = breadcrumb.to_msgpack();
FILE *file = m_impl->breadcrumb_filename.open(
m_impl->breadcrumbs_in_segment == 0 ? "wb" : "a");
FILE *file =
breadcrumb_filename.open(breadcrumbs_in_segment == 0 ? "wb" : "a");
if (file) {
fwrite(mpack.c_str(), 1, mpack.size(), file);
fclose(file);
}

m_impl->breadcrumbs_in_segment++;
breadcrumbs_in_segment++;
}
#endif
16 changes: 11 additions & 5 deletions src/backends/crashpad_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@
#define SENTRY_BACKENDS_CRASHPAD_HPP_INCLUDED
#ifdef SENTRY_WITH_CRASHPAD_BACKEND

#include <mutex>

#include "../internal.hpp"
#include "../path.hpp"
#include "../scope.hpp"

#include "base_backend.hpp"

namespace sentry {
namespace backends {

class CrashpadBackendImpl;

class CrashpadBackend : public Backend {
public:
CrashpadBackend();
~CrashpadBackend();

void start();
void flush_scope_state(const sentry::Scope &scope);
void flush_scope(const sentry::Scope &scope);
void add_breadcrumb(sentry::Value breadcrumb);

private:
CrashpadBackendImpl *m_impl;
Path event_filename;
Path breadcrumb_filename;
std::mutex breadcrumb_lock;
int breadcrumb_fileid;
int breadcrumbs_in_segment;
};
} // namespace backends
} // namespace sentry
Expand Down
12 changes: 4 additions & 8 deletions src/internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@

#define SENTRY_BREADCRUMBS_MAX 100
static const char *SENTRY_RUNS_FOLDER = "sentry-runs";
static const char *SENTRY_EVENT_FILE_NAME = "sentry-event.mp";
static const char *SENTRY_BREADCRUMB1_FILE = "sentry-breadcrumb1.mp";
static const char *SENTRY_BREADCRUMB2_FILE = "sentry-breadcrumb2.mp";
static const char *SENTRY_EVENT_FILE_ATTACHMENT_NAME = "__sentry-event";
static const char *SENTRY_BREADCRUMB1_FILE_ATTACHMENT_NAME =
"__sentry-breadcrumb1";
static const char *SENTRY_BREADCRUMB2_FILE_ATTACHMENT_NAME =
"__sentry-breadcrumb2";
static const char *SENTRY_PENDING_FOLDER = "sentry-pending";
static const char *SENTRY_EVENT_FILE = "__sentry-event";
static const char *SENTRY_BREADCRUMBS1_FILE = "__sentry-breadcrumb1";
static const char *SENTRY_BREADCRUMBS2_FILE = "__sentry-breadcrumb2";

#include "value.hpp"

Expand Down

0 comments on commit 4b1882e

Please sign in to comment.