Skip to content

Commit

Permalink
Merge branch 'main' of github.com:rcaelers/workrave
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaelers committed Nov 25, 2023
2 parents e5687ea + 0fd36cc commit 3240ff5
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/crash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(src)
add_subdirectory(test)
1 change: 1 addition & 0 deletions libs/crash/src/CrashReporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ CrashReporter::Pimpl::init()
#endif

TRACE_MSG("handler = {}", app_dir.string());
TRACE_MSG("report = {}", temp_dir.string());

attachments.emplace_back(log_dir / "workrave.log");
attachments.emplace_back(log_dir / "workrave.1.log");
Expand Down
21 changes: 21 additions & 0 deletions libs/crash/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
if (HAVE_CRASHPAD AND HAVE_TESTS)
add_executable(crash crash.cc)

target_link_libraries(crash workrave-libs-crash)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(crash PRIVATE -gcodeview)
target_link_options(crash PRIVATE -Wl,-pdb=)
endif()

if (MINGW)
if (DUMP_SYMS)
add_custom_command(TARGET crash POST_BUILD
COMMAND ${DUMP_SYMS} $<TARGET_FILE:crash> -o ${CMAKE_BINARY_DIR}/crash.sym)
add_dependencies(crash dump_syms)
endif()

endif()

install(TARGETS crash RUNTIME DESTINATION ${BINDIR} BUNDLE DESTINATION .)
endif()
136 changes: 136 additions & 0 deletions libs/crash/test/crash.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (C) 2023 Rob Caelers & Raymond Penners
// All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#if defined(HAVE_CRASH_REPORT)
# include "crash/CrashReporter.hh"
#endif

#include "debug.hh"
#include "utils/Paths.hh"

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#if SPDLOG_VERSION >= 10600
# include <spdlog/pattern_formatter.h>
#endif
#if SPDLOG_VERSION >= 10801
# include <spdlog/cfg/env.h>
#endif

extern "C" int run(int argc, char **argv);

using namespace workrave::utils;

static void
init_logging()
{
const auto *const log_file = "crash.log";

auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file, 1024 * 1024, 5, true);

auto logger{std::make_shared<spdlog::logger>("workrave", std::initializer_list<spdlog::sink_ptr>{file_sink, console_sink})};
logger->flush_on(spdlog::level::critical);
logger->set_level(spdlog::level::trace);
spdlog::set_default_logger(logger);

spdlog::set_level(spdlog::level::trace);
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%-5l%$] %v");
spdlog::info("Workrave started");
spdlog::info("Log file: {}", log_file);

const auto *const trace_file = "crash-trace.log";
auto trace_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(trace_file, 1024 * 1024, 10, true);
auto tracer = std::make_shared<spdlog::logger>("trace", trace_sink);
tracer->set_level(spdlog::level::trace);
tracer->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%t] %v");
spdlog::info("Trace file: {}", trace_file);

ScopedTrace::init(tracer);
}

static void
crash2()
{
int *p = 0;
*p = 0;
TRACE_ENTRY();
}

static void
crash1()
{
TRACE_ENTRY();
crash2();
}

static void
crash()
{
TRACE_ENTRY();
crash1();
}

int
run(int argc, char **argv)
{
init_logging();
TRACE_ENTRY();

try
{
workrave::crash::CrashReporter::instance().init();
}
catch (std::exception &e)
{
TRACE_VAR(std::string("Crashhandler init exception:") + e.what());
}
crash();
return 0;
}

#if !defined(PLATFORM_OS_WINDOWS) || (!defined(PLATFORM_OS_WINDOWS_NATIVE) && !defined(NDEBUG))
int
main(int argc, char **argv)
{
int ret = run(argc, argv);
return ret;
}

#else

# include <windows.h>

int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
(void)hInstance;
(void)hPrevInstance;
(void)iCmdShow;

char *argv[] = {szCmdLine};
run(sizeof(argv) / sizeof(argv[0]), argv);
return (0);
}

#endif

0 comments on commit 3240ff5

Please sign in to comment.