0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Status.cc
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #include <Common/Compat.h>
28 
29 #include "Status.h"
30 
31 #include <Common/Serialization.h>
32 #include <Common/String.h>
33 
34 #include <boost/algorithm/string.hpp>
35 
36 using namespace Hypertable;
37 using namespace std;
38 
39 const char *Status::code_to_string(Code code) {
40  switch (code) {
41  case Code::OK:
42  return "OK";
43  case Code::WARNING:
44  return "WARNING";
45  case Code::CRITICAL:
46  return "CRITICAL";
47  default:
48  break;
49  }
50  return "UNKNOWN";
51 }
52 
54  boost::trim_if(str, boost::is_any_of("'\""));
55  if (str == "OK")
56  return Code::OK;
57  else if (str == "WARNING")
58  return Code::WARNING;
59  else if (str == "CRITICAL")
60  return Code::CRITICAL;
61  else if (str == "UNKNOWN")
62  return Code::UNKNOWN;
63  HT_THROWF(Error::INVALID_ARGUMENT, "status code string '%s'", str.c_str());
64 }
65 
66 uint8_t Status::encoding_version() const {
67  return 1;
68 }
69 
71  return 4 + Serialization::encoded_length_vstr(m_text);;
72 }
73 
80 void Status::encode_internal(uint8_t **bufp) const {
81  Serialization::encode_i32(bufp, static_cast<int32_t>(m_code));
82  Serialization::encode_vstr(bufp, m_text);
83 }
84 
85 void Status::decode_internal(uint8_t version, const uint8_t **bufp,
86  size_t *remainp) {
87  lock_guard<mutex> lock(m_mutex);
88  m_code = static_cast<Code>(Serialization::decode_i32(bufp, remainp));
89  m_text = Serialization::decode_vstr(bufp, remainp);
90 }
91 
92 std::string Status::format_output_line(const std::string &service) {
93  lock_guard<mutex> lock(m_mutex);
94  if (m_code == Code::OK && m_text.empty())
95  return format("%s OK", service.c_str());
96  return format("%s %s - %s", service.c_str(), code_to_string(m_code), m_text.c_str());
97 }
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
Declarations for Status.
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
Code
Enumeration for status codes.
Definition: Status.h:47
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
static const char * code_to_string(Code code)
Definition: Status.cc:39
void encode_internal(uint8_t **bufp) const override
Writes serialized representation of object to a buffer.
Definition: Status.cc:80
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
uint8_t encoding_version() const override
Returns encoding version.
Definition: Status.cc:66
Functions to serialize/deserialize primitives to/from a memory buffer.
void encode_vstr(uint8_t **bufp, const void *buf, size_t len)
Encode a buffer as variable length string (vint64, data, null)
Hypertable definitions
size_t encoded_length_internal() const override
Returns internal serialized length.
Definition: Status.cc:70
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
static Code string_to_code(std::string str)
Definition: Status.cc:53
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Reads serialized representation of object from a buffer.
Definition: Status.cc:85
A String class based on std::string.
std::string format_output_line(const std::string &service)
Formats a Nagios-style output line.
Definition: Status.cc:92
const char * code_to_string(int var_code)
Converts variable code to variable string.