Skip to content

Commit 88e1cae

Browse files
committed
Fix unused std err member for std logger
Code quality
1 parent 91ea781 commit 88e1cae

File tree

13 files changed

+45
-24
lines changed

13 files changed

+45
-24
lines changed

examples/simple/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ constexpr auto logMessage = "This is a log message";
4343
int main() {
4444

4545
/* configure logging, if you dont it defaults to standard out logging with colors */
46-
ConfigureLogger( { { "type", "std" }, { "color", "true" } } );
46+
ConfigureLogger( { { "type", "std" }, { "color", "true" }, { "stderr", "true" } } );
4747

4848
/* Log some messages */
4949
LogFatal( logMessage );

source/FileLogger.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
/* stl header */
3232
#include <algorithm>
33+
#include <iostream>
3334
#include <mutex>
3435

3536
/* magic enum */
@@ -70,17 +71,20 @@ namespace vx {
7071

7172
m_reopenInterval = std::chrono::seconds( std::stoul( interval->second ) );
7273
}
73-
catch ( [[maybe_unused]] const std::invalid_argument &_exception ) {
74+
catch ( const std::invalid_argument &_exception ) {
7475

76+
std::cout << _exception.what() << std::endl;
7577
throw std::invalid_argument( interval->second + " is not a valid reopen interval." );
7678
}
77-
catch ( [[maybe_unused]] const std::out_of_range &_exception ) {
79+
catch ( const std::out_of_range &_exception ) {
7880

81+
std::cout << _exception.what() << std::endl;
7982
throw std::out_of_range( interval->second + " is out of range reopen interval." );
8083
}
81-
catch ( [[maybe_unused]] const std::exception &_exception ) {
84+
catch ( const std::exception &_exception ) {
8285

8386
/* nothing to do */
87+
std::cout << _exception.what() << std::endl;
8488
}
8589
}
8690

@@ -99,9 +103,10 @@ namespace vx {
99103
m_file.close();
100104
}
101105
}
102-
catch ( [[maybe_unused]] const std::exception &_exception ) {
106+
catch ( const std::exception &_exception ) {
103107

104108
/* nothing to do, file cannot be closed. */
109+
std::cout << _exception.what() << std::endl;
105110
}
106111
}
107112

@@ -119,7 +124,7 @@ namespace vx {
119124
output.append( timestamp() );
120125

121126
std::string severity( magic_enum::enum_name( _severity ) );
122-
std::transform( std::begin( severity ), std::end( severity ), std::begin( severity ), []( auto c ) { return ::toupper( c ); } );
127+
std::transform( std::begin( severity ), std::end( severity ), std::begin( severity ), []( auto c ) { return std::toupper( c ); } );
123128
output.append( " [" + severity + "] " );
124129
if ( std::string( _location.file_name() ) != "unsupported" ) {
125130

@@ -166,9 +171,10 @@ namespace vx {
166171
}
167172
m_file.open( m_filename, std::ofstream::out | std::ofstream::app );
168173
}
169-
catch ( [[maybe_unused]] const std::exception &_exception ) {
174+
catch ( const std::exception &_exception ) {
170175

171176
/* nothing to do, file is not open or cannot be closed. */
177+
std::cout << _exception.what() << std::endl;
172178
}
173179
m_lastReopen = std::chrono::system_clock::now();
174180
}

source/LoggerFactory.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ namespace vx {
4949
m_creators.try_emplace( "file", []( const std::unordered_map<std::string, std::string> &_configuration ) -> std::unique_ptr<Logger> { return std::make_unique<FileLogger>( _configuration ); } );
5050
m_creators.try_emplace( "xml", []( const std::unordered_map<std::string, std::string> &_configuration ) -> std::unique_ptr<Logger> { return std::make_unique<XmlFileLogger>( _configuration ); } );
5151
}
52-
catch ( [[maybe_unused]] const std::bad_alloc &_exception ) {
52+
catch ( const std::bad_alloc &_exception ) {
5353

54-
/* _exception.what() */
54+
std::cout << _exception.what() << std::endl;
5555
}
56-
catch ( [[maybe_unused]] const std::exception &_exception ) {
56+
catch ( const std::exception &_exception ) {
5757

58-
/* _exception.what() */
58+
std::cout << _exception.what() << std::endl;
5959
}
6060
}
6161

source/StdLogger.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace vx {
6565
output.append( timestamp() );
6666

6767
std::string severity( magic_enum::enum_name( _severity ) );
68-
std::transform( std::begin( severity ), std::end( severity ), std::begin( severity ), []( auto c ) { return ::toupper( c ); } );
68+
std::transform( std::begin( severity ), std::end( severity ), std::begin( severity ), []( auto c ) { return std::toupper( c ); } );
6969
if ( m_useColor ) {
7070

7171
switch ( _severity ) {
@@ -121,7 +121,16 @@ namespace vx {
121121
/* though, we make sure to only call the << operator once on std::cout */
122122
/* otherwise the << operators from different threads could interleave */
123123
/* obviously we dont care if flushes interleave */
124-
std::cout << _message;
124+
std::string message( _message );
125+
/* Not optimal to use find here, so if a message contains [FATAL] as text, it will match here */
126+
if ( m_useStdErr && ( message.find( "[FATAL]" ) != std::string::npos || message.find( "[ERROR]" ) != std::string::npos ) ) {
127+
128+
std::cerr << _message;
129+
}
130+
else {
131+
132+
std::cout << _message;
133+
}
125134
std::cout.flush();
126135
}
127136
}

source/XmlFileLogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace vx {
7676
}
7777

7878
std::string severity( magic_enum::enum_name( _severity ) );
79-
std::transform( std::begin( severity ), std::end( severity ), std::begin( severity ), []( auto c ) { return ::toupper( c ); } );
79+
std::transform( std::begin( severity ), std::end( severity ), std::begin( severity ), []( auto c ) { return std::toupper( c ); } );
8080
output.append( "<severity>" );
8181
output.append( severity );
8282
output.append( "</severity>" );

tests/simple/SimpleFileLogger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ int main() {
150150

151151
xmlFileOut.close();
152152
}
153-
catch ( [[maybe_unused]] const std::exception &_exception ) {
153+
catch ( const std::exception &_exception ) {
154154

155155
/* Do not throw exception here */
156+
std::cout << _exception.what() << std::endl;
156157
}
157158

158159
return wasSuccessful ? 0 : 1;

tests/simple/SimpleNullLogger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ int main() {
122122

123123
xmlFileOut.close();
124124
}
125-
catch ( [[maybe_unused]] const std::exception &_exception ) {
125+
catch ( const std::exception &_exception ) {
126126

127127
/* Do not throw exception here */
128+
std::cout << _exception.what() << std::endl;
128129
}
129130

130131
return wasSuccessful ? 0 : 1;

tests/simple/SimpleStdLogger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ int main() {
122122

123123
xmlFileOut.close();
124124
}
125-
catch ( [[maybe_unused]] const std::exception &_exception ) {
125+
catch ( const std::exception &_exception ) {
126126

127127
/* Do not throw exception here */
128+
std::cout << _exception.what() << std::endl;
128129
}
129130

130131
return wasSuccessful ? 0 : 1;

tests/simple/SimpleXmlLogger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ int main() {
149149

150150
xmlFileOut.close();
151151
}
152-
catch ( [[maybe_unused]] const std::exception &_exception ) {
152+
catch ( const std::exception &_exception ) {
153153

154154
/* Do not throw exception here */
155+
std::cout << _exception.what() << std::endl;
155156
}
156157

157158
return wasSuccessful ? 0 : 1;

tests/threads/ThreadsFileLogger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ int main() {
166166

167167
xmlFileOut.close();
168168
}
169-
catch ( [[maybe_unused]] const std::exception &_exception ) {
169+
catch ( const std::exception &_exception ) {
170170

171171
/* Do not throw exception here */
172+
std::cout << _exception.what() << std::endl;
172173
}
173174

174175
return wasSuccessful ? 0 : 1;

0 commit comments

Comments
 (0)