aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/um/service/include/libservice/windows_error.hpp
blob: 13d7f47646ee1e55be8d78fc6892d5edc310e9fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "Windows 7 drivers" project.
// For details, see https://github.com/egor-tensin/windows7-drivers.
// Distributed under the MIT License.

#pragma once

#include "common.hpp"
#include "singleton.hpp"

#include <sstream>
#include <string>
#include <system_error>

namespace libservice
{
    class WindowsErrorCategory
        : public std::error_category
        , public Singleton<WindowsErrorCategory>
    {
    public:
        const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; }

        std::string message(int) const;

    private:
        friend class Singleton<WindowsErrorCategory>;
    };

    namespace error
    {
        inline std::string build_what(
            const char* function,
            const char* file,
            int line)
        {
            std::ostringstream oss;
            oss << "Error in function '" << function
                << "' at file '" << file
                << "', line " << line;
            return oss.str();
        }
    }
}

#if defined(_MSC_VER)
#define LIBSERVICE_ERROR_PREFIX \
    "Error in function '" LIBSERVICE_FUNCTION_NAME "' at file '" LIBSERVICE_FILE_PATH "', line " LIBSERVICE_LINE_NUMBER_STRING
#elif defined(__GNUC__)
#define LIBSERVICE_ERROR_PREFIX \
    libservice::error::build_what(LIBSERVICE_FUNCTION_NAME, LIBSERVICE_FILE_PATH, LIBSERVICE_LINE_NUMBER)
#else
#define LIBSERVICE_ERROR_PREFIX "Error"
#endif