@@ -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
306306template <typename ... Args>
307307void 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
313313template <typename ... Args>
314314void 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
320320template <typename ... Args>
321321void 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
327327template <typename ... Args>
328328void 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
334334template <typename ... Args>
335335void 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
341341template <typename ... Args>
342342void 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
0 commit comments