commit 69492091e7353d1750e70622251a9909987b4e99 Author: hejun Date: Mon Apr 28 20:47:53 2025 +0800 init project diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..de25fcf --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ + +CXX := g++ +CPP_STD := 17 +CPP = c++ +PRJ_NAME = run +PRJ_HEADERS_DIR := $(CURDIR)/headers +SRC_DIR := $(CURDIR)/src +CXX_FLAGS := -Wall -I$(PRJ_HEADERS_DIR) -std=$(CPP)$(CPP_STD) -g +LDFLAGS = +OUTPUT_DIR := $(CURDIR)/output +BUILD_DIR := $(CURDIR)/build + +SRCS := $(wildcard $(SRC_DIR)/*.cc) + +OBJS := $(patsubst $(SRC_DIR)/%.cc, $(BUILD_DIR)/%.o, $(wildcard $(SRC_DIR)/*.cc)) + +TARGET = $(OUTPUT_DIR)/$(PRJ_NAME) + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CXX) -o $@ $^ $(LDFLAGS) + +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cc + $(CXX) $(CXX_FLAGS) -c $< -o $@ + +run: all + @echo "----start exec project------" + @$(TARGET) +clean: + @rm -f $(OBJS) $(TARGET) + +full_clean: + @rm -rf $(BUILD_DIR)/* && rm -rf $(OUTPUT_DIR)/* + +.PHONY: all clean diff --git a/build/main.o b/build/main.o new file mode 100644 index 0000000..4efdf7c Binary files /dev/null and b/build/main.o differ diff --git a/headers/hlog.hpp b/headers/hlog.hpp new file mode 100644 index 0000000..f23dae1 --- /dev/null +++ b/headers/hlog.hpp @@ -0,0 +1,94 @@ +#ifndef HLOG_H +#define HLOG_H + +#include +#include +#include +#include +#include + +#define LOG hlog::getLogger()->WRITE_LOG + +enum class hlog_level +{ + HLOG_INFO, + HLOG_WARN, + HLOG_ERR, + HLOG_CRIT +}; + +namespace +{ + std::string hlog_level_to_string(const hlog_level &level) + { + switch (level) + { + case hlog_level::HLOG_INFO: + return "INFO"; + case hlog_level::HLOG_WARN: + return "WARNING"; + case hlog_level::HLOG_ERR: + return "ERROR"; + case hlog_level::HLOG_CRIT: + return "CRITICAL"; + default: + return "UNKNOWN"; + } + } +} + +class hlog +{ +private: + static hlog *global_hloger; + static std::mutex mtx; + static Htime htimer; + hlog() + { + } + +public: + static hlog *getLogger() + { + if (global_hloger == nullptr) + { + std::lock_guard lock(mtx); + + if (global_hloger == nullptr) + { + global_hloger = new hlog(); + } + } + + return global_hloger; + } + + void WRITE_LOG(hlog_level level, const char *fmt, ...) + { + printf("[%s]:[%-10s]--[", htimer.get_local_time_now().c_str(), hlog_level_to_string(level).c_str()); + + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + + printf("]\n"); + } + + static void destroy() + { + if (global_hloger != nullptr) + { + delete global_hloger; + global_hloger = nullptr; + } + } +}; + +hlog *hlog::global_hloger = nullptr; + +std::mutex hlog::mtx; + +Htime hlog::htimer; + +#endif \ No newline at end of file diff --git a/headers/htime.hpp b/headers/htime.hpp new file mode 100644 index 0000000..d8f2d43 --- /dev/null +++ b/headers/htime.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +class Htime +{ + time_t tv; + +public: + Htime() {} + + std::string get_local_time_now() + { + static char buffer[128]; + + struct tm *_local_time = nullptr; + + memset(buffer, 0x0, sizeof(buffer)); + + tv = time(nullptr); + _local_time = localtime(&tv); + strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", _local_time); + + return std::string(buffer); + } +}; \ No newline at end of file diff --git a/output/run b/output/run new file mode 100755 index 0000000..46672bd Binary files /dev/null and b/output/run differ diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 0000000..9a121cc --- /dev/null +++ b/src/main.cc @@ -0,0 +1,12 @@ +#include + +int main(int argc, char const *argv[]) +{ + + LOG(hlog_level::HLOG_INFO, "This is a logger info"); + LOG(hlog_level::HLOG_WARN, "This is a logger info"); + LOG(hlog_level::HLOG_ERR, "This is a logger info"); + LOG(hlog_level::HLOG_CRIT, "This is a logger info"); + + return 0; +}