Skip to content

Commit

Permalink
减少字符串copy
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo812 committed Sep 23, 2023
1 parent 452352b commit 3d46f94
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
12 changes: 6 additions & 6 deletions extend/lualog/lualog/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ namespace logger {

// class log_message
// --------------------------------------------------------------------------------
void log_message::option(log_level level, vstring msg, vstring tag, vstring feature, vstring source, int line) {
void log_message::option(log_level level, cstring& msg, cstring& tag, cstring& feature, cstring& source, int line) {
log_time_ = log_time::now();
feature_ = feature;
source_ = source;
feature_ = std::move(feature);
source_ = std::move(source);
msg_ = std::move(msg);
tag_ = std::move(tag);
level_ = level;
line_ = line;
msg_ = msg;
tag_ = tag;
}

// class log_message_pool
Expand Down Expand Up @@ -367,7 +367,7 @@ namespace logger {
}
}

void log_service::output(log_level level, vstring msg, vstring tag, vstring feature, vstring source, int line) {
void log_service::output(log_level level, cstring& msg, cstring& tag, cstring& feature, cstring& source, int line) {
if (!log_filter_.is_filter(level)) {
auto logmsg_ = message_pool_->allocate();
logmsg_->option(level, msg, tag, feature, source, line);
Expand Down
6 changes: 3 additions & 3 deletions extend/lualog/lualog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace logger {
void set_grow(bool grow) { grow_ = grow; }
log_level level() const { return level_; }
const log_time& get_log_time()const { return log_time_; }
void option(log_level level, vstring msg, vstring tag, vstring feature, vstring source, int line);
void option(log_level level, cstring& msg, cstring& tag, cstring& feature, cstring& source, int line);

private:
int line_ = 0;
Expand Down Expand Up @@ -239,7 +239,7 @@ namespace logger {
virtual bool add_file_dest(vstring feature, vstring fname) = 0;
virtual void set_dest_clean_time(vstring feature, size_t clean_time) = 0;
virtual void option(vstring log_path, vstring service, vstring index) = 0;
virtual void output(log_level level, vstring msg, vstring tag, vstring feature = "", vstring source = "", int line = 0) = 0;
virtual void output(log_level level, cstring& msg, cstring& tag, cstring& feature = "", cstring& source = "", int line = 0) = 0;
};

class log_service : public logger {
Expand Down Expand Up @@ -268,7 +268,7 @@ namespace logger {
bool is_filter(log_level lv) { return log_filter_.is_filter(lv); }
void filter(log_level lv, bool on) { log_filter_.filter(lv, on); }

void output(log_level level, vstring msg, vstring tag, vstring feature, vstring source, int line);
void output(log_level level, cstring& msg, cstring& tag, cstring& feature, cstring& source, int line);

protected:
path build_path(vstring feature, vstring fpath);
Expand Down
15 changes: 8 additions & 7 deletions extend/lualog/lualog/lualog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ namespace logger {
return "unsuppert data type";
}

int zformat(lua_State* L, log_level lvl, vstring tag, vstring feature, vstring msg) {
get_logger()->output(lvl, msg, tag, feature);
int zformat(lua_State* L, log_level lvl, cstring& tag, cstring& feature, cstring& msg) {
if (lvl == log_level::LOG_LEVEL_FATAL) {
lua_pushlstring(L, msg.data(), msg.size());
lua_pushlstring(L, msg.c_str(), msg.size());
get_logger()->output(lvl, msg, tag, feature);
return 1;
}
get_logger()->output(lvl, msg, tag, feature);
return 0;
}

template<size_t... integers>
int tformat(lua_State* L, log_level lvl, vstring tag, vstring feature, int flag, vstring vfmt, std::index_sequence<integers...>&&) {
int tformat(lua_State* L, log_level lvl, cstring& tag, cstring& feature, int flag, cstring& vfmt, std::index_sequence<integers...>&&) {
try {
auto msg = fmt::format(vfmt, read_args(L, flag, integers + 6)...);
return zformat(L, lvl, tag, feature, msg);
Expand All @@ -68,9 +69,9 @@ namespace logger {
log_level lvl = (log_level)lua_tointeger(L, 1);
if (get_logger()->is_filter(lvl)) return 0;
size_t flag = lua_tointeger(L, 2);
vstring tag = lua_to_native<vstring>(L, 3);
vstring feature = lua_to_native<vstring>(L, 4);
vstring vfmt = lua_to_native<vstring>(L, 5);
sstring tag = lua_to_native<sstring>(L, 3);
sstring feature = lua_to_native<sstring>(L, 4);
sstring vfmt = lua_to_native<sstring>(L, 5);
int arg_num = lua_gettop(L) - 5;
switch (arg_num) {
case 0: return zformat(L, lvl, tag, feature, vfmt);
Expand Down

0 comments on commit 3d46f94

Please sign in to comment.