added new facility "other"
This commit is contained in:
+20
-9
@@ -33,6 +33,7 @@ static const char *facilityNames[] = {
|
||||
"network",
|
||||
"bus",
|
||||
"update",
|
||||
"other",
|
||||
"all",
|
||||
NULL
|
||||
};
|
||||
@@ -73,6 +74,7 @@ bool setLogFacilities(const char* facilities)
|
||||
newFacilites |= 1<<val;
|
||||
}
|
||||
}
|
||||
//s_lastFacilities = newFacilites;
|
||||
s_logFacilites = newFacilites;
|
||||
free(input);
|
||||
return true;
|
||||
@@ -147,28 +149,37 @@ bool needsLog(const LogFacility facility, const LogLevel level)
|
||||
&& (s_logLevel >= level);
|
||||
}
|
||||
|
||||
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...)
|
||||
void logWrite(const char* facility, const char* level, const char* message, va_list ap)
|
||||
{
|
||||
struct timespec ts;
|
||||
struct tm* tm;
|
||||
|
||||
clockGettime(&ts);
|
||||
tm = localtime(&ts.tv_sec);
|
||||
|
||||
char* buf;
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
|
||||
if (vasprintf(&buf, message, ap) >= 0 && buf) {
|
||||
fprintf(s_logFile, "%04d-%02d-%02d %02d:%02d:%02d.%03ld [%s %s] %s\n",
|
||||
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000,
|
||||
facilityNames[facility], levelNames[level], buf);
|
||||
facility, level, buf);
|
||||
fflush(s_logFile);
|
||||
}
|
||||
|
||||
va_end(ap);
|
||||
if (buf) {
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facilityNames[facility], levelNames[level], message, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void logWrite(const char* facility, const LogLevel level, const char* message, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facility, levelNames[level], message, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
+23
-2
@@ -27,11 +27,12 @@ enum LogFacility {
|
||||
lf_network, //!< network related
|
||||
lf_bus, //!< eBUS related
|
||||
lf_update, //!< updates found while listening to the bus
|
||||
lf_COUNT=4 //!< number of available log facilities
|
||||
lf_other, //!< all other log facilities
|
||||
lf_COUNT=5 //!< number of available log facilities
|
||||
};
|
||||
|
||||
/** macro for enabling all log facilities. */
|
||||
#define LF_ALL ((1<<lf_main) | (1<<lf_network) | (1<<lf_bus) | (1<<lf_update))
|
||||
#define LF_ALL ((1<<lf_main) | (1<<lf_network) | (1<<lf_bus) | (1<<lf_update) | (1<<lf_other))
|
||||
|
||||
/** the available log levels. */
|
||||
enum LogLevel {
|
||||
@@ -99,6 +100,15 @@ bool needsLog(const LogFacility facility, const LogLevel level);
|
||||
*/
|
||||
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...);
|
||||
|
||||
/**
|
||||
* Log the message for the specified facility name @a LogLevel (even if logging is not needed for the facility/level).
|
||||
* @param facility the facility name of the message to log.
|
||||
* @param level the @a LogLevel of the message to log.
|
||||
* @param message the message to log.
|
||||
* @param ... variable arguments depending on the @a message.
|
||||
*/
|
||||
void logWrite(const char* facility, const LogLevel level, const char* message, ...);
|
||||
|
||||
/** A macro that calls the logging function only if needed. */
|
||||
#define LOG(facility, level, ...) (needsLog(facility, level) ? logWrite(facility, level, __VA_ARGS__) : void(0))
|
||||
|
||||
@@ -114,5 +124,16 @@ void logWrite(const LogFacility facility, const LogLevel level, const char* mess
|
||||
/** A macro for a debug message that calls the logging function only if needed. */
|
||||
#define logDebug(facility, ...) (needsLog(facility, ll_debug) ? logWrite(facility, ll_debug, __VA_ARGS__) : void(0))
|
||||
|
||||
/** A macro for an error message that calls the logging function only if needed. */
|
||||
#define logOtherError(facility, ...) (needsLog(lf_other, ll_error) ? logWrite(facility, ll_error, __VA_ARGS__) : void(0))
|
||||
|
||||
/** A macro for a notice message that calls the logging function only if needed. */
|
||||
#define logOtherNotice(facility, ...) (needsLog(lf_other, ll_notice) ? logWrite(facility, ll_notice, __VA_ARGS__) : void(0))
|
||||
|
||||
/** A macro for an info message that calls the logging function only if needed. */
|
||||
#define logOtherInfo(facility, ...) (needsLog(lf_other, ll_info) ? logWrite(facility, ll_info, __VA_ARGS__) : void(0))
|
||||
|
||||
/** A macro for a debug message that calls the logging function only if needed. */
|
||||
#define logOtherDebug(facility, ...) (needsLog(lf_other, ll_debug) ? logWrite(facility, ll_debug, __VA_ARGS__) : void(0))
|
||||
|
||||
#endif // LIBUTILS_LOG_H_
|
||||
|
||||
Reference in New Issue
Block a user