Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit 276d4e9

Browse files
authored
Merge pull request #2 from Asteomount/develop
Develop
2 parents d1f9007 + 25c0f64 commit 276d4e9

File tree

8 files changed

+517
-169
lines changed

8 files changed

+517
-169
lines changed

C++/logger/Logger.cpp

+35-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ int Logger::nbWrite = 0;
66

77
pthread_mutex_t Logger::mutex;
88

9+
LoggerOption Logger::verbose = FILE_AND_CONSOLE;
10+
11+
bool Logger::showTrace = true;
12+
13+
std::vector<LoggerType> Logger::showTypes = {INFO, SUCCESS, ERROR, WARNING, DEBUG};
14+
915

1016
std::string Logger::getColor(LoggerColor color) {
1117
switch (color) {
@@ -87,8 +93,12 @@ std::string Logger::getTypeName(LoggerType type) {
8793
}
8894
}
8995

90-
void Logger::init() {
96+
void Logger::init(LoggerOption verboseP, bool showTraceP, const std::vector<LoggerType> &showTypesP) {
9197
if (!file.is_open()) {
98+
verbose = verboseP;
99+
showTrace = showTraceP;
100+
showTypes = showTypesP;
101+
92102
bool dirCreated = false;
93103

94104
if (mkdir(LOG_PATH, S_IRWXU) == 0)
@@ -100,45 +110,56 @@ void Logger::init() {
100110
nbWrite = 0;
101111

102112
if (pthread_mutex_init(&mutex, nullptr) != 0)
103-
error(CONSOLE_ONLY, "Error mutex : %d\n", errno);
113+
ERROR_LOG(CONSOLE_ONLY, "Error mutex : %d\n", errno);
104114

105-
info(FILE_ONLY, "Log start\n");
115+
INFO_LOG(FILE_ONLY, "Log start\n");
106116
if (dirCreated)
107-
warning(FILE_AND_CONSOLE, "Log directory created\n");
117+
WARNING_LOG(FILE_AND_CONSOLE, "Log directory created\n");
108118
} else
109-
warning(FILE_AND_CONSOLE, "Log already init\n");
119+
WARNING_LOG(FILE_AND_CONSOLE, "Log already init\n");
110120
}
111121

112122
void Logger::exit() {
113123
if (file.is_open()) {
114-
info(FILE_ONLY, "End log\n");
124+
INFO_LOG(FILE_ONLY, "End log\n");
115125
file.close();
116126

117127
pthread_mutex_destroy(&mutex);
118128
} else
119-
error(CONSOLE_ONLY, "Please init before exit\n");
129+
ERROR_LOG(CONSOLE_ONLY, "Please init before exit\n");
120130
}
121131

122-
void Logger::genericLog(const std::string &message, LoggerType type, LoggerOption option) {
123-
if (option != FILE_ONLY)
124-
std::cout << getTypeColor(type) << message << getColor(DEFAULT);
132+
void Logger::genericLog(const std::string &function, const std::string &message, LoggerType type, LoggerOption option) {
133+
std::string t = message;
134+
135+
if (showTrace) {
136+
t = "[" + function + "]\t" + message;
137+
}
138+
139+
if (t[t.length() - 1] != '\n') {
140+
t += "\n";
141+
}
142+
143+
if (option != FILE_ONLY && verbose != FILE_ONLY &&
144+
std::find(showTypes.begin(), showTypes.end(), type) != showTypes.end())
145+
std::cout << getTypeColor(type) << t << getColor(DEFAULT);
125146

126-
if (option != CONSOLE_ONLY) {
147+
if (option != CONSOLE_ONLY && verbose != CONSOLE_ONLY) {
127148
pthread_mutex_lock(&mutex);
128-
writeToFile(message, type);
149+
writeToFile(t, type);
129150
pthread_mutex_unlock(&mutex);
130151
}
131152
}
132153

133154
void Logger::writeToFile(const std::string &message, LoggerType type) {
134155
if (file.is_open()) {
135156
std::string toPrint =
136-
"[" + std::to_string(nbWrite) + "-" + getHour() + "-" + getTypeName(type) + "] " + message;
157+
"[" + std::to_string(nbWrite) + "-" + getHour() + "-" + getTypeName(type) + "]\t" + message;
137158
file << toPrint;
138159
file.flush();
139160
nbWrite++;
140161
} else
141-
error(CONSOLE_ONLY, "Please init logger\n");
162+
ERROR_LOG(CONSOLE_ONLY, "Please init logger\n");
142163
}
143164

144165
std::string Logger::getHour() {

C++/logger/Logger.hpp

+42-14
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
#include <sstream>
1010
#include <thread>
1111
#include <cstring>
12+
#include <vector>
13+
#include <algorithm>
1214

1315
/**
1416
* Logger
1517
* <p>
1618
* Use Logger::init() to start the logger and Logger::exit() to close the logger
1719
* <p>
1820
* Simple usage :
19-
* Logger::debug(FILE_AND_CONSOLE, "Is it simple ? YES");
21+
* DEBUG_LOG(FILE_AND_CONSOLE, "Is it simple ? YES");
2022
* Write 'Is it simple ? YES' (without the quote) in the console and the file
2123
* <p>
2224
* Complex usage :
23-
* Logger::info(CONSOLE_ONLY, "Not too complex ? ", "Maybe");
25+
* INFO_LOG(CONSOLE_ONLY, "Not too complex ? ", "Maybe");
2426
* Write 'Not toot complex ? Maybe' (without the quote) only in the console
2527
*
2628
* <!> WARNING <!>
@@ -112,7 +114,9 @@ class Logger {
112114
/**
113115
* Initialisation
114116
*/
115-
static void init();
117+
static void init(LoggerOption verboseP = FILE_AND_CONSOLE,
118+
bool showTraceP = true,
119+
const std::vector<LoggerType> &showTypesP = {INFO, SUCCESS, ERROR, WARNING, DEBUG});
116120

117121
/**
118122
* Quit the log and close the writer
@@ -122,67 +126,73 @@ class Logger {
122126
public:
123127
/**
124128
* Info
129+
* @param function std::string
125130
* @param option LoggerOption
126131
* @param arg const char*
127132
* @param ...
128133
*/
129134
template<typename... Ts>
130-
static void info(LoggerOption option, Ts const &... args) {
131-
genericLog(stringify(args...), INFO, option);
135+
static void info(const std::string &function, LoggerOption option, Ts const &... args) {
136+
genericLog(function, stringify(args...), INFO, option);
132137
}
133138

134139
/**
135140
* Success
141+
* @param function std::string
136142
* @param option LoggerOption
137143
* @param arg const char*
138144
* @param ...
139145
*/
140146
template<typename... Ts>
141-
static void success(LoggerOption option, Ts const &... args) {
142-
genericLog(stringify(args...), SUCCESS, option);
147+
static void success(const std::string &function, LoggerOption option, Ts const &... args) {
148+
genericLog(function, stringify(args...), SUCCESS, option);
143149
}
144150

145151
/**
146152
* Error
153+
* @param function std::string
147154
* @param option LoggerOption
148155
* @param arg const char*
149156
* @param ...
150157
*/
151158
template<typename... Ts>
152-
static void error(LoggerOption option, Ts const &... args) {
153-
genericLog(stringify(args...), ERROR, option);
159+
static void error(const std::string &function, LoggerOption option, Ts const &... args) {
160+
genericLog(function, stringify(args...), ERROR, option);
154161
}
155162

156163
/**
157164
* Warning
165+
* @param function std::string
158166
* @param option LoggerOption
159167
* @param arg const char*
160168
* @param ...
161169
*/
162170
template<typename... Ts>
163-
static void warning(LoggerOption option, Ts const &... args) {
164-
genericLog(stringify(args...), WARNING, option);
171+
static void warning(const std::string &function, LoggerOption option, Ts const &... args) {
172+
genericLog(function, stringify(args...), WARNING, option);
165173
}
166174

167175
/**
168176
* Debug
177+
* @param function std::string
169178
* @param option LoggerOption
170179
* @param arg const char*
171180
* @param ...
172181
*/
173182
template<typename... Ts>
174-
static void debug(LoggerOption option, Ts const &... args) {
175-
genericLog(stringify(args...), DEBUG, option);
183+
static void debug(const std::string &function, LoggerOption option, Ts const &... args) {
184+
genericLog(function, stringify(args...), DEBUG, option);
176185
}
177186

178187
private:
179188
/**
180189
* Generic log use for all logs
190+
* @param function std::string
181191
* @param message std::string
182192
* @param type LoggerType
183193
* @param option LoggerOption
184194
*/
185-
static void genericLog(const std::string &message, LoggerType type, LoggerOption option);
195+
static void genericLog(const std::string &function, const std::string &message, LoggerType type, LoggerOption option);
186196

187197
/**
188198
* Write the log into the file
@@ -218,6 +228,18 @@ class Logger {
218228
* A mutex for writeToFile(), to be thread-safe
219229
*/
220230
static pthread_mutex_t mutex;
231+
/**
232+
* The type of verbose
233+
*/
234+
static LoggerOption verbose;
235+
/**
236+
* Show trace or not
237+
*/
238+
static bool showTrace;
239+
/**
240+
* The types of logs that be shown
241+
*/
242+
static std::vector<LoggerType> showTypes;
221243

222244
private: // Disallow to instance this class
223245
Logger() = default;
@@ -249,4 +271,10 @@ class Logger {
249271
}
250272
};
251273

274+
#define INFO_LOG(option, msg...) Logger::info(__FUNCTION__, option, msg)
275+
#define SUCCESS_LOG(option, msg...) Logger::success(__FUNCTION__, option, msg)
276+
#define ERROR_LOG(option, msg...) Logger::error(__FUNCTION__, option, msg)
277+
#define WARNING_LOG(option, msg...) Logger::warning(__FUNCTION__, option, msg)
278+
#define DEBUG_LOG(option, msg...) Logger::debug(__FUNCTION__, option, msg)
279+
252280
#endif //LOGGER_LOGGER_HPP

0 commit comments

Comments
 (0)