Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
ngraph
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
ngraph
Commits
47ad79fd
Commit
47ad79fd
authored
Jun 29, 2018
by
Yixing Lao
Committed by
Robert Kimball
Jun 29, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Customizable handler for logger function (#1177)
* add lambda handler support for logger * reuse logger function
parent
ae45c984
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
81 deletions
+102
-81
log.cpp
src/ngraph/log.cpp
+55
-44
log.hpp
src/ngraph/log.hpp
+47
-37
No files found.
src/ngraph/log.cpp
View file @
47ad79fd
...
...
@@ -17,6 +17,7 @@
#include <chrono>
#include <condition_variable>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <mutex>
...
...
@@ -25,107 +26,117 @@
#include "ngraph/log.hpp"
using
namespace
std
;
using
namespace
ngraph
;
namespace
ngraph
{
class
thread_s
tarter
;
class
ThreadS
tarter
;
}
string
ngraph
::
logger
::
log_path
;
deque
<
string
>
ngraph
::
logger
::
queue
;
string
Logger
::
m_
log_path
;
deque
<
string
>
Logger
::
m_
queue
;
static
mutex
queue_mutex
;
static
condition_variable
queue_condition
;
static
unique_ptr
<
thread
>
queue_thread
;
static
bool
active
=
false
;
std
::
ostream
&
ngraph
::
get_nil_stream
()
{
static
std
::
stringstream
nil
;
return
nil
;
}
class
ngraph
::
thread_starter
class
ngraph
::
ThreadStarter
{
public
:
thread_starter
()
{
ngraph
::
l
ogger
::
start
();
}
virtual
~
thread_starter
()
{
ngraph
::
l
ogger
::
stop
();
}
ThreadStarter
()
{
L
ogger
::
start
();
}
virtual
~
ThreadStarter
()
{
L
ogger
::
stop
();
}
};
static
ngraph
::
thread_starter
_starter
;
static
ThreadStarter
s
_starter
;
void
ngraph
::
logger
::
set_log_path
(
const
string
&
path
)
ostream
&
ngraph
::
get_nil_stream
(
)
{
log_path
=
path
;
static
stringstream
nil
;
return
nil
;
}
void
ngraph
::
logger
::
start
()
void
Logger
::
set_log_path
(
const
string
&
path
)
{
m_log_path
=
path
;
}
void
Logger
::
start
()
{
active
=
true
;
queue_thread
=
unique_ptr
<
thread
>
(
new
thread
(
&
thread_entry
,
nullptr
));
}
void
ngraph
::
l
ogger
::
stop
()
void
L
ogger
::
stop
()
{
{
unique_lock
<
std
::
mutex
>
lk
(
queue_mutex
);
unique_lock
<
mutex
>
lk
(
queue_mutex
);
active
=
false
;
queue_condition
.
notify_one
();
}
queue_thread
->
join
();
}
void
ngraph
::
l
ogger
::
process_event
(
const
string
&
s
)
void
L
ogger
::
process_event
(
const
string
&
s
)
{
cout
<<
s
<<
"
\n
"
;
}
void
ngraph
::
l
ogger
::
thread_entry
(
void
*
param
)
void
L
ogger
::
thread_entry
(
void
*
param
)
{
unique_lock
<
std
::
mutex
>
lk
(
queue_mutex
);
unique_lock
<
mutex
>
lk
(
queue_mutex
);
while
(
active
)
{
queue_condition
.
wait
(
lk
);
while
(
!
queue
.
empty
())
while
(
!
m_
queue
.
empty
())
{
process_event
(
queue
.
front
());
queue
.
pop_front
();
process_event
(
m_
queue
.
front
());
m_
queue
.
pop_front
();
}
}
}
void
ngraph
::
l
ogger
::
log_item
(
const
string
&
s
)
void
L
ogger
::
log_item
(
const
string
&
s
)
{
unique_lock
<
std
::
mutex
>
lk
(
queue_mutex
);
queue
.
push_back
(
s
);
unique_lock
<
mutex
>
lk
(
queue_mutex
);
m_
queue
.
push_back
(
s
);
queue_condition
.
notify_one
();
}
ngraph
::
log_helper
::
log_helper
(
LOG_TYPE
type
,
const
char
*
file
,
int
line
,
const
char
*
func
)
void
ngraph
::
default_logger_handler_func
(
const
string
&
s
)
{
cout
<<
s
<<
endl
;
}
LogHelper
::
LogHelper
(
LOG_TYPE
type
,
const
char
*
file
,
int
line
,
function
<
void
(
const
string
&
)
>
handler_func
)
:
m_handler_func
(
handler_func
)
{
switch
(
type
)
{
case
LOG_TYPE
:
:
_LOG_TYPE_ERROR
:
_stream
<<
"[ERR
] "
;
break
;
case
LOG_TYPE
:
:
_LOG_TYPE_WARNING
:
_stream
<<
"[WARN] "
;
break
;
case
LOG_TYPE
:
:
_LOG_TYPE_INFO
:
_stream
<<
"[INFO] "
;
break
;
case
LOG_TYPE
:
:
_LOG_TYPE_DEBUG
:
_stream
<<
"[DEBUG] "
;
break
;
case
LOG_TYPE
:
:
_LOG_TYPE_ERROR
:
m_stream
<<
"[ERR
] "
;
break
;
case
LOG_TYPE
:
:
_LOG_TYPE_WARNING
:
m
_stream
<<
"[WARN] "
;
break
;
case
LOG_TYPE
:
:
_LOG_TYPE_INFO
:
m
_stream
<<
"[INFO] "
;
break
;
case
LOG_TYPE
:
:
_LOG_TYPE_DEBUG
:
m
_stream
<<
"[DEBUG] "
;
break
;
}
std
::
time_t
tt
=
chrono
::
system_clock
::
to_time_t
(
chrono
::
system_clock
::
now
());
auto
tm
=
std
::
gmtime
(
&
tt
);
time_t
tt
=
chrono
::
system_clock
::
to_time_t
(
chrono
::
system_clock
::
now
());
auto
tm
=
gmtime
(
&
tt
);
char
buffer
[
256
];
// strftime(buffer,sizeof(buffer), "%d/%b/%Y:%H:%M:%S %z", tm);
// strftime(buffer,sizeof(buffer), "%Y-%m-%d %H:%M:%S UTC", tm);
strftime
(
buffer
,
sizeof
(
buffer
),
"%Y-%m-%dT%H:%M:%Sz"
,
tm
);
_stream
<<
buffer
<<
" "
;
m
_stream
<<
buffer
<<
" "
;
_stream
<<
file
;
_stream
<<
" "
<<
line
;
// _stream << " " << func;
_stream
<<
"
\t
"
;
m_stream
<<
file
;
m_stream
<<
" "
<<
line
;
m_stream
<<
"
\t
"
;
}
ngraph
::
log_helper
::~
log_h
elper
()
LogHelper
::~
LogH
elper
()
{
cout
<<
_stream
.
str
()
<<
endl
;
// logger::log_item(_stream.str());
if
(
m_handler_func
)
{
m_handler_func
(
m_stream
.
str
());
}
// Logger::log_item(m_stream.str());
}
src/ngraph/log.hpp
View file @
47ad79fd
...
...
@@ -17,43 +17,45 @@
#pragma once
#include <deque>
#include <functional>
#include <sstream>
#include <stdexcept>
namespace
ngraph
{
class
consts
tring
class
ConstS
tring
{
public
:
template
<
size_t
SIZE
>
constexpr
consts
tring
(
const
char
(
&
p
)[
SIZE
])
:
_string
(
p
)
,
_size
(
SIZE
)
constexpr
ConstS
tring
(
const
char
(
&
p
)[
SIZE
])
:
m
_string
(
p
)
,
m
_size
(
SIZE
)
{
}
constexpr
char
operator
[](
size_t
i
)
const
{
return
i
<
_size
?
_string
[
i
]
:
throw
std
::
out_of_range
(
""
);
return
i
<
m_size
?
m
_string
[
i
]
:
throw
std
::
out_of_range
(
""
);
}
constexpr
const
char
*
get_ptr
(
size_t
offset
)
const
{
return
&
_string
[
offset
];
}
constexpr
size_t
size
()
const
{
return
_size
;
}
constexpr
const
char
*
get_ptr
(
size_t
offset
)
const
{
return
&
m
_string
[
offset
];
}
constexpr
size_t
size
()
const
{
return
m
_size
;
}
private
:
const
char
*
_string
;
size_t
_size
;
const
char
*
m
_string
;
size_t
m
_size
;
};
constexpr
const
char
*
find_last
(
consts
tring
s
,
size_t
offset
,
char
ch
)
constexpr
const
char
*
find_last
(
ConstS
tring
s
,
size_t
offset
,
char
ch
)
{
return
offset
==
0
?
s
.
get_ptr
(
0
)
:
(
s
[
offset
]
==
ch
?
s
.
get_ptr
(
offset
+
1
)
:
find_last
(
s
,
offset
-
1
,
ch
));
}
constexpr
const
char
*
find_last
(
consts
tring
s
,
char
ch
)
constexpr
const
char
*
find_last
(
ConstS
tring
s
,
char
ch
)
{
return
find_last
(
s
,
s
.
size
()
-
1
,
ch
);
}
constexpr
const
char
*
get_file_name
(
conststring
s
)
{
return
find_last
(
s
,
'/'
);
}
constexpr
const
char
*
get_file_name
(
ConstString
s
)
{
return
find_last
(
s
,
'/'
);
}
enum
class
LOG_TYPE
{
_LOG_TYPE_ERROR
,
...
...
@@ -62,20 +64,24 @@ namespace ngraph
_LOG_TYPE_DEBUG
,
};
class
log_h
elper
class
LogH
elper
{
public
:
log_helper
(
LOG_TYPE
,
const
char
*
file
,
int
line
,
const
char
*
func
);
~
log_helper
();
LogHelper
(
LOG_TYPE
,
const
char
*
file
,
int
line
,
std
::
function
<
void
(
const
std
::
string
&
)
>
m_handler_func
);
~
LogHelper
();
std
::
ostream
&
stream
()
{
return
_stream
;
}
std
::
ostream
&
stream
()
{
return
m
_stream
;
}
private
:
std
::
stringstream
_stream
;
std
::
function
<
void
(
const
std
::
string
&
)
>
m_handler_func
;
std
::
stringstream
m_stream
;
};
class
l
ogger
class
L
ogger
{
friend
class
log_h
elper
;
friend
class
LogH
elper
;
public
:
static
void
set_log_path
(
const
std
::
string
&
path
);
...
...
@@ -86,37 +92,41 @@ namespace ngraph
static
void
log_item
(
const
std
::
string
&
s
);
static
void
process_event
(
const
std
::
string
&
s
);
static
void
thread_entry
(
void
*
param
);
static
std
::
string
log_path
;
static
std
::
deque
<
std
::
string
>
queue
;
static
std
::
string
m_
log_path
;
static
std
::
deque
<
std
::
string
>
m_
queue
;
};
extern
std
::
ostream
&
get_nil_stream
();
void
default_logger_handler_func
(
const
std
::
string
&
s
);
#define NGRAPH_ERR \
ngraph::
log_helper(ngraph::LOG_TYPE::_LOG_TYPE_ERROR,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
__PRETTY_FUNCTION__)
\
ngraph::
LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_ERROR,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
default_logger_handler_func)
\
.stream()
#define NGRAPH_WARN \
ngraph::
log_helper(ngraph::LOG_TYPE::_LOG_TYPE_WARNING,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
__PRETTY_FUNCTION__)
\
ngraph::
LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_WARNING,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
default_logger_handler_func)
\
.stream()
#define NGRAPH_INFO \
ngraph::
log_helper(ngraph::LOG_TYPE::_LOG_TYPE_INFO,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
__PRETTY_FUNCTION__)
\
ngraph::
LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_INFO,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
default_logger_handler_func)
\
.stream()
#ifdef NGRAPH_DEBUG_ENABLE
#define NGRAPH_DEBUG \
ngraph::
log_helper(ngraph::LOG_TYPE::_LOG_TYPE_DEBUG,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
__PRETTY_FUNCTION__)
\
ngraph::
LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_DEBUG,
\
ngraph::get_file_name(__FILE__),
\
__LINE__,
\
default_logger_handler_func)
\
.stream()
#else
#define NGRAPH_DEBUG ngraph::get_nil_stream()
...
...
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