Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
c5c6baad
Commit
c5c6baad
authored
Jul 15, 2016
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added errno description to sdlog exception strings
parent
e5032c8d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
7 deletions
+45
-7
common.h
include/spdlog/common.h
+7
-0
file_helper.h
include/spdlog/details/file_helper.h
+6
-5
os.h
include/spdlog/details/os.h
+29
-0
file_sinks.h
include/spdlog/sinks/file_sinks.h
+3
-2
No files found.
include/spdlog/common.h
View file @
c5c6baad
...
...
@@ -100,10 +100,17 @@ enum class async_overflow_policy
//
// Log exception
//
namespace
details
{
namespace
os
{
std
::
string
errno_str
(
int
err_num
);
}}
class
spdlog_ex
:
public
std
::
exception
{
public
:
spdlog_ex
(
const
std
::
string
&
msg
)
:
_msg
(
msg
)
{}
spdlog_ex
(
const
std
::
string
&
msg
,
int
last_errno
)
{
_msg
=
msg
+
": "
+
details
::
os
::
errno_str
(
last_errno
);
}
const
char
*
what
()
const
SPDLOG_NOEXCEPT
override
{
return
_msg
.
c_str
();
...
...
include/spdlog/details/file_helper.h
View file @
c5c6baad
...
...
@@ -17,6 +17,7 @@
#include <cstdio>
#include <string>
#include <thread>
#include <cerrno>
namespace
spdlog
{
...
...
@@ -58,7 +59,7 @@ public:
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
open_interval
));
}
throw
spdlog_ex
(
"Failed opening file "
+
os
::
filename_to_str
(
_filename
)
+
" for writing"
);
throw
spdlog_ex
(
"Failed opening file "
+
os
::
filename_to_str
(
_filename
)
+
" for writing"
,
errno
);
}
void
reopen
(
bool
truncate
)
...
...
@@ -89,7 +90,7 @@ public:
size_t
msg_size
=
msg
.
formatted
.
size
();
auto
data
=
msg
.
formatted
.
data
();
if
(
std
::
fwrite
(
data
,
1
,
msg_size
,
_fd
)
!=
msg_size
)
throw
spdlog_ex
(
"Failed writing to file "
+
os
::
filename_to_str
(
_filename
));
throw
spdlog_ex
(
"Failed writing to file "
+
os
::
filename_to_str
(
_filename
)
,
errno
);
if
(
_force_flush
)
std
::
fflush
(
_fd
);
...
...
@@ -102,15 +103,15 @@ public:
auto
pos
=
ftell
(
_fd
);
if
(
fseek
(
_fd
,
0
,
SEEK_END
)
!=
0
)
throw
spdlog_ex
(
"fseek failed on file "
+
os
::
filename_to_str
(
_filename
));
throw
spdlog_ex
(
"fseek failed on file "
+
os
::
filename_to_str
(
_filename
)
,
errno
);
auto
file_size
=
ftell
(
_fd
);
if
(
fseek
(
_fd
,
pos
,
SEEK_SET
)
!=
0
)
throw
spdlog_ex
(
"fseek failed on file "
+
os
::
filename_to_str
(
_filename
));
throw
spdlog_ex
(
"fseek failed on file "
+
os
::
filename_to_str
(
_filename
)
,
errno
);
if
(
file_size
==
-
1
)
throw
spdlog_ex
(
"ftell failed on file "
+
os
::
filename_to_str
(
_filename
));
throw
spdlog_ex
(
"ftell failed on file "
+
os
::
filename_to_str
(
_filename
)
,
errno
);
return
file_size
;
}
...
...
include/spdlog/details/os.h
View file @
c5c6baad
...
...
@@ -10,6 +10,7 @@
#include <ctime>
#include <functional>
#include <string>
#include <string.h>
#ifdef _WIN32
...
...
@@ -27,12 +28,16 @@
#endif
#elif __linux__
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#include <sys/stat.h>
#include <unistd.h>
#include <chrono>
#else
#include <thread>
#endif
namespace
spdlog
...
...
@@ -246,6 +251,30 @@ inline std::string filename_to_str(const filename_t& filename)
}
#endif
// Return errno string (thread safe)
inline
std
::
string
errno_str
(
int
err_num
)
{
char
buf
[
256
];
constexpr
auto
buf_size
=
sizeof
(
buf
);
#ifdef _WIN32
if
(
strerror_s
(
buf
,
buf_size
,
err_num
)
==
0
)
return
std
::
string
(
buf
);
else
return
"Unkown error"
;
#elif (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE // posix version
if
(
strerror_r
(
err_num
,
buf
,
buf_size
)
==
0
)
return
std
::
string
(
buf
);
else
return
"Unkown error"
;
#else // gnu version (might not use the given buf, so its retval pointer must be used)
return
std
::
string
(
strerror_r
(
err_num
,
buf
,
buf_size
));
#endif
}
}
//os
}
//details
}
//spdlog
...
...
include/spdlog/sinks/file_sinks.h
View file @
c5c6baad
...
...
@@ -16,6 +16,7 @@
#include <ctime>
#include <mutex>
#include <string>
#include <cerrno>
namespace
spdlog
{
...
...
@@ -119,12 +120,12 @@ private:
{
if
(
details
::
os
::
remove
(
target
)
!=
0
)
{
throw
spdlog_ex
(
"rotating_file_sink: failed removing "
+
filename_to_str
(
target
));
throw
spdlog_ex
(
"rotating_file_sink: failed removing "
+
filename_to_str
(
target
)
,
errno
);
}
}
if
(
details
::
file_helper
::
file_exists
(
src
)
&&
details
::
os
::
rename
(
src
,
target
))
{
throw
spdlog_ex
(
"rotating_file_sink: failed renaming "
+
filename_to_str
(
src
)
+
" to "
+
filename_to_str
(
target
));
throw
spdlog_ex
(
"rotating_file_sink: failed renaming "
+
filename_to_str
(
src
)
+
" to "
+
filename_to_str
(
target
)
,
errno
);
}
}
_file_helper
.
reopen
(
true
);
...
...
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