add support for individual log level per facility

This commit is contained in:
john30
2017-02-19 00:42:55 +01:00
parent aaf46b4ac0
commit cb9b7fc3ac
5 changed files with 162 additions and 84 deletions
+56 -7
View File
@@ -103,6 +103,10 @@ static struct options opt = {
"/var/" PACKAGE "/html", // htmlPath "/var/" PACKAGE "/html", // htmlPath
PACKAGE_LOGFILE, // logFile PACKAGE_LOGFILE, // logFile
-1, // logAreas
ll_COUNT, // logLevel
false, // multiLog
false, // logRaw false, // logRaw
PACKAGE_LOGFILE, // logRawFile PACKAGE_LOGFILE, // logRawFile
100, // logRawSize 100, // logRawSize
@@ -141,7 +145,8 @@ static const char argpdoc[] =
#define O_LOCAL (O_PIDFIL+1) #define O_LOCAL (O_PIDFIL+1)
#define O_HTTPPT (O_LOCAL+1) #define O_HTTPPT (O_LOCAL+1)
#define O_HTMLPA (O_HTTPPT+1) #define O_HTMLPA (O_HTTPPT+1)
#define O_LOGARE (O_HTMLPA+1) #define O_LOG (O_HTMLPA+1)
#define O_LOGARE (O_LOG+1)
#define O_LOGLEV (O_LOGARE+1) #define O_LOGLEV (O_LOGARE+1)
#define O_RAW (O_LOGLEV+1) #define O_RAW (O_LOGLEV+1)
#define O_RAWFIL (O_RAW+1) #define O_RAWFIL (O_RAW+1)
@@ -191,10 +196,12 @@ static const struct argp_option argpoptions[] = {
{NULL, 0, NULL, 0, "Log options:", 5 }, {NULL, 0, NULL, 0, "Log options:", 5 },
{"logfile", 'l', "FILE", 0, "Write log to FILE (only for daemon) [" PACKAGE_LOGFILE "]", 0 }, {"logfile", 'l', "FILE", 0, "Write log to FILE (only for daemon) [" PACKAGE_LOGFILE "]", 0 },
{"logareas", O_LOGARE, "AREAS", 0, "Only write log for matching AREA(S): main,network,bus,update,all " {"log", O_LOG, "AREAS LEVEL", 0, "Only write log for matching AREA(S) below or equal to LEVEL"
"[all]", 0 }, " (alternative to --logareas/--logevel, may be used multiple times) [all notice]", 0 },
{"loglevel", O_LOGLEV, "LEVEL", 0, "Only write log below or equal to LEVEL: error/notice/info/debug " {"logareas", O_LOGARE, "AREAS", 0, "Only write log for matching AREA(S): main|network|bus|update|all"
"[notice]", 0 }, " [all]", 0 },
{"loglevel", O_LOGLEV, "LEVEL", 0, "Only write log below or equal to LEVEL: error|notice|info|debug"
" [notice]", 0 },
{NULL, 0, NULL, 0, "Raw logging options:", 6 }, {NULL, 0, NULL, 0, "Raw logging options:", 6 },
{"lograwdata", O_RAW, NULL, 0, "Log each received/sent byte on the bus", 0 }, {"lograwdata", O_RAW, NULL, 0, "Log each received/sent byte on the bus", 0 },
@@ -431,17 +438,53 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) {
} }
opt->logFile = arg; opt->logFile = arg;
break; break;
case O_LOG: // --log=area(s) level
{
char* pos = strchr(arg, ' ');
if (pos == NULL) {
argp_error(state, "invalid log");
return EINVAL;
}
*pos = 0;
int facilities = parseLogFacilities(arg);
if (facilities == -1) {
argp_error(state, "invalid log: areas");
return EINVAL;
}
LogLevel level = parseLogLevel(pos + 1);
if (level == ll_COUNT) {
argp_error(state, "invalid log: level");
return EINVAL;
}
if (opt->logAreas != -1 || opt->logLevel != ll_COUNT) {
argp_error(state, "invalid log (combined with logareas or loglevel)");
return EINVAL;
}
setFacilitiesLogLevel(facilities, level);
opt->multiLog = true;
}
break;
case O_LOGARE: // --logareas=all case O_LOGARE: // --logareas=all
if (!setLogFacilities(arg)) { opt->logAreas = parseLogFacilities(arg);
if (opt->logAreas == -1) {
argp_error(state, "invalid logareas"); argp_error(state, "invalid logareas");
return EINVAL; return EINVAL;
} }
if (opt->multiLog) {
argp_error(state, "invalid logareas (combined with log)");
return EINVAL;
}
break; break;
case O_LOGLEV: // --loglevel=notice case O_LOGLEV: // --loglevel=notice
if (!setLogLevel(arg)) { opt->logLevel = parseLogLevel(arg);
if (opt->logLevel == ll_COUNT) {
argp_error(state, "invalid loglevel"); argp_error(state, "invalid loglevel");
return EINVAL; return EINVAL;
} }
if (opt->multiLog) {
argp_error(state, "invalid loglevel (combined with log)");
return EINVAL;
}
break; break;
// Raw logging options: // Raw logging options:
@@ -1015,11 +1058,17 @@ int main(int argc, char* argv[]) {
struct argp aargp = { argpoptions, parse_opt, NULL, argpdoc, datahandler_getargs(), NULL, NULL }; struct argp aargp = { argpoptions, parse_opt, NULL, argpdoc, datahandler_getargs(), NULL, NULL };
int arg_index = -1; int arg_index = -1;
setenv("ARGP_HELP_FMT", "no-dup-args-note", 0); setenv("ARGP_HELP_FMT", "no-dup-args-note", 0);
if (argp_parse(&aargp, argc, argv, ARGP_IN_ORDER, &arg_index, &opt) != 0) { if (argp_parse(&aargp, argc, argv, ARGP_IN_ORDER, &arg_index, &opt) != 0) {
logError(lf_main, "invalid arguments"); logError(lf_main, "invalid arguments");
return EINVAL; return EINVAL;
} }
if (opt.logAreas != -1 || opt.logLevel != ll_COUNT) {
setFacilitiesLogLevel(LF_ALL, ll_none);
setFacilitiesLogLevel(opt.logAreas, opt.logLevel);
}
s_messageMap = new MessageMap(opt.checkConfig && opt.scanConfig && arg_index >= argc); s_messageMap = new MessageMap(opt.checkConfig && opt.scanConfig && arg_index >= argc);
if (opt.checkConfig) { if (opt.checkConfig) {
logNotice(lf_main, PACKAGE_STRING "." REVISION " performing configuration check..."); logNotice(lf_main, PACKAGE_STRING "." REVISION " performing configuration check...");
+4
View File
@@ -24,6 +24,7 @@
#include "lib/ebus/data.h" #include "lib/ebus/data.h"
#include "lib/ebus/message.h" #include "lib/ebus/message.h"
#include "lib/ebus/result.h" #include "lib/ebus/result.h"
#include "lib/utils/log.h"
/** \file main.h /** \file main.h
* The main entry method doing all the startup handling. * The main entry method doing all the startup handling.
@@ -68,6 +69,9 @@ struct options {
const char* htmlPath; //!< path for HTML files served by the HTTP port [/var/ebusd/html] const char* htmlPath; //!< path for HTML files served by the HTTP port [/var/ebusd/html]
const char* logFile; //!< log file name [/var/log/ebusd.log] const char* logFile; //!< log file name [/var/log/ebusd.log]
int logAreas; //!< log areas [all]
LogLevel logLevel; //!< log level [notice]
bool multiLog; //!< multiple log levels adjusted with --log=...
bool logRaw; //!< raw log each received/sent byte on the bus bool logRaw; //!< raw log each received/sent byte on the bus
const char* logRawFile; //!< name of raw log file [/var/log/ebusd.log] const char* logRawFile; //!< name of raw log file [/var/log/ebusd.log]
+10 -16
View File
@@ -1295,32 +1295,26 @@ string MainLoop::executeScan(vector<string> &args, string levels) {
string MainLoop::executeLog(vector<string> &args) { string MainLoop::executeLog(vector<string> &args) {
if (args.size() == 1) { if (args.size() == 1) {
ostringstream ret; ostringstream ret;
char str[48]; for (int val = 0; val < lf_COUNT; val++) {
if (getLogFacilities(str)) { LogFacility facility = (LogFacility)val;
ret << str << ' '; ret << getLogFacilityStr(facility) << ": " << getLogLevelStr(getFacilityLogLevel(facility)) << "\n";
} }
ret << getLogLevel();
return ret.str(); return ret.str();
} }
bool result; if (args.size() != 3) {
// old format: log areas AREA[,AREA]*, log level LEVEL
if ((args.size() == 3 || args.size() == 2) && strcasecmp(args[1].c_str(), "AREAS") == 0) {
result = setLogFacilities(args.size() == 3 ? args[2].c_str() : "");
} else if (args.size() == 3 && strcasecmp(args[1].c_str(), "LEVEL") == 0) {
result = setLogLevel(args[2].c_str());
} else if (args.size() == 2) {
result = setLogLevel(args[1].c_str()) || setLogFacilities(args[1].c_str());
} else if (args.size() == 3) {
result = setLogFacilities(args[1].c_str()) && setLogLevel(args[2].c_str());
} else {
return "usage: log [AREA[,AREA]*] [LEVEL]\n" return "usage: log [AREA[,AREA]*] [LEVEL]\n"
" Set log area(s) and/or log level or get current settings.\n" " Set log area(s) and/or log level or get current settings.\n"
" AREA log area to include (main|network|bus|update|all)\n" " AREA log area to include (main|network|bus|update|all)\n"
" LEVEL log level to set (error|notice|info|debug)"; " LEVEL log level to set (error|notice|info|debug)";
} }
if (result) { int facilities = parseLogFacilities(args[1].c_str());
LogLevel level = parseLogLevel(args[2].c_str());
if (facilities != -1 && level != ll_COUNT) {
if (setFacilitiesLogLevel(facilities, level)) {
return getResultCode(RESULT_OK); return getResultCode(RESULT_OK);
} }
return "same";
}
return getResultCode(RESULT_ERR_INVALID_ARG); return getResultCode(RESULT_ERR_INVALID_ARG);
} }
+47 -39
View File
@@ -46,16 +46,28 @@ static const char* levelNames[] = {
NULL NULL
}; };
/** the bit combination of currently active log facilities (1 << @a LogFacility). */ /** the current log level by log facility. */
static int s_logFacilites = LF_ALL; static LogLevel s_facilityLogLevel[] = { ll_notice, ll_notice, ll_notice, ll_notice, ll_notice, };
/** the current log level. */
static LogLevel s_logLevel = ll_notice;
/** the current log FILE. */ /** the current log FILE. */
static FILE* s_logFile = stdout; static FILE* s_logFile = stdout;
bool setLogFacilities(const char* facilities) { LogFacility parseLogFacility(const char* facility) {
if (!facility) {
return lf_COUNT;
}
char *input = strdup(facility);
char *opt = reinterpret_cast<char*>(input), *value = NULL;
int val = getsubopt(&opt, (char *const *)facilityNames, &value);
if (val < 0 || val >= lf_COUNT || value || *opt) {
free(input);
return lf_COUNT;
}
free(input);
return (LogFacility)val;
}
int parseLogFacilities(const char* facilities) {
char *input = strdup(facilities); char *input = strdup(facilities);
char *opt = reinterpret_cast<char*>(input), *value = NULL; char *opt = reinterpret_cast<char*>(input), *value = NULL;
int newFacilites = 0; int newFacilites = 0;
@@ -63,7 +75,7 @@ bool setLogFacilities(const char* facilities) {
int val = getsubopt(&opt, (char *const *)facilityNames, &value); int val = getsubopt(&opt, (char *const *)facilityNames, &value);
if (val < 0 || val > lf_COUNT || value) { if (val < 0 || val > lf_COUNT || value) {
free(input); free(input);
return false; return -1;
} }
if (val == lf_COUNT) { if (val == lf_COUNT) {
newFacilites = LF_ALL; newFacilites = LF_ALL;
@@ -71,49 +83,46 @@ bool setLogFacilities(const char* facilities) {
newFacilites |= 1 << val; newFacilites |= 1 << val;
} }
} }
s_logFacilites = newFacilites;
free(input); free(input);
return true; return newFacilites;
} }
bool getLogFacilities(char* buffer) { LogLevel parseLogLevel(const char* level) {
if (s_logFacilites == LF_ALL) { if (!level) {
return snprintf(buffer, 48, "%s", facilityNames[lf_COUNT]) != 0; return ll_COUNT;
} }
*buffer = 0; // for strcat to work
bool found = false;
size_t len = 0;
for (int val = 0; val < lf_COUNT; val++) {
if (s_logFacilites&(1 << val)) {
if (found) {
len += snprintf(buffer+len, 48-len, ",");
}
found = true;
len += snprintf(buffer+len, 48-len, "%s", facilityNames[val]);
}
}
return true;
}
bool setLogLevel(const char* level) {
char *input = strdup(level); char *input = strdup(level);
char *opt = reinterpret_cast<char*>(input), *value = NULL; char *opt = reinterpret_cast<char*>(input), *value = NULL;
int newLevel = 0;
if (*opt) {
int val = getsubopt(&opt, (char *const *)levelNames, &value); int val = getsubopt(&opt, (char *const *)levelNames, &value);
if (val < 0 || val >= ll_COUNT || value || *opt) { if (val < 0 || val >= ll_COUNT || value || *opt) {
free(input); free(input);
return false; return ll_COUNT;
} }
newLevel = val;
}
s_logLevel = (LogLevel)newLevel;
free(input); free(input);
return true; return (LogLevel)val;
} }
const char* getLogLevel() { const char* getLogFacilityStr(LogFacility facility) {
return levelNames[s_logLevel]; return facilityNames[facility];
}
const char* getLogLevelStr(LogLevel level) {
return levelNames[level];
}
bool setFacilitiesLogLevel(int facilities, LogLevel level) {
bool changed = false;
for (int val = 0; val < lf_COUNT && facilities != 0; val++) {
if ((facilities & (1 << val)) != 0 && s_facilityLogLevel[(LogFacility)val] != level) {
s_facilityLogLevel[(LogFacility)val] = level;
changed = true;
}
}
return changed;
}
LogLevel getFacilityLogLevel(LogFacility facility) {
return s_facilityLogLevel[facility];
} }
bool setLogFile(const char* filename) { bool setLogFile(const char* filename) {
@@ -136,8 +145,7 @@ void closeLogFile() {
} }
bool needsLog(const LogFacility facility, const LogLevel level) { bool needsLog(const LogFacility facility, const LogLevel level) {
return ((s_logFacilites & (1 << facility)) != 0) return s_facilityLogLevel[facility] >= level;
&& (s_logLevel >= level);
} }
void logWrite(const char* facility, const char* level, const char* message, va_list ap) { void logWrite(const char* facility, const char* level, const char* message, va_list ap) {
+37 -14
View File
@@ -31,7 +31,7 @@ enum LogFacility {
lf_COUNT = 5 //!< number of available log facilities lf_COUNT = 5 //!< number of available log facilities
}; };
/** macro for enabling all log facilities. */ /** macro for all log facilities. */
#define LF_ALL ((1 << lf_main) | (1 << lf_network) | (1 << lf_bus) | (1 << lf_update) | (1 << lf_other)) #define LF_ALL ((1 << lf_main) | (1 << lf_network) | (1 << lf_bus) | (1 << lf_update) | (1 << lf_other))
/** the available log levels. */ /** the available log levels. */
@@ -45,31 +45,54 @@ enum LogLevel {
}; };
/** /**
* Set the log facilities from the string. * Parse the log facility from the string.
* @param facilities the string to parse the facilities from (separated by comma). * @param facility the string to parse the singular facility from.
* @return true on success, false on error. * @return the @a LogFacility, or @a lf_COUNT on error.
*/ */
bool setLogFacilities(const char* facilities); LogFacility parseLogFacility(const char* facility);
/** /**
* Get the log facilities. * Parse the log facilities from the string.
* @param buffer the buffer into which the facilities are written to (separated by comma, buffer needs to be at last 48 characters long). * @param facilities the string to parse the list of facilities from (separated by comma).
* @return true on success, false on error. * @return the @a LogFacility list as bit mask (1 << facility), or -1 on error.
*/ */
bool getLogFacilities(char* buffer); int parseLogFacilities(const char* facilities);
/**
* Get the log facility as string.
* @param level the @a LogFacility.
* @return the log facility as string.
*/
const char* getLogFacilityStr(LogFacility facility);
/** /**
* Parse the log level from the string. * Parse the log level from the string.
* @param level the level as string. * @param level the level as string.
* @return true on success, false on error. * @return the @a LogLevel, or @a ll_COUNT on error.
*/ */
bool setLogLevel(const char* level); LogLevel parseLogLevel(const char* level);
/** /**
* Get the log level. * Get the log level as string.
* @return the level as string. * @param level the @a LogLevel.
* @return the log level as string.
*/ */
const char* getLogLevel(); const char* getLogLevelStr(LogLevel level);
/**
* Set the log level for the specified facilities.
* @param facilities the log facilities as bit mask (1 << facility).
* @param level the @a LogLevel to set.
* @return true when a level was changed for a facility, false when no level was changed at all.
*/
bool setFacilitiesLogLevel(int facilities, LogLevel level);
/**
* Get the log level for the specified facility.
* @param facility the @a LogFacility.
* @return the @a LogLevel.
*/
LogLevel getFacilityLogLevel(LogFacility facility);
/** /**
* Set the log file to use. * Set the log file to use.