Skip to content

Commit 2fc72f0

Browse files
committed
feat: Make logging period adjustable with defualt of 200 ms
1 parent 131a8b2 commit 2fc72f0

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

example/efp_logger_example.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ int main() {
88
// Optional log level setting. Default is LogLevel::Info
99
Logger::set_log_level(LogLevel::Trace);
1010

11+
// Optional log period setting. Default is 200ms
12+
// Logger::set_log_period(std::chrono::milliseconds(1000));
13+
1114
// Optional log output setting. // default is stdout
1215
// Logger::set_output("./efp_logger_test.log");
1316
// Logger::set_output(stdout);
@@ -21,10 +24,15 @@ int main() {
2124

2225
// Use the logging functions
2326
trace("This is a trace message with no formating");
24-
debug("This is a debug message with a pointer: {:p}", (void*)&x);
27+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
28+
debug("This is a debug message with a pointer: {}", (void*)&x);
29+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2530
info("This is a info message with a float: {}", 3.14f);
31+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2632
warn("This is a warn message with a int: {}", 42);
33+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2734
error("This is a error message with a string literal: {}", "error");
35+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2836
// ! Sending std::string to the buffer is O(n) and may increase risk of buffer overflow
2937
// ! Every 20 ~ 30 char will take one buffer space.
3038
fatal("This is a fatal message with a std::string: {}", std::string("fatal error"));

include/efp/logger.hpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ namespace efp {
8686
LogLevel level;
8787
};
8888

89-
constexpr uint8_t stl_string_data_capacity = sizeof(FormatedMessage);
90-
constexpr uint8_t stl_string_head_capacity = stl_string_data_capacity - sizeof(size_t);
89+
constexpr size_t stl_string_data_capacity = sizeof(FormatedMessage);
90+
constexpr size_t stl_string_head_capacity = stl_string_data_capacity - sizeof(size_t);
9191

9292
// Data structure for std::string preventing each char takes size of full Enum;
9393
struct StlStringHead {
@@ -380,15 +380,21 @@ namespace efp {
380380
}
381381

382382
static inline void set_log_level(LogLevel log_level) {
383-
384383
instance()._log_buffer.set_log_level(log_level);
385384
}
386385

387386
static inline LogLevel get_log_level() {
388387
return instance()._log_buffer.get_log_level();
389388
}
390389

391-
static void set_output(FILE* output_file) {
390+
static inline void set_log_period(std::chrono::milliseconds period) {
391+
_period = period;
392+
}
393+
394+
static inline std::chrono::milliseconds get_log_period() { return _period; }
395+
396+
static void
397+
set_output(FILE* output_file) {
392398
instance()._log_buffer.set_output_file(output_file);
393399
}
394400

@@ -449,25 +455,35 @@ namespace efp {
449455
_run(true),
450456
_thread([&]() {
451457
while (_run.load()) {
458+
const auto start_time_point = std::chrono::steady_clock::now();
452459
#if EFP_LOG_TIME_STAMP == true
453460
process_with_time();
454461
#else
455462
process();
456463
#endif
457-
// todo periodic
458-
std::this_thread::sleep_for(std::chrono::milliseconds{1});
464+
const auto end_time_point = std::chrono::steady_clock::now();
465+
const auto elapsed_time =
466+
std::chrono::duration_cast<std::chrono::milliseconds>(
467+
end_time_point - start_time_point);
468+
469+
if (elapsed_time < _period) {
470+
std::this_thread::sleep_for(_period - elapsed_time);
471+
}
459472
}
460473
}) {
461474
}
462475

476+
static std::chrono::milliseconds _period;
477+
463478
LogLevel _log_level;
464479

465480
detail::LogBuffer _log_buffer;
466-
467481
std::atomic<bool> _run;
468482
std::thread _thread;
469483
};
470484

485+
std::chrono::milliseconds Logger::_period = std::chrono::milliseconds(200);
486+
471487
// LogLevel Logger::instance().log_level = LogLevel::Debug;
472488

473489
namespace detail {

0 commit comments

Comments
 (0)