diff --git a/src/assert.cc b/src/assert.cc index eeb4b36e..a1f82672 100644 --- a/src/assert.cc +++ b/src/assert.cc @@ -1,8 +1,8 @@ #include "assert.hh" -#include "exception.hh" -#include "debug.hh" #include "backtrace.hh" +#include "buffer_utils.hh" +#include "exception.hh" #if defined(__CYGWIN__) #include @@ -48,7 +48,7 @@ bool notify_fatal_error(const String& msg) void on_assert_failed(const char* message) { String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), Backtrace{}.desc()); - write_debug(format("assert failed: '{}'\n{}", message, debug_info)); + write_to_debug_buffer(format("assert failed: '{}'\n{}", message, debug_info)); const auto msg = format("{}\n[Debug Infos]\n{}", message, debug_info); if (not notify_fatal_error(msg)) diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index ff05e781..d208386e 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -166,4 +166,23 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) return buffer; } +void write_to_debug_buffer(StringView str) +{ + if (not BufferManager::has_instance()) + { + write(2, str.data(), (int)str.length()); + write(2, "\n", 1); + return; + } + + const StringView debug_buffer_name = "*debug*"; + if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name)) + buffer->insert(buffer->end(), str); + else + { + String line = str + ((str.empty() or str.back() != '\n') ? "\n" : ""); + create_buffer_from_data(line, debug_buffer_name, Buffer::Flags::NoUndo); + } +} + } diff --git a/src/buffer_utils.hh b/src/buffer_utils.hh index 591ea455..33609150 100644 --- a/src/buffer_utils.hh +++ b/src/buffer_utils.hh @@ -36,6 +36,8 @@ Buffer* create_buffer_from_data(StringView data, StringView name, Buffer::Flags flags, time_t fs_timestamp = InvalidTime); +void write_to_debug_buffer(StringView str); + } #endif // buffer_utils_hh_INCLUDED diff --git a/src/commands.cc b/src/commands.cc index ce32f08e..821a42e9 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -9,7 +9,6 @@ #include "completion.hh" #include "containers.hh" #include "context.hh" -#include "debug.hh" #include "event_manager.hh" #include "face_registry.hh" #include "file.hh" @@ -870,7 +869,7 @@ const CommandDesc echo_cmd = { { String message = join(parser, ' ', false); if (parser.get_switch("debug")) - write_debug(message); + write_to_debug_buffer(message); else { auto face = get_face(parser.get_switch("color").value_or("StatusLine").str()); @@ -898,34 +897,34 @@ const CommandDesc debug_cmd = { { if (parser[0] == "info") { - write_debug(format("pid: {}", getpid())); - write_debug(format("session: {}", Server::instance().session())); + write_to_debug_buffer(format("pid: {}", getpid())); + write_to_debug_buffer(format("session: {}", Server::instance().session())); } else if (parser[0] == "buffers") { - write_debug("Buffers:"); + write_to_debug_buffer("Buffers:"); for (auto& buffer : BufferManager::instance()) - write_debug(buffer->debug_description()); + write_to_debug_buffer(buffer->debug_description()); } else if (parser[0] == "options") { - write_debug("Options:"); + write_to_debug_buffer("Options:"); for (auto& option : context.options().flatten_options()) - write_debug(format(" * {}: {}", option->name(), option->get_as_string())); + write_to_debug_buffer(format(" * {}: {}", option->name(), option->get_as_string())); } else if (parser[0] == "memory") { auto total = 0; - write_debug("Memory usage:"); + write_to_debug_buffer("Memory usage:"); for (int domain = 0; domain < (int)MemoryDomain::Count; ++domain) { size_t count = domain_allocated_bytes[domain]; total += count; - write_debug(format(" {}: {}", domain_name((MemoryDomain)domain), count)); + write_to_debug_buffer(format(" {}: {}", domain_name((MemoryDomain)domain), count)); } - write_debug(format(" Total: {}", total)); + write_to_debug_buffer(format(" Total: {}", total)); #if defined(__GLIBC__) || defined(__CYGWIN__) - write_debug(format(" Malloced: {}", mallinfo().uordblks)); + write_to_debug_buffer(format(" Malloced: {}", mallinfo().uordblks)); #endif } else if (parser[0] == "shared-strings") @@ -954,7 +953,7 @@ const CommandDesc source_cmd = { } catch (Kakoune::runtime_error& err) { - write_debug(format("{}:{}", parser[0], err.what())); + write_to_debug_buffer(format("{}:{}", parser[0], err.what())); throw; } } diff --git a/src/debug.cc b/src/debug.cc deleted file mode 100644 index 799bffb8..00000000 --- a/src/debug.cc +++ /dev/null @@ -1,31 +0,0 @@ -#include "debug.hh" - -#include "assert.hh" -#include "buffer.hh" -#include "buffer_manager.hh" -#include "buffer_utils.hh" -#include "string.hh" - -namespace Kakoune -{ - -void write_debug(StringView str) -{ - if (not BufferManager::has_instance()) - { - write(2, str.data(), (int)str.length()); - write(2, "\n", 1); - return; - } - - const StringView debug_buffer_name = "*debug*"; - if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name)) - buffer->insert(buffer->end(), str); - else - { - String line = str + ((str.empty() or str.back() != '\n') ? "\n" : ""); - create_buffer_from_data(line, debug_buffer_name, Buffer::Flags::NoUndo); - } -} - -} diff --git a/src/debug.hh b/src/debug.hh deleted file mode 100644 index 02e46708..00000000 --- a/src/debug.hh +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef debug_hh_INCLUDED -#define debug_hh_INCLUDED - -namespace Kakoune -{ - -class StringView; - -void write_debug(StringView str); - -} - -#endif // debug_hh_INCLUDED diff --git a/src/file.cc b/src/file.cc index 9f299a93..bea46dbb 100644 --- a/src/file.cc +++ b/src/file.cc @@ -4,7 +4,6 @@ #include "buffer.hh" #include "buffer_manager.hh" #include "buffer_utils.hh" -#include "debug.hh" #include "unicode.hh" #include "regex.hh" #include "string.hh" diff --git a/src/hook_manager.cc b/src/hook_manager.cc index da57a1da..2afb80ac 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -2,7 +2,7 @@ #include "containers.hh" #include "context.hh" -#include "debug.hh" +#include "buffer_utils.hh" #include "regex.hh" namespace Kakoune @@ -60,7 +60,7 @@ void HookManager::run_hook(StringView hook_name, } catch (runtime_error& err) { - write_debug(format("error running hook {}/{}: {}", + write_to_debug_buffer(format("error running hook {}/{}: {}", hook_name, hook.first, err.what())); } } diff --git a/src/insert_completer.cc b/src/insert_completer.cc index 24863676..8f3b98c6 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -3,7 +3,6 @@ #include "buffer_manager.hh" #include "buffer_utils.hh" #include "context.hh" -#include "debug.hh" #include "display_buffer.hh" #include "face_registry.hh" #include "file.hh" @@ -430,7 +429,7 @@ bool InsertCompleter::try_complete(CompleteFunc complete_func) } catch (runtime_error& e) { - write_debug(format("error while trying to run completer: {}", e.what())); + write_to_debug_buffer(format("error while trying to run completer: {}", e.what())); return false; } if (not m_completions.is_valid()) diff --git a/src/main.cc b/src/main.cc index 3d79d16a..a0bbf0f8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,7 +7,6 @@ #include "commands.hh" #include "containers.hh" #include "context.hh" -#include "debug.hh" #include "event_manager.hh" #include "face_registry.hh" #include "file.hh" @@ -383,7 +382,7 @@ int run_server(StringView session, StringView init_command, register_commands(); register_highlighters(); - write_debug("*** This is the debug buffer, where debug info will be written ***"); + write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***"); Server server(session.empty() ? to_string(getpid()) : session.str()); @@ -395,11 +394,11 @@ int run_server(StringView session, StringView init_command, } catch (Kakoune::runtime_error& error) { - write_debug(format("error while parsing kakrc:\n {}", error.what())); + write_to_debug_buffer(format("error while parsing kakrc:\n {}", error.what())); } catch (Kakoune::client_removed&) { - write_debug("error while parsing kakrc: asked to quit"); + write_to_debug_buffer("error while parsing kakrc: asked to quit"); } { @@ -419,7 +418,7 @@ int run_server(StringView session, StringView init_command, } catch (Kakoune::runtime_error& error) { - write_debug(format("error while opening command line files: {}", error.what())); + write_to_debug_buffer(format("error while opening command line files: {}", error.what())); } else new Buffer("*scratch*", Buffer::Flags::None); diff --git a/src/normal.cc b/src/normal.cc index f2b983df..20c2ce5a 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -2,15 +2,15 @@ #include "buffer.hh" #include "buffer_manager.hh" +#include "buffer_utils.hh" #include "client_manager.hh" #include "command_manager.hh" #include "commands.hh" #include "containers.hh" #include "context.hh" -#include "debug.hh" #include "face_registry.hh" -#include "flags.hh" #include "file.hh" +#include "flags.hh" #include "option_manager.hh" #include "register_manager.hh" #include "selectors.hh" diff --git a/src/remote.cc b/src/remote.cc index 3875b3fb..61434c38 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -1,9 +1,9 @@ #include "remote.hh" #include "buffer_manager.hh" +#include "buffer_utils.hh" #include "client_manager.hh" #include "command_manager.hh" -#include "debug.hh" #include "display_buffer.hh" #include "event_manager.hh" @@ -289,12 +289,12 @@ RemoteUI::RemoteUI(int socket) m_input_callback(mode); }) { - write_debug(format("remote client connected: {}", m_socket_watcher.fd())); + write_to_debug_buffer(format("remote client connected: {}", m_socket_watcher.fd())); } RemoteUI::~RemoteUI() { - write_debug(format("remote client disconnected: {}", m_socket_watcher.fd())); + write_to_debug_buffer(format("remote client disconnected: {}", m_socket_watcher.fd())); m_socket_watcher.close_fd(); } @@ -402,7 +402,7 @@ Key RemoteUI::get_key() } catch (socket_error&) { - write_debug("ungraceful deconnection detected"); + write_to_debug_buffer("ungraceful deconnection detected"); throw client_removed{}; } } @@ -585,7 +585,7 @@ private: } catch (runtime_error& e) { - write_debug(format("error running command '{}': {}", + write_to_debug_buffer(format("error running command '{}': {}", m_buffer, e.what())); } catch (client_removed&) {} diff --git a/src/shared_string.cc b/src/shared_string.cc index 2aec6920..b594a56f 100644 --- a/src/shared_string.cc +++ b/src/shared_string.cc @@ -1,5 +1,5 @@ #include "shared_string.hh" -#include "debug.hh" +#include "buffer_utils.hh" namespace Kakoune { @@ -28,7 +28,7 @@ void StringRegistry::purge_unused() void StringRegistry::debug_stats() const { - write_debug("Shared Strings stats:"); + write_to_debug_buffer("Shared Strings stats:"); size_t total_refcount = 0; size_t total_size = 0; size_t count = m_strings.size(); @@ -37,8 +37,8 @@ void StringRegistry::debug_stats() const total_refcount += st.m_storage->refcount - 1; total_size += (int)st.m_storage->length; } - write_debug(format(" data size: {}, mean: {}", total_size, (float)total_size/count)); - write_debug(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count)); + write_to_debug_buffer(format(" data size: {}, mean: {}", total_size, (float)total_size/count)); + write_to_debug_buffer(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count)); } } diff --git a/src/shell_manager.cc b/src/shell_manager.cc index a7dae6cb..ce926ecb 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -1,7 +1,7 @@ #include "shell_manager.hh" #include "context.hh" -#include "debug.hh" +#include "buffer_utils.hh" #include "event_manager.hh" #include "file.hh" @@ -63,7 +63,7 @@ std::pair ShellManager::eval( } if (not child_stderr.empty()) - write_debug("shell stderr: <<<\n" + child_stderr + ">>>"); + write_to_debug_buffer(format("shell stderr: <<<\n{}>>>", child_stderr)); int status = 0; waitpid(pid, &status, 0);