added getLogFacilities() and getLogLevel(), do not manipulate input string in setLog* methods

This commit is contained in:
john30
2016-04-30 16:23:55 +02:00
parent 7af79dd461
commit 650b68b9a2
2 changed files with 51 additions and 10 deletions
+38 -10
View File
@@ -22,6 +22,7 @@
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h>
#include "clock.h" #include "clock.h"
using namespace std; using namespace std;
@@ -57,38 +58,65 @@ static FILE* logFile = stdout;
bool setLogFacilities(const char* facilities) bool setLogFacilities(const char* facilities)
{ {
char *opt = (char*)facilities, *value = NULL; char *input = strdup(facilities);
char *opt = (char*)input, *value = NULL;
int newFacilites = 0; int newFacilites = 0;
while (*opt) { while (*opt) {
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);
return false; return false;
if (val == lf_COUNT) {
newFacilites = LF_ALL;
break;
} }
newFacilites |= 1<<val; if (val == lf_COUNT)
newFacilites = LF_ALL;
else
newFacilites |= 1<<val;
} }
logFacilites = newFacilites; logFacilites = newFacilites;
free(input);
return true;
}
bool getLogFacilities(char* buffer)
{
if (logFacilites==LF_ALL)
return strcpy(buffer, facilityNames[lf_COUNT]) != NULL;
*buffer = 0; // for strcat to work
bool found = false;
for (int val=0; val<lf_COUNT; val++) {
if (logFacilites&(1<<val)) {
if (found)
strcat(buffer, ",");
found = true;
strcat(buffer, facilityNames[val]);
}
}
return true; return true;
} }
bool setLogLevel(const char* level) bool setLogLevel(const char* level)
{ {
char *opt = (char*)level, *value = NULL; char *input = strdup(level);
char *opt = (char*)input, *value = NULL;
int newLevel = 0; int newLevel = 0;
if (*opt) { 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);
return false; return false;
}
newLevel = val; newLevel = val;
} }
logLevel = (LogLevel)newLevel; logLevel = (LogLevel)newLevel;
free(input);
return true; return true;
} }
const char* getLogLevel()
{
return levelNames[logLevel];
}
bool setLogFile(const char* filename) bool setLogFile(const char* filename)
{ {
FILE* newFile = fopen(filename, "a"); FILE* newFile = fopen(filename, "a");
+13
View File
@@ -50,6 +50,13 @@ enum LogLevel {
*/ */
bool setLogFacilities(const char* facilities); bool setLogFacilities(const char* facilities);
/**
* Get the log facilities.
* @param buffer the buffer into which the facilities are written to (separated by comma, buffer needs to be at last 32 characters long).
* @return true on success, false on error.
*/
bool getLogFacilities(char* buffer);
/** /**
* 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.
@@ -57,6 +64,12 @@ bool setLogFacilities(const char* facilities);
*/ */
bool setLogLevel(const char* level); bool setLogLevel(const char* level);
/**
* Get the log level.
* @return the level as string.
*/
const char* getLogLevel();
/** /**
* Set the log file to use. * Set the log file to use.
* @param filename the name of the log file to use. * @param filename the name of the log file to use.