aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/unwind_message.h
blob: 10d8680e14f8daaa6c93978fb178d12e6445f869 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "macro.h"
#include "stringfmt.h"

namespace vespalib {

/**
 * This class contains a message that will be printed to stderr if the
 * object is destructed due to stack unwinding caused by an exception.
 **/
class UnwindMessage {
private:
    int _num_active;
    vespalib::string _message;
public:
    UnwindMessage(const vespalib::string &msg);
    UnwindMessage(UnwindMessage &&rhs) noexcept ;
    UnwindMessage(const UnwindMessage &) = delete;
    UnwindMessage &operator=(const UnwindMessage &) = delete;
    UnwindMessage &operator=(UnwindMessage &&) = delete;
    ~UnwindMessage();
};

extern UnwindMessage unwind_msg(const char *fmt, ...) __attribute__ ((format (printf,1,2)));

// make an unwind message with a hopefully unique name on the stack
#define UNWIND_MSG(...) auto VESPA_CAT(unwindMessageOnLine, __LINE__) = unwind_msg(__VA_ARGS__)

// make an unwind message quoting a piece of code and then perform that code
#define UNWIND_DO(...) do { UNWIND_MSG("%s:%d: %s", __FILE__, __LINE__, VESPA_STRINGIZE(__VA_ARGS__)); __VA_ARGS__; } while(false)

} // namespace