Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
be4f3692
Commit
be4f3692
authored
Dec 19, 2020
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[debug log]: supported labels
parent
b34bf169
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
10 deletions
+114
-10
DebugStream.h
include/DebugStream.h
+26
-2
DebugStream.cc
src/Log/DebugStream.cc
+45
-7
test_debugstream.cc
tests/test_debugstream.cc
+43
-1
No files found.
include/DebugStream.h
View file @
be4f3692
...
...
@@ -22,6 +22,7 @@
#include <iostream>
#include <string>
#include <sigc++/sigc++.h>
#include <vector>
#include "Debug.h"
#ifdef TEST_DEBUGSTREAM
...
...
@@ -220,6 +221,16 @@ class DebugStream : public std::ostream
show_logtype
=
s
;
}
inline
void
showLabels
(
bool
s
)
noexcept
{
show_labels
=
s
;
}
inline
void
hideLabelKey
(
bool
s
)
noexcept
{
hide_label_key
=
s
;
}
inline
std
::
ostream
&
log
(
Debug
::
type
l
)
noexcept
{
return
this
->
operator
[](
l
);
...
...
@@ -239,6 +250,15 @@ class DebugStream : public std::ostream
// example: dlog.V(1)[Debug::INFO] << "some log.." << endl;
DebugStream
&
V
(
Debug
::
verbosity
v
)
noexcept
;
// labels
typedef
std
::
pair
<
std
::
string
,
std
::
string
>
Label
;
void
addLabel
(
const
std
::
string
&
key
,
const
std
::
string
&
value
)
noexcept
;
void
delLabel
(
const
std
::
string
&
key
)
noexcept
;
void
cleanupLabels
()
noexcept
;
std
::
vector
<
Label
>
getLabels
()
noexcept
;
// -----------------------------------------------------
// короткие функции (для удобства)
// log.level1() - вывод с датой и временем "date time [LEVEL] ...",
// если вывод даты и времени не выключен при помощи showDateTime(false)
...
...
@@ -315,8 +335,12 @@ class DebugStream : public std::ostream
bool
isWriteLogFile
=
{
false
};
bool
onScreen
=
{
true
};
Debug
::
verbosity
verb
=
0
;
Debug
::
verbosity
vv
=
0
;
Debug
::
verbosity
verb
=
{
0
};
Debug
::
verbosity
vv
=
{
0
};
std
::
vector
<
Label
>
labels
;
bool
show_labels
=
{
true
};
bool
hide_label_key
=
{
false
};
};
// ------------------------------------------------------------------------------------------------
...
...
src/Log/DebugStream.cc
View file @
be4f3692
...
...
@@ -18,19 +18,13 @@
#include "Debug.h"
#include "Mutex.h"
//�Since the current C++ lib in egcs does not have a standard implementation
// of basic_streambuf and basic_filebuf we don't have to include this
// header.
//#define MODERN_STL_STREAMS
#ifdef MODERN_STL_STREAMS
#include <fstream>
#endif
#include <iostream>
#include <sstream>
#include <iomanip>
#include <time.h>
#include <iomanip>
#include <ctime>
#include <algorithm>
#include "DebugExtBuf.h"
#include "UniSetTypes.h"
...
...
@@ -183,6 +177,19 @@ std::ostream& DebugStream::debug(Debug::type t) noexcept
if
(
show_logtype
)
*
this
<<
"("
<<
std
::
setfill
(
' '
)
<<
std
::
setw
(
6
)
<<
t
<<
"): "
;
// "):\t";
if
(
show_labels
)
{
for
(
const
auto
&
l
:
labels
)
{
*
this
<<
"["
;
if
(
!
hide_label_key
)
*
this
<<
l
.
first
<<
"="
;
*
this
<<
l
.
second
<<
"]"
;
}
}
return
*
this
;
}
...
...
@@ -301,6 +308,37 @@ DebugStream::StreamEvent_Signal DebugStream::signal_stream_event()
return
s_stream
;
}
//--------------------------------------------------------------------------
void
DebugStream
::
addLabel
(
const
std
::
string
&
key
,
const
std
::
string
&
value
)
noexcept
{
auto
it
=
std
::
find_if
(
labels
.
begin
(),
labels
.
end
(),
[
key
]
(
const
Label
&
l
)
{
return
l
.
first
==
key
;
}
);
if
(
it
==
labels
.
end
()
)
labels
.
emplace_back
(
key
,
value
);
}
//--------------------------------------------------------------------------
void
DebugStream
::
delLabel
(
const
std
::
string
&
key
)
noexcept
{
auto
it
=
std
::
find_if
(
labels
.
begin
(),
labels
.
end
(),
[
key
]
(
const
Label
&
l
)
{
return
l
.
first
==
key
;
}
);
if
(
it
!=
labels
.
end
()
)
labels
.
erase
(
it
);
}
//--------------------------------------------------------------------------
void
DebugStream
::
cleanupLabels
()
noexcept
{
labels
.
clear
();
}
//--------------------------------------------------------------------------
std
::
vector
<
DebugStream
::
Label
>
DebugStream
::
getLabels
()
noexcept
{
return
labels
;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
#ifdef TEST_DEBUGSTREAM
...
...
tests/test_debugstream.cc
View file @
be4f3692
...
...
@@ -86,10 +86,18 @@ TEST_CASE("Debugstream: verbose", "[debugstream][verbose]" )
d
.
signal_stream_event
().
connect
(
&
test_log_buffer
);
d
.
V
(
1
)[
Debug
::
INFO
]
<<
"text"
<<
endl
;
d
.
V
(
1
)[
Debug
::
INFO
]
<<
"text"
;
REQUIRE
(
test_log_str
.
str
()
==
""
);
test_log_str
.
str
(
""
);
// clean
d
.
V
(
0
)(
Debug
::
INFO
)
<<
"text"
;
REQUIRE
(
test_log_str
.
str
().
find
(
"text"
)
!=
std
::
string
::
npos
);
test_log_str
.
str
(
""
);
// clean
d
[
Debug
::
INFO
]
<<
"text"
;
REQUIRE
(
test_log_str
.
str
().
find
(
"text"
)
!=
std
::
string
::
npos
);
test_log_str
.
str
(
""
);
// clean
d
.
verbose
(
1
);
d
.
V
(
1
)(
Debug
::
INFO
)
<<
"text"
;
d
.
V
(
2
)(
Debug
::
INFO
)
<<
"text2"
;
...
...
@@ -109,3 +117,37 @@ TEST_CASE("Debugstream: verbose", "[debugstream][verbose]" )
REQUIRE
(
test_log_str
.
str
()
==
""
);
}
// -----------------------------------------------------------------------------
TEST_CASE
(
"Debugstream: labels"
,
"[debugstream][labels]"
)
{
DebugStream
d
(
Debug
::
INFO
);
d
.
signal_stream_event
().
connect
(
&
test_log_buffer
);
d
.
addLabel
(
"module"
,
"uniset"
);
d
.
addLabel
(
"version"
,
std
::
to_string
(
100
));
auto
labels
=
d
.
getLabels
();
REQUIRE
(
labels
.
size
()
==
2
);
REQUIRE
(
labels
[
0
].
first
==
"module"
);
REQUIRE
(
labels
[
0
].
second
==
"uniset"
);
test_log_str
.
str
(
""
);
// clean
d
[
Debug
::
INFO
]
<<
"text"
;
REQUIRE
(
test_log_str
.
str
().
find
(
"[module=uniset][version=100]"
)
!=
std
::
string
::
npos
);
d
.
delLabel
(
"module"
);
test_log_str
.
str
(
""
);
// clean
d
[
Debug
::
INFO
]
<<
"text"
;
REQUIRE
(
test_log_str
.
str
().
find
(
"[module=uniset]"
)
==
std
::
string
::
npos
);
d
.
cleanupLabels
();
labels
=
d
.
getLabels
();
REQUIRE
(
labels
.
size
()
==
0
);
test_log_str
.
str
(
""
);
// clean
d
[
Debug
::
INFO
]
<<
"text"
;
REQUIRE
(
test_log_str
.
str
().
find
(
"[version=100]"
)
==
std
::
string
::
npos
);
}
// -----------------------------------------------------------------------------
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