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
d8d8dfd3
Commit
d8d8dfd3
authored
Mar 21, 2016
by
PedroRod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
made the prepared statement re-usable and renamed .h to sqlite_sink
parent
571e85d0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
20 deletions
+21
-20
sqlite_sink.h
include/spdlog/sinks/sqlite_sink.h
+21
-20
No files found.
include/spdlog/sinks/
database_logger
_sink.h
→
include/spdlog/sinks/
sqlite
_sink.h
View file @
d8d8dfd3
...
...
@@ -8,28 +8,33 @@ namespace spdlog
{
namespace
sinks
{
class
database_logger
_sink
:
class
sqlite
_sink
:
public
sink
{
public
:
explicit
database_logger
_sink
(
const
std
::
string
&
databaseName
)
explicit
sqlite
_sink
(
const
std
::
string
&
databaseName
)
{
if
(
sqlite3_open
(
databaseName
.
c_str
(),
&
_database
))
throw
spdlog_ex
(
"Error opening database"
);
if
(
sqlite3_prepare_v2
(
_database
,
"INSERT INTO Logs (TimeStamp,Level,Message,LoggerName,ThreadId) VALUES (?,?,?,?,?)"
,
-
1
,
&
_query_stmt
,
nullptr
)
!=
SQLITE_OK
)
throw
spdlog_ex
(
sqlite3_errmsg
(
_database
));
}
~
database_logger
_sink
()
~
sqlite
_sink
()
{
sqlite
3_close
(
_database
);
sqlite
_sink
::
flush
(
);
}
void
flush
()
override
{
sqlite3_close
(
_database
);
sqlite3_finalize
(
_query_stmt
);
}
sqlite3_stmt
*
prepare_query
(
const
details
::
log_msg
&
msg
)
const
void
bind_to_statement
(
const
details
::
log_msg
&
msg
)
const
{
auto
time
=
std
::
chrono
::
system_clock
::
to_time_t
(
msg
.
time
);
...
...
@@ -37,35 +42,31 @@ namespace spdlog
ctime_s
(
time_str
,
sizeof
(
time_str
),
&
time
);
sqlite3_stmt
*
query_stmt
;
if
(
sqlite3_prepare_v2
(
_database
,
"INSERT INTO Logs (TimeStamp,Level,Message,LoggerName,ThreadId) VALUES (?,?,?,?,?)"
,
-
1
,
&
query_stmt
,
nullptr
)
!=
SQLITE_OK
)
if
(
sqlite3_bind_text
(
_query_stmt
,
1
,
time_str
,
-
1
,
SQLITE_STATIC
)
!=
SQLITE_OK
||
sqlite3_bind_text
(
_query_stmt
,
2
,
to_str
(
msg
.
level
),
-
1
,
SQLITE_STATIC
)
!=
SQLITE_OK
||
sqlite3_bind_text
(
_query_stmt
,
3
,
msg
.
raw
.
c_str
(),
-
1
,
nullptr
)
!=
SQLITE_OK
||
sqlite3_bind_text
(
_query_stmt
,
4
,
msg
.
logger_name
.
c_str
(),
-
1
,
SQLITE_STATIC
)
!=
SQLITE_OK
||
sqlite3_bind_int
(
_query_stmt
,
5
,
msg
.
thread_id
)
!=
SQLITE_OK
)
throw
spdlog_ex
(
sqlite3_errmsg
(
_database
));
if
(
sqlite3_bind_text
(
query_stmt
,
1
,
time_str
,
-
1
,
SQLITE_STATIC
)
!=
SQLITE_OK
||
sqlite3_bind_text
(
query_stmt
,
2
,
to_str
(
msg
.
level
),
-
1
,
SQLITE_STATIC
)
!=
SQLITE_OK
||
sqlite3_bind_text
(
query_stmt
,
3
,
msg
.
raw
.
c_str
(),
-
1
,
nullptr
)
!=
SQLITE_OK
||
sqlite3_bind_text
(
query_stmt
,
4
,
"'''''''''''"
,
-
1
,
SQLITE_STATIC
)
!=
SQLITE_OK
||
sqlite3_bind_int
(
query_stmt
,
5
,
msg
.
thread_id
)
!=
SQLITE_OK
)
throw
spdlog_ex
(
sqlite3_errmsg
(
_database
));
return
query_stmt
;
}
void
log
(
const
details
::
log_msg
&
msg
)
override
{
auto
query_stmt
=
prepare_query
(
msg
);
bind_to_statement
(
msg
);
if
(
sqlite3_step
(
query_stmt
)
!=
SQLITE_DONE
)
if
(
sqlite3_step
(
_
query_stmt
)
!=
SQLITE_DONE
)
{
throw
spdlog_ex
(
sqlite3_errmsg
(
_database
));
}
sqlite3_finalize
(
query_stmt
);
sqlite3_reset
(
_query_stmt
);
sqlite3_clear_bindings
(
_query_stmt
);
}
private
:
sqlite3
*
_database
;
sqlite3_stmt
*
_query_stmt
;
};
}
}
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