Added logger which is implemented using standard EPICSv3 logging facilities.

This commit is contained in:
miha_vitorovic
2010-12-10 15:12:28 +01:00
parent 6bebb6594c
commit e40e183884
5 changed files with 143 additions and 1 deletions

View File

@@ -18,11 +18,15 @@ PROD_HOST += growingCircularBufferTest
growingCircularBufferTest_SRCS += growingCircularBufferTest.cpp
growingCircularBufferTest_LIBS += pvAccessUtils Com
PROD_HOST += inetAddressUtilsTest
inetAddressUtilsTest_SRCS += inetAddressUtilsTest.cpp
inetAddressUtilsTest_LIBS += pvAccessUtils Com
PROD_HOST += loggerTest
loggerTest_SRCS += loggerTest.cpp
loggerTest_LIBS += pvAccessUtils Com
include $(TOP)/configure/RULES
#----------------------------------------
# ADD RULES AFTER THIS LINE

View File

@@ -0,0 +1,28 @@
/*
* loggerTest.cpp
*
* Created on: Dec 10, 2010
* Author: Miha Vitorovic
*/
#include "logger.h"
#include <errlog.h>
#include <epicsExit.h>
#include <iostream>
using namespace epics::pvAccess;
using std::cout;
int main(int argc, char *argv[]) {
createFileLogger("loggerTest.log");
errlogSetSevToLog(errlogMinor);
errlogSevPrintf( errlogInfo, "This will not appear");
errlogSevPrintf( errlogMajor, "This is a test %d", 42);
errlogSevPrintf( errlogFatal, "This is another test %f", 3.14);
epicsExit(0);
}

View File

@@ -7,10 +7,12 @@ INC += wildcharMatcher.h
INC += arrayFIFO.h
INC += growingCircularBuffer.h
INC += inetAddressUtil.h
INC += logger.h
LIBSRCS += hexDump.cpp
LIBSRCS += wildcharMatcher.cpp
LIBSRCS += inetAddressUtil.cpp
LIBSRCS += logger.cpp
LIBRARY = pvAccessUtils
pvAccessUtils_LIBS += Com

View File

@@ -0,0 +1,77 @@
/*
* logger.cpp
*
* Created on: Dec 10, 2010
* Author: Miha Vitorovic
*/
#include "logger.h"
#include <noDefaultMethods.h>
#include <lock.h>
#include <pvType.h>
#include <epicsExit.h>
#include <errlog.h>
#include <fstream>
#include <iostream>
#include <time.h>
#include <cstring>
using namespace epics::pvData;
using std::ofstream;
using std::ios;
using std::endl;
namespace epics {
namespace pvAccess {
class FileLogger : public NoDefaultMethods {
public:
FileLogger(String name) {
logFile.open(name.data(), ios::app);
}
~FileLogger() {
logFile.close();
}
void logMessage(const char* message) {
time_t rawtime;
time(&rawtime);
char* timeStr = ctime(&rawtime);
timeStr[strlen(timeStr)-1]='\0'; // remove newline
logFile<<timeStr<<"\t"<<message; // the newline is added by the caller
}
private:
ofstream logFile;
};
static FileLogger* fileLogger = NULL;
static void errLogFileListener(void* pPrivate, const char *message) {
fileLogger->logMessage(message);
}
static void exitFileLoggerHandler(void* pPrivate) {
errlogFlush();
delete fileLogger;
}
void createFileLogger(String fname) {
static Mutex mutex = Mutex();
Lock xx(&mutex);
if(fileLogger==NULL) {
fileLogger = new FileLogger(fname);
errlogInit(2048);
errlogAddListener(errLogFileListener, NULL);
epicsAtExit(exitFileLoggerHandler, NULL);
}
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* logger.h
*
* Created on: Dec 10, 2010
* Author: Miha Vitorovic
*/
#ifndef LOGGER_H_
#define LOGGER_H_
#include <pvType.h>
using epics::pvData::String;
namespace epics {
namespace pvAccess {
/**
* Create a logger that will write to file indicated by the <tt>fname</tt>.
* After creation you are free to use standard EPICSv3 functions from
* <tt>errlog.h</tt>.
*
* @param[in] fname The file to write to. If the file exists, it
* is opened for append.
*/
void createFileLogger( String fname );
}
}
#endif /* LOGGER_H_ */