Skip to content

Commit

Permalink
feat replace blocking console log with async log file
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyncs committed Jan 31, 2024
1 parent 29b74d5 commit 0968c00
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/turbomind/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,27 @@ set_property(TARGET cuda_utils PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET cuda_utils PROPERTY CUDA_RESOLVE_DEVICE_SYMBOLS ON)
target_link_libraries(cuda_utils PUBLIC CUDA::cudart)

if (UNIX)
include(FetchContent)
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.13.0
)
FetchContent_MakeAvailable(spdlog)
endif ()

add_library(logger STATIC logger.cc)
set_property(TARGET logger PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET logger PROPERTY CUDA_RESOLVE_DEVICE_SYMBOLS ON)

target_link_libraries(logger PUBLIC CUDA::cudart)

if (UNIX)
target_include_directories(logger PRIVATE _deps/spdlog-build/CMakeFiles/spdlog.dir/include)
target_link_libraries(logger PUBLIC spdlog::spdlog)
endif ()

add_library(cublasAlgoMap STATIC cublasAlgoMap.cc)
set_property(TARGET cublasAlgoMap PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET cublasAlgoMap PROPERTY CUDA_RESOLVE_DEVICE_SYMBOLS ON)
Expand Down
41 changes: 41 additions & 0 deletions src/turbomind/utils/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@
#include "src/turbomind/utils/logger.h"
#include <cuda_runtime.h>

#ifndef _MSC_VER
#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/hourly_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#endif

namespace turbomind {

Logger::Logger()
{
#ifndef _MSC_VER
// TODO: use config
SpdLogger::get_instance().set_log_path("/var/log/lmdeploy.log");
SpdLogger::get_instance().init();
#endif
char* is_first_rank_only_char = std::getenv("TM_LOG_FIRST_RANK_ONLY");
bool is_first_rank_only =
(is_first_rank_only_char != nullptr && std::string(is_first_rank_only_char) == "ON") ? true : false;
Expand Down Expand Up @@ -56,4 +68,33 @@ Logger::Logger()
}
}

#ifndef _MSC_VER
void SpdLogger::init()
{
if (inited_) {
return;
}

spdlog::init_thread_pool(8192, 1);

// rotate 500 MB
auto basic_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(path_, 500 * 1024 * 1024, 0);
// async
auto logger = std::make_shared<spdlog::async_logger>(
"logger", basic_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::overrun_oldest);
logger->set_level(spdlog::level::trace);
// ms, thread_id
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%t] %v");

logger_ = logger;

spdlog::register_logger(logger_);

// real-time refresh
logger_->flush_on(spdlog::level::trace);

inited_ = true;
}
#endif

} // namespace turbomind
54 changes: 47 additions & 7 deletions src/turbomind/utils/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include <map>
#include <string>

#ifndef _MSC_VER
#include <spdlog/spdlog.h>
#endif

#include "src/turbomind/utils/string_utils.h"

namespace turbomind {
Expand All @@ -30,11 +34,41 @@ namespace turbomind {
#undef ERROR
#endif

#ifndef _MSC_VER
class SpdLogger {
public:
static SpdLogger& get_instance()
{
static SpdLogger spdlogger;
return spdlogger;
}
spdlog::logger* get_logger()
{
return logger_.get();
}

void set_log_path(const std::string& path)
{
path_ = path;
}
void init();

private:
SpdLogger() = default;
~SpdLogger() = default;
SpdLogger(const SpdLogger&) = delete;
SpdLogger& operator=(const SpdLogger&) = delete;

std::string path_;
bool inited_ = false;
std::shared_ptr<spdlog::logger> logger_;
};
#endif

class Logger {

public:
enum Level
{
enum Level {
TRACE = 0,
DEBUG = 10,
INFO = 20,
Expand All @@ -47,28 +81,34 @@ class Logger {
thread_local Logger instance;
return instance;
}
Logger(Logger const&) = delete;
Logger(Logger const&) = delete;
void operator=(Logger const&) = delete;

template<typename... Args>
void log(const Level level, const std::string format, const Args&... args)
{
if (level_ <= level) {
std::string fmt = getPrefix(level) + format + "\n";
// FILE* out = level_ < WARNING ? stdout : stderr;
std::string fmt = getPrefix(level) + format + "\n";
std::string logstr = fmtstr(fmt, args...);
#ifdef _MSC_VER
fprintf(stderr, "%s", logstr.c_str());
#else
SpdLogger::get_instance().get_logger()->log(spdlog::level::trace, logstr);
#endif
}
}

template<typename... Args>
void log(const Level level, const int rank, const std::string format, const Args&... args)
{
if (level_ <= level) {
std::string fmt = getPrefix(level, rank) + format + "\n";
// FILE* out = level_ < WARNING ? stdout : stderr;
std::string fmt = getPrefix(level, rank) + format + "\n";
std::string logstr = fmtstr(fmt, args...);
#ifdef _MSC_VER
fprintf(stderr, "%s", logstr.c_str());
#else
SpdLogger::get_instance().get_logger()->log(spdlog::level::trace, logstr);
#endif
}
}

Expand Down

0 comments on commit 0968c00

Please sign in to comment.