3636#define SECT_SIZE_ERR 666
3737#define DISK_SIZE_ERR 999
3838
39- bool verb = false ;
39+ #include < format>
40+ static bool verbose = false ;
41+
42+ template <class ... Args>
43+ inline void infof (const char * from, std::format_string<Args...> fmt, Args&&... args) {
44+ if (!verbose) return ;
45+ auto msg = std::format (fmt, std::forward<Args>(args)...);
46+ std::fprintf (stderr, " [ %13s ] %s\n " , from, msg.c_str ());
47+ }
48+ template <class ... Args>
49+ inline void warnf (const char * from, std::format_string<Args...> fmt, Args&&... args) {
50+ auto msg = std::format (fmt, std::forward<Args>(args)...);
51+ std::fprintf (stderr, " [ %13s ] Warning: %s\n " , from, msg.c_str ());
52+ }
53+ template <class ... Args>
54+ [[noreturn]] inline void errorf (const char * from, std::format_string<Args...> fmt, Args&&... args) {
55+ auto msg = std::format (fmt, std::forward<Args>(args)...);
56+ std::fprintf (stderr, " [ %13s ] Error: %s\n " , from, msg.c_str ());
57+ std::terminate ();
58+ }
4059
41- #define INFO_ (FROM, TEXT, ...) if (verb) fprintf(stderr, " %13s ] " TEXT " \n " , " [ " FROM, ##__VA_ARGS__)
42- #define INFO (X,...) INFO_(" Vmbuild" , X, ##__VA_ARGS__)
43- #define WARN (X,...) fprintf(stderr, " [ vmbuild ] Warning: " X " \n " , ##__VA_ARGS__)
44- #define ERROR (X,...) fprintf(stderr, " [ vmbuild ] Error: " X " \n " , ##__VA_ARGS__); std::terminate()
60+ #define INFO (FMT, ...) infof(" vmbuild" , FMT, ##__VA_ARGS__)
61+ #define WARN (FMT, ...) warnf(" vmbuild" , FMT, ##__VA_ARGS__)
62+ #define ERROR (FMT, ...) errorf(" vmbuild" , FMT, ##__VA_ARGS__)
4563
4664
4765// Special variables inside the bootloader
@@ -95,26 +113,26 @@ int main(int argc, char** argv)
95113 // Set verbose from environment
96114 const char * env_verb = getenv (" VERBOSE" );
97115 if (env_verb && strlen (env_verb) > 0 )
98- verb = true ;
116+ verbose = true ;
99117
100118 const std::string bootloader_path = get_bootloader_path (argc, argv);
101119
102120 if (argc > 2 )
103121 const std::string bootloader_path {argv[2 ]};
104122
105- INFO (" Using bootloader %s " , bootloader_path. c_str () );
123+ INFO (" Using bootloader {} " , bootloader_path);
106124
107125 const std::string elf_binary_path {argv[1 ]};
108126 const std::string img_name {elf_binary_path.substr (elf_binary_path.find_last_of (" /" ) + 1 , std::string::npos) + " .img" };
109127
110- INFO (" Creating image '%s' " , img_name. c_str () );
128+ INFO (" Creating image '{}' " , img_name);
111129
112130 if (argc > 3 ) {
113131 if (std::string{argv[3 ]} == " -test" ) {
114132 test = true ;
115- verb = true ;
133+ verbose = true ;
116134 } else if (std::string{argv[3 ]} == " -v" ){
117- verb = true ;
135+ verbose = true ;
118136 }
119137 }
120138
@@ -123,39 +141,39 @@ int main(int argc, char** argv)
123141
124142 // Validate boot loader
125143 if (stat (bootloader_path.c_str (), &stat_boot) == -1 ) {
126- INFO (" Could not open %s , exiting\n " , bootloader_path. c_str () );
144+ INFO (" Could not open {} , exiting" , bootloader_path);
127145 return errno;
128146 }
129147
130148 if (stat_boot.st_size != SECT_SIZE) {
131- INFO (" Boot sector not exactly one sector in size (%ld bytes, expected %i )" ,
149+ INFO (" Boot sector not exactly one sector in size ({} bytes, expected {} )" ,
132150 stat_boot.st_size , SECT_SIZE);
133151 return SECT_SIZE_ERR;
134152 }
135153
136- INFO (" Size of bootloader: %ld \t " , stat_boot.st_size );
154+ INFO (" Size of bootloader: {} " , stat_boot.st_size );
137155
138156 // Validate service binary location
139157 if (stat (elf_binary_path.c_str (), &stat_binary) == -1 ) {
140- ERROR (" vmbuild: Could not open '%s' \n " , elf_binary_path. c_str () );
158+ ERROR (" vmbuild: Could not open '{}' " , elf_binary_path);
141159 return errno;
142160 }
143161
144162 intmax_t binary_sectors = stat_binary.st_size / SECT_SIZE;
145163 if (stat_binary.st_size & (SECT_SIZE-1 )) binary_sectors += 1 ;
146164
147- INFO (" Size of service: \t %ld bytes" , stat_binary.st_size );
165+ INFO (" Size of service: {} bytes" , stat_binary.st_size );
148166
149167 const decltype (binary_sectors) img_size_sect {1 + binary_sectors};
150168 const decltype (binary_sectors) img_size_bytes {img_size_sect * SECT_SIZE};
151169 assert ((img_size_bytes & (SECT_SIZE-1 )) == 0 );
152170
153- INFO (" Total disk size: \t %ld bytes, => %ld sectors" ,
171+ INFO (" Total disk size: {} bytes, => {} sectors" ,
154172 img_size_bytes, img_size_sect);
155173
156174 const auto disk_size = img_size_bytes;
157175
158- INFO (" Creating disk of size %ld sectors / %ld bytes" ,
176+ INFO (" Creating disk of size {} sectors / {} bytes" ,
159177 (disk_size / SECT_SIZE), disk_size);
160178
161179 std::vector<char > disk (disk_size);
@@ -165,14 +183,14 @@ int main(int argc, char** argv)
165183 std::ifstream file_boot {bootloader_path}; // < Load the boot loader into memory
166184
167185 auto read_bytes = file_boot.read (disk_head, stat_boot.st_size ).gcount ();
168- INFO (" Read %ld bytes from boot image" , read_bytes);
186+ INFO (" Read {} bytes from boot image" , read_bytes);
169187
170188 std::ifstream file_binary {elf_binary_path}; // < Load the service into memory
171189
172190 auto * binary_imgloc = disk_head + SECT_SIZE; // < Location of service code within the image
173191
174192 read_bytes = file_binary.read (binary_imgloc, stat_binary.st_size ).gcount ();
175- INFO (" Read %ld bytes from service image" , read_bytes);
193+ INFO (" Read {} bytes from service image" , read_bytes);
176194
177195 // only accept ELF binaries
178196 if (not (binary_imgloc[EI_MAG0] == ELFMAG0
@@ -197,11 +215,11 @@ int main(int argc, char** argv)
197215 binary.validate ();
198216 srv_entry = binary.entry ();
199217
200- INFO (" Found 32-bit ELF with entry at 0x%x " , srv_entry);
218+ INFO (" Found 32-bit ELF with entry at {:#010x} " , srv_entry);
201219
202220 auto loadable = binary.loadable_segments ();
203221 if (loadable.size () > 1 ) {
204- WARN (" found %zu loadable segments. Loading as one." ,loadable.size ());
222+ WARN (" found {} loadable segments. Loading as one." , loadable.size ());
205223 }
206224 srv_load_addr = loadable[0 ]->p_paddr ;
207225 binary_load_offs = loadable[0 ]->p_offset ;
@@ -218,7 +236,7 @@ int main(int argc, char** argv)
218236 binary.validate ();
219237 srv_entry = binary.entry ();
220238
221- INFO (" Found 64-bit ELF with entry at 0x%x " , srv_entry);
239+ INFO (" Found 64-bit ELF with entry at {:#010x} " , srv_entry);
222240
223241 auto loadable = binary.loadable_segments ();
224242 // Expects(loadable.size() == 1);
@@ -238,25 +256,25 @@ int main(int argc, char** argv)
238256
239257
240258 INFO (" Verifying multiboot header:" );
241- INFO (" Magic value: 0x%x " , multiboot_hdr->magic );
259+ INFO (" Magic value: {:#010x} " , multiboot_hdr->magic );
242260 if (multiboot_hdr->magic != MULTIBOOT_HEADER_MAGIC) {
243- ERROR (" Multiboot magic mismatch: 0x%08x vs %#x " ,
261+ ERROR (" Multiboot magic mismatch: {:#010x} vs {:#x} " ,
244262 multiboot_hdr->magic , MULTIBOOT_HEADER_MAGIC);
245263 }
246264
247-
248- INFO (" Flags: 0x%x " , multiboot_hdr->flags );
249- INFO (" Checksum: 0x%x " , multiboot_hdr-> checksum );
250- INFO ( " Checksum computed: 0x%x " , multiboot_hdr->checksum + multiboot_hdr->flags + multiboot_hdr->magic );
265+ INFO ( " Flags: {:#010x} " , multiboot_hdr-> flags );
266+ INFO (" Checksum: {:#010x} " , multiboot_hdr->checksum );
267+ INFO (" Checksum computed: {:#010x} " ,
268+ multiboot_hdr->checksum + multiboot_hdr->flags + multiboot_hdr->magic );
251269
252270 // Verify multiboot header checksum
253271 assert (multiboot_hdr->checksum + multiboot_hdr->flags + multiboot_hdr->magic == 0 );
254272
255- INFO (" Header addr: 0x%x " , multiboot_hdr->header_addr );
256- INFO (" Load start: 0x%x " , multiboot_hdr->load_addr );
257- INFO (" Load end: 0x%x " , multiboot_hdr->load_end_addr );
258- INFO (" BSS end: 0x%x " , multiboot_hdr->bss_end_addr );
259- INFO (" Entry: 0x%x " , multiboot_hdr->entry_addr );
273+ INFO (" Header addr: {:#010x} " , multiboot_hdr->header_addr );
274+ INFO (" Load start: {:#010x} " , multiboot_hdr->load_addr );
275+ INFO (" Load end: {:#010x} " , multiboot_hdr->load_end_addr );
276+ INFO (" BSS end: {:#010x} " , multiboot_hdr->bss_end_addr );
277+ INFO (" Entry: {:#010x} " , multiboot_hdr->entry_addr );
260278
261279 assert (multiboot_hdr->entry_addr == srv_entry);
262280
@@ -272,9 +290,9 @@ int main(int argc, char** argv)
272290 boot->entry = srv_entry;
273291 boot->load_addr = srv_load_addr;
274292
275- INFO (" srv_size: %i " , srv_size);
276- INFO (" srv_entry: 0x%x " , srv_entry);
277- INFO (" srv_load: 0x%x " , srv_load_addr);
293+ INFO (" srv_size: {} " , srv_size);
294+ INFO (" srv_entry: {:#010x} " , srv_entry);
295+ INFO (" srv_load: {:#010x} " , srv_load_addr);
278296
279297 if (test) {
280298 INFO (" \n TEST overwriting service with testdata" );
@@ -287,8 +305,8 @@ int main(int argc, char** argv)
287305 auto * image = fopen (img_name.c_str (), " w" );
288306 auto wrote = fwrite (disk_head, 1 , disk_size, image);
289307
290- INFO (" Wrote %ld bytes => %ld sectors to '%s '" ,
291- wrote, (wrote / SECT_SIZE), img_name. c_str () );
308+ INFO (" Wrote {} bytes => {} sectors to '{} '" ,
309+ wrote, (wrote / SECT_SIZE), img_name);
292310
293311 fclose (image);
294312}
0 commit comments