Electroneum
Loading...
Searching...
No Matches
expect.cpp
Go to the documentation of this file.
1//
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are
5// permitted provided that the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of
8// conditions and the following disclaimer.
9//
10// 2. Redistributions in binary form must reproduce the above copyright notice, this list
11// of conditions and the following disclaimer in the documentation and/or other
12// materials provided with the distribution.
13//
14// 3. Neither the name of the copyright holder nor the names of its contributors may be
15// used to endorse or promote products derived from this software without specific
16// prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
19// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "expect.h"
29
30#include <easylogging++.h>
31#include <string>
32
33namespace detail
34{
35 namespace
36 {
37 std::string generate_error(const char* msg, const char* file, unsigned line)
38 {
39 std::string error_msg{};
40 if (msg)
41 {
42 error_msg.append(msg);
43 if (file)
44 error_msg.append(" (");
45 }
46 if (file)
47 {
48 error_msg.append("thrown at ");
49
50 // remove path, get just filename + extension
51 char buff[256] = {0};
52 el::base::utils::File::buildBaseFilename(file, buff, sizeof(buff) - 1);
53 error_msg.append(buff);
54
55 error_msg.push_back(':');
56 error_msg.append(std::to_string(line));
57 }
58 if (msg && file)
59 error_msg.push_back(')');
60 return error_msg;
61 }
62 }
63
64 void expect::throw_(std::error_code ec, const char* msg, const char* file, unsigned line)
65 {
66 if (msg || file)
67 throw std::system_error{ec, generate_error(msg, file, line)};
68 throw std::system_error{ec};
69 }
70} // detail
static void buildBaseFilename(const std::string &fullPath, char buff[], std::size_t limit=base::consts::kSourceFilenameMaxLength, const char *seperator=base::consts::kFilePathSeperator)
builds base filename and puts it in buff
declaration and default definition for the functions used the API
Definition expect.cpp:34
static void throw_(std::error_code ec, const char *msg, const char *file, unsigned line)
Definition expect.cpp:64