Browse Source

adding log output with LOG4CPLUS macros

tempestpy_adaptions
gereon 12 years ago
parent
commit
3782122ac6
  1. 23
      src/parser/parser.cpp
  2. 19
      src/parser/readLabFile.cpp
  3. 34
      src/parser/readTraFile.cpp

23
src/parser/parser.cpp

@ -9,10 +9,15 @@
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <cstring>
#include "src/exceptions/file_IO_exception.h"
#include "src/exceptions/wrong_file_format.h"
#include "log4cplus/logger.h"
#include "log4cplus/loggingmacros.h"
extern log4cplus::Logger logger;
/*!
* Calls strtol() internally and checks if the new pointer is different
* from the original one, i.e. if str != *end. If they are the same, a
@ -24,7 +29,12 @@
uint_fast64_t mrmc::parser::checked_strtol(const char* str, char** end)
{
uint_fast64_t res = strtol(str, end, 10);
if (str == *end) throw mrmc::exceptions::wrong_file_format();
if (str == *end)
{
LOG4CPLUS_ERROR(logger, "Error while parsing integer. Next input token is not a number.");
LOG4CPLUS_ERROR(logger, "\tUpcoming input is: \"" << std::string(str, 0, 16) << "\"");
throw mrmc::exceptions::wrong_file_format();
}
return res;
}
@ -55,12 +65,14 @@ mrmc::parser::MappedFile::MappedFile(const char* filename)
*/
if (stat64(filename, &(this->st)) != 0)
{
LOG4CPLUS_ERROR(logger, "Error in stat(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in stat()");
}
this->file = open(filename, O_RDONLY);
if (this->file < 0)
{
LOG4CPLUS_ERROR(logger, "Error in open(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in open()");
}
@ -68,6 +80,7 @@ mrmc::parser::MappedFile::MappedFile(const char* filename)
if (this->data == (char*)-1)
{
close(this->file);
LOG4CPLUS_ERROR(logger, "Error in mmap(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in mmap()");
}
this->dataend = this->data + this->st.st_size;
@ -78,20 +91,23 @@ mrmc::parser::MappedFile::MappedFile(const char* filename)
*/
if (_stat64(filename, &(this->st)) != 0)
{
LOG4CPLUS_ERROR(logger, "Error in _stat(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in stat()");
}
this->file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (this->file == INVALID_HANDLE_VALUE)
{
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFile()");
LOG4CPLUS_ERROR(logger, "Error in CreateFileA(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileA()");
}
this->mapping = CreateFileMappingA(this->file, NULL, PAGE_READONLY, (DWORD)(st.st_size >> 32), (DWORD)st.st_size, NULL);
if (this->mapping == NULL)
{
CloseHandle(this->file);
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileMapping()");
LOG4CPLUS_ERROR(logger, "Error in CreateFileMappingA(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileMappingA()");
}
this->data = static_cast<char*>(MapViewOfFile(this->mapping, FILE_MAP_READ, 0, 0, this->st.st_size));
@ -99,6 +115,7 @@ mrmc::parser::MappedFile::MappedFile(const char* filename)
{
CloseHandle(this->mapping);
CloseHandle(this->file);
LOG4CPLUS_ERROR(logger, "Error in MapViewOfFile(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in MapViewOfFile()");
}
this->dataend = this->data + this->st.st_size;

19
src/parser/readLabFile.cpp

@ -20,13 +20,18 @@
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <locale.h>
#if defined LINUX || defined MACOSX
#include <sys/mman.h>
#elif defined WINDOWS
#define strncpy strncpy_s
#define strncpy strncpy_s
#endif
#include <fcntl.h>
#include <locale.h>
#include "log4cplus/logger.h"
#include "log4cplus/loggingmacros.h"
extern log4cplus::Logger logger;
namespace mrmc {
namespace parser {
@ -88,7 +93,13 @@ mrmc::models::AtomicPropositionsLabeling * readLabFile(uint_fast64_t node_count,
/*
* If #DECLARATION or #END were not found, the file format is wrong
*/
if (! (foundDecl && foundEnd)) throw mrmc::exceptions::wrong_file_format();
if (! (foundDecl && foundEnd))
{
LOG4CPLUS_ERROR(logger, "Wrong file format in (" << filename << "). File header is corrupted.");
if (! foundDecl) LOG4CPLUS_ERROR(logger, "\tDid not find #DECLARATION token.");
if (! foundEnd) LOG4CPLUS_ERROR(logger, "\tDid not find #END token.");
throw mrmc::exceptions::wrong_file_format();
}
}
/*

34
src/parser/readTraFile.cpp

@ -26,6 +26,10 @@
#include <fcntl.h>
#include <locale.h>
#include "log4cplus/logger.h"
#include "log4cplus/loggingmacros.h"
extern log4cplus::Logger logger;
namespace mrmc {
namespace parser{
@ -51,11 +55,19 @@ static uint_fast32_t makeFirstPass(char* buf, uint_fast32_t &maxnode)
/*
* check file header and extract number of transitions
*/
if (strncmp(buf, "STATES ", 7) != 0) return 0;
if (strncmp(buf, "STATES ", 7) != 0)
{
LOG4CPLUS_ERROR(logger, "Error: expected \"STATES\" but got \"" << std::string(buf, 0, 16) << "\".");
return 0;
}
buf += 7; // skip "STATES "
if (strtol(buf, &buf, 10) == 0) return 0;
buf = skipWS(buf);
if (strncmp(buf, "TRANSITIONS ", 12) != 0) return 0;
if (strncmp(buf, "TRANSITIONS ", 12) != 0)
{
LOG4CPLUS_ERROR(logger, "Error: expected \"TRANSITIONS\" but got \"" << std::string(buf, 0, 16) << "\".");
return 0;
}
buf += 12; // skip "TRANSITIONS "
if ((non_zero = strtol(buf, &buf, 10)) == 0) return 0;
@ -120,7 +132,11 @@ mrmc::storage::SquareSparseMatrix<double> * readTraFile(const char * filename) {
/*
* if first pass returned zero, the file format was wrong
*/
if (non_zero == 0) throw mrmc::exceptions::wrong_file_format();
if (non_zero == 0)
{
LOG4CPLUS_ERROR(logger, "Error while parsing " << filename << ": erroneous file format.");
throw mrmc::exceptions::wrong_file_format();
}
/*
* perform second pass
@ -144,7 +160,11 @@ mrmc::storage::SquareSparseMatrix<double> * readTraFile(const char * filename) {
* non-zero elements has to be specified (which is non_zero, computed by make_first_pass)
*/
sp = new mrmc::storage::SquareSparseMatrix<double>(maxnode + 1);
if (sp == NULL) throw std::bad_alloc();
if (sp == NULL)
{
LOG4CPLUS_ERROR(logger, "Could not create matrix of size " << (maxnode+1) << " x " << (maxnode+1) << ".");
throw std::bad_alloc();
}
sp->initialize(non_zero);
uint_fast64_t row, col;
@ -165,7 +185,11 @@ mrmc::storage::SquareSparseMatrix<double> * readTraFile(const char * filename) {
/*
* only values in (0, 1] are meaningful
*/
if ((val <= 0.0) || (val > 1.0)) throw mrmc::exceptions::wrong_file_format();
if ((val <= 0.0) || (val > 1.0))
{
LOG4CPLUS_ERROR(logger, "Found transition probability of " << val << ", but we think probabilities should be from (0,1].");
throw mrmc::exceptions::wrong_file_format();
}
sp->addNextValue(row,col,val);
buf = skipWS(buf);
}

Loading…
Cancel
Save