Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
6883d6e7
Commit
6883d6e7
authored
Mar 06, 2016
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #285 from mwinterb/winerror_winu
Changed format_windows_error to not need LocalFree
parents
5324d385
8f4b8edb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
19 deletions
+38
-19
cppformat/format.cc
cppformat/format.cc
+17
-19
test/util-test.cc
test/util-test.cc
+21
-0
No files found.
cppformat/format.cc
View file @
6883d6e7
...
...
@@ -588,27 +588,25 @@ FMT_FUNC void fmt::WindowsError::init(
FMT_FUNC
void
fmt
::
internal
::
format_windows_error
(
fmt
::
Writer
&
out
,
int
error_code
,
fmt
::
StringRef
message
)
FMT_NOEXCEPT
{
class
String
{
private:
LPWSTR
str_
;
public:
String
()
:
str_
()
{}
~
String
()
{
LocalFree
(
str_
);
}
LPWSTR
*
ptr
()
{
return
&
str_
;
}
LPCWSTR
c_str
()
const
{
return
str_
;
}
};
FMT_TRY
{
String
system_message
;
if
(
FormatMessageW
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
0
,
error_code
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
reinterpret_cast
<
LPWSTR
>
(
system_message
.
ptr
()),
0
,
0
))
{
UTF16ToUTF8
utf8_message
;
if
(
utf8_message
.
convert
(
system_message
.
c_str
())
==
ERROR_SUCCESS
)
{
out
<<
message
<<
": "
<<
utf8_message
;
return
;
MemoryBuffer
<
wchar_t
,
INLINE_BUFFER_SIZE
>
buffer
;
buffer
.
resize
(
INLINE_BUFFER_SIZE
);
for
(;;)
{
wchar_t
*
system_message
=
&
buffer
[
0
];
int
result
=
FormatMessageW
(
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
0
,
error_code
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
system_message
,
static_cast
<
uint32_t
>
(
buffer
.
size
()),
0
);
if
(
result
!=
0
)
{
UTF16ToUTF8
utf8_message
;
if
(
utf8_message
.
convert
(
system_message
)
==
ERROR_SUCCESS
)
{
out
<<
message
<<
": "
<<
utf8_message
;
return
;
}
break
;
}
if
(
GetLastError
()
!=
ERROR_INSUFFICIENT_BUFFER
)
break
;
// Can't get error message, report error code instead.
buffer
.
resize
(
buffer
.
size
()
*
2
);
}
}
FMT_CATCH
(...)
{}
fmt
::
format_error_code
(
out
,
error_code
,
message
);
// 'fmt::' is for bcc32.
...
...
test/util-test.cc
View file @
6883d6e7
...
...
@@ -873,6 +873,27 @@ TEST(UtilTest, FormatWindowsError) {
EXPECT_EQ
(
fmt
::
format
(
"error {}"
,
ERROR_FILE_EXISTS
),
actual_message
.
str
());
}
TEST
(
UtilTest
,
FormatLongWindowsError
)
{
LPWSTR
message
=
0
;
// this error code is not available on all Windows platforms and
// Windows SDKs, so do not fail the test if the error string cannot
// be retrieved.
const
int
provisioning_not_allowed
=
0x80284013L
/*TBS_E_PROVISIONING_NOT_ALLOWED*/
;
if
(
FormatMessageW
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
0
,
provisioning_not_allowed
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
reinterpret_cast
<
LPWSTR
>
(
&
message
),
0
,
0
)
==
0
)
{
return
;
}
fmt
::
internal
::
UTF16ToUTF8
utf8_message
(
message
);
LocalFree
(
message
);
fmt
::
MemoryWriter
actual_message
;
fmt
::
internal
::
format_windows_error
(
actual_message
,
provisioning_not_allowed
,
"test"
);
EXPECT_EQ
(
fmt
::
format
(
"test: {}"
,
utf8_message
.
str
()),
actual_message
.
str
());
}
TEST
(
UtilTest
,
WindowsError
)
{
check_throw_error
<
fmt
::
WindowsError
>
(
ERROR_FILE_EXISTS
,
fmt
::
internal
::
format_windows_error
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment