Skip to content
Open
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
17 changes: 11 additions & 6 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@ else()
message(WARNING "Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository.")
endif()

set(TEMPLATE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.cpp.in")
set(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/build-info.cpp")
set(BUILD_INFO_FILE_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build-info.cpp.in")
set(BUILD_INFO_FILE_OUT "${CMAKE_CURRENT_BINARY_DIR}/build-info.cpp")

configure_file(${TEMPLATE_FILE} ${OUTPUT_FILE})
configure_file(${BUILD_INFO_FILE_TMPL} ${BUILD_INFO_FILE_OUT})

set(TARGET llama-common-base)
add_library(${TARGET} STATIC ${OUTPUT_FILE})
add_library(${TARGET} STATIC
log.cpp
log.h
${BUILD_INFO_FILE_OUT}
build-info.h
)

target_include_directories(${TARGET} PUBLIC .)

target_link_libraries(${TARGET} PUBLIC ggml)

if (BUILD_SHARED_LIBS)
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
Expand Down Expand Up @@ -80,8 +87,6 @@ add_library(${TARGET}
json-partial.h
json-schema-to-grammar.cpp
llguidance.cpp
log.cpp
log.h
ngram-cache.cpp
ngram-cache.h
ngram-map.cpp
Expand Down
24 changes: 23 additions & 1 deletion common/log.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "common.h"
#include "log.h"

#include <chrono>
Expand Down Expand Up @@ -61,6 +60,29 @@ static std::vector<const char *> g_col = {
"",
};

static bool tty_can_use_colors() {
// Check NO_COLOR environment variable (https://no-color.org/)
if (const char * no_color = std::getenv("NO_COLOR")) {
if (no_color[0] != '\0') {
return false;
}
}

// Check TERM environment variable
if (const char * term = std::getenv("TERM")) {
if (std::strcmp(term, "dumb") == 0) {
return false;
}
}

// Check if stdout and stderr are connected to a terminal
// We check both because log messages can go to either
bool stdout_is_tty = isatty(fileno(stdout));
bool stderr_is_tty = isatty(fileno(stderr));

return stdout_is_tty || stderr_is_tty;
}

struct common_log_entry {
enum ggml_log_level level;

Expand Down
Loading