Skip to content

Commit

Permalink
Auto-update from libvoxelstorm: latest logstorm improvements from Arm…
Browse files Browse the repository at this point in the history
…chair Engine: new Emscripten-specific log sinks, use scoped_lock instead of lock_guard, default d'tors, and move timestamp code out of header into source (760781a)
  • Loading branch information
slowriot committed Jan 7, 2025
1 parent 479cf27 commit b068c82
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 81 deletions.
8 changes: 8 additions & 0 deletions logstorm/logstorm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
/// a std::endl() and then send the line as a whole.

#include "manager.h"
#include "timestamp.h"
#include "sink/dummy.h"
#include "sink/stream.h"
#include "sink/console.h"
#include "sink/console_err.h"
#include "sink/fstream.h"
#include "sink/file.h"
#include "sink/circular_buffer.h"

#ifdef __EMSCRIPTEN__
#include "sink/emscripten_out.h"
#include "sink/emscripten_err.h"
#include "sink/emscripten_dbg.h"
#include "sink/emscripten_dbg_backtrace.h"
#endif //__EMSCRIPTEN__
4 changes: 1 addition & 3 deletions logstorm/sink/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ base::base(timestamp::types timestamp_type)
/// Default constructor
}

base::~base() {
/// Default destructor
}
base::~base() = default;

}
4 changes: 1 addition & 3 deletions logstorm/sink/circular_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ circular_buffer::circular_buffer(unsigned int this_size, timestamp::types timest
/// Default constructor
}

circular_buffer::~circular_buffer() {
/// Default destructor
}
circular_buffer::~circular_buffer() = default;

void circular_buffer::log(std::string const &log_entry) {
/// Log this line
Expand Down
8 changes: 3 additions & 5 deletions logstorm/sink/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ console::console(timestamp::types timestamp_type)
/// Default constructor
}

console::~console() {
/// Default destructor
}
console::~console() = default;

void console::log(std::string const &log_entry) {
/// Log this line
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
std::cout << time() << log_entry << std::endl;
}
void console::log_fragment(std::string const &log_entry) {
/// Log this fragment without ending the line
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
#ifdef LOGSTORM_COMPOSE_FRAGMENTS_SEPARATELY
if(line_in_progress.empty()) { // if this is the start of a line, add a timestamp and cache it
Expand Down
8 changes: 3 additions & 5 deletions logstorm/sink/console_err.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ console_err::console_err(timestamp::types timestamp_type)
/// Default constructor
}

console_err::~console_err() {
/// Default destructor
}
console_err::~console_err() = default;

void console_err::log(std::string const &log_entry) {
/// Log this line
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
std::cerr << time() << log_entry << std::endl;
}
void console_err::log_fragment(std::string const &log_entry) {
/// Log this fragment without ending the line
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
#ifdef LOGSTORM_COMPOSE_FRAGMENTS_SEPARATELY
if(line_in_progress.empty()) { // if this is the start of a line, add a timestamp and cache it
Expand Down
4 changes: 1 addition & 3 deletions logstorm/sink/dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ dummy::dummy(timestamp::types timestamp_type)
/// Default constructor
}

dummy::~dummy() {
/// Default destructor
}
dummy::~dummy() = default;

void dummy::log(std::string const &log_entry [[maybe_unused]]) {
/// Dummy function to not do anything (for use in a non-logging environment)
Expand Down
6 changes: 3 additions & 3 deletions logstorm/sink/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ file::file(std::string const &target_filename, timestamp::types timestamp_type)
file::~file() {
/// Default destructor
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
stream.close();
}
Expand All @@ -24,7 +24,7 @@ void file::log(std::string const &log_entry) {
/// Log this line
if(stream.good()) {
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
stream << time() << log_entry << std::endl;
}
Expand All @@ -45,7 +45,7 @@ void file::log_fragment(std::string const &log_entry) {
#else
if(stream.good()) {
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
stream << log_entry;
}
Expand Down
8 changes: 3 additions & 5 deletions logstorm/sink/fstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ fstream::fstream(std::ofstream &target_stream, timestamp::types timestamp_type)
/// Default constructor
}

fstream::~fstream() {
/// Default destructor
}
fstream::~fstream() = default;

void fstream::log(std::string const &log_entry) {
/// Log this line
if(stream.good()) {
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
stream << time() << log_entry << std::endl;
}
Expand All @@ -37,7 +35,7 @@ void fstream::log_fragment(std::string const &log_entry) {
#else
if(stream.good()) {
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
stream << log_entry;
}
Expand Down
8 changes: 3 additions & 5 deletions logstorm/sink/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ stream::stream(std::ostream &target_ostream, timestamp::types timestamp_type)
/// Default constructor
}

stream::~stream() {
/// Default destructor
}
stream::~stream() = default;

void stream::log(std::string const &log_entry) {
/// Log this line
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
ostream << time() << log_entry << std::endl;
}
void stream::log_fragment(std::string const &log_entry) {
/// Log this fragment without ending the line
#ifndef LOGSTORM_SINGLE_THREADED
std::lock_guard lock(output_mutex);
std::scoped_lock lock(output_mutex);
#endif // LOGSTORM_SINGLE_THREADED
#ifdef LOGSTORM_COMPOSE_FRAGMENTS_SEPARATELY
if(line_in_progress.empty()) { // if this is the start of a line, add a timestamp and cache it
Expand Down
51 changes: 49 additions & 2 deletions logstorm/timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,55 @@ timestamp::timestamp(types this_type)
/// Default constructor
}

timestamp::~timestamp() {
/// Default destructor
timestamp::~timestamp() = default;

std::string timestamp::operator()() {
/// Generate a timestamp as appropriate to this timestamp's type
switch(type) {
case types::NONE:
return {};
case types::TIME:
{
std::time_t time = std::time(nullptr);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%Y-%m-%d ");
return ss.str();
}
case types::DATE:
{
std::time_t time = std::time(nullptr);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%H:%M:%S ");
return ss.str();
}
case types::DATE_TIME:
{
std::time_t time = std::time(nullptr);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S ");
return ss.str();
}
case types::UNIX:
{
std::stringstream ss;
ss << std::time(nullptr) << " ";
return ss.str();
}
case types::SINCE_START:
{
// give time in seconds to two decimal places
std::chrono::duration<float> const time_elapsed = std::chrono::system_clock::now() - time_start;
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << time_elapsed.count() << " ";
return ss.str();
}
}
#ifdef DISABLE_EXCEPTION_THROWING
return {};
#else // DISABLE_EXCEPTION_THROWING
throw std::runtime_error("LogStorm: Invalid timestamp type " + std::to_string(static_cast<unsigned int>(type)));
#endif // DISABLE_EXCEPTION_THROWING
}


}
48 changes: 1 addition & 47 deletions logstorm/timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,7 @@ class timestamp {
DEFAULT = NONE
} type{types::DEFAULT};

std::string operator()() {
/// Generate a timestamp as appropriate to this timestamp's type
switch(type) {
case types::NONE:
return {};
case types::TIME:
{
std::time_t time = std::time(nullptr);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%Y-%m-%d ");
return ss.str();
}
case types::DATE:
{
std::time_t time = std::time(nullptr);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%H:%M:%S ");
return ss.str();
}
case types::DATE_TIME:
{
std::time_t time = std::time(nullptr);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S ");
return ss.str();
}
case types::UNIX:
{
std::stringstream ss;
ss << std::time(nullptr) << " ";
return ss.str();
}
case types::SINCE_START:
{
// give time in seconds to two decimal places
std::chrono::duration<float> const time_elapsed = std::chrono::system_clock::now() - time_start;
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << time_elapsed.count() << " ";
return ss.str();
}
}
#ifdef DISABLE_EXCEPTION_THROWING
return {};
#else // DISABLE_EXCEPTION_THROWING
throw std::runtime_error("LogStorm: Invalid timestamp type " + std::to_string(static_cast<unsigned int>(type)));
#endif // DISABLE_EXCEPTION_THROWING
}
std::string operator()();

explicit timestamp(types this_type = types::NONE);
~timestamp();
Expand Down

0 comments on commit b068c82

Please sign in to comment.