init project

This commit is contained in:
hejun 2025-04-28 20:47:53 +08:00
commit 69492091e7
6 changed files with 170 additions and 0 deletions

36
Makefile Normal file
View File

@ -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

BIN
build/main.o Normal file

Binary file not shown.

94
headers/hlog.hpp Normal file
View File

@ -0,0 +1,94 @@
#ifndef HLOG_H
#define HLOG_H
#include <string>
#include <mutex>
#include <memory>
#include <htime.hpp>
#include <cstdarg>
#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<std::mutex> 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

28
headers/htime.hpp Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <ctime>
#include <cstring>
#include <string>
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);
}
};

BIN
output/run Executable file

Binary file not shown.

12
src/main.cc Normal file
View File

@ -0,0 +1,12 @@
#include <hlog.hpp>
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;
}