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
42e30468
Commit
42e30468
authored
May 11, 2019
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added rotating_file_sink to static build
parent
3834acad
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
153 additions
and
96 deletions
+153
-96
rotating_file_sink-inl.h
include/spdlog/sinks/rotating_file_sink-inl.h
+132
-0
rotating_file_sink.h
include/spdlog/sinks/rotating_file_sink.h
+16
-95
spdlog.cpp
src/spdlog.cpp
+5
-1
No files found.
include/spdlog/sinks/rotating_file_sink-inl.h
0 → 100644
View file @
42e30468
// Copyright(c) 2015-present Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include "spdlog/common.h"
#include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h"
#include <cerrno>
#include <chrono>
#include <ctime>
#include <mutex>
#include <string>
#include <tuple>
namespace
spdlog
{
namespace
sinks
{
template
<
typename
Mutex
>
SPDLOG_INLINE
rotating_file_sink
<
Mutex
>::
rotating_file_sink
(
filename_t
base_filename
,
std
::
size_t
max_size
,
std
::
size_t
max_files
,
bool
rotate_on_open
)
:
base_filename_
(
std
::
move
(
base_filename
))
,
max_size_
(
max_size
)
,
max_files_
(
max_files
)
{
file_helper_
.
open
(
calc_filename
(
base_filename_
,
0
));
current_size_
=
file_helper_
.
size
();
// expensive. called only once
if
(
rotate_on_open
&&
current_size_
>
0
)
{
rotate_
();
}
}
// calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
template
<
typename
Mutex
>
SPDLOG_INLINE
filename_t
rotating_file_sink
<
Mutex
>::
calc_filename
(
const
filename_t
&
filename
,
std
::
size_t
index
)
{
typename
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
memory_buffer
,
fmt
::
wmemory_buffer
>::
type
w
;
if
(
index
!=
0u
)
{
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extension
(
filename
);
fmt
::
format_to
(
w
,
SPDLOG_FILENAME_T
(
"{}.{}{}"
),
basename
,
index
,
ext
);
}
else
{
fmt
::
format_to
(
w
,
SPDLOG_FILENAME_T
(
"{}"
),
filename
);
}
return
fmt
::
to_string
(
w
);
}
template
<
typename
Mutex
>
SPDLOG_INLINE
const
filename_t
&
rotating_file_sink
<
Mutex
>::
filename
()
const
{
return
file_helper_
.
filename
();
}
template
<
typename
Mutex
>
SPDLOG_INLINE
void
rotating_file_sink
<
Mutex
>::
sink_it_
(
const
details
::
log_msg
&
msg
)
{
fmt
::
memory_buffer
formatted
;
sink
::
formatter_
->
format
(
msg
,
formatted
);
current_size_
+=
formatted
.
size
();
if
(
current_size_
>
max_size_
)
{
rotate_
();
current_size_
=
formatted
.
size
();
}
file_helper_
.
write
(
formatted
);
}
template
<
typename
Mutex
>
SPDLOG_INLINE
void
rotating_file_sink
<
Mutex
>::
flush_
()
{
file_helper_
.
flush
();
}
// Rotate files:
// log.txt -> log.1.txt
// log.1.txt -> log.2.txt
// log.2.txt -> log.3.txt
// log.3.txt -> delete
template
<
typename
Mutex
>
SPDLOG_INLINE
void
rotating_file_sink
<
Mutex
>::
rotate_
()
{
using
details
::
os
::
filename_to_str
;
file_helper_
.
close
();
for
(
auto
i
=
max_files_
;
i
>
0
;
--
i
)
{
filename_t
src
=
calc_filename
(
base_filename_
,
i
-
1
);
if
(
!
details
::
file_helper
::
file_exists
(
src
))
{
continue
;
}
filename_t
target
=
calc_filename
(
base_filename_
,
i
);
if
(
!
rename_file
(
src
,
target
))
{
// if failed try again after a small delay.
// this is a workaround to a windows issue, where very high rotation
// rates can cause the rename to fail with permission denied (because of antivirus?).
details
::
os
::
sleep_for_millis
(
100
);
if
(
!
rename_file
(
src
,
target
))
{
file_helper_
.
reopen
(
true
);
// truncate the log file anyway to prevent it to grow beyond its limit!
current_size_
=
0
;
throw
spdlog_ex
(
"rotating_file_sink: failed renaming "
+
filename_to_str
(
src
)
+
" to "
+
filename_to_str
(
target
),
errno
);
}
}
}
file_helper_
.
reopen
(
true
);
}
// delete the target if exists, and rename the src file to target
// return true on success, false otherwise.
template
<
typename
Mutex
>
SPDLOG_INLINE
bool
rotating_file_sink
<
Mutex
>::
rename_file
(
const
filename_t
&
src_filename
,
const
filename_t
&
target_filename
)
{
// try to delete the target file in case it already exists.
(
void
)
details
::
os
::
remove
(
target_filename
);
return
details
::
os
::
rename
(
src_filename
,
target_filename
)
==
0
;
}
}
// namespace sinks
}
// namespace spdlog
include/spdlog/sinks/rotating_file_sink.h
View file @
42e30468
...
@@ -2,22 +2,16 @@
...
@@ -2,22 +2,16 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#pragma once
#ifndef SPDLOG_H
#ifndef SPDLOG_H
#include "spdlog/spdlog.h"
#include "spdlog/spdlog.h"
#endif
#endif
#include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/details/file_helper.h"
#include <cerrno>
#include <chrono>
#include <chrono>
#include <ctime>
#include <mutex>
#include <mutex>
#include <string>
#include <string>
#include <tuple>
namespace
spdlog
{
namespace
spdlog
{
namespace
sinks
{
namespace
sinks
{
...
@@ -29,60 +23,15 @@ template<typename Mutex>
...
@@ -29,60 +23,15 @@ template<typename Mutex>
class
rotating_file_sink
final
:
public
base_sink
<
Mutex
>
class
rotating_file_sink
final
:
public
base_sink
<
Mutex
>
{
{
public
:
public
:
rotating_file_sink
(
filename_t
base_filename
,
std
::
size_t
max_size
,
std
::
size_t
max_files
,
bool
rotate_on_open
=
false
)
rotating_file_sink
(
filename_t
base_filename
,
std
::
size_t
max_size
,
std
::
size_t
max_files
,
bool
rotate_on_open
=
false
);
:
base_filename_
(
std
::
move
(
base_filename
))
static
filename_t
calc_filename
(
const
filename_t
&
filename
,
std
::
size_t
index
);
,
max_size_
(
max_size
)
const
filename_t
&
filename
()
const
;
,
max_files_
(
max_files
)
{
file_helper_
.
open
(
calc_filename
(
base_filename_
,
0
));
current_size_
=
file_helper_
.
size
();
// expensive. called only once
if
(
rotate_on_open
&&
current_size_
>
0
)
{
rotate_
();
}
}
// calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
static
filename_t
calc_filename
(
const
filename_t
&
filename
,
std
::
size_t
index
)
{
typename
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
memory_buffer
,
fmt
::
wmemory_buffer
>::
type
w
;
if
(
index
!=
0u
)
{
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extension
(
filename
);
fmt
::
format_to
(
w
,
SPDLOG_FILENAME_T
(
"{}.{}{}"
),
basename
,
index
,
ext
);
}
else
{
fmt
::
format_to
(
w
,
SPDLOG_FILENAME_T
(
"{}"
),
filename
);
}
return
fmt
::
to_string
(
w
);
}
const
filename_t
&
filename
()
const
{
return
file_helper_
.
filename
();
}
protected
:
protected
:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
;
{
void
flush_
()
override
;
fmt
::
memory_buffer
formatted
;
sink
::
formatter_
->
format
(
msg
,
formatted
);
current_size_
+=
formatted
.
size
();
if
(
current_size_
>
max_size_
)
{
rotate_
();
current_size_
=
formatted
.
size
();
}
file_helper_
.
write
(
formatted
);
}
void
flush_
()
override
{
file_helper_
.
flush
();
}
private
:
private
:
// Rotate files:
// Rotate files:
...
@@ -90,45 +39,11 @@ private:
...
@@ -90,45 +39,11 @@ private:
// log.1.txt -> log.2.txt
// log.1.txt -> log.2.txt
// log.2.txt -> log.3.txt
// log.2.txt -> log.3.txt
// log.3.txt -> delete
// log.3.txt -> delete
void
rotate_
()
void
rotate_
();
{
using
details
::
os
::
filename_to_str
;
file_helper_
.
close
();
for
(
auto
i
=
max_files_
;
i
>
0
;
--
i
)
{
filename_t
src
=
calc_filename
(
base_filename_
,
i
-
1
);
if
(
!
details
::
file_helper
::
file_exists
(
src
))
{
continue
;
}
filename_t
target
=
calc_filename
(
base_filename_
,
i
);
if
(
!
rename_file
(
src
,
target
))
{
// if failed try again after a small delay.
// this is a workaround to a windows issue, where very high rotation
// rates can cause the rename to fail with permission denied (because of antivirus?).
details
::
os
::
sleep_for_millis
(
100
);
if
(
!
rename_file
(
src
,
target
))
{
file_helper_
.
reopen
(
true
);
// truncate the log file anyway to prevent it to grow beyond its limit!
current_size_
=
0
;
throw
spdlog_ex
(
"rotating_file_sink: failed renaming "
+
filename_to_str
(
src
)
+
" to "
+
filename_to_str
(
target
),
errno
);
}
}
}
file_helper_
.
reopen
(
true
);
}
// delete the target if exists, and rename the src file to target
// delete the target if exists, and rename the src file to target
// return true on success, false otherwise.
// return true on success, false otherwise.
bool
rename_file
(
const
filename_t
&
src_filename
,
const
filename_t
&
target_filename
)
bool
rename_file
(
const
filename_t
&
src_filename
,
const
filename_t
&
target_filename
);
{
// try to delete the target file in case it already exists.
(
void
)
details
::
os
::
remove
(
target_filename
);
return
details
::
os
::
rename
(
src_filename
,
target_filename
)
==
0
;
}
filename_t
base_filename_
;
filename_t
base_filename_
;
std
::
size_t
max_size_
;
std
::
size_t
max_size_
;
...
@@ -160,3 +75,8 @@ inline std::shared_ptr<logger> rotating_logger_st(
...
@@ -160,3 +75,8 @@ inline std::shared_ptr<logger> rotating_logger_st(
return
Factory
::
template
create
<
sinks
::
rotating_file_sink_st
>
(
logger_name
,
filename
,
max_file_size
,
max_files
,
rotate_on_open
);
return
Factory
::
template
create
<
sinks
::
rotating_file_sink_st
>
(
logger_name
,
filename
,
max_file_size
,
max_files
,
rotate_on_open
);
}
}
}
// namespace spdlog
}
// namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "rotating_file_sink-inl.h"
#endif
\ No newline at end of file
src/spdlog.cpp
View file @
42e30468
...
@@ -31,6 +31,11 @@
...
@@ -31,6 +31,11 @@
template
class
spdlog
::
sinks
::
base_sink
<
std
::
mutex
>
;
template
class
spdlog
::
sinks
::
base_sink
<
std
::
mutex
>
;
template
class
spdlog
::
sinks
::
base_sink
<
spdlog
::
details
::
null_mutex
>
;
template
class
spdlog
::
sinks
::
base_sink
<
spdlog
::
details
::
null_mutex
>
;
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/rotating_file_sink-inl.h"
template
class
spdlog
::
sinks
::
rotating_file_sink
<
std
::
mutex
>
;
template
class
spdlog
::
sinks
::
rotating_file_sink
<
spdlog
::
details
::
null_mutex
>
;
#include "spdlog/details/registry.h"
#include "spdlog/details/registry.h"
#include "spdlog/details/registry-inl.h"
#include "spdlog/details/registry-inl.h"
...
@@ -62,7 +67,6 @@ template class spdlog::sinks::ansicolor_sink<spdlog::details::console_stderr, sp
...
@@ -62,7 +67,6 @@ template class spdlog::sinks::ansicolor_sink<spdlog::details::console_stderr, sp
template
void
spdlog
::
details
::
fmt_helper
::
append_string_view
(
spdlog
::
string_view_t
view
,
fmt
::
memory_buffer
&
dest
);
template
void
spdlog
::
details
::
fmt_helper
::
append_string_view
(
spdlog
::
string_view_t
view
,
fmt
::
memory_buffer
&
dest
);
template
spdlog
::
string_view_t
spdlog
::
details
::
fmt_helper
::
to_string_view
(
const
fmt
::
memory_buffer
&
buf
)
SPDLOG_NOEXCEPT
;
template
spdlog
::
string_view_t
spdlog
::
details
::
fmt_helper
::
to_string_view
(
const
fmt
::
memory_buffer
&
buf
)
SPDLOG_NOEXCEPT
;
// Slightly modified version of fmt lib's format.cc source file.
// Slightly modified version of fmt lib's format.cc source file.
// Copyright (c) 2012 - 2016, Victor Zverovich
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
// All rights reserved.
...
...
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