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

Implemented log message formatting #7

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 68 additions & 12 deletions logtools/src/logtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,22 @@
#ifndef INCLUDE_LOGTOOLS_H
#define INCLUDE_LOGTOOLS_H

// Platform definitions
#if defined(__linux__)
#define LOGTOOLS_LINUX
#elif defined(_WIN32) | defined(_WIN64)
#define LOGTOOLS_WINDOWS
#else
#error Unsupported platform! Logtools only works on Linux or Windows.
#endif

// Check if C++17 or greater, by checking if we have a C++17 specific feature.
#if __cpp_inline_variables < 201606
#error Incorrect C++ version. Logtools requires C++ version 17 or greater.
#endif

////////////////// Colors //////////////////
#if defined(__linux__)
#if defined(LOGTOOLS_LINUX)
// Linux Colors

#define BLACK "\033[30m"
Expand All @@ -69,7 +78,7 @@

#define RESET "\033[0m"

#elif defined(_WIN32) || defined(_WIN64)
#elif defined(LOGTOOLS_WINDOWS)
// Windows Colors

#define WIN32_LEAN_AND_MEAN
Expand All @@ -91,6 +100,8 @@
//////////////// Main Logger ////////////////
#include <string>
#include <iostream>
#include <cstdio>
#include <cstdarg>

enum LogLevel
{
Expand All @@ -104,7 +115,7 @@ class Logger
public:
static void Init()
{
#if defined(_WIN32) || defined(_WIN64)
#if defined(LOGTOOLS_WINDOWS)
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
}
Expand Down Expand Up @@ -142,29 +153,74 @@ class Logger
LogColor("[ERROR] " + msg, RED);
}

static void LogInfo(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
SetConsoleColor(WHITE);
std::cout << "[INFO] ";
std::vprintf(fmt, args);
std::cout << std::endl;
ResetConsoleColor();
va_end(args);
}

static void LogWarning(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
SetConsoleColor(YELLOW);
std::cout << "[WARNING] ";
std::vprintf(fmt, args);
std::cout << std::endl;
ResetConsoleColor();
va_end(args);
}

static void LogError(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
SetConsoleColor(RED);
std::cout << "[ERROR] ";
std::vprintf(fmt, args);
std::cout << std::endl;
ResetConsoleColor();
va_end(args);
}

private:
#if defined(__linux__)
#if defined(LOGTOOLS_LINUX)
// Linux specific
static void ResetConsoleColors()
static void SetConsoleColor(const std::string& color)
{
std::cout << color;
}
static void ResetConsoleColor()
{
std::cout << RESET;
}
static void LogColor(const std::string& msg, const std::string& color)
{
std::cout << color << msg << std::endl;
ResetConsoleColors();
SetConsoleColor(color);
std::cout << msg << std::endl;
ResetConsoleColor();
}
#elif defined(_WIN32) || defined(_WIN64)
#elif defined(LOGTOOLS_WINDOWS)
// Windows specific
static void ResetConsoleColors()
static void SetConsoleColor(const WORD color)
{
SetConsoleTextAttribute(Instance().hConsole, color);
}
static void ResetConsoleColor()
{
SetConsoleTextAttribute(Instance().hConsole, RESET);
}
static void LogColor(const std::string& msg, const WORD color)
{
SetConsoleTextAttribute(Instance().hConsole, color);
SetConsoleColor(color);
std::cout << msg << std::endl;
ResetConsoleColors();
ResetConsoleColor();
}
#endif

Expand All @@ -175,7 +231,7 @@ class Logger
}

private:
#if defined(_WIN32) || defined(_WIN64)
#if defined(LOGTOOLS_WINDOWS)
inline static HANDLE hConsole;
#endif
};
Expand Down
4 changes: 4 additions & 0 deletions logtools/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ int main()
Logger::LogWarning("This is a warning");
Logger::LogError("Failed to do something important!");
Logger::Log(LogLevel::Info, "Greetings!");

Logger::LogInfo("Hello %d", 1);
Logger::LogWarning("Hello %s", "abc");
Logger::LogError("Hello %.2f", 1.2345f);
}