Separate a lambda definition from calling it
[openal-soft.git] / al / error.cpp
blob5611f1968c62996fa30aecc72876f847d5422c09
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2000 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #ifdef _WIN32
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26 #endif
28 #include <atomic>
29 #include <csignal>
30 #include <cstdarg>
31 #include <cstdio>
32 #include <cstdlib>
33 #include <cstring>
34 #include <limits>
35 #include <mutex>
36 #include <vector>
38 #include "AL/al.h"
39 #include "AL/alc.h"
41 #include "al/debug.h"
42 #include "alc/alconfig.h"
43 #include "alc/context.h"
44 #include "almalloc.h"
45 #include "alstring.h"
46 #include "core/except.h"
47 #include "core/logging.h"
48 #include "direct_defs.h"
49 #include "opthelpers.h"
50 #include "strutils.h"
53 bool TrapALError{false};
55 void ALCcontext::setError(ALenum errorCode, const char *msg, ...)
57 auto message = std::vector<char>(256);
59 /* NOLINTBEGIN(*-array-to-pointer-decay) */
60 va_list args, args2;
61 va_start(args, msg);
62 va_copy(args2, args);
63 int msglen{std::vsnprintf(message.data(), message.size(), msg, args)};
64 if(msglen >= 0 && static_cast<size_t>(msglen) >= message.size())
66 message.resize(static_cast<size_t>(msglen) + 1u);
67 msglen = std::vsnprintf(message.data(), message.size(), msg, args2);
69 va_end(args2);
70 va_end(args);
71 /* NOLINTEND(*-array-to-pointer-decay) */
73 if(msglen >= 0)
74 msg = message.data();
75 else
77 msg = "<internal error constructing message>";
78 msglen = static_cast<int>(strlen(msg));
81 WARN("Error generated on context %p, code 0x%04x, \"%s\"\n",
82 decltype(std::declval<void*>()){this}, errorCode, msg);
83 if(TrapALError)
85 #ifdef _WIN32
86 /* DebugBreak will cause an exception if there is no debugger */
87 if(IsDebuggerPresent())
88 DebugBreak();
89 #elif defined(SIGTRAP)
90 raise(SIGTRAP);
91 #endif
94 ALenum curerr{AL_NO_ERROR};
95 mLastError.compare_exchange_strong(curerr, errorCode);
97 debugMessage(DebugSource::API, DebugType::Error, 0, DebugSeverity::High,
98 {msg, static_cast<uint>(msglen)});
101 /* Special-case alGetError since it (potentially) raises a debug signal and
102 * returns a non-default value for a null context.
104 AL_API auto AL_APIENTRY alGetError() noexcept -> ALenum
106 if(auto context = GetContextRef()) LIKELY
107 return alGetErrorDirect(context.get());
109 auto get_value = [](const char *envname, const char *optname) -> ALenum
111 auto optstr = al::getenv(envname);
112 if(!optstr)
113 optstr = ConfigValueStr({}, "game_compat", optname);
114 if(optstr)
116 char *end{};
117 auto value = std::strtoul(optstr->c_str(), &end, 0);
118 if(end && *end == '\0' && value <= std::numeric_limits<ALenum>::max())
119 return static_cast<ALenum>(value);
120 ERR("Invalid default error value: \"%s\"", optstr->c_str());
122 return AL_INVALID_OPERATION;
124 static const ALenum deferror{get_value("__ALSOFT_DEFAULT_ERROR", "default-error")};
126 WARN("Querying error state on null context (implicitly 0x%04x)\n", deferror);
127 if(TrapALError)
129 #ifdef _WIN32
130 if(IsDebuggerPresent())
131 DebugBreak();
132 #elif defined(SIGTRAP)
133 raise(SIGTRAP);
134 #endif
136 return deferror;
139 FORCE_ALIGN ALenum AL_APIENTRY alGetErrorDirect(ALCcontext *context) noexcept
141 return context->mLastError.exchange(AL_NO_ERROR);