1300906Sasomers/*-
2300906Sasomers * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation
3300906Sasomers * All rights reserved.
4300906Sasomers *
5300906Sasomers * Redistribution and use in source and binary forms, with or without
6300906Sasomers * modification, are permitted provided that the following conditions
7300906Sasomers * are met:
8300906Sasomers * 1. Redistributions of source code must retain the above copyright
9300906Sasomers *    notice, this list of conditions, and the following disclaimer,
10300906Sasomers *    without modification.
11300906Sasomers * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12300906Sasomers *    substantially similar to the "NO WARRANTY" disclaimer below
13300906Sasomers *    ("Disclaimer") and any redistribution must be conditioned upon
14300906Sasomers *    including a substantially similar Disclaimer requirement for further
15300906Sasomers *    binary redistribution.
16300906Sasomers *
17300906Sasomers * NO WARRANTY
18300906Sasomers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19300906Sasomers * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20300906Sasomers * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21300906Sasomers * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22300906Sasomers * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23300906Sasomers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24300906Sasomers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25300906Sasomers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26300906Sasomers * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27300906Sasomers * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28300906Sasomers * POSSIBILITY OF SUCH DAMAGES.
29300906Sasomers *
30300906Sasomers * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31300906Sasomers */
32300906Sasomers
33300906Sasomers/**
34300906Sasomers * \file exception.cc
35300906Sasomers */
36300906Sasomers#include <sys/cdefs.h>
37300906Sasomers
38300906Sasomers#include <syslog.h>
39300906Sasomers
40300906Sasomers#include <cstdio>
41300906Sasomers#include <cstdarg>
42300906Sasomers#include <sstream>
43300906Sasomers#include <string>
44300906Sasomers
45300906Sasomers#include "exception.h"
46300906Sasomers
47300906Sasomers__FBSDID("$FreeBSD$");
48300906Sasomers
49300906Sasomers/*============================ Namespace Control =============================*/
50300906Sasomersusing std::string;
51300906Sasomersusing std::stringstream;
52300906Sasomersusing std::endl;
53300906Sasomersnamespace DevdCtl
54300906Sasomers{
55300906Sasomers
56300906Sasomers/*=========================== Class Implementations ==========================*/
57300906Sasomers/*--------------------------------- Exception --------------------------------*/
58300906Sasomersvoid
59300906SasomersException::FormatLog(const char *fmt, va_list ap)
60300906Sasomers{
61300906Sasomers	char buf[256];
62300906Sasomers
63300906Sasomers	vsnprintf(buf, sizeof(buf), fmt, ap);
64300906Sasomers	m_log = buf;
65300906Sasomers}
66300906Sasomers
67300906SasomersException::Exception(const char *fmt, ...)
68300906Sasomers{
69300906Sasomers	va_list ap;
70300906Sasomers
71300906Sasomers	va_start(ap, fmt);
72300906Sasomers	FormatLog(fmt, ap);
73300906Sasomers	va_end(ap);
74300906Sasomers}
75300906Sasomers
76300906SasomersException::Exception()
77300906Sasomers{
78300906Sasomers}
79300906Sasomers
80300906Sasomersvoid
81300906SasomersException::Log() const
82300906Sasomers{
83300906Sasomers	syslog(LOG_ERR, "%s", m_log.c_str());
84300906Sasomers}
85300906Sasomers
86300906Sasomers/*------------------------------ ParseException ------------------------------*/
87300906Sasomers//- ParseException Inline Public Methods ---------------------------------------
88300906SasomersParseException::ParseException(Type type, const std::string &parsedBuffer,
89300906Sasomers			       size_t offset)
90300906Sasomers : Exception(),
91300906Sasomers   m_type(type),
92300906Sasomers   m_parsedBuffer(parsedBuffer),
93300906Sasomers   m_offset(offset)
94300906Sasomers{
95300906Sasomers        stringstream logstream;
96300906Sasomers
97300906Sasomers        logstream << "Parsing ";
98300906Sasomers
99300906Sasomers        switch (Type()) {
100300906Sasomers        case INVALID_FORMAT:
101300906Sasomers                logstream << "invalid format ";
102300906Sasomers                break;
103300906Sasomers        case DISCARDED_EVENT_TYPE:
104300906Sasomers                logstream << "discarded event ";
105300906Sasomers                break;
106300906Sasomers        case UNKNOWN_EVENT_TYPE:
107300906Sasomers                logstream << "unknown event ";
108300906Sasomers                break;
109300906Sasomers        default:
110300906Sasomers                break;
111300906Sasomers        }
112300906Sasomers        logstream << "exception on buffer: \'";
113300906Sasomers        if (GetOffset() == 0) {
114300906Sasomers                logstream << m_parsedBuffer << '\'' << endl;
115300906Sasomers        } else {
116300906Sasomers                string markedBuffer(m_parsedBuffer);
117300906Sasomers
118300906Sasomers                markedBuffer.insert(GetOffset(), "<HERE-->");
119300906Sasomers                logstream << markedBuffer << '\'' << endl;
120300906Sasomers        }
121300906Sasomers
122300906Sasomers	GetString() = logstream.str();
123300906Sasomers}
124300906Sasomers
125300906Sasomers} // namespace DevdCtl
126