Skip to content

Commit d880d43

Browse files
updated
1 parent f93b3a1 commit d880d43

File tree

4 files changed

+165
-26
lines changed

4 files changed

+165
-26
lines changed

src/tglobal.h

Lines changed: 104 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ constexpr auto WriteOnly = QIODeviceBase::WriteOnly;
256256
#include <cstring>
257257
#include <functional>
258258
#include <algorithm>
259-
#if __has_include(<format>)
259+
#if __cplusplus >= 202002L && __has_include(<format>)
260260
# include <format>
261261
#endif
262262

@@ -300,49 +300,139 @@ inline bool strcmp(const QByteArray &str1, const QByteArray &str2)
300300
return str1.length() == str2.length() && !std::strncmp(str1.data(), str2.data(), str1.length());
301301
}
302302

303-
#if __has_include(<format>)
303+
#if __cplusplus >= 202002L && __has_include(<format>)
304304

305305
// Logging for developer
306306
template<typename... Args>
307307
void fatal(const std::format_string<Args...> &fmt, Args&&... args)
308308
{
309-
std::string str = std::format(fmt, std::forward<Args>(args)...);
310-
Tf::logging(Tf::FatalLevel, str);
309+
std::string msg = std::format(fmt, std::forward<Args>(args)...);
310+
Tf::logging(Tf::FatalLevel, msg);
311311
}
312312

313313
template<typename... Args>
314314
void error(const std::format_string<Args...> &fmt, Args&&... args)
315315
{
316-
std::string str = std::format(fmt, std::forward<Args>(args)...);
317-
Tf::logging(Tf::ErrorLevel, str);
316+
std::string msg = std::format(fmt, std::forward<Args>(args)...);
317+
Tf::logging(Tf::ErrorLevel, msg);
318318
}
319319

320320
template<typename... Args>
321321
void warn(const std::format_string<Args...> &fmt, Args&&... args)
322322
{
323-
std::string str = std::format(fmt, std::forward<Args>(args)...);
324-
Tf::logging(Tf::WarnLevel, str);
323+
std::string msg = std::format(fmt, std::forward<Args>(args)...);
324+
Tf::logging(Tf::WarnLevel, msg);
325325
}
326326

327327
template<typename... Args>
328328
void info(const std::format_string<Args...> &fmt, Args&&... args)
329329
{
330-
std::string str = std::format(fmt, std::forward<Args>(args)...);
331-
Tf::logging(Tf::InfoLevel, str);
330+
std::string msg = std::format(fmt, std::forward<Args>(args)...);
331+
Tf::logging(Tf::InfoLevel, msg);
332332
}
333333

334334
template<typename... Args>
335335
void debug(const std::format_string<Args...> &fmt, Args&&... args)
336336
{
337-
std::string str = std::format(fmt, std::forward<Args>(args)...);
338-
Tf::logging(Tf::DebugLevel, str);
337+
std::string msg = std::format(fmt, std::forward<Args>(args)...);
338+
Tf::logging(Tf::DebugLevel, msg);
339339
}
340340

341341
template<typename... Args>
342342
void trace(const std::format_string<Args...> &fmt, Args&&... args)
343343
{
344-
std::string str = std::format(fmt, std::forward<Args>(args)...);
345-
Tf::logging(Tf::TraceLevel, str);
344+
std::string msg = std::format(fmt, std::forward<Args>(args)...);
345+
Tf::logging(Tf::TraceLevel, msg);
346+
}
347+
348+
#else
349+
350+
template<typename... Args>
351+
std::string simple_format(const std::string &format, Args&&... args)
352+
{
353+
QByteArray res;
354+
const size_t len = format.size();
355+
QVariantList vars = { QVariant(args)... };
356+
size_t pos = 0;
357+
int argidx = 0;
358+
res.reserve(len * 2);
359+
360+
while (pos < len) {
361+
if (format[pos] == '{') {
362+
if (pos + 1 < len && format[pos + 1] == '}') {
363+
if (argidx < vars.count()) {
364+
res += vars.value(argidx).toByteArray();
365+
argidx++;
366+
pos += 2; // Skip 2 characters, '{}'
367+
continue;
368+
}
369+
} else {
370+
auto e = format.find('}', pos + 2);
371+
if (e != std::string::npos) {
372+
auto sz = e - pos - 1;
373+
auto subs = format.substr(pos + 1, sz);
374+
if (subs == ":x") {
375+
auto num = vars.value(argidx).toULongLong();
376+
res += QString::number(num, 16).toLatin1();
377+
} else if (subs == ":#x") {
378+
auto num = vars.value(argidx).toULongLong();
379+
res += "0x";
380+
res += QString::number(num, 16).toLatin1();
381+
} else {
382+
// other format
383+
}
384+
argidx++;
385+
pos += sz + 2;
386+
continue;
387+
}
388+
}
389+
}
390+
res += format[pos++];
391+
}
392+
return res.toStdString();
393+
}
394+
395+
// Logging for developer
396+
template<typename... Args>
397+
void fatal(const std::string &fmt, Args&&... args)
398+
{
399+
std::string msg = simple_format(std::string(fmt), std::forward<Args>(args)...);
400+
Tf::logging(Tf::FatalLevel, msg);
401+
}
402+
403+
template<typename... Args>
404+
void error(const std::string &fmt, Args&&... args)
405+
{
406+
std::string msg = simple_format(std::string(fmt), std::forward<Args>(args)...);
407+
Tf::logging(Tf::ErrorLevel, msg);
408+
}
409+
410+
template<typename... Args>
411+
void warn(const std::string &fmt, Args&&... args)
412+
{
413+
std::string msg = simple_format(std::string(fmt), std::forward<Args>(args)...);
414+
Tf::logging(Tf::WarnLevel, msg);
415+
}
416+
417+
template<typename... Args>
418+
void info(const std::string &fmt, Args&&... args)
419+
{
420+
std::string msg = simple_format(std::string(fmt), std::forward<Args>(args)...);
421+
Tf::logging(Tf::InfoLevel, msg);
422+
}
423+
424+
template<typename... Args>
425+
void debug(const std::string &fmt, Args&&... args)
426+
{
427+
std::string msg = simple_format(std::string(fmt), std::forward<Args>(args)...);
428+
Tf::logging(Tf::DebugLevel, msg);
429+
}
430+
431+
template<typename... Args>
432+
void trace(const std::string &fmt, Args&&... args)
433+
{
434+
std::string msg = simple_format(std::string(fmt), std::forward<Args>(args)...);
435+
Tf::logging(Tf::TraceLevel, msg);
346436
}
347437

348438
#endif

src/tsystemglobal.h

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#include <QSettings>
44
#include <QVariant>
55
#include <TGlobal>
6-
#if __has_include(<format>)
6+
#if __cplusplus >= 202002L && __has_include(<format>)
77
#include <format>
88
#endif
9+
#include <tuple>
910

1011
class TSystemLogger;
1112
class TAccessLog;
@@ -24,7 +25,7 @@ T_CORE_EXPORT void writeAccessLog(const TAccessLog &log); // write access log
2425
T_CORE_EXPORT void writeQueryLog(const QString &query, bool success, const QSqlError &error, int duration);
2526
T_CORE_EXPORT void traceQuery(int duration, const std::string &msg);
2627

27-
#if __has_include(<format>)
28+
#if __cplusplus >= 202002L && __has_include(<format>)
2829

2930
template<typename... Args>
3031
void traceQueryLog(int duration, const std::format_string<Args...> &fmt, Args&&... args)
@@ -33,6 +34,15 @@ void traceQueryLog(int duration, const std::format_string<Args...> &fmt, Args&&.
3334
traceQuery(duration, msg);
3435
}
3536

37+
#else
38+
39+
template<typename... Args>
40+
void traceQueryLog(int duration, const std::string &fmt, Args&&... args)
41+
{
42+
auto msg = Tf::simple_format(std::string(fmt), std::forward<Args>(args)...);
43+
traceQuery(duration, msg);
44+
}
45+
3646
#endif
3747

3848
enum SystemOpCode {
@@ -47,7 +57,7 @@ enum SystemOpCode {
4757
T_CORE_EXPORT QMap<QString, QVariant> settingsToMap(QSettings &settings, const QString &env = QString());
4858
}
4959

50-
#if __has_include(<format>)
60+
#if __cplusplus >= 202002L && __has_include(<format>)
5161

5262
template<typename... Args>
5363
void tSystemError(const std::format_string<Args...> &fmt, Args&&... args)
@@ -70,9 +80,33 @@ void tSystemInfo(const std::format_string<Args...> &fmt, Args&&... args)
7080
Tf::tSystemMessage((int)Tf::InfoLevel, msg);
7181
}
7282

83+
#else
84+
85+
template<typename... Args>
86+
void tSystemError(const std::string &fmt, Args&&... args)
87+
{
88+
std::string msg = Tf::simple_format(std::string(fmt), std::forward<Args>(args)...);
89+
Tf::tSystemMessage((int)Tf::ErrorLevel, msg);
90+
}
91+
92+
template<typename... Args>
93+
void tSystemWarn(const std::string &fmt, Args&&... args)
94+
{
95+
std::string msg = Tf::simple_format(std::string(fmt), std::forward<Args>(args)...);
96+
Tf::tSystemMessage((int)Tf::WarnLevel, msg);
97+
}
98+
99+
template<typename... Args>
100+
void tSystemInfo(const std::string &fmt, Args&&... args)
101+
{
102+
auto msg = Tf::simple_format(std::string(fmt), std::forward<Args>(args)...);
103+
Tf::tSystemMessage((int)Tf::InfoLevel, msg);
104+
}
105+
73106
#endif
74107

75-
#if !defined(TF_NO_DEBUG) && __has_include(<format>)
108+
#if !defined(TF_NO_DEBUG)
109+
#if __cplusplus >= 202002L && __has_include(<format>)
76110

77111
template<typename... Args>
78112
void tSystemDebug(const std::format_string<Args...> &fmt, Args&&... args)
@@ -91,13 +125,28 @@ void tSystemTrace(const std::format_string<Args...> &fmt, Args&&... args)
91125
#else
92126

93127
template<typename... Args>
94-
void tSystemDebug(const std::format_string<Args...> &, Args&&...)
128+
void tSystemDebug(const std::string &fmt, Args&&... args)
95129
{
130+
auto msg = Tf::simple_format(std::string(fmt), std::forward<Args>(args)...);
131+
Tf::tSystemMessage((int)Tf::DebugLevel, msg);
96132
}
97133

98134
template<typename... Args>
99-
void tSystemTrace(const std::format_string<Args...> &, Args&&...)
135+
void tSystemTrace(const std::string &fmt, Args&&... args)
100136
{
137+
auto msg = Tf::simple_format(std::string(fmt), std::forward<Args>(args)...);
138+
Tf::tSystemMessage((int)Tf::TraceLevel, msg);
101139
}
102140

103141
#endif
142+
#else
143+
144+
template<typename... Args>
145+
void tSystemDebug(Args&&...)
146+
{}
147+
148+
template<typename... Args>
149+
void tSystemTrace(Args&&...)
150+
{}
151+
152+
#endif

tools/tfmanager/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ bool startDaemon()
186186

187187
void writeStartupLog()
188188
{
189-
tSystemInfo("TreeFrog Framework version %s", TF_VERSION_STR);
189+
tSystemInfo("TreeFrog Framework version {}", TF_VERSION_STR);
190190

191191
QString qtversion = QLatin1String("Execution environment: Qt ") + qVersion();
192192
#if QT_VERSION >= 0x050400
@@ -203,7 +203,7 @@ void writeStartupLog()
203203
}
204204
#endif
205205
#endif
206-
tSystemInfo("%s", qtversion.toLatin1().data());
206+
tSystemInfo("{}", qtversion.toLatin1().data());
207207
}
208208

209209

@@ -685,7 +685,7 @@ int managerMain(int argc, char *argv[])
685685
pidfile.write(QJsonDocument(json).toJson(QJsonDocument::Indented));
686686
pidfile.close();
687687
} else {
688-
tSystemError("File open failed: %s", qUtf8Printable(pidfile.fileName()));
688+
tSystemError("File open failed: {}", qUtf8Printable(pidfile.fileName()));
689689
}
690690

691691
ret = app.exec();

tools/tfserver/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void messageOutput(QtMsgType type, const QMessageLogContext &context, const QStr
5858
#if defined(Q_OS_UNIX) || !defined(TF_NO_DEBUG)
5959
void writeFailure(const char *data, size_t size)
6060
{
61-
tSystemError("%s", QByteArray(data, size).replace('\n', "").data());
61+
tSystemError("{}", QByteArray(data, size).replace('\n', "").data());
6262
}
6363
#endif
6464

@@ -226,7 +226,7 @@ int main(int argc, char *argv[])
226226
if (!loc.isEmpty()) {
227227
QLocale locale(loc);
228228
QLocale::setDefault(locale);
229-
tSystemInfo("Application's default locale: %s", qUtf8Printable(locale.name()));
229+
tSystemInfo("Application's default locale: {}", qUtf8Printable(locale.name()));
230230
}
231231

232232
#if QT_VERSION < 0x060000
@@ -240,7 +240,7 @@ int main(int argc, char *argv[])
240240
std::fprintf(stderr, "No such directory\n");
241241
goto finish;
242242
}
243-
tSystemDebug("Web Root: %s", qUtf8Printable(webapp.webRootPath()));
243+
tSystemDebug("Web Root: {}", qUtf8Printable(webapp.webRootPath()));
244244

245245
if (!webapp.appSettingsFileExists()) {
246246
tSystemError("Settings file not found");

0 commit comments

Comments
 (0)