Skip to content

Commit

Permalink
static/logger: Cache FD for each loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Oct 14, 2024
1 parent 5a09ef6 commit a157459
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion poseidon/fiber/redis_scan_and_get_future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Redis_Scan_and_Get_Future
struct Result
{
cow_string pattern; // input
::asteria::cow_dictionary<cow_string> pairs;
cow_dictionary<cow_string> pairs;
};

private:
Expand Down
1 change: 1 addition & 0 deletions poseidon/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ using ::rocket::recursive_mutex;
using ::rocket::condition_variable;
using ::rocket::array;
using ::rocket::cow_vector;
using ::asteria::cow_dictionary;
using ::rocket::cow_hashmap;
using ::rocket::static_vector;
using ::rocket::cow_string;
Expand Down
22 changes: 13 additions & 9 deletions poseidon/static/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Level_Config
bool p_stderr = false;
bool trivial = false;
cow_string color;
cow_string file;
prehashed_string file;
};

struct Message
Expand Down Expand Up @@ -126,7 +126,6 @@ do_load_level_config(Level_Config& lconf, const Config_File& conf_file, const ch
name, conf_value, conf_file.path());
}

inline
void
do_color(linear_buffer& mtext, const Level_Config& lconf, const char* code)
{
Expand All @@ -141,7 +140,7 @@ do_color(linear_buffer& mtext, const Level_Config& lconf, const char* code)
}

void
do_write_nothrow(const Level_Config& lconf, const Message& msg) noexcept
do_write_nothrow(cow_dictionary<unique_posix_fd>& io_files, const Level_Config& lconf, const Message& msg) noexcept
try {
linear_buffer mtext;
::rocket::ascii_numput nump;
Expand Down Expand Up @@ -241,10 +240,13 @@ do_write_nothrow(const Level_Config& lconf, const Message& msg) noexcept
if(lconf.p_stderr)
(void)! ::write(STDERR_FILENO, mtext.data(), mtext.size());

if(lconf.file != "")
if(const auto fd = unique_posix_fd(::open(lconf.file.c_str(),
O_WRONLY | O_APPEND | O_CREAT, 0644)))
(void)! ::write(fd, mtext.data(), mtext.size());
if(lconf.file != "") {
auto r = io_files.try_emplace(lconf.file);
if(r.second)
r.first->second.reset(::open(lconf.file.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644));
if(r.first->second)
(void)! ::write(r.first->second, mtext.data(), mtext.size());
}
}
catch(exception& stdex) {
::fprintf(stderr,
Expand Down Expand Up @@ -330,9 +332,10 @@ thread_loop()
// Write all elements.
for(const auto& msg : this->m_io_queue)
if((msg.level < levels.size()) && (!levels[msg.level].trivial || (queue_size < 1000)))
do_write_nothrow(levels[msg.level], msg);
do_write_nothrow(this->m_io_files, levels[msg.level], msg);

this->m_io_queue.clear();
this->m_io_files.clear();
io_sync_lock.unlock();
::sync();
}
Expand Down Expand Up @@ -379,9 +382,10 @@ synchronize() noexcept
// Write all elements.
for(const auto& msg : this->m_io_queue)
if(msg.level < levels.size())
do_write_nothrow(levels[msg.level], msg);
do_write_nothrow(this->m_io_files, levels[msg.level], msg);

this->m_io_queue.clear();
this->m_io_files.clear();
io_sync_lock.unlock();
::sync();
}
Expand Down
1 change: 1 addition & 0 deletions poseidon/static/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Logger

mutable recursive_mutex m_io_mutex;
cow_vector<X_Message> m_io_queue;
cow_dictionary<unique_posix_fd> m_io_files;

public:
// Creates a logger that outputs to nowhere.
Expand Down

0 comments on commit a157459

Please sign in to comment.