Move write_debug to buffer utils as write_to_debug_buffer

This commit is contained in:
Maxime Coste 2015-06-06 11:54:48 +01:00
parent be03db9a24
commit e6f0e8ef75
14 changed files with 56 additions and 83 deletions

View File

@ -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 <windows.h>
@ -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))

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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"

View File

@ -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()));
}
}

View File

@ -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())

View File

@ -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);

View File

@ -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"

View File

@ -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&) {}

View File

@ -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));
}
}

View File

@ -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<String, int> 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);