From 62de8a62240273ac678325eb30125c41d4c6bffd Mon Sep 17 00:00:00 2001 From: staviq Date: Wed, 23 Aug 2023 02:27:26 +0200 Subject: [PATCH] initial, base LOG macro --- Makefile | 2 +- common/log.h | 78 ++++++++++++++++++++++++++++++++++++++++++ examples/main/main.cpp | 19 ++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 common/log.h diff --git a/Makefile b/Makefile index d31acc450..6bbd2fa78 100644 --- a/Makefile +++ b/Makefile @@ -351,7 +351,7 @@ clean: # Examples # -main: examples/main/main.cpp build-info.h ggml.o llama.o common.o console.o grammar-parser.o $(OBJS) +main: examples/main/main.cpp build-info.h ggml.o llama.o common.o common/log.h console.o grammar-parser.o $(OBJS) $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS) @echo @echo '==== Run ./main -h for help. ====' diff --git a/common/log.h b/common/log.h new file mode 100644 index 000000000..492f98ed8 --- /dev/null +++ b/common/log.h @@ -0,0 +1,78 @@ +#pragma once + +#include + +// Specifies a log target. +// default simply prints log to stderr +// this can be changed, by defining LOG_TARGET +// like so: +// +// #define LOG_TARGET (a valid FILE*) +// #include "log.h" +// +// The log target can also be redirected to a function +// like so: +// +// #define LOG_TARGET log_handler() +// #include "log.h" +// +// FILE* log_handler() +// { +// return stderr; +// } +// +// or: +// +// #define LOG_TARGET log_handler("somelog.log") +// #include "log.h" +// +// FILE* log_handler(char*filename) +// { +// (...) +// return fopen(...) +// } +// +#ifndef LOG_TARGET +#define LOG_TARGET stderr +#endif + +// Allows disabling timestamps. +// in order to disable, define LOG_NO_TIMESTAMPS +// like so: +// +// #define LOG_NO_TIMESTAMPS +// #include "log.h" +// +#ifndef LOG_NO_TIMESTAMPS +#define LOG_TIMESTAMP_FMT "[%lu]" +#define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast>(std::chrono::system_clock::now().time_since_epoch())).count() +#else +#define LOG_TIMESTAMP_FMT +#define LOG_TIMESTAMP_VAL +#endif + +// Allows disabling file/line/function prefix +// in order to disable, define LOG_NO_FILE_LINE_FUNCTION +// like so: +// +// #define LOG_NO_FILE_LINE_FUNCTION +// #include "log.h" +// +#ifndef LOG_NO_FILE_LINE_FUNCTION +#define LOG_FLF_FMT "[%24s:%5d][%24s] " +#define LOG_FLF_VAL , __FILE__, __LINE__, __FUNCTION__ +#else +#define LOG_FLF_FMT +#define LOG_FLF_VAL +#endif + +#define _LOG(str, ...) \ + { \ + fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%c" LOG_TIMESTAMP_VAL LOG_FLF_VAL, ##__VA_ARGS__); \ + fflush(LOG_TARGET); \ + } + +// This us a trick to bypass the silly +// "warning: ISO C++11 requires at least one argument for the "..." in a variadic macro" +// so we xan gave a single macro which can be called just like printf. +#define LOG(...) _LOG(__VA_ARGS__, '\n') \ No newline at end of file diff --git a/examples/main/main.cpp b/examples/main/main.cpp index 388e1f7d7..31d376985 100644 --- a/examples/main/main.cpp +++ b/examples/main/main.cpp @@ -3,7 +3,11 @@ #define _GNU_SOURCE #endif +#define LOG_TARGET log_handler("asd") +//#define LOG_NO_TIMESTAMPS #include "common.h" + +#include "log.h" #include "console.h" #include "llama.h" #include "build-info.h" @@ -54,9 +58,24 @@ void sigint_handler(int signo) { } #endif +inline FILE *log_handler(std::string s) +{ + static bool initialized{false}; + + if (!initialized)[[unlikely]] + { + fprintf(stderr,"arg: %s", s.c_str()); + initialized=true; + } + + return stderr; +} + int main(int argc, char ** argv) { gpt_params params; + LOG("Hello World!") + if (gpt_params_parse(argc, argv, params) == false) { return 1; }