Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NP_TMcode
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Giacomo Mulas
NP_TMcode
Commits
99990106
Commit
99990106
authored
1 year ago
by
Giovanni La Mura
Browse files
Options
Downloads
Patches
Plain Diff
Save last_message as pointer to string
parent
4ac15ae9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/include/logging.h
+11
-3
11 additions, 3 deletions
src/include/logging.h
src/libnptm/logging.cpp
+20
-6
20 additions, 6 deletions
src/libnptm/logging.cpp
with
31 additions
and
9 deletions
src/include/logging.h
+
11
−
3
View file @
99990106
...
...
@@ -27,6 +27,10 @@
* the terminal), as well as to record the execution history in appropriate log
* files. The `Logger` class offers an implementation of logging system complying
* with the requirements of the NP_TMcode project.
*
* The Logger class is designed to work with open files. It is a user responsibility
* to check that the required log files are properly opened before use, and closed
* thereafter, if they are not the standard `stdout` and `stderr` streams.
*/
class
Logger
{
protected:
...
...
@@ -35,7 +39,7 @@ class Logger {
//! \brief Pointer to logging stream.
FILE
*
log_output
;
//! \brief Last logged message.
std
::
string
last_message
;
std
::
string
*
last_message
;
//! \brief Threshold of logging level.
int
log_threshold
;
//! \brief Number of identical message repetitions.
...
...
@@ -48,13 +52,17 @@ class Logger {
* be `LOG_DEBG` (log everything), `LOG_INFO` (give detailed information),
* `LOG_WARN` (log odd looking effects), or `LOG_ERRO` (print error messages,
* `always active). The default behaviour is `LOG_WARN`.
* \param logging_output: `FILE *` Pointer to an output file for common messages
* \param logging_output: `FILE *` Pointer to an
open
output file for common messages
* (optional, default is `stdout`).
* \param error_output: `FILE *` Pointer to an output file for error messages
* \param error_output: `FILE *` Pointer to an
open
output file for error messages
* (optional, default is `stderr`).
*/
Logger
(
int
threshold
,
FILE
*
logging_output
=
stdout
,
FILE
*
error_output
=
stderr
);
/*! \brief Logger instance destroyer.
*/
~
Logger
();
/*! \brief Print a message to the error output.
*
* \param message: `string` The message to be printed.
...
...
This diff is collapsed.
Click to expand it.
src/libnptm/logging.cpp
+
20
−
6
View file @
99990106
...
...
@@ -14,42 +14,56 @@
using
namespace
std
;
Logger
::
Logger
(
int
threshold
,
FILE
*
logging_output
,
FILE
*
error_output
)
{
last_message
=
""
;
last_message
=
new
string
(
""
)
;
log_threshold
=
threshold
;
log_output
=
logging_output
;
err_output
=
error_output
;
repetitions
=
0
;
}
Logger
::~
Logger
()
{
delete
last_message
;
}
void
Logger
::
err
(
std
::
string
message
)
{
fprintf
(
err_output
,
"%s"
,
message
.
c_str
());
fflush
(
err_output
);
}
void
Logger
::
flush
(
int
level
)
{
string
summary
=
"
\"
"
+
last_message
+
"
\"
issued "
+
to_string
(
repetitions
);
string
summary
=
"
\"
"
+
*
last_message
+
"
\"
issued "
+
to_string
(
repetitions
);
if
(
repetitions
==
1
)
summary
+=
" time.
\n
"
;
else
summary
+=
" times.
\n
"
;
if
(
level
==
LOG_ERRO
)
err
(
summary
);
else
{
if
(
level
>=
log_threshold
)
fprintf
(
log_output
,
"%s"
,
summary
.
c_str
());
if
(
level
>=
log_threshold
)
{
fprintf
(
log_output
,
"%s"
,
summary
.
c_str
());
fflush
(
log_output
);
}
}
delete
last_message
;
last_message
=
new
string
(
""
);
repetitions
=
0
;
}
void
Logger
::
log
(
std
::
string
message
,
int
level
)
{
if
(
level
==
LOG_ERRO
)
err
(
message
);
else
{
if
(
level
>=
log_threshold
)
fprintf
(
log_output
,
"%s"
,
message
.
c_str
());
if
(
level
>=
log_threshold
)
{
fprintf
(
log_output
,
"%s"
,
message
.
c_str
());
fflush
(
log_output
);
}
}
}
void
Logger
::
push
(
std
::
string
message
)
{
if
(
repetitions
>
0
)
{
if
(
message
.
compare
(
last_message
)
!=
0
)
{
if
(
message
.
compare
(
*
last_message
)
!=
0
)
{
flush
(
LOG_DEBG
);
}
}
log
(
message
,
LOG_DEBG
);
last_message
=
message
;
delete
last_message
;
last_message
=
new
string
(
message
);
repetitions
++
;
}
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