Commit 5c50ab77 authored by Bob Obara's avatar Bob Obara

ENH:Added Logger Class and Test

parent 7ae31465
......@@ -6,8 +6,11 @@ smtk_source_group(attribute)
add_subdirectory(model)
smtk_source_group(model)
set(smtk_headers ${attributeHeaders} ${modelHeaders})
set(smtk_srcs ${attributeSrcs} ${modelSrcs})
add_subdirectory(util)
smtk_source_group(util)
set(smtk_headers ${attributeHeaders} ${modelHeaders} ${utilHeaders})
set(smtk_srcs ${attributeSrcs} ${modelSrcs} ${utilSrcs})
add_library(SMTKCore ${smtk_srcs})
smtk_export_header(SMTKCore SMTKCoreExports.h)
......
project(SMTK_Util)
# set up sources to build
set(utilSrcs
Logger.cxx
)
set(utilHeaders
Logger.h
)
#install the headers
smtk_public_headers(${utilHeaders})
if(SMTK_ENABLE_TESTING)
add_subdirectory(Testing)
endif(SMTK_ENABLE_TESTING)
/*=========================================================================
Copyright (c) 1998-2013 Kitware Inc. 28 Corporate Drive,
Clifton Park, NY, 12065, USA.
All rights reserved. No part of this software may be reproduced, distributed,
or modified, in any form or by any means, without permission in writing from
Kitware Inc.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
#include "smtk/util/Logger.h"
using namespace smtk::util;
//----------------------------------------------------------------------------
void Logger::addRecord(Severity s, const std::string &m,
const std::string &fname,
unsigned int line)
{
if ((s == Logger::ERROR) || (s == Logger::FATAL))
{
this->m_hasErrors = true;
}
this->m_records.push_back(Record(s, m, fname, line));
}
//----------------------------------------------------------------------------
void Logger::reset()
{
this->m_hasErrors = false;
this->m_records.empty();
}
//----------------------------------------------------------------------------
std::string Logger::severityAsString(Severity s)
{
switch (s)
{
case DEBUG:
return "DEBUG";
case INFO:
return "INFO";
case WARNING:
return "WARNING";
case ERROR:
return "ERROR";
case FATAL:
return "FATAL";
default:
return "UNKNOWN";
}
return "UNKNOWN";
}
/*=========================================================================
Copyright (c) 1998-2013 Kitware Inc. 28 Corporate Drive,
Clifton Park, NY, 12065, USA.
All rights reserved. No part of this software may be reproduced, distributed,
or modified, in any form or by any means, without permission in writing from
Kitware Inc.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
// .NAME Logger.h -
// .SECTION Description
// .SECTION See Also
#ifndef __smtk_util_Logger_h
#define __smtk_util_Logger_h
#include "smtk/SMTKCoreExports.h"
#include <string>
#include <sstream>
#include <vector>
#define smtkErrorMacro(logger, x) do { \
std::stringstream s1; \
s1 << x << std::endl; \
logger.addRecord(smtk::util::Logger::ERROR, \
s1.str(), __FILE__, __LINE__); \
} while (0)
#define smtkWarningMacro(logger, x) do { \
std::stringstream s1; \
s1 << x << std::endl; \
logger.addRecord(smtk::util::Logger::WARNING, \
s1.str(), __FILE__, __LINE__); \
} while (0)
#define smtkDebugMacro(logger, x) do { \
std::stringstream s1; \
s1 << x << std::endl; \
logger.addRecord(smtk::util::Logger::DEBUG, \
s1.str(), __FILE__, __LINE__); \
} while (0)
namespace smtk
{
namespace util
{
class SMTKCORE_EXPORT Logger
{
public:
enum Severity
{DEBUG, INFO, WARNING, ERROR, FATAL};
struct Record
{
Severity severity;
std::string message;
std::string fileName;
unsigned int lineNumber;
Record(Severity s, const std::string &m,
const std::string &f="", unsigned int l=0):
severity(s), message(m), fileName(f), lineNumber(l) {}
Record():
severity(INFO), lineNumber(0) {}
};
Logger(): m_hasErrors(false) {}
std::size_t numberOfRecords() const
{return this->m_records.size();}
bool hasErrors() const
{return this->m_hasErrors;}
void addRecord(Severity s, const std::string &m,
const std::string &fname="",
unsigned int line=0);
const Record &record(int i) const
{return this->m_records[i];}
void reset();
static std::string severityAsString(Severity s);
protected:
std::vector<Record> m_records;
bool m_hasErrors;
private:
};
};
};
#endif /* __smtk_util_Logger_h */
add_executable(loggerTest loggerTest.cxx)
target_link_libraries(loggerTest SMTKCore)
add_test(loggerTest ${EXECUTABLE_OUTPUT_PATH}/loggerTest)
/*=========================================================================
Copyright (c) 1998-2013 Kitware Inc. 28 Corporate Drive,
Clifton Park, NY, 12065, USA.
All rights reserved. No part of this software may be reproduced, distributed,
or modified, in any form or by any means, without permission in writing from
Kitware Inc.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
// .NAME Logger.h -
// .SECTION Description
// .SECTION See Also
#include "smtk/util/Logger.h"
#include <iostream>
int main()
{
smtk::util::Logger logger;
smtkErrorMacro(logger, "this is an error no = " << 45 << " ERROR!");
smtkWarningMacro(logger, "this is a warning no = " << 10.1234 << " WARNING!");
smtkDebugMacro(logger, "this is a Debug no = " << 1 << " DEBUG!");
logger.addRecord(smtk::util::Logger::INFO, "Sample Info String\n");
int i, n = logger.numberOfRecords();
if (n != 4)
{
std::cerr << "Wrong number of records! Got " << n << " Should be 4!\n";
return -1;
}
smtk::util::Logger::Record r;
for (i = 0; i < n; i++)
{
r = logger.record(i);
std::cerr << " Record " << i << ": \n\tSeverity = "
<< smtk::util::Logger::severityAsString(r.severity)
<< "\n\tMessage = " << r.message
<< "\tFile = " << r.fileName << "\n\tLine = "
<< r.lineNumber << std::endl;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment