Skip to content

Commit ad972cb

Browse files
committed
UPD | fix ferror in log & add exception with fmt
1 parent 6db6803 commit ad972cb

5 files changed

Lines changed: 46 additions & 2 deletions

File tree

include/ManapiErrors.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ namespace manapi {
206206
*/
207207
explicit exception (manapi::err_num errnum, std::string_view message);
208208

209+
/**
210+
* initialize exception
211+
* @param errnum error code
212+
* @param fmt error message. must contains a zero end
213+
* @param ... arguments
214+
*/
215+
explicit exception (manapi::err_num errnum, std::string_view fmt, ...);
216+
209217
/**
210218
* initialize exception
211219
* @param errnum error code

include/std/ManapiAsyncLogger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ namespace manapi {
109109

110110
void fdebug (const char *fmt, ...) MANAPIHTTP_NOEXCEPT;
111111

112-
void ferror (const char *fmt, int error_code, ...) MANAPIHTTP_NOEXCEPT;
112+
void ferror (int error_code, const char *fmt, ...) MANAPIHTTP_NOEXCEPT;
113113

114114
void fwarning (const char *fmt, ...) MANAPIHTTP_NOEXCEPT;
115115
private:

src/ManapiErrors.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,32 @@ manapi::exception::exception(manapi::err_num errnum, std::string_view message) {
206206
this->m_data.msg_view(message);
207207
}
208208

209+
manapi::exception::exception(manapi::err_num errnum, std::string_view fmt, ...) {
210+
std::string message;
211+
212+
va_list args;
213+
va_start(args, fmt);
214+
215+
try {
216+
va_list args_copy;
217+
va_copy(args_copy, args);
218+
std::size_t size = vsnprintf(nullptr, 0, fmt.data(), args_copy);
219+
va_end(args_copy);
220+
221+
message.resize(size);
222+
vsnprintf(message.data(), message.size() + 1, fmt.data(), args);
223+
}
224+
catch (std::exception const &e) {
225+
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "exception failed due to %s",
226+
e.what());
227+
}
228+
229+
va_end(args);
230+
231+
this->m_data.errnum(errnum);
232+
this->m_data.msg(std::move(message));
233+
}
234+
209235
manapi::exception::exception(manapi::err_num errnum, const char *message) {
210236
this->m_data.errnum(errnum);
211237
this->m_data.msg_view(message);

src/std/ManapiAsyncLogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void manapi::logger::fdebug(const char *fmt, ...) MANAPIHTTP_NOEXCEPT {
134134

135135
}
136136

137-
void manapi::logger::ferror(const char *fmt, int error_code, ...) MANAPIHTTP_NOEXCEPT {
137+
void manapi::logger::ferror(int error_code, const char *fmt, ...) MANAPIHTTP_NOEXCEPT {
138138
va_list args;
139139
va_start(args, fmt);
140140
fcallback (logger_type::LOGGER_ERROR, this->m_service, error_code, fmt, args);

tests/test_std.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ManapiString.hpp"
22
#include "std/ManapiChain.hpp"
33
#include "ManapiMath.hpp"
4+
#include "ManapiErrors.hpp"
45

56
#include <deque>
67

@@ -135,4 +136,13 @@ UTEST(std_string, chain_4) {
135136
}
136137
}
137138

139+
UTEST(std_exception, exception_fmt) {
140+
try {
141+
throw manapi::exception (manapi::ERR_INTERNAL, "YOU ARE %s", "LUCKY");
142+
}
143+
catch (manapi::exception const &e) {
144+
ASSERT_TRUE(std::string_view{e.what()} == "YOU ARE LUCKY");
145+
}
146+
}
147+
138148
UTEST_MAIN();

0 commit comments

Comments
 (0)