Skip to content

Add oslogger spd sink for macOS #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ set(sources
main.cpp
utils.cpp
)

if(APPLE)
set(headers ${headers} oslogger.hpp)
endif()

list(TRANSFORM headers PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/include/")
list(TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/src/")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/version.hpp.in version.hpp)
Expand Down
72 changes: 72 additions & 0 deletions include/oslogger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// oslogger.hpp
// opencl-language-server
//
// Created by Ilia Shoshin on 24/02/24.
//

#pragma once

#include <string>
#include <mutex>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/null_mutex.h>
#include <os/log.h>

namespace ocls {

template <typename Mutex>
class oslogger_sink : public spdlog::sinks::base_sink<Mutex>
{
protected:
void sink_it_(const spdlog::details::log_msg& msg) override
{
spdlog::memory_buf_t formatted;
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
const std::string message = fmt::to_string(formatted);
const std::string category(msg.logger_name.begin(), msg.logger_name.end());

/*
* From <os/log.h>
*
* The logging runtime maintains a global collection of all os_log_t
* objects, one per subsystem/category pair. The os_log_t for a given
* subsystem/category is created upon the first call to os_log_create and
* any subsequent calls return the same object. These objects are never
* deallocated, so dynamic creation (e.g. on a per-operation basis) is
* generally inappropriate.
*
* A value will always be returned to allow for dynamic enablement.
*/
const os_log_t log = os_log_create("com.galarius.vscode-opencl.server", category.c_str());

switch (msg.level)
{
case spdlog::level::trace:
case spdlog::level::debug:
os_log_debug(log, "%{public}s", message.c_str());
break;
case spdlog::level::info:
case spdlog::level::warn:
os_log_info(log, "%{public}s", message.c_str());
break;
case spdlog::level::err:
os_log_error(log, "%{public}s", message.c_str());
break;
case spdlog::level::critical:
os_log_fault(log, "%{public}s", message.c_str());
break;
default:
os_log(log, "%{public}s", message.c_str());
break;
}
}

void flush_() override {}
};

using oslogger_sink_mt = oslogger_sink<std::mutex>;
using oslogger_sink_st = oslogger_sink<spdlog::details::null_mutex>;

} // namespace ocls
21 changes: 13 additions & 8 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#include "log.hpp"
#include "oslogger.hpp"
#include "utils.hpp"

#include <iostream>
Expand All @@ -26,23 +27,27 @@ void ConfigureLogging(bool fileLogging, const std::string& filename, spdlog::lev
{
try
{
spdlog::sink_ptr sink;
std::vector<spdlog::sink_ptr> sinks;
if (fileLogging)
{
sink = std::make_shared<spdlog::sinks::basic_file_sink_st>(filename);
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_st>(filename));
}
else
{
sink = std::make_shared<spdlog::sinks::null_sink_st>();
sinks.push_back(std::make_shared<spdlog::sinks::null_sink_st>());
}
#if defined(__APPLE__)
sinks.push_back(std::make_shared<ocls::oslogger_sink_mt>());
#endif

spdlog::set_default_logger(std::make_shared<spdlog::logger>(LogName::main, sink));
spdlog::set_default_logger(std::make_shared<spdlog::logger>(LogName::main, sinks.begin(), sinks.end()));
spdlog::set_level(level);
std::vector<std::shared_ptr<spdlog::logger>> subLoggers = {
std::make_shared<spdlog::logger>(LogName::clinfo, sink),
std::make_shared<spdlog::logger>(LogName::diagnostics, sink),
std::make_shared<spdlog::logger>(LogName::jrpc, sink),
std::make_shared<spdlog::logger>(LogName::lsp, sink)};
std::make_shared<spdlog::logger>(LogName::clinfo, sinks.begin(), sinks.end()),
std::make_shared<spdlog::logger>(LogName::diagnostics, sinks.begin(), sinks.end()),
std::make_shared<spdlog::logger>(LogName::jrpc, sinks.begin(), sinks.end()),
std::make_shared<spdlog::logger>(LogName::lsp, sinks.begin(), sinks.end())
};

for (const auto& logger : subLoggers)
{
Expand Down