Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CTA
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cta
CTA
Commits
c1962037
Commit
c1962037
authored
10 months ago
by
Joao Afonso
Browse files
Options
Downloads
Patches
Plain Diff
Fix special character encoding in json logging
parent
e04a2a10
No related branches found
No related tags found
1 merge request
!525
Fix special character encoding in json logging
Changes
4
Pipelines
23
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
ReleaseNotes.md
+1
-0
1 addition, 0 deletions
ReleaseNotes.md
common/log/JsonTest.cpp
+45
-0
45 additions, 0 deletions
common/log/JsonTest.cpp
common/log/Param.cpp
+34
-2
34 additions, 2 deletions
common/log/Param.cpp
common/log/Param.hpp
+13
-0
13 additions, 0 deletions
common/log/Param.hpp
with
93 additions
and
2 deletions
ReleaseNotes.md
+
1
−
0
View file @
c1962037
...
...
@@ -20,6 +20,7 @@
-
cta/CTA#683 - Fix problems with field consistency in json logging
-
cta/CTA#688 - Fix tapeserver umask to allow directory creation in POSIX filesystems
-
cta/CTA#693 - Fix tapeserver tool regressions
-
cta/CTA#704 - Fix special character encoding in json logging
### Continuous Integration
-
cta/CTA#615 - Going to xrdfs xattr API for EOS5 extended attribute tests (EOS >= 5.2.17)
...
...
This diff is collapsed.
Click to expand it.
common/log/JsonTest.cpp
+
45
−
0
View file @
c1962037
...
...
@@ -151,4 +151,49 @@ TEST_F(cta_log_JsonTest, testJsonPrinting) {
ASSERT_EQ
(
0
,
jObjB
.
jsonGetValueProbe
<
int64_t
>
(
"ParamsB_null"
));
}
TEST_F
(
cta_log_JsonTest
,
testJsonStringEscape
)
{
using
namespace
cta
::
log
;
// Prepare the logger for inspection
StringLogger
logger
(
"dummy"
,
"cta_log_JsonTest"
,
cta
::
log
::
DEBUG
);
LogContext
logContext
(
logger
);
logger
.
setLogFormat
(
"json"
);
{
cta
::
log
::
ScopedParamContainer
paramsA
(
logContext
);
paramsA
.
add
(
"key_
\"
"
,
"value_
\"
"
);
paramsA
.
add
(
"key_
\\
"
,
"value_
\\
"
);
paramsA
.
add
(
"key_
\b
"
,
"value_
\b
"
);
paramsA
.
add
(
"key_
\n
"
,
"value_
\n
"
);
paramsA
.
add
(
"key_
\f
"
,
"value_
\f
"
);
paramsA
.
add
(
"key_
\r
"
,
"value_
\r
"
);
paramsA
.
add
(
"key_
\t
"
,
"value_
\t
"
);
paramsA
.
add
(
"key_
\x00
"
,
"value_
\x00
"
);
paramsA
.
add
(
"key_
\x1f
"
,
"value_
\x1f
"
);
paramsA
.
add
(
"key_
\x20
"
,
"value_
\x20
"
);
//This is a whitespace character
logContext
.
log
(
INFO
,
"Testing escaped values"
);
}
std
::
string
logLine
=
logger
.
getLog
();
JSONCObjectProbe
jObj
;
jObj
.
buildFromJSON
(
logLine
);
// Check that JSON is parsed correctly
ASSERT_NO_THROW
(
jObj
.
getJSON
());
// Check expected keys and values
// Strings should be converted back to cpp with the escape characters correctly decoded
ASSERT_EQ
(
"value_
\"
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\"
"
));
ASSERT_EQ
(
"value_
\\
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\\
"
));
ASSERT_EQ
(
"value_
\b
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\b
"
));
ASSERT_EQ
(
"value_
\n
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\n
"
));
ASSERT_EQ
(
"value_
\f
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\f
"
));
ASSERT_EQ
(
"value_
\r
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\r
"
));
ASSERT_EQ
(
"value_
\t
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\t
"
));
ASSERT_EQ
(
"value_
\x00
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\x00
"
));
ASSERT_EQ
(
"value_
\x1f
"
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_
\x1f
"
));
ASSERT_EQ
(
"value_ "
,
jObj
.
jsonGetValueProbe
<
std
::
string
>
(
"key_ "
));
}
}
// namespace unitTests
This diff is collapsed.
Click to expand it.
common/log/Param.cpp
+
34
−
2
View file @
c1962037
...
...
@@ -19,6 +19,7 @@
#include
<iostream>
#include
<iomanip>
#include
<algorithm>
namespace
cta
::
log
{
...
...
@@ -61,14 +62,14 @@ std::string Param::getValueStr() const noexcept {
//------------------------------------------------------------------------------
std
::
string
Param
::
getKeyValueJSON
()
const
noexcept
{
std
::
ostringstream
oss
;
oss
<<
"
\"
"
<<
m_name
<<
"
\"
:"
;
oss
<<
"
\"
"
<<
stringFormattingJSON
(
m_name
)
<<
"
\"
:"
;
if
(
m_value
.
has_value
())
{
std
::
visit
([
&
oss
](
auto
&&
arg
)
{
using
T
=
std
::
decay_t
<
decltype
(
arg
)
>
;
if
constexpr
(
std
::
is_same_v
<
T
,
bool
>
)
{
oss
<<
(
arg
?
"true"
:
"false"
);
}
else
if
constexpr
(
std
::
is_same_v
<
T
,
std
::
string
>
)
{
oss
<<
"
\"
"
<<
arg
<<
"
\"
"
;
oss
<<
"
\"
"
<<
stringFormattingJSON
(
arg
)
<<
"
\"
"
;
}
else
if
constexpr
(
std
::
is_integral_v
<
T
>
)
{
oss
<<
arg
;
}
else
if
constexpr
(
std
::
is_floating_point_v
<
T
>
)
{
...
...
@@ -88,4 +89,35 @@ void Param::setValue<ParamValType>(const ParamValType& value) noexcept {
m_value
=
value
;
}
//------------------------------------------------------------------------------
// stringFormattingJSON nested class
//------------------------------------------------------------------------------
Param
::
stringFormattingJSON
::
stringFormattingJSON
(
const
std
::
string
&
str
)
:
m_value
(
str
)
{}
//------------------------------------------------------------------------------
// stringFormattingJSON << operator overload
//------------------------------------------------------------------------------
std
::
ostream
&
operator
<<
(
std
::
ostream
&
oss
,
const
Param
::
stringFormattingJSON
&
fp
)
{
std
::
ostringstream
oss_tmp
;
for
(
char
c
:
fp
.
m_value
)
{
switch
(
c
)
{
case
'\"'
:
oss_tmp
<<
R"(\")"
;
break
;
case
'\\'
:
oss_tmp
<<
R"(\\)"
;
break
;
case
'\b'
:
oss_tmp
<<
R"(\b)"
;
break
;
case
'\f'
:
oss_tmp
<<
R"(\f)"
;
break
;
case
'\n'
:
oss_tmp
<<
R"(\n)"
;
break
;
case
'\r'
:
oss_tmp
<<
R"(\r)"
;
break
;
case
'\t'
:
oss_tmp
<<
R"(\t)"
;
break
;
default:
if
(
'\x00'
<=
c
&&
c
<=
'\x1f'
)
{
oss_tmp
<<
R"(\u)"
<<
std
::
hex
<<
std
::
setw
(
4
)
<<
std
::
setfill
(
'0'
)
<<
static_cast
<
unsigned
int
>
(
c
);
}
else
{
oss_tmp
<<
c
;
}
}
}
oss
<<
oss_tmp
.
str
();
return
oss
;
}
}
// namespace cta::log
This diff is collapsed.
Click to expand it.
common/log/Param.hpp
+
13
−
0
View file @
c1962037
...
...
@@ -149,6 +149,19 @@ protected:
*/
ParamValType
m_value
;
/**
* Helper class to format string values in JSON
*/
class
stringFormattingJSON
{
public:
explicit
stringFormattingJSON
(
const
std
::
string
&
str
);
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
oss
,
const
stringFormattingJSON
&
fp
);
private:
const
std
::
string
&
m_value
;
};
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
oss
,
const
Param
::
stringFormattingJSON
&
fp
);
/**
* Helper class to format floating-point values
*/
...
...
This diff is collapsed.
Click to expand it.
Pablo Oliver Cortes
@poliverc
mentioned in issue
#899 (closed)
·
5 months ago
mentioned in issue
#899 (closed)
mentioned in issue #899
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment