1/*- 2 * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * substantially similar to the "NO WARRANTY" disclaimer below 13 * ("Disclaimer") and any redistribution must be conditioned upon 14 * including a substantially similar Disclaimer requirement for further 15 * binary redistribution. 16 * 17 * NO WARRANTY 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGES. 29 * 30 * Authors: Justin T. Gibbs (Spectra Logic Corporation) 31 */ 32 33/** 34 * \file exception.cc 35 */ 36#include <sys/cdefs.h> 37 38#include <syslog.h> 39 40#include <cstdio> 41#include <cstdarg> 42#include <sstream> 43#include <string> 44 45#include "exception.h" 46/*============================ Namespace Control =============================*/ 47using std::string; 48using std::stringstream; 49using std::endl; 50namespace DevdCtl 51{ 52 53/*=========================== Class Implementations ==========================*/ 54/*--------------------------------- Exception --------------------------------*/ 55void 56Exception::FormatLog(const char *fmt, va_list ap) 57{ 58 char buf[256]; 59 60 vsnprintf(buf, sizeof(buf), fmt, ap); 61 m_log = buf; 62} 63 64Exception::Exception(const char *fmt, ...) 65{ 66 va_list ap; 67 68 va_start(ap, fmt); 69 FormatLog(fmt, ap); 70 va_end(ap); 71} 72 73Exception::Exception() 74{ 75} 76 77void 78Exception::Log() const 79{ 80 syslog(LOG_ERR, "%s", m_log.c_str()); 81} 82 83/*------------------------------ ParseException ------------------------------*/ 84//- ParseException Inline Public Methods --------------------------------------- 85ParseException::ParseException(Type type, const std::string &parsedBuffer, 86 size_t offset) 87 : Exception(), 88 m_type(type), 89 m_parsedBuffer(parsedBuffer), 90 m_offset(offset) 91{ 92 stringstream logstream; 93 94 logstream << "Parsing "; 95 96 switch (Type()) { 97 case INVALID_FORMAT: 98 logstream << "invalid format "; 99 break; 100 case DISCARDED_EVENT_TYPE: 101 logstream << "discarded event "; 102 break; 103 case UNKNOWN_EVENT_TYPE: 104 logstream << "unknown event "; 105 break; 106 default: 107 break; 108 } 109 logstream << "exception on buffer: \'"; 110 if (GetOffset() == 0) { 111 logstream << m_parsedBuffer << '\'' << endl; 112 } else { 113 string markedBuffer(m_parsedBuffer); 114 115 markedBuffer.insert(GetOffset(), "<HERE-->"); 116 logstream << markedBuffer << '\'' << endl; 117 } 118 119 GetString() = logstream.str(); 120} 121 122} // namespace DevdCtl 123