Skip to content

Commit 79fa7c6

Browse files
committed
More std::format
1 parent 32653bf commit 79fa7c6

21 files changed

+119
-178
lines changed

src/native/monodroid/embedded-assemblies-zip.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ EmbeddedAssemblies::zip_load_entry_common (size_t entry_index, std::vector<uint8
4545
);
4646
}
4747

48-
log_debug (LOG_ASSEMBLY, std::format (" ZIP: local header offset: {}; data offset: {}; file size: {}", state.local_header_offset, state.data_offset, state.file_size).c_str ());
48+
log_debug (LOG_ASSEMBLY, std::format (" ZIP: local header offset: {}; data offset: {}; file size: {}", state.local_header_offset, state.data_offset, state.file_size));
4949
if (state.compression_method != 0) {
5050
return false;
5151
}

src/native/monodroid/internal-pinvokes.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <string_view>
2+
13
#include "android-system.hh"
24
#include "globals.hh"
35
#include "internal-pinvokes.hh"
@@ -31,30 +33,30 @@ monodroid_log (LogLevel level, LogCategories category, const char *message)
3133
switch (level) {
3234
case LogLevel::Verbose:
3335
case LogLevel::Debug:
34-
log_debug_nocheck (category, message);
36+
log_debug_nocheck (category, std::string_view { message });
3537
break;
3638

3739
case LogLevel::Info:
38-
log_info_nocheck (category, message);
40+
log_info_nocheck (category, std::string_view { message });
3941
break;
4042

4143
case LogLevel::Warn:
4244
case LogLevel::Silent: // warn is always printed
43-
log_warn (category, message);
45+
log_warn (category, std::string_view { message });
4446
break;
4547

4648
case LogLevel::Error:
47-
log_error (category, message);
49+
log_error (category, std::string_view { message });
4850
break;
4951

5052
case LogLevel::Fatal:
51-
log_fatal (category, message);
53+
log_fatal (category, std::string_view { message });
5254
break;
5355

5456
default:
5557
case LogLevel::Unknown:
5658
case LogLevel::Default:
57-
log_info_nocheck (category, message);
59+
log_info_nocheck (category, std::string_view { message });
5860
break;
5961
}
6062
}
@@ -156,7 +158,7 @@ _monodroid_timezone_get_default_id ()
156158
const char *mutf8 = env->GetStringUTFChars (id, nullptr);
157159

158160
if (mutf8 == nullptr) {
159-
log_error (LOG_DEFAULT, "Failed to convert Java TimeZone ID to UTF8 (out of memory?)");
161+
log_error (LOG_DEFAULT, "Failed to convert Java TimeZone ID to UTF8 (out of memory?)"sv);
160162
env->DeleteLocalRef (id);
161163
env->DeleteLocalRef (d);
162164
return nullptr;

src/native/monodroid/mono-image-loader.hh

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ namespace xamarin::android::internal {
110110
force_inline static MonoImage* stash_and_return (MonoImage *image, MonoImageOpenStatus status, [[maybe_unused]] hash_t hash) noexcept
111111
{
112112
if (image == nullptr || status != MonoImageOpenStatus::MONO_IMAGE_OK) {
113-
log_warn (LOG_ASSEMBLY, "Failed to open assembly image. %s", mono_image_strerror (status));
113+
log_warn (LOG_ASSEMBLY, std::format ("Failed to open assembly image. {}", mono_image_strerror (status)));
114114
return nullptr;
115115
}
116116

@@ -119,8 +119,8 @@ namespace xamarin::android::internal {
119119
if (index < 0) {
120120
Helpers::abort_application (
121121
LOG_ASSEMBLY,
122-
Util::monodroid_strdup_printf (
123-
"Failed to look up image index for hash 0x%zx",
122+
std::format (
123+
"Failed to look up image index for hash {:x}",
124124
hash
125125
)
126126
);

src/native/monodroid/monodroid-glue-internal.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace xamarin::android::internal
166166
void *symptr = MonodroidDl::monodroid_dlsym (handle, name, &err, nullptr);
167167

168168
if (symptr == nullptr) {
169-
log_warn (LOG_DEFAULT, "Failed to load symbol '%s' library with handle %p. %s", name, handle, err == nullptr ? "Unknown error" : err);
169+
log_warn (LOG_DEFAULT, std::format ("Failed to load symbol '{}' library with handle {}. {}", name, handle, err == nullptr ? "Unknown error"sv : err));
170170
fnptr = nullptr;
171171
return;
172172
}

src/native/monodroid/monodroid-networkinfo.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ _monodroid_get_network_interface_state (const char *ifname, mono_bool *is_up, mo
7676

7777
if (!NetworkInterface_class || !NetworkInterface_getByName) {
7878
if (!NetworkInterface_class)
79-
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface' Java class");
79+
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface' Java class"sv);
8080
if (!NetworkInterface_getByName)
81-
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface.getByName' function");
82-
log_warn (LOG_NET, "Unable to determine network interface state because of missing Java API");
81+
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface.getByName' function"sv);
82+
log_warn (LOG_NET, "Unable to determine network interface state because of missing Java API"sv);
8383
goto leave;
8484
}
8585

@@ -88,37 +88,37 @@ _monodroid_get_network_interface_state (const char *ifname, mono_bool *is_up, mo
8888
networkInterface = env->CallStaticObjectMethod (NetworkInterface_class, NetworkInterface_getByName, NetworkInterface_nameArg);
8989
env->DeleteLocalRef (NetworkInterface_nameArg);
9090
if (env->ExceptionOccurred ()) {
91-
log_warn (LOG_NET, "Java exception occurred while looking up the interface '%s'", ifname);
91+
log_warn (LOG_NET, std::format ("Java exception occurred while looking up the interface '{}'", ifname));
9292
env->ExceptionDescribe ();
9393
env->ExceptionClear ();
9494
goto leave;
9595
}
9696

9797
if (!networkInterface) {
98-
log_warn (LOG_NET, "Failed to look up interface '%s' using Java API", ifname);
98+
log_warn (LOG_NET, std::format ("Failed to look up interface '{}' using Java API", ifname));
9999
ret = FALSE;
100100
goto leave;
101101
}
102102

103103
if (is_up) {
104104
if (!NetworkInterface_isUp) {
105-
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface.isUp' function. Unable to determine interface operational state");
105+
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface.isUp' function. Unable to determine interface operational state"sv);
106106
ret = FALSE;
107107
} else
108108
*is_up = (mono_bool)env->CallBooleanMethod (networkInterface, NetworkInterface_isUp);
109109
}
110110

111111
if (supports_multicast) {
112112
if (!NetworkInterface_supportsMulticast) {
113-
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface.supportsMulticast' function. Unable to determine whether interface supports multicast");
113+
log_warn (LOG_NET, "Failed to find the 'java.net.NetworkInterface.supportsMulticast' function. Unable to determine whether interface supports multicast"sv);
114114
ret = FALSE;
115115
} else
116116
*supports_multicast = (mono_bool)env->CallBooleanMethod (networkInterface, NetworkInterface_supportsMulticast);
117117
}
118118

119119
leave:
120120
if (!ret)
121-
log_warn (LOG_NET, "Unable to determine interface '%s' state using Java API", ifname);
121+
log_warn (LOG_NET, std::format ("Unable to determine interface '{}' state using Java API", ifname));
122122

123123
if (networkInterface != nullptr && env != nullptr) {
124124
env->DeleteLocalRef (networkInterface);
@@ -143,7 +143,7 @@ int
143143
_monodroid_get_dns_servers (void **dns_servers_array)
144144
{
145145
if (!dns_servers_array) {
146-
log_warn (LOG_NET, "Unable to get DNS servers, no location to store data in");
146+
log_warn (LOG_NET, "Unable to get DNS servers, no location to store data in"sv);
147147
return -1;
148148
}
149149
*dns_servers_array = nullptr;

src/native/monodroid/monodroid-tracing.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ MonodroidRuntime::log_traces (JNIEnv *env, TraceKind kind, const char *first_lin
2727
char *err = nullptr;
2828
void *handle = MonodroidDl::monodroid_dlopen (SharedConstants::xamarin_native_tracing_name.data (), MONO_DL_EAGER, &err, nullptr);
2929
if (handle == nullptr) {
30-
log_warn (LOG_DEFAULT, "Failed to load native tracing library '%s'. %s", SharedConstants::xamarin_native_tracing_name, err == nullptr ? "Unknown error" : err);
30+
log_warn (LOG_DEFAULT, std::format ("Failed to load native tracing library '{}'. {}", SharedConstants::xamarin_native_tracing_name, err == nullptr ? "Unknown error"sv : err));
3131
} else {
3232
load_symbol (handle, "xa_get_native_backtrace", _xa_get_native_backtrace);
3333
load_symbol (handle, "xa_get_managed_backtrace", _xa_get_managed_backtrace);

src/native/monodroid/xamarin-android-app-context.cc

+29-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ MonodroidRuntime::get_method_name (uint32_t mono_image_index, uint32_t method_to
1515
{
1616
uint64_t id = (static_cast<uint64_t>(mono_image_index) << 32) | method_token;
1717

18-
log_debug (LOG_ASSEMBLY, "MM: looking for name of method with id 0x%llx, in mono image at index %u", id, mono_image_index);
18+
log_debug (LOG_ASSEMBLY, std::format ("MM: looking for name of method with id {:x}, in mono image at index {}", id, mono_image_index));
1919
size_t i = 0uz;
2020
while (mm_method_names[i].id != 0) {
2121
if (mm_method_names[i].id == id) {
@@ -43,15 +43,16 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
4343
{
4444
log_debug (
4545
LOG_ASSEMBLY,
46-
"MM: Trying to look up pointer to method '%s' (token 0x%x) in class '%s' (index %u)",
47-
get_method_name (mono_image_index, method_token), method_token,
48-
get_class_name (class_index), class_index
46+
std::format ("MM: Trying to look up pointer to method '{}' (token {:x}) in class '{}' (index {})",
47+
get_method_name (mono_image_index, method_token), method_token,
48+
get_class_name (class_index), class_index
49+
)
4950
);
5051

5152
if (class_index >= marshal_methods_number_of_classes) [[unlikely]] {
5253
Helpers::abort_application (
53-
Util::monodroid_strdup_printf (
54-
"Internal error: invalid index for class cache (expected at most %u, got %u)",
54+
std::format (
55+
"Internal error: invalid index for class cache (expected at most {}, got {})",
5556
marshal_methods_number_of_classes - 1,
5657
class_index
5758
)
@@ -78,29 +79,40 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
7879
target_ptr = ret;
7980
}
8081

81-
log_debug (LOG_ASSEMBLY, "Loaded pointer to method %s (%p) (mono_image_index == %u; class_index == %u; method_token == 0x%x)", mono_method_full_name (method, true), ret, mono_image_index, class_index, method_token);
82+
log_debug (
83+
LOG_ASSEMBLY,
84+
std::format ("Loaded pointer to method {} ({:p}) (mono_image_index == {}; class_index == {}; method_token == {:x})",
85+
mono_method_full_name (method, true),
86+
ret,
87+
mono_image_index,
88+
class_index,
89+
method_token
90+
)
91+
);
8292
return;
8393
}
8494

8595
log_fatal (
8696
LOG_DEFAULT,
87-
"Failed to obtain function pointer to method '%s' in class '%s'",
88-
get_method_name (mono_image_index, method_token),
89-
get_class_name (class_index)
97+
std::format ("Failed to obtain function pointer to method '{}' in class '{}'",
98+
get_method_name (mono_image_index, method_token),
99+
get_class_name (class_index)
100+
)
90101
);
91102

92103
log_fatal (
93104
LOG_DEFAULT,
94-
"Looked for image index %u, class index %u, method token 0x%x",
95-
mono_image_index,
96-
class_index,
97-
method_token
105+
std::format ("Looked for image index {}, class index {}, method token {:x}",
106+
mono_image_index,
107+
class_index,
108+
method_token
109+
)
98110
);
99111

100112
if (image == nullptr) {
101-
log_fatal (LOG_DEFAULT, "Failed to load MonoImage for the assembly");
113+
log_fatal (LOG_DEFAULT, "Failed to load MonoImage for the assembly"sv);
102114
} else if (method == nullptr) {
103-
log_fatal (LOG_DEFAULT, "Failed to load class from the assembly");
115+
log_fatal (LOG_DEFAULT, "Failed to load class from the assembly"sv);
104116
}
105117

106118
const char *message = nullptr;
@@ -109,7 +121,7 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
109121
}
110122

111123
Helpers::abort_application (
112-
message == nullptr ? "Failure to obtain marshal methods function pointer" : message
124+
message == nullptr ? "Failure to obtain marshal methods function pointer"sv : message
113125
);
114126
}
115127

src/native/pinvoke-override/precompiled.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ PinvokeOverride::monodroid_pinvoke_override (const char *library_name, const cha
6767

6868
load_library_entry (library_name, entrypoint_name, *entry, dotnet_dso_handle);
6969
if (entry->func == nullptr) {
70-
log_fatal (LOG_ASSEMBLY, "Failed to load symbol '%s' from shared library '%s'", entrypoint_name, library_name);
70+
log_fatal (LOG_ASSEMBLY, std::format ("Failed to load symbol '{}' from shared library '{}'", entrypoint_name, library_name));
7171
return nullptr; // let Mono deal with the fallout
7272
}
7373

src/native/runtime-base/internal-pinvokes.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <cstdio>
44

55
#include <mono/utils/mono-publib.h>
6-
#include <java-interop-logger.h>
6+
//#include <java-interop-logger.h>
77
#include <java-interop.h>
88
#include <jni.h>
99

src/native/runtime-base/logger.cc

+12-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ namespace {
2727
FILE *f;
2828

2929
if (path && access (path, W_OK) < 0) {
30-
log_warn (category, "Could not open path '%s' for logging (\"%s\"). Using '%s/%s' instead.",
31-
path, strerror (errno), override_dir, filename);
30+
log_warn (category,
31+
std::format (
32+
"Could not open path '{}' for logging (\"{}\"). Using '{}/{}' instead.",
33+
path,
34+
strerror (errno),
35+
override_dir,
36+
filename
37+
)
38+
);
3239
path = NULL;
3340
}
3441

@@ -45,7 +52,7 @@ namespace {
4552
if (f) {
4653
Util::set_world_accessable (path);
4754
} else {
48-
log_warn (category, "Could not open path '%s' for logging: %s", path, strerror (errno));
55+
log_warn (category, std::format ("Could not open path '{}' for logging: {}", path, strerror (errno)));
4956
}
5057

5158
free (p);
@@ -71,12 +78,12 @@ Logger::set_debugger_log_level (const char *level) noexcept
7178

7279
unsigned long v = strtoul (level, nullptr, 0);
7380
if (v == std::numeric_limits<unsigned long>::max () && errno == ERANGE) {
74-
log_error (LOG_DEFAULT, "Invalid debugger log level value '%s', expecting a positive integer or zero", level);
81+
log_error (LOG_DEFAULT, std::format ("Invalid debugger log level value '{}', expecting a positive integer or zero", level));
7582
return;
7683
}
7784

7885
if (v > std::numeric_limits<int>::max ()) {
79-
log_warn (LOG_DEFAULT, "Debugger log level value is higher than the maximum of %u, resetting to the maximum value.", std::numeric_limits<int>::max ());
86+
log_warn (LOG_DEFAULT, std::format ("Debugger log level value is higher than the maximum of {}, resetting to the maximum value.", std::numeric_limits<int>::max ()));
8087
v = std::numeric_limits<int>::max ();
8188
}
8289

src/native/runtime-base/strings.hh

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ namespace xamarin::android::internal
156156
}
157157

158158
if (!can_access (start_index)) {
159-
log_error (LOG_DEFAULT, "Cannot convert string to integer, index %u is out of range", start_index);
159+
log_error (LOG_DEFAULT, std::format ("Cannot convert string to integer, index {} is out of range", start_index));
160160
return false;
161161
}
162162

@@ -180,17 +180,17 @@ namespace xamarin::android::internal
180180
}
181181

182182
if (out_of_range || errno == ERANGE) {
183-
log_error (LOG_DEFAULT, "Value %s is out of range of this type (%lld..%llu)", s, static_cast<int64_t>(min), static_cast<uint64_t>(max));
183+
log_error (LOG_DEFAULT, std::format ("Value {} is out of range of this type ({}..{})", reinterpret_cast<char*>(s), static_cast<int64_t>(min), static_cast<uint64_t>(max)));
184184
return false;
185185
}
186186

187187
if (endp == s) {
188-
log_error (LOG_DEFAULT, "Value %s does not represent a base %d integer", s, base);
188+
log_error (LOG_DEFAULT, std::format ("Value {} does not represent a base {} integer", reinterpret_cast<char*>(s), base));
189189
return false;
190190
}
191191

192192
if (*endp != '\0') {
193-
log_error (LOG_DEFAULT, "Value %s has non-numeric characters at the end", s);
193+
log_error (LOG_DEFAULT, std::format ("Value {} has non-numeric characters at the end", reinterpret_cast<char*>(s)));
194194
return false;
195195
}
196196

src/native/runtime-base/timing-internal.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ FastTiming::dump () noexcept
7878
log_write (LOG_TIMING, LogLevel::Info, "[2/4] Accumulated performance results");
7979

8080
ns_to_time (total_assembly_load_time, sec, ms, ns);
81-
log_info_nocheck (LOG_TIMING, " [2/5] Assembly load: %u:%u::%u", sec, ms, ns);
81+
log_info_nocheck (LOG_TIMING, std::format (" [2/5] Assembly load: {}:{}::{}", sec, ms, ns));
8282

8383
ns_to_time (total_java_to_managed_time, sec, ms, ns);
84-
log_info_nocheck (LOG_TIMING, " [2/6] Java to Managed lookup: %u:%u::%u", sec, ms, ns);
84+
log_info_nocheck (LOG_TIMING, std::format (" [2/6] Java to Managed lookup: {}:{}::{}", sec, ms, ns));
8585

8686
ns_to_time (total_managed_to_java_time, sec, ms, ns);
87-
log_info_nocheck (LOG_TIMING, " [2/7] Managed to Java lookup: %u:%u::%u", sec, ms, ns);
87+
log_info_nocheck (LOG_TIMING, std::format (" [2/7] Managed to Java lookup: {}:{}::{}", sec, ms, ns));
8888
}

src/native/runtime-base/timing-internal.hh

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ namespace xamarin::android::internal
144144
// likely we'll run out of memory way, way, way before that happens
145145
size_t old_size = events.capacity ();
146146
events.reserve (old_size << 1);
147-
log_warn (LOG_TIMING, "Reallocated timing event buffer from %zu to %zu", old_size, events.size ());
147+
log_warn (LOG_TIMING, std::format ("Reallocated timing event buffer from {} to {}", old_size, events.size ()));
148148
}
149149
}
150150

@@ -246,7 +246,7 @@ namespace xamarin::android::internal
246246
force_inline bool is_valid_event_index (size_t index, const char *method_name) noexcept
247247
{
248248
if (index >= events.capacity ()) [[unlikely]] {
249-
log_warn (LOG_TIMING, "Invalid event index passed to method '%s'", method_name);
249+
log_warn (LOG_TIMING, std::format ("Invalid event index passed to method '{}'", method_name));
250250
return false;
251251
}
252252

0 commit comments

Comments
 (0)