Navigation Menu

Skip to content

Commit

Permalink
Rename grnxx::Error to grnxx::Errno.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 28, 2013
1 parent 8d32cfc commit f5c87ec
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -32,7 +32,7 @@ test/test_backtrace
test/test_bytes
test/test_charset
test/test_duration
test/test_error
test/test_errno
test/test_exception
test/test_features
test/test_geo_point
Expand Down
4 changes: 2 additions & 2 deletions lib/grnxx/Makefile.am
Expand Up @@ -19,7 +19,7 @@ libgrnxx_la_SOURCES = \
broken_down_time.cpp \
charset.cpp \
duration.cpp \
error.cpp \
errno.cpp \
geo_point.cpp \
grnxx.cpp \
logger.cpp \
Expand All @@ -45,7 +45,7 @@ libgrnxx_include_HEADERS = \
bytes.hpp \
charset.hpp \
duration.hpp \
error.hpp \
errno.hpp \
exception.hpp \
features.hpp \
flags_impl.hpp \
Expand Down
53 changes: 35 additions & 18 deletions lib/grnxx/error.cpp → lib/grnxx/errno.cpp
@@ -1,5 +1,5 @@
/*
Copyright (C) 2012 Brazil, Inc.
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -15,12 +15,11 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grnxx/error.hpp"
#include "grnxx/errno.hpp"

#include <string.h>

#ifdef GRNXX_WINDOWS
# ifndef NOMINMAX
# define NOMINMAX
# endif // NOMINMAX
# include <windows.h>
#endif // GRNXX_WINDOWS

Expand All @@ -31,6 +30,8 @@
namespace grnxx {
namespace {

constexpr size_t MESSAGE_BUF_SIZE = 256;

#ifdef GRNXX_WINDOWS
class Freer {
public:
Expand All @@ -42,36 +43,52 @@ class Freer {

} // namespace

StringBuilder &Error::write_to(StringBuilder &builder) const {
StringBuilder &Errno::write_to(StringBuilder &builder) const {
switch (type_) {
case POSIX_ERROR: {
return builder << '(' << std::strerror(posix_error_) << ')';
case STANDARD_ERRNO: {
// Note that a string returned by ::strerror() may be modified by a
// subsequent call to ::perror() or ::strerror().
const char *message = "n/a";
#ifdef GRNXX_MSC
char message_buf[MESSAGE_BUF_SIZE];
if (::strerror_s(message_buf, MESSAGE_BUF_SIZE, standard_errno_) == 0) {
message = message_buf;
}
#elif defined(GRNXX_HAS_XSI_STRERROR)
char message_buf[MESSAGE_BUF_SIZE];
if (::strerror_r(standard_errno_, message_buf, MESSAGE_BUF_SIZE) == 0) {
message = message_buf;
}
#elif defined(GRNXX_HAS_GNU_STRERROR)
// ::strerror_r() may not use "message_buf" when an immutable error
// message is available.
char message_buf[MESSAGE_BUF_SIZE];
message = ::strerror_r(standard_errno_, message_buf, MESSAGE_BUF_SIZE);
#else // defined(GRNXX_HAS_GNU_STRERROR)
message = ::strerror(standard_errno_);
#endif // defined(GRNXX_HAS_GNU_STRERROR)
return builder << standard_errno_ << " (" << message << ')';
}
#ifdef GRNXX_WINDOWS
case WINDOWS_ERROR: {
case WINDOWS_ERRNO: {
char *message;
if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, windows_error_,
nullptr, windows_errno_,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&message),
0, nullptr) != 0) {
std::unique_ptr<char[], Freer> message_freer(message);
builder << '(' << message << ')';
builder << windows_errno_ << " (" << message << ')';
} else {
builder << "(n/a)";
builder << windows_errno << " (n/a)";
}
return builder;
}
#endif // GRNXX_WINDOWS
case GRNXX_ERROR: {
// TODO
// grnxx_error_
return builder << "(undefined)";
}
default: {
return builder << "(undefined)";
return builder << "n/a";
}
}
}
Expand Down
66 changes: 28 additions & 38 deletions lib/grnxx/error.hpp → lib/grnxx/errno.hpp
@@ -1,5 +1,5 @@
/*
Copyright (C) 2012 Brazil, Inc.
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -15,77 +15,67 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GRNXX_ERROR_HPP
#define GRNXX_ERROR_HPP
#ifndef GRNXX_ERRNO_HPP
#define GRNXX_ERRNO_HPP

#include "grnxx/features.hpp"

namespace grnxx {

class StringBuilder;

enum ErrorType {
POSIX_ERROR,
enum ErrnoType {
STANDARD_ERRNO,
#ifdef GRNXX_WINDOWS
WINDOWS_ERROR,
WINDOWS_ERRNO
#endif // GRNXX_WINDOWS
GRNXX_ERROR
};

enum ErrorCode {
// TODO
};

class Error {
class GRNXX_EXPORT Errno {
public:
// For errno.
explicit Error(int error_code)
: type_(POSIX_ERROR),
posix_error_(error_code) {}
explicit Errno(int error_code)
: type_(STANDARD_ERRNO),
standard_errno_(error_code) {}
#ifdef GRNXX_WINDOWS
// For DWORD returned by ::GetLastError().
explicit Error(unsigned long error_code)
: type_(WINDOWS_ERROR),
windows_error_(error_code) {}
// For DWORD returned by ::GetLastErrno().
explicit Errno(unsigned long error_code)
: type_(WINDOWS_ERRNO),
windows_errno_(error_code) {}
#endif // GRNXX_WINDOWS
explicit Error(ErrorCode error_code)
: type_(GRNXX_ERROR),
grnxx_error_(error_code) {}

ErrorType type() const {
// Return the errno type.
ErrnoType type() const {
return type_;
}
int posix_error() const {
return posix_error_;
// Return the standard errno.
int standard_errno() const {
return standard_errno_;
}
#ifdef GRNXX_WINDOWS
unsigned long windows_error() const {
return windows_error_;
// Return the windows errno.
unsigned long windows_errno() const {
return windows_errno_;
}
#endif // GRNXX_WINDOWS
ErrorCode grnxx_error() const {
return grnxx_error_;
}

// Write a human-readable error message to "builder".
StringBuilder &write_to(StringBuilder &builder) const;

private:
ErrorType type_;
ErrnoType type_;
union {
int posix_error_;
int standard_errno_;
#ifdef GRNXX_WINDOWS
unsigned long windows_error_;
unsigned long windows_errno_;
#endif // GRNXX_WINDOWS
ErrorCode grnxx_error_;
};

// Copyable.
};

inline StringBuilder &operator<<(StringBuilder &builder, const Error &error) {
inline StringBuilder &operator<<(StringBuilder &builder, const Errno &error) {
return error.write_to(builder);
}

} // namespace grnxx

#endif // GRNXX_ERROR_HPP
#endif // GRNXX_ERRNO_HPP
13 changes: 13 additions & 0 deletions lib/grnxx/features.hpp
Expand Up @@ -144,6 +144,12 @@

// Source features.

#ifdef _GNU_SOURCE
# define GRNXX_GNU_SOURCE _GNU_SOURCE
#else // _GNU_SOURCE
# define GRNXX_GNU_SOURCE 0
#endif // _GNU_SOURCE

#ifdef _POSIX_C_SOURCE
# define GRNXX_POSIX_C_SOURCE _POSIX_C_SOURCE
#else // _POSIX_C_SOURCE
Expand Down Expand Up @@ -182,6 +188,13 @@

// Available functions.

#if ((GRNXX_POSIX_C_SOURCE >= 20112L) || (GRNXX_XOPEN_SOURCE >= 600)) &&\
!GRNXX_GNU_SOURCE
# define GRNXX_HAS_XSI_STRERROR
#else // ((GRNXX_POSIX_C_SOURCE >= 20112L) || ...
# define GRNXX_HAS_GNU_STRERROR
#endif // ((GRNXX_POSIX_C_SOURCE >= 20112L) || ...

#if GRNXX_POSIX_C_SOURCE >= 199309L
# define GRNXX_HAS_NANOSLEEP
#endif // GRNXX_POSIX_C_SOURCE >= 199309L
Expand Down
4 changes: 2 additions & 2 deletions lib/grnxx/os.cpp
Expand Up @@ -21,7 +21,7 @@
#include <cstring>
#include <cerrno>

#include "grnxx/error.hpp"
#include "grnxx/errno.hpp"
#include "grnxx/lock.hpp"
#include "grnxx/logger.hpp"

Expand All @@ -48,7 +48,7 @@ char *OS::get_environment_variable(const char *name) {
size_t value_size;
if (::_dupenv_s(&value, &value_size, name) != 0) {
GRNXX_ERROR() << "failed to get environment variable: name = " << name
<< ": '::_dupenv_s' " << Error(errno);
<< ": '::_dupenv_s' " << Errno(errno);
return nullptr;
}
char * const result = new (std::nothrow) char[value_size + 1];
Expand Down
10 changes: 5 additions & 5 deletions lib/grnxx/storage/chunk-posix.cpp
Expand Up @@ -26,7 +26,7 @@
#include <memory>
#include <new>

#include "grnxx/error.hpp"
#include "grnxx/errno.hpp"
#include "grnxx/logger.hpp"
#include "grnxx/storage/file.hpp"

Expand All @@ -47,7 +47,7 @@ ChunkImpl::ChunkImpl()
ChunkImpl::~ChunkImpl() {
if (address_ != MAP_FAILED) {
if (::munmap(address_, static_cast<size_t>(size_)) != 0) {
GRNXX_ERROR() << "failed to unmap chunk: '::munmap' " << Error(errno);
GRNXX_ERROR() << "failed to unmap chunk: '::munmap' " << Errno(errno);
}
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ bool ChunkImpl::sync(uint64_t offset, uint64_t size) {
if (size > 0) {
if (::msync(static_cast<char *>(address_) + offset, size, MS_SYNC) != 0) {
GRNXX_ERROR() << "failed to sync chunk: offset = " << offset
<< ", size = " << size << ": '::msync' " << Error(errno);
<< ", size = " << size << ": '::msync' " << Errno(errno);
return false;
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset,
<< "file_path = " << file->path()
<< ", file_size = " << file_size
<< ", offset = " << offset << ", size = " << size
<< ", flags = " << flags << ": '::mmap' " << Error(errno);
<< ", flags = " << flags << ": '::mmap' " << Errno(errno);
return false;
}
return true;
Expand Down Expand Up @@ -176,7 +176,7 @@ bool ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) {
address_ = ::mmap(nullptr, size, protection_flags, mmap_flags, -1, 0);
if (address_ == MAP_FAILED) {
GRNXX_ERROR() << "failed to map anonymous chunk: size = " << size
<< ", flags = " << flags << ": '::mmap' " << Error(errno);
<< ", flags = " << flags << ": '::mmap' " << Errno(errno);
return false;
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/grnxx/storage/chunk-windows.cpp
Expand Up @@ -21,7 +21,7 @@

#include <new>

#include "grnxx/error.hpp"
#include "grnxx/errno.hpp"
#include "grnxx/logger.hpp"
#include "grnxx/storage/file.hpp"

Expand All @@ -38,13 +38,13 @@ ChunkImpl::~ChunkImpl() {
if (address_) {
if (!::UnmapViewOfFile(address_)) {
GRNXX_ERROR() << "failed to unmap chunk"
<< ": '::UnmapViewOfFile' " << Error(::GetLastError());
<< ": '::UnmapViewOfFile' " << Errno(::GetLastError());
}
}
if (handle_) {
if (!::CloseHandle(handle_)) {
GRNXX_ERROR() << "failed to close file mapping"
<< ": '::CloseHandle' " << Error(::GetLastError());
<< ": '::CloseHandle' " << Errno(::GetLastError());
}
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ bool ChunkImpl::sync(uint64_t offset, uint64_t size) {
if (!::FlushViewOfFile(static_cast<char *>(address_) + offset, size)) {
GRNXX_ERROR() << "failed to sync chunk: offset = " << offset
<< ", size = " << size
<< ": '::FlushViewOfFile' " << Error(::GetLastError());
<< ": '::FlushViewOfFile' " << Errno(::GetLastError());
return false;
}
return true;
Expand Down Expand Up @@ -130,7 +130,7 @@ bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset,
<< "file_path = " << file->path()
<< ", file_size = " << file_size << ", offset = " << offset
<< ", size = " << size << ", flags = " << flags
<< ": '::CreateFileMapping' " << Error(::GetLastError());
<< ": '::CreateFileMapping' " << Errno(::GetLastError());
return false;
}
const DWORD offset_high = static_cast<DWORD>(offset >> 32);
Expand All @@ -142,7 +142,7 @@ bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset,
<< "file_path = " << file->path()
<< ", file_size = " << file_size << ", offset = " << offset
<< ", size = " << size << ", flags = " << flags
<< ": '::MapViewOfFile' " << Error(::GetLastError());
<< ": '::MapViewOfFile' " << Errno(::GetLastError());
return false;
}
return true;
Expand All @@ -162,14 +162,14 @@ bool ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) {
if (!handle_) {
GRNXX_ERROR() << "failed to create anonymous file mapping: "
<< "size = " << size << ", flags = " << flags
<< ": '::CreateFileMapping' " << Error(::GetLastError());
<< ": '::CreateFileMapping' " << Errno(::GetLastError());
return false;
}
address_ = ::MapViewOfFile(handle_, FILE_MAP_WRITE, 0, 0, 0);
if (!address_) {
GRNXX_ERROR() << "failed to map anonymous chunk: "
<< "size = " << size << ", flags = " << flags
<< ": '::MapViewOfFile' " << Error(::GetLastError());
<< ": '::MapViewOfFile' " << Errno(::GetLastError());
return false;
}
return true;
Expand Down

0 comments on commit f5c87ec

Please sign in to comment.