OpenM++ runtime library (libopenm)
omError.h
Go to the documentation of this file.
1
5// Copyright (c) 2013-2015 OpenM++
6// This code is licensed under the MIT license (see LICENSE.txt for details)
7
8#ifndef OM_H_ERROR_H
9#define OM_H_ERROR_H
10
11#include <cstdarg>
12#include "common/omHelper.h"
13
14namespace openm
15{
17 template <size_t maxMsgSize, const char * defaultMessage>
18 class OpenmException : public std::exception
19 {
20 public:
21
27 OpenmException(const char * i_format, ...)
28 {
29 try {
30 if (i_format == NULL) {
31 strncpy(msg, defaultMessage, maxMsgSize - 1);
32 }
33 else {
34 va_list args;
35 va_start(args, i_format);
36 formatTo(maxMsgSize, msg, i_format, args);
37 va_end(args);
38 }
39 msg[maxMsgSize - 1] = '\0';
40 }
41 catch (...) { }
42 }
43
45 const char * what(void) const noexcept { return msg; }
46
47 private:
48 char msg[maxMsgSize]; // error message
49 };
50
52 extern const char helperUnknownErrorMessage[];
53
56
61 {
65 const char * ellipt(const char * i_src) { return elliptString(i_src, maxItemSize, msgItem); }
66
70 const char * ellipt(const std::string & i_src) { return elliptString(i_src.c_str(), maxItemSize, msgItem); }
71
72 private:
73 static const size_t maxItemSize = 40; // max size of string item inside of error message
74 char msgItem[maxItemSize + 1]; // string item inside of error message, truncated to max size and ellipted
75 };
76}
77
78#endif // OM_H_ERROR_H
openM++ exceptions
Definition: omError.h:19
OpenmException(const char *i_format,...)
create openM++ exception.
Definition: omError.h:27
const char * what(void) const noexcept
return error message
Definition: omError.h:45
openM++ namespace
Definition: log.h:32
const char * elliptString(const char *i_src, size_t i_size, char *io_buffer)
if source string exceed max size than return ellipted copy into the buffer
Definition: helper.cpp:440
const char helperUnknownErrorMessage[]
helper library exception default error message: "unknown error in helper method"
Definition: log.cpp:22
void formatTo(size_t i_size, char *io_buffer, const char *i_format, va_list io_args)
format message into supplied buffer using vsnprintf()
Definition: helper.cpp:406
OpenmException< 4000, helperUnknownErrorMessage > HelperException
helper library exception
Definition: omError.h:55
OpenM++ common helper utilities.
helper to return ellipted string if source string exceed max size.
Definition: omError.h:61
const char * ellipt(const std::string &i_src)
if source string exceed max size than return ellipted copy.
Definition: omError.h:70
const char * ellipt(const char *i_src)
if source string exceed max size than return ellipted copy.
Definition: omError.h:65