Skip to content

Commit 17b459e

Browse files
committed
Logger api documentation
1 parent 925e03c commit 17b459e

File tree

4 files changed

+169
-24
lines changed

4 files changed

+169
-24
lines changed

source/FileLogger.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <chrono>
3535
#include <fstream>
3636
#include <string>
37+
#include <unordered_map>
3738

3839
/* local header */
3940
#include "Logger.h"
@@ -43,25 +44,62 @@
4344
*/
4445
namespace vx {
4546

46-
/* logger that writes to file */
47+
/**
48+
* @brief The FileLogger class for writing messages to file.
49+
* @author Florian Becker <fb\@vxapps.com> (VX APPS)
50+
*/
4751
class FileLogger : public Logger {
4852

4953
public:
54+
/**
55+
* @brief Deletet default constructor for FileLogger.
56+
*/
5057
FileLogger() = delete;
5158

59+
/**
60+
* @brief Default constructor for FileLogger.
61+
* @param _configuration Logger configuration.
62+
*/
5263
explicit FileLogger( const std::unordered_map<std::string, std::string> &_config );
5364

65+
/**
66+
* @brief Build the log message.
67+
* @param _message Message to log.
68+
* @param _severity Severity level of the message.
69+
*/
5470
virtual void log( const std::string &_message, const Severity _severity ) override;
5571

72+
/**
73+
* @brief Output the log message.
74+
* @param _message Message to log.
75+
*/
5676
virtual void log( const std::string &_message ) override;
5777

5878
protected:
79+
/**
80+
* @brief Close and reopen the log file.
81+
*/
5982
void reopen();
6083

6184
private:
85+
/**
86+
* @brief Log filename.
87+
*/
6288
std::string m_filename;
89+
90+
/**
91+
* @brief Log file handle.
92+
*/
6393
std::ofstream m_file;
94+
95+
/**
96+
* @brief Interval for reopening the log file.
97+
*/
6498
std::chrono::seconds m_reopenInterval;
99+
100+
/**
101+
* @brief Timestamp of last reopen activity.
102+
*/
65103
std::chrono::system_clock::time_point m_lastReopen;
66104
};
67105
}

source/Logger.h

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
/* stl header */
3434
#include <chrono>
3535
#include <iomanip>
36-
#include <mutex>
3736
#include <sstream>
3837
#include <string>
3938
#include <unordered_map>
@@ -43,18 +42,19 @@
4342
*/
4443
namespace vx {
4544

46-
/* the log levels we support */
45+
/**
46+
* @brief The Severity enum.
47+
*/
4748
enum class Severity {
4849

49-
Verbose,
50-
Debug,
51-
Info,
52-
Warning,
53-
Error,
54-
Fatal
50+
Verbose, /**< Verbose level. */
51+
Debug, /**< Debug level. */
52+
Info, /**< Info level. */
53+
Warning, /**< Warning level. */
54+
Error, /**< Error level. */
55+
Fatal /**< Fatal error level. */
5556
};
5657

57-
/* all, something in between, none or default to info */
5858
#if defined(LOGGINGALL) || defined(LOGGINGVERBOSE)
5959
constexpr Severity avoidLogAbove = Severity::Verbose;
6060
#elif defined(LOGGINGDEBUG)
@@ -71,7 +71,10 @@ namespace vx {
7171
constexpr Severity avoidLogAbove = Severity::Info;
7272
#endif
7373

74-
/* returns formated to: 'Y-m-dTh:m:s.xxxxxx' */
74+
/**
75+
* @brief Create thread-safe timestamp.
76+
* @return Timestamp as 'Y-m-dThh:mm:ss.xxxxxx'
77+
*/
7578
inline std::string timestamp() {
7679

7780
/* get a precise timestamp as a string */
@@ -97,23 +100,51 @@ namespace vx {
97100
return result;
98101
}
99102

100-
inline std::string threadId() {
103+
// TODO: Currently not used
104+
/* inline std::string threadId() {
101105
102106
std::ostringstream s;
103107
s << " [" << std::this_thread::get_id() << "]";
104108
s.flush();
105109
std::string result = s.str();
106110
return result;
107-
}
111+
} */
108112

109-
/* logger base class, not pure virtual so you can use as a null logger if you want */
113+
/**
114+
* @brief The Logger class.
115+
* @note Not pure virtual to use as /dev/null logger.
116+
* @author Florian Becker <fb\@vxapps.com> (VX APPS)
117+
*/
110118
class Logger {
111119

112120
public:
121+
/**
122+
* @brief Deletet default constructor for Logger.
123+
*/
113124
Logger() = delete;
125+
126+
/**
127+
* @brief Default constructor for Logger.
128+
* @param _configuration Logger configuration.
129+
*/
114130
explicit Logger( const std::unordered_map<std::string, std::string> &_configuration );
131+
132+
/**
133+
* @brief Default destructor for Logger.
134+
*/
115135
virtual ~Logger() = default;
136+
137+
/**
138+
* @brief Build the log message.
139+
* @param _message Message to log.
140+
* @param _severity Severity level of the message.
141+
*/
116142
virtual void log( const std::string &_message, const Severity _severity );
143+
144+
/**
145+
* @brief Output the log message.
146+
* @param _message Message to log.
147+
*/
117148
virtual void log( const std::string &_message );
118149
};
119150
}

source/LoggerFactory.h

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,70 +45,119 @@
4545
*/
4646
namespace vx {
4747

48-
/* a factory that can create loggers (that derive from 'logger') via function pointers */
49-
/* this way you could make your own logger that sends log messages to who knows where */
48+
/**
49+
* @brief The LoggerFactory class.
50+
* @author Florian Becker <fb\@vxapps.com> (VX APPS)
51+
*/
5052
class LoggerFactory : public Singleton<LoggerFactory> {
5153

5254
public:
55+
/**
56+
* @brief Default constructor for LoggerFactory.
57+
*/
5358
explicit LoggerFactory();
5459

60+
/**
61+
* @brief Create all siblings of different log possibilities.
62+
* @param _config Configuration for logger.
63+
* @return Logger class with specific config.
64+
*/
5565
Logger *produce( const std::unordered_map<std::string, std::string> &_config ) const;
5666

5767
private:
68+
/**
69+
* @brief Created loggers.
70+
*/
5871
std::unordered_map<std::string, Logger *( * )( const std::unordered_map<std::string, std::string> & )> m_creators;
5972
};
6073

61-
/* get at the singleton */
74+
/**
75+
* @brief Create instance with default configuration for logger.
76+
* @param _config Configuration for logger.
77+
*/
6278
inline Logger &instance( const std::unordered_map<std::string, std::string> &_config = { { "type", "std" }, { "color", "" } } ) {
6379

6480
static std::unique_ptr<Logger> singleton( LoggerFactory::instance().produce( _config ) );
6581
return *singleton;
6682
}
6783

68-
/* configure the singleton (once only) */
84+
/**
85+
* @brief Change the configuration at runtime.
86+
* @param _config Logger configuration.
87+
*/
6988
inline void LogConfigure( const std::unordered_map<std::string, std::string> &_config ) {
7089

7190
instance( _config );
7291
}
7392

74-
/* statically log manually without the macros below */
75-
inline void Log( const std::string &_message, const Severity _level ) {
93+
/**
94+
* @brief Direct function for logging.
95+
* @param _message Message to log.
96+
* @param _severity Severity level for message to log.
97+
*/
98+
inline void Log( const std::string &_message, const Severity _severity ) {
7699

77-
instance().log( _message, _level );
100+
instance().log( _message, _severity );
78101
}
79102

80-
/* statically log manually without a level or maybe with a custom one */
103+
/**
104+
* @brief Direct function for logging.
105+
* @param _message Message to log.
106+
*/
81107
inline void Log( const std::string &_message ) {
82108

83109
instance().log( _message );
84110
}
85111

86-
/* these standout when reading code */
112+
/**
113+
* @brief Direct function for logging with verbose serivity.
114+
* @param _message Message to log.
115+
*/
87116
inline void LogVerbose( const std::string &_message ) {
88117

89118
instance().log( _message, Severity::Verbose );
90119
};
91120

121+
/**
122+
* @brief Direct function for logging with debug serivity.
123+
* @param _message Message to log.
124+
*/
92125
inline void LogDebug( const std::string &_message ) {
93126

94127
instance().log( _message, Severity::Debug );
95128
};
96129

130+
/**
131+
* @brief Direct function for logging with info serivity.
132+
* @param _message Message to log.
133+
*/
97134
inline void LogInfo( const std::string &_message ) {
98135

99136
instance().log( _message, Severity::Info );
100137
};
101138

139+
/**
140+
* @brief Direct function for logging with warning serivity.
141+
* @param _message Message to log.
142+
*/
102143
inline void LogWarning( const std::string &_message ) {
103144

104145
instance().log( _message, Severity::Warning );
105146
};
106147

148+
/**
149+
* @brief Direct function for logging with error serivity.
150+
* @param _message Message to log.
151+
*/
107152
inline void LogError( const std::string &_message ) {
108153

109154
instance().log( _message, Severity::Error );
110155
};
111156

157+
/**
158+
* @brief Direct function for logging with fatal error serivity.
159+
* @param _message Message to log.
160+
*/
112161
inline void LogFatal( const std::string &_message ) {
113162

114163
instance().log( _message, Severity::Fatal );

source/StdLogger.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,46 @@
4242
*/
4343
namespace vx {
4444

45-
/* logger that writes to standard out */
45+
/**
46+
* @brief The StdLogger class for writing messages to stdout.
47+
* @author Florian Becker <fb\@vxapps.com> (VX APPS)
48+
*/
4649
class StdLogger : public Logger {
4750

4851
public:
52+
/**
53+
* @brief Deletet default constructor for StdLogger.
54+
*/
4955
StdLogger() = delete;
56+
57+
/**
58+
* @brief Default constructor for StdLogger.
59+
* @param _configuration Logger configuration.
60+
*/
5061
explicit StdLogger( const std::unordered_map<std::string, std::string> &_config );
5162

63+
/**
64+
* @brief Build the log message.
65+
* @param _message Message to log.
66+
* @param _severity Severity level of the message.
67+
*/
5268
virtual void log( const std::string &m_message, const Severity _severity ) override;
5369

70+
/**
71+
* @brief Output the log message.
72+
* @param _message Message to log.
73+
*/
5474
virtual void log( const std::string &_message ) override;
5575

5676
private:
77+
/**
78+
* @brief Use colored message output.
79+
*/
5780
bool m_useColor = false;
81+
82+
/**
83+
* @brief Use stderr for severity >= Error.
84+
*/
5885
bool m_useStdErr = false;
5986
};
6087
}

0 commit comments

Comments
 (0)