Skip to content

Commit

Permalink
feat: add SentryHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 2, 2025
1 parent d8e3f06 commit ecee99b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ endif ()
# =======
# options
# =======
option(CODE_COVERAGE "Enable code coverage reporting" false)
option(ENDSTONE_ENABLE_DEVTOOLS "Build Endstone with DevTools enabled." false)
option(ENDSTONE_ENABLE_SENTRY "Enable Sentry integration for error reporting." false)
option(CODE_COVERAGE "Enable code coverage reporting" OFF)
option(ENDSTONE_ENABLE_DEVTOOLS "Build Endstone with DevTools enabled." OFF)
option(ENDSTONE_ENABLE_SENTRY "Enable Sentry integration for error reporting." OFF)
set(ENDSTONE_SENTRY_DSN "" CACHE STRING "Set the Sentry DSN (Data Source Name) for error tracking.")
set(ENDSTONE_SENTRY_ENVIRONMENT "production" CACHE STRING "Set the environment for Sentry (e.g., development, staging, production).")
set(ENDSTONE_SENTRY_ENVIRONMENT "development" CACHE STRING "Set the environment for Sentry (e.g., development, production).")

if (NOT BUILD_TESTING STREQUAL OFF)
enable_testing()
Expand Down
27 changes: 27 additions & 0 deletions include/endstone/detail/sentry_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#ifdef ENDSTONE_WITH_SENTRY

namespace endstone::detail {

class SentryHandler {
public:
SentryHandler();
~SentryHandler();
};

} // namespace endstone::detail
#endif
4 changes: 4 additions & 0 deletions src/endstone_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ add_library(endstone_runtime SHARED
hook.cpp
loader.cpp
main.cpp
sentry_handler.cpp
)
add_library(endstone::runtime ALIAS endstone_runtime)
target_link_libraries(endstone_runtime PRIVATE endstone::core funchook::funchook)
Expand All @@ -125,6 +126,9 @@ endif ()
if (ENDSTONE_ENABLE_SENTRY)
find_package(sentry REQUIRED)
target_link_libraries(endstone_runtime PRIVATE sentry::sentry)
target_compile_definitions(endstone_runtime PRIVATE ENDSTONE_WITH_SENTRY)
target_compile_definitions(endstone_runtime PRIVATE ENDSTONE_SENTRY_DSN="${ENDSTONE_SENTRY_DSN}")
target_compile_definitions(endstone_runtime PRIVATE ENDSTONE_SENTRY_ENVIRONMENT="${ENDSTONE_SENTRY_ENVIRONMENT}")
endif ()
if (WIN32)
target_link_libraries(endstone_runtime PRIVATE dbghelp.lib ws2_32.lib)
Expand Down
50 changes: 50 additions & 0 deletions src/endstone_runtime/sentry_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifdef ENDSTONE_WITH_SENTRY

#include "endstone/detail/sentry_handler.h"

#include <sentry.h>

namespace endstone::detail {

namespace {
sentry_value_t on_crash(const sentry_ucontext_t *ctx, sentry_value_t event, void *closure)
{
// TODO(misc): print stack traces here
return event;
}
} // namespace

SentryHandler::SentryHandler()
{
static_assert(sizeof(ENDSTONE_SENTRY_DSN) > 1, "The Sentry DSN is not set");
sentry_options_t *options = sentry_options_new();
sentry_options_set_dsn(options, ENDSTONE_SENTRY_DSN);
sentry_options_set_database_path(options, "data/.sentry-native");
// TODO: set crashpad handler path
sentry_options_set_release(options, "endstone@" ENDSTONE_VERSION);
sentry_options_set_environment(options, ENDSTONE_SENTRY_ENVIRONMENT);
sentry_init(options);
}

SentryHandler::~SentryHandler()
{
sentry_close();
}

} // namespace endstone::detail

#endif

0 comments on commit ecee99b

Please sign in to comment.