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
f7939869
Commit
f7939869
authored
Apr 30, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ThrowCError to throw an exception on C library errors.
parent
2c6372d7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
13 deletions
+55
-13
format-test.cc
format-test.cc
+1
-0
format.cc
format.cc
+14
-8
format.h
format.h
+40
-5
No files found.
format-test.cc
View file @
f7939869
...
...
@@ -260,6 +260,7 @@ TEST(UtilTest, StrError) {
message
=
StrError
(
-
1
,
buffer
,
BUFFER_SIZE
);
EXPECT_EQ
(
0
,
errno
);
EXPECT_GE
(
BUFFER_SIZE
-
1
,
std
::
strlen
(
message
));
EXPECT_STREQ
(
strerror
(
-
1
),
message
);
message
=
StrError
(
-
1
,
buffer
,
std
::
strlen
(
message
));
EXPECT_EQ
(
ERANGE
,
errno
);
}
...
...
format.cc
View file @
f7939869
...
...
@@ -224,19 +224,13 @@ char *fmt::internal::StrError(
#endif
}
void
fmt
::
internal
::
Format
System
ErrorMessage
(
void
fmt
::
internal
::
Format
C
ErrorMessage
(
fmt
::
Writer
&
out
,
int
error_code
,
fmt
::
StringRef
message
)
{
#ifndef _WIN32
Array
<
char
,
INLINE_BUFFER_SIZE
>
buffer
;
buffer
.
resize
(
INLINE_BUFFER_SIZE
);
char
*
system_message
=
0
;
for
(;;)
{
errno
=
0
;
# ifdef _GNU_SOURCE
system_message
=
strerror_r
(
error_code
,
&
buffer
[
0
],
buffer
.
size
());
# else
strerror_r
(
error_code
,
system_message
=
&
buffer
[
0
],
buffer
.
size
());
# endif
system_message
=
StrError
(
error_code
,
&
buffer
[
0
],
buffer
.
size
());
if
(
errno
==
0
)
break
;
if
(
errno
!=
ERANGE
)
{
...
...
@@ -247,6 +241,12 @@ void fmt::internal::FormatSystemErrorMessage(
buffer
.
resize
(
buffer
.
size
()
*
2
);
}
out
<<
message
<<
": "
<<
system_message
;
}
void
fmt
::
internal
::
FormatSystemErrorMessage
(
fmt
::
Writer
&
out
,
int
error_code
,
fmt
::
StringRef
message
)
{
#ifndef _WIN32
FormatCErrorMessage
(
out
,
error_code
,
message
);
#else
class
String
{
private:
...
...
@@ -806,6 +806,12 @@ void fmt::SystemErrorSink::operator()(const fmt::Writer &w) const {
throw
SystemError
(
message
.
c_str
(),
error_code_
);
}
void
fmt
::
CErrorSink
::
operator
()(
const
Writer
&
w
)
const
{
Writer
message
;
internal
::
FormatCErrorMessage
(
message
,
error_code_
,
w
.
c_str
());
throw
SystemError
(
message
.
c_str
(),
error_code_
);
}
void
fmt
::
ANSITerminalSink
::
operator
()(
const
fmt
::
BasicWriter
<
char
>
&
w
)
const
{
char
escape
[]
=
"
\x1b
[30m"
;
...
...
format.h
View file @
f7939869
...
...
@@ -499,6 +499,9 @@ class UTF16ToUTF8 {
// Buffer should be at least of size 1.
char
*
StrError
(
int
error_code
,
char
*
buffer
,
std
::
size_t
buffer_size
);
// Formats a standard C library error message writing the output to out.
void
FormatCErrorMessage
(
Writer
&
out
,
int
error_code
,
StringRef
message
);
// Formats a system error message writing the output to out.
void
FormatSystemErrorMessage
(
Writer
&
out
,
int
error_code
,
StringRef
message
);
...
...
@@ -1520,7 +1523,7 @@ class Formatter : private Sink, public BasicFormatter<Char> {
See also `Format String Syntax`_.
\endrst
*/
*/
inline
Formatter
<>
Format
(
StringRef
format
)
{
Formatter
<>
f
(
format
);
return
f
;
...
...
@@ -1532,7 +1535,7 @@ inline Formatter<NullSink, wchar_t> Format(WStringRef format) {
}
/**
A sink that gets the
system error message corresponding to the
error code
A sink that gets the
error message corresponding to a system
error code
and throws SystemError.
*/
class
SystemErrorSink
{
...
...
@@ -1545,13 +1548,45 @@ class SystemErrorSink {
void
operator
()(
const
Writer
&
w
)
const
;
};
/** Throws SystemError with a code and a formatted message. */
/**
Formats a message and throws SystemError with the description of the form
"<message>: <system-message>", where <message> is the formatted message and
<system-message> is the system message corresponding to the error code.
error_code is a system error code as given by errno for POSIX and
GetLastError for Windows.
*/
inline
Formatter
<
SystemErrorSink
>
ThrowSystemError
(
int
error_code
,
StringRef
format
=
0
)
{
int
error_code
,
StringRef
format
)
{
Formatter
<
SystemErrorSink
>
f
(
format
,
SystemErrorSink
(
error_code
));
return
f
;
}
/**
A sink that gets the error message corresponding to a standard C library
error code as given by errno and throws SystemError.
*/
class
CErrorSink
{
private:
int
error_code_
;
public:
explicit
CErrorSink
(
int
error_code
)
:
error_code_
(
error_code
)
{}
void
operator
()(
const
Writer
&
w
)
const
;
};
/**
Formats a message and throws SystemError with the description of the form
"<message>: <system-message>", where <message> is the formatted message and
<system-message> is the system message corresponding to the error code.
error_code is an error code as given by errno after calling a C library
function.
*/
inline
Formatter
<
CErrorSink
>
ThrowCError
(
int
error_code
,
StringRef
format
)
{
Formatter
<
CErrorSink
>
f
(
format
,
CErrorSink
(
error_code
));
return
f
;
}
/** A sink that writes output to a file. */
class
FileSink
{
private:
...
...
@@ -1563,7 +1598,7 @@ class FileSink {
/** Writes the output to a file. */
void
operator
()(
const
BasicWriter
<
char
>
&
w
)
const
{
if
(
std
::
fwrite
(
w
.
data
(),
w
.
size
(),
1
,
file_
)
==
0
)
Throw
System
Error
(
errno
,
"cannot write to file"
);
Throw
C
Error
(
errno
,
"cannot write to file"
);
}
};
...
...
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