Skip to content

Commit

Permalink
Merge pull request #18 from SeppahBaws/release/v1.2
Browse files Browse the repository at this point in the history
v1.2 release
  • Loading branch information
SeppahBaws authored Jul 4, 2021
2 parents ce248f7 + 7763e10 commit 4cf210f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
100 changes: 74 additions & 26 deletions logtools/src/logtools.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* LogTools // v1.1.2 // small and simple cross-platform logging toolset.
* LogTools // v1.2 // small and simple cross-platform logging toolset.
* ************ https://github.com/SeppahBaws/logtools ************
*
* -----------------------------[ LICENSE ]-----------------------------
MIT License
Copyright (c) 2020 Seppe Dekeyser
Copyright (c) 2020-2021 Seppe Dekeyser
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -99,10 +99,14 @@

//////////////// Main Logger ////////////////
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdarg>
#include <ctime>
#include <vector>

enum class LogLevel
{
Expand All @@ -125,19 +129,36 @@ struct LogBinding

struct LoggerSettings
{
bool showDate;
bool showTime;
bool showDate = false;
bool showTime = false;
};

class Logger
{
public:
static void Init()
static void Init(bool logToFile = false, const std::string& filePath = "", bool append = false)
{
#if defined(LOGTOOLS_WINDOWS)
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
m_Settings = { false, false };

if (logToFile && !filePath.empty())
{
if (append)
{
m_LogFile.open(filePath, std::fstream::out | std::fstream::ate | std::fstream::app);
}
else
{
m_LogFile.open(filePath, std::fstream::out | std::fstream::ate);
}
}
}

static void Shutdown()
{
m_LogFile.close();
}

static void SetLevel(LogLevel level)
Expand All @@ -149,7 +170,7 @@ class Logger
{
m_Settings = settings;
}


static void LogTrace(const std::string& msg)
{
Expand Down Expand Up @@ -224,9 +245,19 @@ class Logger
return;

SetConsoleColor(m_Bindings[static_cast<int>(level)].color);
PrintTime();
std::cout << "[" << m_Bindings[static_cast<int>(level)].identifier << "] ";
std::cout << msg << std::endl;

std::stringstream ss;
ss << PrintTime();
ss << "[" << m_Bindings[static_cast<int>(level)].identifier << "] ";
ss << msg << std::endl;
std::cout << ss.str();

if (m_LogFile && m_LogFile.is_open())
{
m_LogFile << ss.str();
m_LogFile.flush();
}

ResetConsoleColor();
}

Expand All @@ -236,17 +267,33 @@ class Logger
return;

SetConsoleColor(m_Bindings[static_cast<int>(level)].color);
PrintTime();
std::cout << "[" << m_Bindings[static_cast<int>(level)].identifier << "] ";
std::vprintf(fmt, args);
std::cout << std::endl;

std::stringstream ss;
ss << PrintTime();
ss << "[" << m_Bindings[static_cast<int>(level)].identifier << "] ";

va_list args2;
va_copy(args2, args);
std::vector<char> buf(1 + std::vsnprintf(nullptr, 0, fmt, args));
std::vsnprintf(buf.data(), buf.size(), fmt, args2);
va_end(args2);
ss << buf.data() << std::endl;

std::cout << ss.str();

if (m_LogFile && m_LogFile.is_open())
{
m_LogFile << ss.str();
m_LogFile.flush();
}

ResetConsoleColor();
}

static void PrintTime()
static std::string PrintTime()
{
if (!m_Settings.showDate && !m_Settings.showTime)
return;
return "";

time_t t = time(nullptr);
std::tm now{};
Expand All @@ -257,30 +304,30 @@ class Logger
localtime_r(&t, &now);
#endif

std::printf("[");
std::stringstream ss;
ss << "[";

if (m_Settings.showDate)
{
std::printf("%.4d-%.2d-%.2d",
now.tm_year + 1900,
now.tm_mon + 1,
now.tm_mday);
ss << std::setw(4) << now.tm_year + 1900 << "-";
ss << std::setw(2) << std::setfill('0') << now.tm_mon + 1 << "-";
ss << std::setw(2) << std::setfill('0') << now.tm_mday;
}

if (m_Settings.showDate && m_Settings.showTime)
{
std::printf(" ");
ss << " ";
}

if (m_Settings.showTime)
{
std::printf("%.2d:%.2d:%.2d",
now.tm_hour,
now.tm_min,
now.tm_sec);
ss << std::setw(2) << std::setfill('0') << now.tm_hour << ":";
ss << std::setw(2) << std::setfill('0') << now.tm_min << ":";
ss << std::setw(2) << std::setfill('0') << now.tm_sec;
}

std::printf("] ");
ss << "] ";
return ss.str();
}

private:
Expand Down Expand Up @@ -340,6 +387,7 @@ class Logger
inline static LogLevel m_LogLevel = LogLevel::Trace;

inline static LoggerSettings m_Settings = { false, false };
inline static std::fstream m_LogFile;
};

#endif //INCLUDE_LOGTOOLS_H
5 changes: 3 additions & 2 deletions logtools/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "logtools.h"
#include <iostream>
#include <ctime>

int main()
{
Logger::Init(); // Very important!
Logger::Init(true, "output.log", false); // Very important!

Logger::Configure({ true, true });

Expand Down Expand Up @@ -46,4 +45,6 @@ int main()
Logger::Configure({ false, false });

Logger::LogTrace("Hello World!");

Logger::Shutdown(); // Very important!
}
2 changes: 1 addition & 1 deletion premake5.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
workspace "logtools"
architecture "x64"
architecture "x86_64"

configurations
{
Expand Down

0 comments on commit 4cf210f

Please sign in to comment.