Skip to content

Commit

Permalink
refactor(fw/log): vargs related
Browse files Browse the repository at this point in the history
  • Loading branch information
berdal84 committed Feb 16, 2024
1 parent 224e216 commit bf19a7d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 93 deletions.
13 changes: 13 additions & 0 deletions src/fw/core/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ void format::limit_trailing_zeros(std::string& str, int _trailing_max)
}

}

fw::string32 format::time_point_to_string(const std::chrono::system_clock::time_point &time_point)
{
std::time_t time = std::chrono::system_clock::to_time_t(time_point);
// The result of ctime and ctime_s is formatted like: "Www Mmm dd hh:mm:ss yyyy\n\0" (24 chars + end of line + end of string)
#ifdef WIN32
char str[26];
ctime_s(str, sizeof str, &time);
return {str, 24};
#else
return {ctime(&time), 24};
#endif
}
9 changes: 6 additions & 3 deletions src/fw/core/format.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include <string>
#include "./string.h"
#include "./types.h"
#include <cassert>
#include <cstring>
#include <string>
#include <chrono>
#include <xxhash/xxhash64.h>
#include <assert.h>
#include "types.h"

namespace fw
{
Expand Down Expand Up @@ -37,5 +39,6 @@ namespace fw
return result;
}
void limit_trailing_zeros(std::string& str, int _trailing_max);
fw::string32 time_point_to_string(const std::chrono::system_clock::time_point&);
};
}
64 changes: 0 additions & 64 deletions src/fw/core/log.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
#include "log.h"

#include <cstdarg> // va_list, va_start, va_end
#include <cstdio> // vfprintf
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstring>

using namespace fw;

std::deque<log::Message> log::s_logs;
log::Verbosity log::s_verbosity = Verbosity_DEFAULT;

static fw::string32 time_point_to_string(const std::chrono::system_clock::time_point &time_point)
{
std::time_t time = std::chrono::system_clock::to_time_t(time_point);
// The result of ctime and ctime_s is formatted like: "Www Mmm dd hh:mm:ss yyyy\n\0" (24 chars + end of line + end of string)
#ifdef WIN32
char str[26];
ctime_s(str, sizeof str, &time);
return {str, 24};
#else
return {ctime(&time), 24};
#endif
}

std::map<std::string, log::Verbosity>& log::get_verbosity_by_category()
{
// use singleton pattern instead of static member to avoid static code issues
Expand All @@ -40,52 +22,6 @@ log::Verbosity log::get_verbosity(const std::string& _category)
return s_verbosity;
}

void log::push_message(Verbosity _verbosity, const char* _category, const char* _format, ...)
{
// Print log only if verbosity level allows it

if (_verbosity <= get_verbosity(_category) )
{
{};
// Store the message in the front of the queue
Message& message = s_logs.emplace_front();
message.verbosity = _verbosity;
message.category = _category;

message.text.push_back('[');
message.text.append( time_point_to_string(message.date) );
message.text.push_back('|');
message.text.append(log::to_string(_verbosity));
message.text.push_back('|');
message.text.append( _category );
message.text.push_back(']');
message.text.push_back(' ');

// Fill a buffer with the formatted message
va_list arglist;
va_start( arglist, _format );
message.text.append_fmt(_format, arglist);
va_end( arglist );

// Select the appropriate color depending on the verbosity
switch (_verbosity)
{
case log::Verbosity_Error: std::cout << RED; break;
case log::Verbosity_Warning: std::cout << MAGENTA; break;
default: std::cout << RESET; break;
}

// print the text and reset the color
printf("%s" RESET, message.text.c_str());

// Constraint the queue to have a limited size
constexpr size_t max_count = 5000; // a Message is 512 bytes
constexpr size_t min_count = 4000; //
if (s_logs.size() > max_count ) s_logs.resize(min_count);
}

}

const char* log::to_string(log::Verbosity _verbosity)
{
switch (_verbosity)
Expand Down
65 changes: 51 additions & 14 deletions src/fw/core/log.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once

#include "./string.h"
#include "format.h"
#include <chrono>
#include <ctime>
#include <deque>
#include <string>
#include <iostream>
#include <map>
#include <chrono>
#include "./string.h"
#include <string>

#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
Expand All @@ -27,13 +30,13 @@
#define KO RED "[KO]" RESET // red colored "[KO]" string.
#define OK GREEN "[OK]" RESET // green colored "[OK]" string.

# define LOG_ERROR(...) fw::log::push_message( fw::log::Verbosity_Error , __VA_ARGS__ ); fw::log::flush();
# define LOG_WARNING(...) fw::log::push_message( fw::log::Verbosity_Warning, __VA_ARGS__ );
# define LOG_MESSAGE(...) fw::log::push_message( fw::log::Verbosity_Message, __VA_ARGS__ );
# define LOG_ERROR(...) fw::log::push_message( fw::log::Verbosity_Error , ##__VA_ARGS__ ); fw::log::flush();
# define LOG_WARNING(...) fw::log::push_message( fw::log::Verbosity_Warning, ##__VA_ARGS__ );
# define LOG_MESSAGE(...) fw::log::push_message( fw::log::Verbosity_Message, ##__VA_ARGS__ );
# define LOG_FLUSH() fw::log::flush();

#if NDBL_DEBUG
# define LOG_VERBOSE(...) fw::log::push_message( fw::log::Verbosity_Verbose, __VA_ARGS__ );
# define LOG_VERBOSE(...) fw::log::push_message( fw::log::Verbosity_Verbose, ##__VA_ARGS__ );
#else
# define LOG_VERBOSE(...)
#endif
Expand Down Expand Up @@ -66,12 +69,6 @@ namespace fw {
Verbosity verbosity=Verbosity_DEFAULT; // verbosity level
};

private:
static std::deque<Message> s_logs; // message history
static Verbosity s_verbosity; // global verbosity level
static std::map<std::string, Verbosity>& get_verbosity_by_category();

public:
static const std::deque<Message>& get_messages(); // Get message history
static void set_verbosity(const std::string& _category, Verbosity _level) // Set verbosity level for a given category
{ get_verbosity_by_category().insert_or_assign(_category, _level ); }
Expand All @@ -85,6 +82,46 @@ namespace fw {
static Verbosity get_verbosity(const std::string& _category); // Get verbosity level for a given category
inline static Verbosity get_verbosity() { return s_verbosity; } // Get global verbosity level
static void flush(); // Ensure all messages have been printed out
static void push_message(Verbosity, const char* _category, const char* _format, ...); // Push a new message for a given category

template<typename...Args>
static void push_message(Verbosity _verbosity, const char* _category, const char* _format, Args... args) // Push a new message for a given category
{
// Print log only if verbosity level allows it

if (_verbosity <= get_verbosity(_category) )
{
Message& message = s_logs.emplace_front(); // Store a new message in the front of the queue
message.verbosity = _verbosity;
message.category = _category;
message.text.append_fmt("[%s|%s|%s] " // Append a formatted prefix with time, verbosity level and category
, format::time_point_to_string(message.date).c_str()
, log::to_string(_verbosity)
, _category );

message.text.append_fmt(_format, args...); // Fill a buffer with the formatted message

// Select the appropriate color depending on the verbosity
switch (_verbosity)
{
case log::Verbosity_Error: std::cout << RED; break;
case log::Verbosity_Warning: std::cout << MAGENTA; break;
default: std::cout << RESET; break;
}

// print the text and reset the color
printf("%s" RESET, message.text.c_str());

// Constraint the queue to have a limited size
constexpr size_t max_count = 5000; // a Message is 512 bytes
constexpr size_t min_count = 4000; //
if (s_logs.size() > max_count ) s_logs.resize(min_count);
}

}

private:
static std::deque<Message> s_logs; // message history
static Verbosity s_verbosity; // global verbosity level
static std::map<std::string, Verbosity>& get_verbosity_by_category();
};
}
23 changes: 11 additions & 12 deletions src/fw/core/string.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <assert.h>
#include <cassert>
#include <cstring> // for memcpy
#include <memory> // for std::move
#include <cstdarg> // va_list, va_start, va_end

#include "types.h"

Expand All @@ -29,7 +30,7 @@ namespace fw
, m_ptr(nullptr)
{}

basic_string(const CharT* str)
explicit basic_string(const CharT* str)
: m_alloc_strategy(alloc_strategy::HEAP)
, m_length(strlen(str))
, m_capacity(0)
Expand All @@ -47,7 +48,7 @@ namespace fw
: basic_string(other.c_str())
{}

basic_string(basic_string&& other)
basic_string(basic_string&& other) noexcept
: m_alloc_strategy(alloc_strategy::HEAP)
, m_length(0)
, m_capacity(0)
Expand All @@ -56,14 +57,11 @@ namespace fw
*this = std::move(other);
}

basic_string& operator=(basic_string&& other)
basic_string& operator=(basic_string&& other) noexcept
{
if( m_alloc_strategy == alloc_strategy::HEAP )
{
if( m_ptr != nullptr)
{
delete[] m_ptr;
}
delete[] m_ptr;
m_ptr = other.m_ptr;
m_length = other.m_length;
m_capacity = other.m_capacity;
Expand Down Expand Up @@ -135,10 +133,11 @@ namespace fw
{ return append(str, strlen(str)); }

template<typename ...Args>
inline size_t append_fmt(const char* _format, Args... args )
{
return m_length = vsnprintf(m_ptr+m_length, m_capacity-m_length, _format, args...);
}
inline size_t append_fmt(const char* _format, Args...args )
{ return m_length = snprintf(m_ptr+m_length, m_capacity-m_length, _format, args... ); }

inline size_t append_fmt(const char* _str )
{ return m_length = snprintf(m_ptr+m_length, m_capacity-m_length, "%s", _str ); }

/** provided to easily switch to/from std::string */
inline basic_string& push_back(CharT str)
Expand Down

0 comments on commit bf19a7d

Please sign in to comment.