Skip to content

Commit

Permalink
log: avoid memory copy
Browse files Browse the repository at this point in the history
  • Loading branch information
sniper00 committed Jan 27, 2022
1 parent 4cdd215 commit 20f2084
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
26 changes: 13 additions & 13 deletions lualib-src/lua_moon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ static int lmoon_log(lua_State* L)
lua_service* S = lua_service::get(L);
auto level = (moon::LogLevel)luaL_checkinteger(L, 1);
if (S->logger()->get_level() < level)
{
return 0;
}

moon::buffer line = S->logger()->make_line(true, level, S->id());

int n = lua_gettop(L); /* number of arguments */
int i;
buffer buf;
for (i = 2; i <= n; i++) { /* for each argument */
size_t l;
const char* s = luaL_tolstring(L, i, &l); /* convert it to string */
if (i > 2) /* not the first element? */
buf.write_back("\t", 1); /* add a tab before it */
buf.write_back(s, l); /* print it */
line.write_back('\t'); /* add a tab before it */
line.write_back(s, l); /* print it */
lua_pop(L, 1); /* pop result */
}

Expand All @@ -113,16 +113,16 @@ static int lmoon_log(lua_State* L)
{
if (lua_getinfo(L, "Sl", &ar))
{
buf.write_back("\t(", 2);
if (ar.source != nullptr && ar.source[0] != '\0')
buf.write_back(ar.source + 1, std::strlen(ar.source) - 1);
buf.write_back(":", 1);
buf.write_chars(ar.currentline);
buf.write_back(")", 1);
line.write_back('\t');
line.write_back('(');
if (ar.srclen>1)
line.write_back(ar.source + 1, ar.srclen - 1);
line.write_back(':');
line.write_chars(ar.currentline);
line.write_back(')');
}
}

S->logger()->logstring(true, level, std::string_view{ buf.data(), buf.size() }, S->id());
S->logger()->push_line(std::move(line));
return 0;
}

Expand Down
24 changes: 17 additions & 7 deletions moon-src/core/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,34 @@ namespace moon
logstring(console, level, std::string_view{ str });
}

void logstring(bool console, LogLevel level, std::string_view s, uint64_t serviceid = 0)
buffer make_line(bool console, LogLevel level, uint64_t serviceid = 0, size_t datasize = 0)
{
if (level_ < level)
{
return;
}

if (level == LogLevel::Error)
++error_count_;

console = enable_console_ ? console : enable_console_;

auto line = buffer{64+s.size()};
auto line = buffer{ (datasize>0)?(64 + datasize):256 };
auto it = line.begin();
*(it++) = static_cast<char>(console);
*(it++) = static_cast<char>(level);
size_t offset = format_header(std::addressof(*it), level, serviceid);
line.commit(2 + offset);
return line;
}

void push_line(buffer line)
{
log_queue_.push_back(std::move(line));
++size_;
}

void logstring(bool console, LogLevel level, std::string_view s, uint64_t serviceid = 0)
{
if (level_ < level)
return;

buffer line = make_line(console, level, serviceid, s.size());
line.write_back(s.data(), s.size());
log_queue_.push_back(std::move(line));
++size_;
Expand Down

0 comments on commit 20f2084

Please sign in to comment.