Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.2 release #18

Merged
merged 10 commits into from
Jul 4, 2021
95 changes: 69 additions & 26 deletions logtools/src/logtools.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* LogTools // v1.1 // 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,31 @@ 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);
SeppahBaws marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
m_LogFile.open(filePath, std::fstream::out | std::fstream::ate);
}
}
}

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


static void LogTrace(const std::string& msg)
{
Expand Down Expand Up @@ -224,9 +240,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 +262,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 +299,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_hour);
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 +382,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
3 changes: 1 addition & 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
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