Skip to content

Commit

Permalink
add single release header
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabbleman committed Jun 10, 2024
1 parent 6f9f675 commit 38e2081
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 210 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ project(Log)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_STANDARD_REQUIRED 17)
include_directories(
${CMAKE_SOURCE_DIR}/include
release
)

add_subdirectory(src)

add_subdirectory(sample)
49 changes: 0 additions & 49 deletions include/Log.hh

This file was deleted.

22 changes: 0 additions & 22 deletions include/Timestamp.hh

This file was deleted.

102 changes: 102 additions & 0 deletions release/log.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#pragma once

#include <string.h>
#include <unistd.h>

#include <chrono>
#include <iomanip>
#include <memory>
#include <sstream>

#define size_t unsigned long
namespace TB {
enum Level { INFO = 0, WARNING, ERROR, NUM };

class Timestamp {
public:
Timestamp() {}
// Timestamp(size_t time);
~Timestamp() {}

public:
size_t now() {
auto timestamp = std::chrono::seconds(std::time(NULL));
return static_cast<size_t>(timestamp.count());
}
std::string toString() {
std::string tmp = std::to_string(now());

return tmp;
}
std::string toFormatString() {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);

std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
return ss.str();
}
};

class LogImpl {
public:
LogImpl() {}
LogImpl(const char *file, int line, Level level)
: file_(file), line_(line), level_(level), timestamp_() {
char log_header[1024];
snprintf(log_header, sizeof(log_header), "[%s] %s\t%s:%d ",
level_name[level_], timestamp_.toFormatString().c_str(), file_,
line_);
oss_ << log_header;
}

~LogImpl() {}

public:
void finish() { oss_ << '\n'; }

public:
Level level_;
std::ostringstream oss_;
const char *file_;
int line_;
Timestamp timestamp_;
constexpr static const char *level_name[Level::NUM] = {
"\033[1;32mINFO\033[1;37m",
"\033[1;33mWARNING\033[1;37m",
"\033[1;31mERROR\033[1;37m",
};
};

class Log {
public:
public:
Log() : impl_(new LogImpl(__FILE__, __LINE__, Level::INFO)) {}
Log(Level level) : impl_(new LogImpl(__FILE__, __LINE__, level)) {}
Log(const char *file, int line, Level level)
: impl_(new LogImpl(file, line, level)) {}
~Log() {
impl_->finish();
std::string buffer(impl_->oss_.str());

::write(1, buffer.data(), buffer.size());
}

std::ostream &stream() { return impl_->oss_; }

public:
std::shared_ptr<LogImpl> impl_;

}; // class Log

// @ref https://stackoverflow.com/questions/8487986/file-macro-shows-full-path
#define __FILENAME__ \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

#define LOG_INFO Log(__FILENAME__, __LINE__, Level::INFO).stream()
#define LOG_WARNING Log(__FILENAME__, __LINE__, Level::WARNING).stream()
#define LOG_ERROR Log(__FILENAME__, __LINE__, Level::ERROR).stream()

#define LOG LOG_INFO

} // namespace TB
7 changes: 3 additions & 4 deletions sample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
add_executable(info InfoTest.cc)
target_link_libraries(info Log)
add_executable(example example.cc)
target_link_libraries(example Log)
add_executable(SingleHeaderTest SingleHeaderTest.cc)

target_include_directories(SingleHeaderTest PRIVATE ${CMAKE_SOURCE_DIR}/release)
19 changes: 0 additions & 19 deletions sample/InfoTest.cc

This file was deleted.

15 changes: 15 additions & 0 deletions sample/SingleHeaderTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <log.h>

using namespace TB;

int main(int argc, char const *argv[])
{

LOG_INFO << "test info";

LOG_WARNING << "test warning";

LOG_ERROR << "test error";

return 0;
}
35 changes: 0 additions & 35 deletions sample/example.cc

This file was deleted.

5 changes: 0 additions & 5 deletions src/CMakeLists.txt

This file was deleted.

47 changes: 0 additions & 47 deletions src/Log.cc

This file was deleted.

26 changes: 0 additions & 26 deletions src/Timestamp.cc

This file was deleted.

0 comments on commit 38e2081

Please sign in to comment.