Skip to content

Commit c3972d5

Browse files
committed
refactor: replace most + with format
1 parent b340151 commit c3972d5

File tree

9 files changed

+41
-23
lines changed

9 files changed

+41
-23
lines changed

src/legacy/api/APIHelp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ bool IsInstanceOf(Local<Value> v) {
2020
std::string ValueKindToString(ValueKind const& kind);
2121

2222
// 输出脚本调用堆栈,API名称,以及插件名
23-
inline Exception CreateExceptionWithInfo(std::string const& func, std::string const& msg) {
24-
return Exception(fmt::format("In API: {}, In plugin: {}, {}", func, getEngineOwnData()->pluginName, msg));
23+
template <typename... Args>
24+
Exception CreateExceptionWithInfo(std::string const& func, Args... msg) {
25+
return Exception(fmt::format("In API: {}, In plugin: {}, {}", func, getEngineOwnData()->pluginName, msg...));
2526
}
2627

2728
inline void LogErrorWithInfo(std::string const& func) {

src/legacy/api/CommandAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void onExecute(CommandOrigin const& origin, CommandOutput& output, RuntimeComman
412412
output.mSuccessCount = outp->output->mSuccessCount;
413413
outp->isAsync = true;
414414
}
415-
CATCH_WITH_MESSAGE("Fail in executing command \"" + commandName + "\"!")
415+
CATCH_WITH_MESSAGE("Fail in executing command \"{}\"!", commandName)
416416
}
417417

418418
// name, type, optional, description, identifier, option

src/legacy/api/EventAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void CallEventImpl(EventListener& listener, bool& returnValue, EVENT_TYPES type,
179179
returnValue = false;
180180
}
181181
}
182-
CATCH_WITH_MESSAGE("CallEvent Callback Failed! In Event: " + EventTypeToString(type))
182+
CATCH_WITH_MESSAGE("CallEvent Callback Failed! In Event: {}", EventTypeToString(type))
183183
}
184184

185185
#define FakeCallEvent(ENGINE, TYPE, ...) \
@@ -205,7 +205,7 @@ void FakeCallEventImpl(EventListener& listener, ScriptEngine* engine, EVENT_TYPE
205205
try
206206
#define IF_LISTENED_END(TYPE) \
207207
catch (...) { \
208-
lse::LegacyScriptEngine::getLogger().error("Event Callback Failed! In Event: " + EventTypeToString(TYPE)); \
208+
lse::LegacyScriptEngine::getLogger().error("Event Callback Failed! In Event: {}", EventTypeToString(TYPE)); \
209209
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger()); \
210210
} \
211211
}

src/legacy/api/FileSystemAPI.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ FileClass* FileClass::constructor(Arguments const& args) {
9999
std::filesystem::create_directories(path.parent_path());
100100
}
101101
} else {
102-
throw CreateExceptionWithInfo(__FUNCTION__, "File " + args[0].asString().toString() + " doesn't exist!");
102+
throw CreateExceptionWithInfo(__FUNCTION__, "File {} doesn't exist!", args[0].asString().toString());
103103
}
104104
FileOpenMode fMode = static_cast<FileOpenMode>(args[1].asNumber().toInt32());
105105
// Auto Create
@@ -126,7 +126,7 @@ FileClass* FileClass::constructor(Arguments const& args) {
126126

127127
std::fstream fs(path, mode);
128128
if (!fs.is_open()) {
129-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to Open File " + path.string() + "!");
129+
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to Open File {}!", path.string());
130130
}
131131
return new FileClass(args.thiz(), std::move(fs), path.string(), isBinary);
132132
}
@@ -644,10 +644,16 @@ Local<Value> FileWriteTo(Arguments const& args) {
644644
std::error_code code;
645645
std::filesystem::create_directories(path.parent_path(), code);
646646
if (code) {
647-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to create directory " + path.parent_path().string() + "!");
647+
throw CreateExceptionWithInfo(
648+
__FUNCTION__,
649+
"Fail to create directory " + path.parent_path().string() + "!"
650+
);
648651
}
649652
} else {
650-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to create directory of " + args[0].asString().toString() + "!");
653+
throw CreateExceptionWithInfo(
654+
__FUNCTION__,
655+
"Fail to create directory of " + args[0].asString().toString() + "!"
656+
);
651657
}
652658
return Boolean::newBoolean(ll::file_utils::writeFile(path, args[1].asString().toString(), false));
653659
}
@@ -665,10 +671,16 @@ Local<Value> FileWriteLine(Arguments const& args) {
665671
std::error_code code;
666672
std::filesystem::create_directories(path.parent_path(), code);
667673
if (code) {
668-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to create directory " + path.parent_path().string() + "!");
674+
throw CreateExceptionWithInfo(
675+
__FUNCTION__,
676+
"Fail to create directory " + path.parent_path().string() + "!"
677+
);
669678
}
670679
} else {
671-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to create directory of " + args[0].asString().toString() + "!");
680+
throw CreateExceptionWithInfo(
681+
__FUNCTION__,
682+
"Fail to create directory of " + args[0].asString().toString() + "!"
683+
);
672684
}
673685

674686
std::ofstream fileWrite(path, std::ios::app);
@@ -693,10 +705,14 @@ Local<Value> OpenFile(Arguments const& args) {
693705
std::error_code code;
694706
std::filesystem::create_directories(path.parent_path(), code);
695707
if (code) {
696-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to create directory " + path.parent_path().string() + "!");
708+
throw CreateExceptionWithInfo(
709+
__FUNCTION__,
710+
"Fail to create directory {}!",
711+
path.parent_path().string()
712+
);
697713
}
698714
} else {
699-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to create directory " + args[0].asString().toString() + "!");
715+
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to create directory {}!", args[0].asString().toString());
700716
}
701717

702718
FileOpenMode fMode = static_cast<FileOpenMode>(args[1].asNumber().toInt32());
@@ -716,7 +732,7 @@ Local<Value> OpenFile(Arguments const& args) {
716732

717733
std::fstream fs(path, mode);
718734
if (!fs.is_open()) {
719-
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to Open File " + path.string() + "!");
735+
throw CreateExceptionWithInfo(__FUNCTION__, "Fail to Open File {}!", path.string());
720736
}
721737
return FileClass::newFile(std::move(fs), path.string(), isBinary);
722738
}

src/legacy/api/NetworkAPI.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void WSClientClass::addListener(string const& event, Local<Function> const& func
282282
{EngineScope::currentEngine(), script::Global<Function>(func)}
283283
);
284284
else {
285-
throw CreateExceptionWithInfo(__FUNCTION__, "WSClient Event \"" + event + "\" No Found!");
285+
throw CreateExceptionWithInfo(__FUNCTION__, "WSClient Event \"{}\" No Found!", event);
286286
}
287287
}
288288

@@ -342,7 +342,8 @@ Local<Value> WSClientClass::connectAsync(Arguments const& args) {
342342
NewTimeout(callback.get(), {Boolean::newBoolean(result)}, 0);
343343
} catch (...) {
344344
lse::LegacyScriptEngine::getLogger().error(
345-
"WSClientClass::connectAsync Failed! In plugin: {}", pluginName
345+
"WSClientClass::connectAsync Failed! In plugin: {}",
346+
pluginName
346347
);
347348
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger());
348349
}

src/legacy/legacyapi/db/impl/mysql/Session.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void MySQLSession::open(ConnParams const& params) {
106106
}
107107

108108
bool MySQLSession::execute(std::string const& query) {
109-
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("MySQLSession::execute: Executing > " + query);
109+
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("MySQLSession::execute: Executing > {}", query);
110110
auto res = mysql_query(conn, query.c_str());
111111
return res == OK;
112112
}
@@ -119,7 +119,7 @@ bool MySQLSession::relogin(std::string const& user, std::string const& password,
119119
}
120120

121121
Session& MySQLSession::query(std::string const& query, std::function<bool(Row const&)> callback) {
122-
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("MySQLSession::query: Querying > " + query);
122+
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("MySQLSession::query: Querying > {}", query);
123123
auto res = mysql_query(conn, query.c_str());
124124
if (res != OK) {
125125
throw std::runtime_error("MySQLSession::query: Failed to query database: " + std::string(mysql_error(conn)));

src/legacy/legacyapi/db/impl/mysql/Stmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ SharedPointer<Stmt> MySQLStmt::create(std::weak_ptr<Session> const& session, std
616616
result->query = sql;
617617
result->paramIndexes = params;
618618
result->setDebugOutput(raw->debugOutput);
619-
if (raw->debugOutput) lse::LegacyScriptEngine::getLogger().debug("MySQLStmt::create: Prepared > " + query);
619+
if (raw->debugOutput) lse::LegacyScriptEngine::getLogger().debug("MySQLStmt::create: Prepared > {}", query);
620620
auto shared = SharedPointer<Stmt>(result);
621621
result->self = shared;
622622
return shared;

src/legacy/legacyapi/db/impl/sqlite/Session.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ void SQLiteSession::open(ConnParams const& params) {
7373
if (res != SQLITE_OK) {
7474
throw std::runtime_error("SQLiteSession::open: Failed to open database: " + std::string(sqlite3_errmsg(conn)));
7575
}
76-
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("SQLiteSession::open: Opened database: " + std::string(path));
76+
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("SQLiteSession::open: Opened database: {}", std::string(path));
7777
}
7878

7979
bool SQLiteSession::execute(std::string const& query) {
80-
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("SQLiteSession::execute: Executing > " + query);
80+
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("SQLiteSession::execute: Executing > {}", query);
8181
auto res = sqlite3_exec(conn, query.c_str(), nullptr, nullptr, nullptr);
8282
return res == SQLITE_OK;
8383
}
8484

8585
Session& SQLiteSession::query(std::string const& query, std::function<bool(Row const&)> callback) {
86-
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("SQLiteSession::query: Querying > " + query);
86+
IF_ENDBG lse::LegacyScriptEngine::getLogger().debug("SQLiteSession::query: Querying > {}", query);
8787
auto stmt = prepare(query, false);
8888
stmt->fetchAll(callback);
8989
return *this;

src/legacy/legacyapi/db/impl/sqlite/Stmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ SQLiteStmt::create(std::weak_ptr<Session> const& session, std::string const& sql
313313
auto result = std::make_shared<SQLiteStmt>(stmt, session, autoExecute);
314314
result->parent = session;
315315
result->setDebugOutput(raw->debugOutput);
316-
if (raw->debugOutput) lse::LegacyScriptEngine::getLogger().debug("SQLiteStmt::create: Prepared > " + sql);
316+
if (raw->debugOutput) lse::LegacyScriptEngine::getLogger().debug("SQLiteStmt::create: Prepared > {}", sql);
317317
auto shared = SharedPointer<Stmt>(result);
318318
result->self = shared;
319319
return shared;

0 commit comments

Comments
 (0)