Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
jami-daemon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository 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
savoirfairelinux
jami-daemon
Commits
d48291ac
Commit
d48291ac
authored
2 years ago
by
Adrien Béraud
Committed by
Sébastien Blin
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
logger: format header on demand
Change-Id: I6308ad4ed087458e39f7396c7ac223a072093c28
parent
32f39e65
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/logger.cpp
+40
-48
40 additions, 48 deletions
src/logger.cpp
with
40 additions
and
48 deletions
src/logger.cpp
+
40
−
48
View file @
d48291ac
...
...
@@ -62,19 +62,6 @@
#endif
/* APP_NAME */
#endif
#define BLACK "\033[22;30m"
#define GREEN "\033[22;32m"
#define BROWN "\033[22;33m"
#define BLUE "\033[22;34m"
#define MAGENTA "\033[22;35m"
#define GREY "\033[22;37m"
#define DARK_GREY "\033[01;30m"
#define LIGHT_RED "\033[01;31m"
#define LIGHT_SCREEN "\033[01;32m"
#define LIGHT_BLUE "\033[01;34m"
#define LIGHT_MAGENTA "\033[01;35m"
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"
#define END_COLOR "\033[0m"
#ifndef _WIN32
...
...
@@ -133,13 +120,14 @@ strErr()
static
const
char
*
stripDirName
(
const
char
*
path
)
{
const
char
*
occur
=
strrchr
(
path
,
DIR_SEPARATOR_CH
);
return
occur
?
occur
+
1
:
path
;
if
(
path
)
{
const
char
*
occur
=
strrchr
(
path
,
DIR_SEPARATOR_CH
);
return
occur
?
occur
+
1
:
path
;
}
else
return
nullptr
;
}
static
std
::
string
contex
tHeader
(
const
char
*
const
file
,
int
line
)
std
::
string
forma
tHeader
(
const
char
*
const
file
,
int
line
)
{
#ifdef __linux__
auto
tid
=
syscall
(
__NR_gettid
)
&
0xffff
;
...
...
@@ -202,29 +190,37 @@ struct Logger::Msg
Msg
()
=
delete
;
Msg
(
int
level
,
const
char
*
file
,
int
line
,
bool
linefeed
,
std
::
string
&&
message
)
:
payload_
(
std
::
move
(
message
))
,
header_
(
contextHeader
(
file
,
line
))
:
file_
(
stripDirName
(
file
))
,
line_
(
line
)
,
payload_
(
std
::
move
(
message
))
,
level_
(
level
)
,
linefeed_
(
linefeed
)
{}
Msg
(
int
level
,
const
char
*
file
,
int
line
,
bool
linefeed
,
const
char
*
fmt
,
va_list
ap
)
:
payload_
(
formatPrintfArgs
(
fmt
,
ap
))
,
header_
(
contextHeader
(
file
,
line
))
:
file_
(
stripDirName
(
file
))
,
line_
(
line
)
,
payload_
(
formatPrintfArgs
(
fmt
,
ap
))
,
level_
(
level
)
,
linefeed_
(
linefeed
)
{}
Msg
(
Msg
&&
other
)
{
file_
=
other
.
file_
;
line_
=
other
.
line_
;
payload_
=
std
::
move
(
other
.
payload_
);
header_
=
std
::
move
(
other
.
header_
);
level_
=
other
.
level_
;
linefeed_
=
other
.
linefeed_
;
}
inline
std
::
string
header
()
const
{
return
formatHeader
(
file_
,
line_
);
}
const
char
*
file_
;
unsigned
line_
;
std
::
string
payload_
;
std
::
string
header_
;
int
level_
;
bool
linefeed_
;
};
...
...
@@ -255,7 +251,7 @@ public:
}
#ifdef _WIN32
void
printLogImpl
(
jami
::
Logger
::
Msg
&
msg
,
bool
with_color
)
void
printLogImpl
(
Logger
::
Msg
&
msg
,
bool
with_color
)
{
// If we are using Visual Studio, we can use OutputDebugString to print
// to the "Output" window. Otherwise, we just use fputs to stderr.
...
...
@@ -274,6 +270,7 @@ public:
WORD
saved_attributes
;
static
HANDLE
hConsole
=
GetStdHandle
(
STD_OUTPUT_HANDLE
);
auto
header
=
msg
.
header
();
if
(
with_color
)
{
static
WORD
color_header
=
CYAN
;
WORD
color_prefix
=
LIGHT_GREEN
;
...
...
@@ -293,12 +290,12 @@ public:
saved_attributes
=
consoleInfo
.
wAttributes
;
SetConsoleTextAttribute
(
hConsole
,
color_header
);
printFunc
(
msg
.
header
_
.
c_str
());
printFunc
(
header
.
c_str
());
SetConsoleTextAttribute
(
hConsole
,
saved_attributes
);
SetConsoleTextAttribute
(
hConsole
,
color_prefix
);
}
else
{
printFunc
(
msg
.
header
_
.
c_str
());
printFunc
(
header
.
c_str
());
}
printFunc
(
msg
.
payload_
.
c_str
());
...
...
@@ -312,8 +309,9 @@ public:
}
}
#else
void
printLogImpl
(
jami
::
Logger
::
Msg
&
msg
,
bool
with_color
)
void
printLogImpl
(
const
Logger
::
Msg
&
msg
,
bool
with_color
)
{
auto
header
=
msg
.
header
();
if
(
with_color
)
{
const
char
*
color_header
=
CYAN
;
const
char
*
color_prefix
=
""
;
...
...
@@ -329,26 +327,25 @@ public:
}
fputs
(
color_header
,
stderr
);
f
puts
(
msg
.
header
_
.
c_str
(),
stderr
);
f
write
(
header
.
c_str
(),
1
,
header
.
size
(),
stderr
);
fputs
(
END_COLOR
,
stderr
);
fputs
(
color_prefix
,
stderr
);
}
else
{
f
puts
(
msg
.
header
_
.
c_str
(),
stderr
);
f
write
(
header
.
c_str
(),
1
,
header
.
size
(),
stderr
);
}
fputs
(
msg
.
payload_
.
c_str
(),
stderr
);
if
(
msg
.
linefeed_
)
{
putc
(
ENDL
,
stderr
);
}
if
(
with_color
)
{
fputs
(
END_COLOR
,
stderr
);
}
if
(
msg
.
linefeed_
)
{
putc
(
ENDL
,
stderr
);
}
}
#endif
/* _WIN32 */
virtual
void
consume
(
jami
::
Logger
::
Msg
&
msg
)
override
void
consume
(
Logger
::
Msg
&
msg
)
override
{
static
bool
with_color
=
!
(
getenv
(
"NO_COLOR"
)
||
getenv
(
"NO_COLORS"
)
||
getenv
(
"NO_COLOUR"
)
||
getenv
(
"NO_COLOURS"
));
...
...
@@ -406,10 +403,10 @@ public:
#endif
/* _WIN32 */
}
virtual
void
consume
(
Logger
::
Msg
&
msg
)
override
void
consume
(
Logger
::
Msg
&
msg
)
override
{
#ifdef __ANDROID__
__android_log_
p
ri
n
t
(
msg
.
level_
,
APP_NAME
,
"%s%s"
,
msg
.
header_
.
c_str
()
,
msg
.
payload_
.
c_str
());
__android_log_
w
rit
e
(
msg
.
level_
,
msg
.
file_
,
msg
.
payload_
.
c_str
());
#else
::
syslog
(
msg
.
level_
,
"%.*s"
,
(
int
)
msg
.
payload_
.
size
(),
msg
.
payload_
.
data
());
#endif
...
...
@@ -433,15 +430,10 @@ public:
return
*
self
;
}
virtual
void
consume
(
jami
::
Logger
::
Msg
&
msg
)
override
void
consume
(
Logger
::
Msg
&
msg
)
override
{
/*
* TODO - Maybe change the MessageSend sigature to avoid copying
* of message payload?
*/
auto
tmp
=
msg
.
header_
+
msg
.
payload_
;
jami
::
emitSignal
<
libjami
::
ConfigurationSignal
::
MessageSend
>
(
tmp
);
auto
message
=
msg
.
header
()
+
msg
.
payload_
;
emitSignal
<
libjami
::
ConfigurationSignal
::
MessageSend
>
(
message
);
}
};
...
...
@@ -493,6 +485,7 @@ public:
do_consume
(
file
,
pendingQ_
);
pendingQ_
.
clear
();
}
file
.
close
();
});
}
...
...
@@ -503,7 +496,7 @@ public:
thread_
.
join
();
}
virtual
void
consume
(
Logger
::
Msg
&
msg
)
override
void
consume
(
Logger
::
Msg
&
msg
)
override
{
notify
([
&
,
this
]
{
currentQ_
.
emplace_back
(
std
::
move
(
msg
));
});
}
...
...
@@ -520,8 +513,7 @@ private:
void
do_consume
(
std
::
ofstream
&
file
,
const
std
::
vector
<
Logger
::
Msg
>&
messages
)
{
for
(
const
auto
&
msg
:
messages
)
{
file
<<
msg
.
header_
<<
msg
.
payload_
;
file
<<
msg
.
header
()
<<
msg
.
payload_
;
if
(
msg
.
linefeed_
)
file
<<
ENDL
;
}
...
...
This diff is collapsed.
Click to expand it.
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