/*-1* Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions, and the following disclaimer,9* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* substantially similar to the "NO WARRANTY" disclaimer below12* ("Disclaimer") and any redistribution must be conditioned upon13* including a substantially similar Disclaimer requirement for further14* binary redistribution.15*16* NO WARRANTY17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR20* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,25* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING26* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGES.28*29* Authors: Justin T. Gibbs (Spectra Logic Corporation)30*/3132/**33* \file exception.cc34*/35#include <sys/cdefs.h>3637#include <syslog.h>3839#include <cstdio>40#include <cstdarg>41#include <sstream>42#include <string>4344#include "exception.h"45/*============================ Namespace Control =============================*/46using std::string;47using std::stringstream;48using std::endl;49namespace DevdCtl50{5152/*=========================== Class Implementations ==========================*/53/*--------------------------------- Exception --------------------------------*/54void55Exception::FormatLog(const char *fmt, va_list ap)56{57char buf[256];5859vsnprintf(buf, sizeof(buf), fmt, ap);60m_log = buf;61}6263Exception::Exception(const char *fmt, ...)64{65va_list ap;6667va_start(ap, fmt);68FormatLog(fmt, ap);69va_end(ap);70}7172Exception::Exception()73{74}7576void77Exception::Log() const78{79syslog(LOG_ERR, "%s", m_log.c_str());80}8182/*------------------------------ ParseException ------------------------------*/83//- ParseException Inline Public Methods ---------------------------------------84ParseException::ParseException(Type type, const std::string &parsedBuffer,85size_t offset)86: Exception(),87m_type(type),88m_parsedBuffer(parsedBuffer),89m_offset(offset)90{91stringstream logstream;9293logstream << "Parsing ";9495switch (Type()) {96case INVALID_FORMAT:97logstream << "invalid format ";98break;99case DISCARDED_EVENT_TYPE:100logstream << "discarded event ";101break;102case UNKNOWN_EVENT_TYPE:103logstream << "unknown event ";104break;105default:106break;107}108logstream << "exception on buffer: \'";109if (GetOffset() == 0) {110logstream << m_parsedBuffer << '\'' << endl;111} else {112string markedBuffer(m_parsedBuffer);113114markedBuffer.insert(GetOffset(), "<HERE-->");115logstream << markedBuffer << '\'' << endl;116}117118GetString() = logstream.str();119}120121} // namespace DevdCtl122123124